Está en la página 1de 60

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.

Este tutorial lo realice con el propsito de ayudar a otros en cuanto a la conexin y prueba de los programas en opengl y netbeans.

Primero descarguen las libreras que ocuparemos lwjgl-sourse-2.7.1.zip , lwjgl-docs2.7.1.zip,lwjgl-2.7.1.zip

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 1

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


http://sourceforge.net/projects/java-game-lib/files/Official%20Releases/LWJGL%202.7.1/

Descarga este cdigo y aljalo en tu escritorio http://sites.google.com/site/tlazalohua/

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 2

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


Enseguida descomprime el archivo lwjgl-2.7.1.zip y aljalos en tus documentos. y la carpeta que lleva el mismo nombre

lwjgl

Das doble clic a esta carpeta recin cambiada el nombre das clic a la carpeta doc y en ella ay un archivo llamada readme a esta dale doble clic.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 3

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Esto se hace para saber que el programa este funcionando correctamente. brelo con un bloq de notas.

Enseguida Accesa al comando ejecutar o oprime las teclas inicio + r

Escribe cmd y enter

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 4

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Enseguida te aparece la pantalla negra el objetivo es acesar a la carpeta donde se encuentra el archivo de prueba que esta dentro de la carpeta lwjgl para ello sigue estos pasos:

Mete a tus documentos , a la carpeta lwjgl

Enseguida nos vamos al archivo que abrimos anterior mete llamado readme de la carpeta doc y pegamos este escrito:

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 5

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


nota: debemos de borrarle el signo de mayor que java -cp .;res;jar\lwjgl.jar;jar\lwjgl_test.jar;jar\lwjgl_util.jar;jar\jinput.jar; -Djava.library.path=native\<Windows asi que dara: java -cp .;res;jar\lwjgl.jar;jar\lwjgl_test.jar;jar\lwjgl_util.jar;jar\jinput.jar; -Djava.library.path=native\windows nos vamos a la ventana de el cmd y damos clic en pegar

Si en tu caso fue Linux debes de poner Linux en lugar de windows

El siguiente paso es irnos de nuevo a nuestro archivo readme y copiar este cdigo

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 6

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Copiarlo y pegarlo en cmd (la pantalla negra)

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 7

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Enseguida daremos enter y saldr un jueguito

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 8

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Ahora es momento de enfocarnos a nuestro proyecto que crearemos en netbeans y opengl

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 9

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 10

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Como vemos ya creamos nuestro proyecto

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 11

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


Si no se visualizamos la carpeta del proyecto sigamos estos pasos:

Lo siguiente es crear un paquete

Mi paquete se llamo miopengl

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 12

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Lo siguiente un frame dentro del paquete

Y damos finish

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 13

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

En este momento creamos nuestro frame

nota importante: Es necesario que en este momento pongas en set layout a este panel que creamos

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 14

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


sigamos estos pasos:

en la parte de abajo donde aparecen los frames da clic derecho

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 15

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Y asi se mostrar

Enseguida arrastra un panel y arrstralo donde esta en frame.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 16

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

A este panel es necesario ponerle nombre para ello vete a inspector selecciona el panel da clic derecho.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 17

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

En este momento ya tiene nombre nuestro panel que agregamos enseguida tenemos que agregarle un toggle botn

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 18

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Ponle el nombre animar

Y dale nombre al toggle button

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 19

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Ahora es momento de irnos a nuestro constructor del Frame ventana y damos clic en sourse.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 20

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

private Claseopengl pnLog1; /** Creates new form Ventana */ public Ventana() { initComponents();

try{
Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 21

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


pnLog1= new Claseopengl(); }

catch (org.lwjgl.LWJGLException e){ System.out.println(e.getMessage()); }

add (pnLog1 , java.awt.BorderLayout.CENTER); this.setSize(680,450);

} enseguida nos vamos a la ventana de diseo: al botn damos un evento:

pnLog1.start();

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 22

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Ahora que ya terminamos con el programa debemos de crear una clase y lo meteremos dentro de nuestro paquete. Sigamos estos pasos:

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 23

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 24

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


El paso siguiente es abrir el archivo descargado llamado : OpenGL Es necesario cambiarle el nombre de la clase y el nombre del constructor y borrarle el main.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 25

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


El cdigo completo quedara asi:

package miopengl;

/** * * @author pedro */ import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU;

public class Claseopengl extends org.lwjgl.opengl.AWTGLCanvas implements Runnable {

private Thread thread;

public float rtri; public float rquad;

// Angle For The Triangle ( NEW ) // Angle For The Quad ( NEW )

public float xTras; public float yTras; public float zTras;

public float xRot; public float yRot; public float zRot;

public Claseopengl() throws org.lwjgl.LWJGLException {

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 26

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


super(); }

private void ejes() { GL11.glBegin(GL11.GL_LINES); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(10.0f, 0.0f, 0.0f);

GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 10.0f, 0.0f);

GL11.glColor3f(0.0f, 0.0f, 1.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 10.0f); GL11.glEnd(); }

private void triangulo() { GL11.glPushMatrix(); //GL11.glLoadIdentity(); GL11.glTranslatef(-1.5f,0.0f,-6.0f); GL11.glRotatef(rtri,0.0f,1.0f,0.0f); GL11.glBegin(GL11.GL_TRIANGLES); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); // Red // Top Of Triangle (Front) // Green // Left Of Triangle (Front) // Move Left 1.5 Units And Into The Screen 6.0 // Rotate The Triangle On The Y axis ( NEW ) // Drawing Using Triangles

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 27

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, -1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, -1.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f,-1.0f, -1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glEnd(); GL11.glPopMatrix(); rtri+=0.5f; } // Increase The Rotation Variable For The Triangle ( NEW ) // Blue // Right Of Triangle (Front) // Red // Top Of Triangle (Right) // Blue // Left Of Triangle (Right) // Green // Right Of Triangle (Right) // Red // Top Of Triangle (Back) // Green // Left Of Triangle (Back) // Blue // Right Of Triangle (Back) // Red // Top Of Triangle (Left) // Blue // Left Of Triangle (Left) // Green // Right Of Triangle (Left) // Finished Drawing The Triangle

private void cubo() { GL11.glPushMatrix(); GL11.glTranslatef(1.5f,0.0f,-7.0f); GL11.glRotatef(rquad,1.0f,1.0f,1.0f); // Move Right 1.5 Units And Into The Screen 6.0 // Rotate The Quad On The X axis ( NEW )

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 28

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


GL11.glColor3f(0.5f,0.5f,1.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f,0.5f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); // Set The Color To Blue One Time Only // Draw A Quad // Set The Color To Green // Top Right Of The Quad (Top) // Top Left Of The Quad (Top) // Bottom Left Of The Quad (Top) // Bottom Right Of The Quad (Top) // Set The Color To Orange // Top Right Of The Quad (Bottom) // Top Left Of The Quad (Bottom) // Bottom Left Of The Quad (Bottom) // Bottom Right Of The Quad (Bottom) // Set The Color To Red // Top Right Of The Quad (Front) // Top Left Of The Quad (Front) // Bottom Left Of The Quad (Front) // Bottom Right Of The Quad (Front) // Set The Color To Yellow // Bottom Left Of The Quad (Back) // Bottom Right Of The Quad (Back) // Top Right Of The Quad (Back) // Top Left Of The Quad (Back) // Set The Color To Blue // Top Right Of The Quad (Left) // Top Left Of The Quad (Left) // Bottom Left Of The Quad (Left) // Bottom Right Of The Quad (Left) // Set The Color To Violet // Top Right Of The Quad (Right)

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 29

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glEnd(); GL11.glPopMatrix(); rquad-=0.5f; } // Decrease The Rotation Variable For The Quad ( NEW ) // Top Left Of The Quad (Right) // Bottom Left Of The Quad (Right) // Bottom Right Of The Quad (Right) // Done Drawing The Quad

/** * Override this to do initialising of the context. * It will be called once from paint(), immediately after * the context is created and made current. */ @Override protected void initGL() { GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background GL11.glClearDepth(1.0); // Depth Buffer Setup GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix GL11.glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window GLU.gluPerspective( 45.0f, (float) this.getWidth() / (float) this.getHeight(),

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 30

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


0.1f, 100.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

// Really Nice Perspective Calculations GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); }

/** Override this to do painting */ @Override protected void paintGL() {

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); And The Depth Buffer

// Clear The Screen

GL11.glLoadIdentity();

// Reset The Current Modelview Matrix

GL11.glTranslatef(xTras, yTras, zTras); GL11.glRotatef(xRot, 1.0f, 0.0f, 0.0f); GL11.glRotatef(yRot, 0.0f, 1.0f, 0.0f); GL11.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

ejes(); triangulo(); cubo();

try { this.swapBuffers(); } catch (org.lwjgl.LWJGLException e){

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 31

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


e.printStackTrace(); } }

public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); }

public void stop() { if (thread != null) { thread.interrupt(); } thread = null; }

public void run() { Thread me = Thread.currentThread(); while (thread == me) { repaint(); } thread = null; }

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 32

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos clic en new library

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 33

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Y damos clic en ok en mi caso como ya lo tengo agregado por eso me salio de color rojo el mensaje.

Lo que tenemos que hacer en seguida es agregar los jar a nuestra librera recin creada llamada lwjgl:

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 34

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos clic en add jar

Damos doble clic a la carpeta


Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 35

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos doble clic en jar

Seleccionamos los 4 y damos en agreagrar

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 36

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Y asi aparecern

Nos vamos en sourse y agregaremos otra librera

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 37

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Nos vamos a nuestro documentos y damos clic en agregar

En javadoc hacemos lo mismo


Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 38

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Y as agregamos las libreras.

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 39

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos clic derecho a nuestro proyecto elegimos en el men contextual la opcin propiedades

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 40

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos clic en agregar libreras y lo agregamos:

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 41

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

En run agregamos tambin esta misma librera:

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 42

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Copiamos este cdigo del archivo readme y lo pegamos

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 43

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

-Djava.library.path=

Enseguida tenemos que pegar la extensin donde se encuentran los .ddl

Accesamos a nuestros documentos y damos clic a la carpeta


Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 44

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos clic en native

Damos clic en la carpeta Windows

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 45

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Seleccionamos la direccin que se encuantra en la parte de arriba

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 46

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

Damos cli en ok y nuestro programa estar funcionando al 100 %

-Djava.library.path=C:\Users\pedro\Documents\lwjgl\native\windows

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 47

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


Shift + F6

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 48

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


Si damos clic en animar las imagine s se movern.

Cdigo de la ventana: /* * To change this template, choose Tools | Templates * and open the template in the editor. */

/* * Ventana.java * * Created on 8/06/2011, 12:51:12 PM */

package miopengl; Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 49

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

/** * * @author pedro */ public class Ventana extends javax.swing.JFrame { private Claseopengl pnLog1; /** Creates new form Ventana */ public Ventana() { initComponents();

try{ pnLog1= new Claseopengl(); }

catch (org.lwjgl.LWJGLException e){ System.out.println(e.getMessage()); }

add (pnLog1 , java.awt.BorderLayout.CENTER); this.setSize(680,450);

/** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 50

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


* always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {

pnHerramientas = new javax.swing.JPanel(); btnAnimar = new javax.swing.JToggleButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

btnAnimar.setText("Animar"); btnAnimar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAnimarActionPerformed(evt); } });

javax.swing.GroupLayout pnHerramientasLayout = new javax.swing.GroupLayout(pnHerramientas); pnHerramientas.setLayout(pnHerramientasLayout); pnHerramientasLayout.setHorizontalGroup( pnHerramientasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnHerramientasLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnAnimar)) ); Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 51

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


pnHerramientasLayout.setVerticalGroup( pnHerramientasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI NG) .addGroup(pnHerramientasLayout.createSequentialGroup() .addGap(52, 52, 52) .addComponent(btnAnimar) .addContainerGap(225, Short.MAX_VALUE)) );

getContentPane().add(pnHerramientas, java.awt.BorderLayout.LINE_START);

pack(); }// </editor-fold>

private void btnAnimarActionPerformed(java.awt.event.ActionEvent evt) { pnLog1.start(); }

/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Ventana().setVisible(true); } }); } Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 52

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1

// Variables declaration - do not modify private javax.swing.JToggleButton btnAnimar; private javax.swing.JPanel pnHerramientas; // End of variables declaration

Cdigo de la clase

package miopengl;

/** * * @author pedro */ import org.lwjgl.opengl.GL11; import org.lwjgl.util.glu.GLU;

public class Claseopengl extends org.lwjgl.opengl.AWTGLCanvas implements Runnable {

private Thread thread;

public float rtri; public float rquad;

// Angle For The Triangle ( NEW ) // Angle For The Quad ( NEW )

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 53

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


public float xTras; public float yTras; public float zTras;

public float xRot; public float yRot; public float zRot;

public Claseopengl() throws org.lwjgl.LWJGLException { super(); }

private void ejes() { GL11.glBegin(GL11.GL_LINES); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(10.0f, 0.0f, 0.0f);

GL11.glColor3f(0.0f, 1.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 10.0f, 0.0f);

GL11.glColor3f(0.0f, 0.0f, 1.0f); GL11.glVertex3f(0.0f, 0.0f, 0.0f); GL11.glVertex3f(0.0f, 0.0f, 10.0f); GL11.glEnd();

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 54

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


}

private void triangulo() { GL11.glPushMatrix(); //GL11.glLoadIdentity(); GL11.glTranslatef(-1.5f,0.0f,-6.0f); GL11.glRotatef(rtri,0.0f,1.0f,0.0f); GL11.glBegin(GL11.GL_TRIANGLES); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, -1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, -1.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f,-1.0f, -1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); // Red // Top Of Triangle (Front) // Green // Left Of Triangle (Front) // Blue // Right Of Triangle (Front) // Red // Top Of Triangle (Right) // Blue // Left Of Triangle (Right) // Green // Right Of Triangle (Right) // Red // Top Of Triangle (Back) // Green // Left Of Triangle (Back) // Blue // Right Of Triangle (Back) // Red // Move Left 1.5 Units And Into The Screen 6.0 // Rotate The Triangle On The Y axis ( NEW ) // Drawing Using Triangles

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 55

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


GL11.glVertex3f( 0.0f, 1.0f, 0.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glEnd(); GL11.glPopMatrix(); rtri+=0.5f; } // Increase The Rotation Variable For The Triangle ( NEW ) // Top Of Triangle (Left) // Blue // Left Of Triangle (Left) // Green // Right Of Triangle (Left) // Finished Drawing The Triangle

private void cubo() { GL11.glPushMatrix(); GL11.glTranslatef(1.5f,0.0f,-7.0f); GL11.glRotatef(rquad,1.0f,1.0f,1.0f); GL11.glColor3f(0.5f,0.5f,1.0f); GL11.glBegin(GL11.GL_QUADS); GL11.glColor3f(0.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f,0.5f,0.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glColor3f(1.0f,0.0f,0.0f); // Move Right 1.5 Units And Into The Screen 6.0 // Rotate The Quad On The X axis ( NEW ) // Set The Color To Blue One Time Only // Draw A Quad // Set The Color To Green // Top Right Of The Quad (Top) // Top Left Of The Quad (Top) // Bottom Left Of The Quad (Top) // Bottom Right Of The Quad (Top) // Set The Color To Orange // Top Right Of The Quad (Bottom) // Top Left Of The Quad (Bottom) // Bottom Left Of The Quad (Bottom) // Bottom Right Of The Quad (Bottom) // Set The Color To Red

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 56

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,1.0f,0.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); GL11.glColor3f(0.0f,0.0f,1.0f); GL11.glVertex3f(-1.0f, 1.0f, 1.0f); GL11.glVertex3f(-1.0f, 1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f,-1.0f); GL11.glVertex3f(-1.0f,-1.0f, 1.0f); GL11.glColor3f(1.0f,0.0f,1.0f); GL11.glVertex3f( 1.0f, 1.0f,-1.0f); GL11.glVertex3f( 1.0f, 1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f, 1.0f); GL11.glVertex3f( 1.0f,-1.0f,-1.0f); GL11.glEnd(); GL11.glPopMatrix(); rquad-=0.5f; } // Decrease The Rotation Variable For The Quad ( NEW ) // Top Right Of The Quad (Front) // Top Left Of The Quad (Front) // Bottom Left Of The Quad (Front) // Bottom Right Of The Quad (Front) // Set The Color To Yellow // Bottom Left Of The Quad (Back) // Bottom Right Of The Quad (Back) // Top Right Of The Quad (Back) // Top Left Of The Quad (Back) // Set The Color To Blue // Top Right Of The Quad (Left) // Top Left Of The Quad (Left) // Bottom Left Of The Quad (Left) // Bottom Right Of The Quad (Left) // Set The Color To Violet // Top Right Of The Quad (Right) // Top Left Of The Quad (Right) // Bottom Left Of The Quad (Right) // Bottom Right Of The Quad (Right) // Done Drawing The Quad

/** * Override this to do initialising of the context. * It will be called once from paint(), immediately after

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 57

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


* the context is created and made current. */ @Override protected void initGL() { GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background GL11.glClearDepth(1.0); // Depth Buffer Setup GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix GL11.glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window GLU.gluPerspective( 45.0f, (float) this.getWidth() / (float) this.getHeight(), 0.1f, 100.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

// Really Nice Perspective Calculations GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); }

/** Override this to do painting */

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 58

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


@Override protected void paintGL() {

GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); The Screen And The Depth Buffer

// Clear

GL11.glLoadIdentity();

// Reset The Current Modelview Matrix

GL11.glTranslatef(xTras, yTras, zTras); GL11.glRotatef(xRot, 1.0f, 0.0f, 0.0f); GL11.glRotatef(yRot, 0.0f, 1.0f, 0.0f); GL11.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

ejes(); triangulo(); cubo();

try { this.swapBuffers(); } catch (org.lwjgl.LWJGLException e){ e.printStackTrace(); } }

public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 59

Tutorial: como ejecutar un proyecto opengl y netbeans 6.9.1


}

public void stop() { if (thread != null) { thread.interrupt(); } thread = null; }

public void run() { Thread me = Thread.currentThread(); while (thread == me) { repaint(); } thread = null; }

Tutorial: Pedro Lpez Salazar Instituto tecnolgico Superior de Zapopan Pgina 60

También podría gustarte