Você está na página 1de 10
715122, 1108 JOGL -Fundamentos 30 JOGL - Fundamentos 3D En capitulos anteriores hemos visto cémo crear objetos 2d, aplicarles efectos y transformar el objeto. Este capitulo le ensefia cémo dibujar una linea con la tercera dimensién y algunas formas. Dibujemos una linea simple con el eje z y veamos la diferencia entre las lineas 2D y 3D. Primero dibuje una linea simple, luego dibuje la segunda linea 3 unidades en la ventana. Repasemos el programa para dibujar una linea 3D: import import import import import import import import public pri ov pub’ @ov javax.media.opengl.GL2; javax.media. opengl .GLautoDrawable; javax.media.opengl.GLCapabilities; javax.media.opengl.GLeventListener; javax.media.opengl.GLProfile; javax.media.opengl.awt .GLCanvas; Javax.media.opengl.glu.GLU; javax. swing. JFrame; class Line3d implements GLEventListener { vate GLU glu = new GLU(); erride lic void display( GLautoDrawable drawable ) { final GL2 gl = drawable.getGL().getGL2(); gl.glTranslatef( of, Of, -2.5F ); gl.glBegin( GL2.GL_LINES ); gl.glvertex3F( -0.75F,0F,0 ); gl.glvertex3f( @f,-0.75F, @ ); gl.glEnd(); //3d Line gl.glBegin( GL2.GL_LINES ); gl.glvertex3F( -0.75f,0f,3f );// 3 units into the gl.glvertex3#( Of, -0.75F,3F ); gl.gl€nd(); erride hitpsshwoltoriaepoint comjoglag! 3d basics him window sno 1715122, 1108 JOGL -Fundamentos 30 puv1ie voru Uspuse, GLAULOUraWaDe argo) ¢ //method body @override public void init( GLAutoDrawable argo ) { // method body @override public void reshape( GLautoDrawable drawable, int x, int y, int width, int height GL2 gl = drawable. getGL() .getGL2(); if( height height @) final float h = ( float ) width / ( float ) height; gl.glViewport( @, @, width, height ); gl.glMatrixMode( GL2.GL_PROJECTION ); gl.glloadidentity(); glu.gluPerspective( 45.6f, h, 1.0, 20.0 )5 gl.glMatrixMode( GL2.GL_MODELVIEW ); gl.glloadIdentity(); public static void main( String[] args ) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get( GLProfile.GL2 GlCapabilities capabilities = new GLCapabilities (profile); // The canvas final GLCanvas glcanvas = new GLCanvas( capabilities ); Line3d line3d = new Line3d(); glcanvas.addGLeventListener( line3d ); glcanvas.setSize( 400, 408 ); //creating frame final JFrame frame = new JFrame (" 3d line"); //adding canvas to it frame.getContentPane().add( glcanvas ); frame, setSize(frame.getContentPane().getPreferredSize() ); hitpshwwcutorialepeint comjoglogl 34 basics him 2n0 1715122, 1108 JOGL -Fundamentos 30 Trame.sevisaoiey urue )5 Wend of main )//end of class Cuando compila y ejecuta el programa anterior, se genera el siguiente resultado: Las formas 3D se pueden dibujar dando valores distintos de cero al cuadrante z del método glVertex3f() , que genera la vista anterior. Ahora, unir las lineas restantes conduciré a un borde 30. Ahora, de la misma manera, desarrollemos un borde con 3ra dimensién. import javax.media.opengl.GL2; import javax.media.opengl.GLautoDrawable; import. javax.media.opengl.GLCapabilities; import javax.media.opengl.GLeventListener; import. javax.media.opengl.GLProfile; import javax.media. opengl. awt.GLCanvas; import javax.media.opengl.glu.GLU; import javax. swing. JFrame; public class Edge1 implements GLeventListener { private GLU glu = new GLU(); @override hitpsshwwtorialepoint comjoalogl_3d_basics him ano ‘7522, 11.08 JOGL -Fundamentos 30 public void display(GLAutoDrawable drawable) { // TODO Auto-generated method stub final GL2 gl = drawable. getGL().getL2(); gl.gltranslatef (of, Of, -2.5); gl.glBegin(GL2.GL_LINES) ; gl.glVertex3F(-0.75F,0F,0); gl.glvertex3f(@f,-0.75F, 0); gl.glend(); //3d Line gl.glBegin(GL2.GL_LINES) ; //3 units in to the window gl.glVertex3f(-0.75f, ef, 3f) gl.glvertex3# (Ff, -0.75f,3f) gl.glend(); //top gl.glBegin(GL2.GL_LINES) ; gl.glvertex3(-0.75F,0F,2) 5 gl.glVertex3F(-.75F,0F, 3F) 5 gl.gl€nd(); //bottom gl.glBegin(GL2.GL_LINES) ; gl.glvertex3f(OF,-0.75F, @); gl.glvertex3F(@f,-0.75#,3F); gl.gl€nd(); @0verride public void dispose(GLautoDrawable arg®) { //method body @0verride public void init(GLautoDrawable argd) { // method body @override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) hitpsshwwucutoriaepoint comjoalogl_3d_basies him ano ‘7522, 11.08 OGL - Fundamentos 30 // 10v0 Auto-generated metnod stuvfinat GL2 gl = drawable. getGL().getGL2(); if(height <= 0) height = 15 final float h = (float) width / (float) height; gl.glViewport(@, 0, width, height); gl.glMatrixMode(GL2.GL_PROJECTION) ; gl.glloadidentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL2.GL_MODELVIEW) ; gl.glloadidentity(); public static void main(String[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile. get (GLProfile.GL2) ; GLCapabilities capabilities = new GLCapabilities (profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities) ; Edge b = new Edget(); glcanvas.addGLeventListener(b) ; glcanvas.setSize(400, 400); //creating frame final JFrame frame = new JFrame (" 3d edge"); //adding canvas to it frame. getContentPane() .add(glcanvas) ; frame. setSize (frame. getContentPane().getPreferredSize()); frame. setVisible(true) ; Wend of main }J/end of class Cuando compila y ejecuta el programa anterior, se genera el siguiente resultado: htpshwwtterilspoint comjoglog|_ | basics him sno 715122, 1108 JOGL -Fundamentos 30 De la misma manera, al desarrollar aristas 3D en los lados correspondientes de cualquier cuadrilétero 2D y unir los vértices adyacentes, puede obtener un cuadrilétero 3D. A ccontinuacién se muestra un programa para dibujar un rombo usando JOGL. import. javax.media.opengl.GL2; import. javax.media.opengl.GLautoDrawable; import. javax.media.opengl.GLCapabilities; import. javax.media.opengl.GLeventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.glu.GLU; import javax.swing. JFrame; public class Rhombus implements GLEventListener { private GLU glu = new GLU(); @0verride public void display(GLautoDrawable drawable) { final GL2 gl = drawable.getGL().getL2(); gl.glTranslatef(@f, ef, -2.5F); //drawing edgel..... gl.glBegin(GL2.GL_LINES) ; hitpsshwotoriaepoint comjoalogl_3d_basics him eno 1715122, 1108 JOGL -Fundamentos 30 gl.glVertex3F(-0.75F,0f,0); gl.glvertex3f(af,-0.75F, ®); gl.glénd(); gl.glBegin(GL2.GL_LINES) ; gl.glVertex3f(-0.75,0F,3F); // 3 units into the window gl.glVertex3#(@F,-0.75F,3F); gl.glend(); //top gl.glBegin(GL2.GL_LINES) ; gl.glVertex3F(-0.75F,0F,0) 5 gl.glvertex3f(-0.75f,0F,3f); gl.gl€nd(); // bottom gl.glBegin(GL2.GL_LINES) ; gl.glvertex3F(Of,-0.75F, 0); gl.glVertex3#(0F,-0.75#,3F) 5 gl.gl€nd(); // edge 2.... gl.glBegin(GL2.GL_LINES) ; gl.glvertex3F(@f,-0.75F, @)3 gl.glvertex3f(0.754,0F, 0); gl.gl€nd(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3#(0f,-2.75f, 34); gl.glvertex3#(0.75#,0f, 34); gl.glend(); gl.glBegin(GL2.GL_LINES) ; gl.glVertex3F(@F,-0.75F, ®)5 gl.glvertex3f(@f,-0.75F, 3f); gl.gl€nd(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3f(0.75F,0F, 0); gl.glvertex3f(0.75F,0F, 3F); gl.gl€nd(); //Edge 3... ++ gl.glBegin(GL2.GL_LINES) ; gl.glvertex3f( @.0f,0.75F,0); hitpsshwotoriaepoint comjoalogl_3d_basics him m0 ‘7522, 11.08 JOGL -Fundamentos 30 gl.glVertex3F(-0.75F,0F,0); gl.gl€nd(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3#( 0.0f,0.75F,3#); gl.glvertex3#(-0.75F, 0F,3F); gl.glend(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3F( 0.0f,0.75f,0); gl.glvertex3f( 0.0f,0.75F,3F); gl.glend(); gl.glBegin(GL2.GL_LINES) ; gl.glVertex3F(-0.75F,0F,0)5 gl.glVertex3f(-0.75F,0F, 3F); gl.gl€nd(); //final edge gl.glBegin(GL2.GL_LINES) ; gl.glvertex3F(0.75#,0F, 0); gl.glvertex3#( 0.0f,0.754,0); gl.gl€nd(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3#(0.75#,0F,3f); gl.glvertex3#( @.0f,0.75F,3F); gl.glend(); gl.glBegin(GL2.GL_LINES) ; gl.glVertex3f(2.75#,0F, 0); gl.glVertex3f(0.75f,0F, 3); gl.gl€nd(); gl.glBegin(GL2.GL_LINES) ; gl.glvertex3f( @.0f,0.75F,0); gl.glVertex3F( @.0f,0.75F,3F)5 gl.gl€nd(); @0verride public void dispose(GLautoDrawable argd) { //method body hitpsshwotoriaepoint comjoalogl_3d_basics him ano ‘7522, 11.08 JOGL -Fundamentos 30 @override public void init(GLautoDrawable argd) { // method body @override public void reshape(GLautoDrawable drawable, int x, int y, int width, int height) // TODO Auto-generated method stub final GL2 gl = drawable. getGL() .getGL2(); if(height 1t;= ©) height = 15 final float h = (float) width / (float) height; gl.glViewport(3, 6, width, height); gl.glMatrixMode(GL2.GL_PROJECTION) ; gl.glloadidentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL2.GL_MODELVIEW) ; gl.glloadidentity(); public static void main(string[] args) { //getting the capabilities object of GL2 profile final GLProfile profile = GLProfile.get (GLProfile.GL2) ; GlCapabilities capabilities = new GLCapabilities(profile); // The canvas final GLCanvas glcanvas = new GLCanvas(capabilities) ; Rhombus b = new Rhombus(); glcanvas.addGLeventListener(b); glcanvas.setSize(40e, 400); //creating frame final JFrame frame = new JFrame (“ Rhombus 3d"); //adding canvas to it frame. getContentPane().add(glcanvas) ; frame. setSize (frame. getContentPane().getPreferredSize()); frame. setVisible(true) ; Wend of main }/end of classimport javax.media.opengl.GL2; hitpsshwotoriaepoint comjoalogl_3d_basics him sno 1715122, 1108 JOGL -Fundamentos 30 Cuando compila y ejecuta el programa anterior, se genera el siguiente resultado, Muestra un rombo dibujado usando lineas 3D. Los parémetros predefinidos del método glBegin() se pueden usar para dibujar formas 3D. hitpsshwwtorialepoint comjoalag|_3d_basics him 1010

Você também pode gostar