Sunteți pe pagina 1din 28

DESARROLLO E IMPLEMENTACION DE UNA APLICACIÓN QUE

UTILIZANDO OPENGL TRASLADE Y ROTE FIGURAS GEOMETRICAS

Para nuestro trabajo final utilizaremos el IDE NETBEANS 8.2, utilizando el lenguaje de
Programación JAVA

Para lo cual importamos los PLUGINS de OPENGL a nuestro entorno de desarrollo, lo


cual estarán reservados en el modulo JOGL

Ya instalados comenzamos a crear un proyecto del tipo OPENGL


Creamos un proyecto java con el módulo JOGL (OPENGL)

Con el nombre “ProgramaOpenGL”

Ya creado el proyecto diseñamos nuestra interfaz


Implementamos un JFrame con un menú donde muestra las opciones de figuras, asi como
sus herramientas además de incorporar un JPanel para el CanvasOpenGL, quedando de
la siguiente manera:

Al lado izquierdo se muestra las clases y componentes de la Ventana así como el modulo
incorporado al proyecto los cuales son:
GLUEGEN-RT-gluegen-rt.jar y JOGL – jogl.jar
En la parte central se muestra el diseño de la interfaz, y en la parte derecha se muestra los
atributos por componentes

A continuación se detalla el código empleado para el desarrollo de los componentes así


como de las acciones de estos:

CODIGO EN JAVA

//IMPORTAMOS LAS LIBRERIAS DE OPENGL AL ARCHIVO JAVA


package org.yourorghere;

import com.sun.opengl.util.Animator;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import com.sun.opengl.util.GLUT;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author usuario
*/
public class FramePintorOpenGL extends JFrame {
GraphicListener listener;
GLCanvas canvas;

// CONSTRUCTOR QUE CREA EL OBJETO A OPENGL


public FramePintorOpenGL() {
initComponents();
listener = new GraphicListener(2);
canvas= new GLCanvas(new GLCapabilities());
canvas.addGLEventListener(listener);
canvas.setBounds(0, 0, 600, 600);
pan1.add(canvas);
Animator animator = new Animator(canvas);
animator.start();
setBounds(0,0,610,640);
setVisible(true);
}

//CREAMOS LOS COMPONENTES DE LA INTERFAZ


private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();


buttonGroup2 = new javax.swing.ButtonGroup();
pan1 = new javax.swing.JPanel();
bcrM = new javax.swing.JButton();
bcrA = new javax.swing.JButton();
bcrV = new javax.swing.JButton();
bcB = new javax.swing.JButton();
bcR = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
menuFigura = new javax.swing.JMenu();
mlin = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
mtri = new javax.swing.JMenuItem();
mcua = new javax.swing.JMenuItem();
mrec = new javax.swing.JMenuItem();
mpol = new javax.swing.JMenuItem();
mher = new javax.swing.JMenu();
jMenu4 = new javax.swing.JMenu();
maum = new javax.swing.JMenuItem();
mdis = new javax.swing.JMenuItem();
jMenu5 = new javax.swing.JMenu();
mgr1 = new javax.swing.JRadioButtonMenuItem();
mg3 = new javax.swing.JRadioButtonMenuItem();
mgr6 = new javax.swing.JRadioButtonMenuItem();
jMenu6 = new javax.swing.JMenu();
mesv = new javax.swing.JCheckBoxMenuItem();
meli = new javax.swing.JCheckBoxMenuItem();
mesr = new javax.swing.JCheckBoxMenuItem();
mtra = new javax.swing.JMenu();
mizq = new javax.swing.JMenuItem();
mder = new javax.swing.JMenuItem();
marr = new javax.swing.JMenuItem();
maba = new javax.swing.JMenuItem();
matr = new javax.swing.JMenuItem();
made = new javax.swing.JMenuItem();
mrot = new javax.swing.JMenu();
mrizq = new javax.swing.JMenuItem();
mrder = new javax.swing.JMenuItem();
mrarr = new javax.swing.JMenuItem();
mraba = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();

//DEFINIMOS LAS ACCIONES DE LOS COMPONENTES DE LA INTERFAZ

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("FIGURAS GEOMETRICAS OPENGL - JAVA");
getContentPane().setLayout(null);

pan1.setBackground(new java.awt.Color(255, 255, 255));


pan1.setLayout(null);
getContentPane().add(pan1);
pan1.setBounds(0, 0, 590, 530);

bcrM.setBackground(new java.awt.Color(0, 255, 255));


bcrM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcrMActionPerformed(evt);
}
});
getContentPane().add(bcrM);
bcrM.setBounds(10, 540, 100, 30);

bcrA.setBackground(new java.awt.Color(0, 0, 255));


bcrA.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcrAActionPerformed(evt);
}
});
getContentPane().add(bcrA);
bcrA.setBounds(240, 540, 100, 30);

bcrV.setBackground(new java.awt.Color(0, 255, 0));


bcrV.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcrVActionPerformed(evt);
}
});
getContentPane().add(bcrV);
bcrV.setBounds(350, 540, 110, 30);

bcB.setBackground(new java.awt.Color(255, 255, 255));


bcB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcBActionPerformed(evt);
}
});
getContentPane().add(bcB);
bcB.setBounds(470, 540, 110, 30);

bcR.setBackground(new java.awt.Color(255, 0, 0));


bcR.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bcRActionPerformed(evt);
}
});
getContentPane().add(bcR);
bcR.setBounds(120, 540, 110, 30);

menuFigura.setText("FIGURAS 2D");

mlin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_U, 0));
mlin.setMnemonic('U');
mlin.setText("CIRCULO");
mlin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mlinActionPerformed(evt);
}
});
menuFigura.add(mlin);

jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyE
vent.VK_L, 0));
jMenuItem1.setText("LINEA");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
menuFigura.add(jMenuItem1);
mtri.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_T, 0));
mtri.setText("TRIANGULO");
mtri.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mtriActionPerformed(evt);
}
});
menuFigura.add(mtri);

mcua.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_C, 0));
mcua.setText("CUADRADO");
mcua.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mcuaActionPerformed(evt);
}
});
menuFigura.add(mcua);

mrec.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_R, 0));
mrec.setText("RECTANGULO");
mrec.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrecActionPerformed(evt);
}
});
menuFigura.add(mrec);

mpol.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_P, 0));
mpol.setText("POLIGONO");
mpol.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mpolActionPerformed(evt);
}
});
menuFigura.add(mpol);

mher.setText("HERRAMIENTAS");

jMenu4.setText("ZOOM");
maum.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_ADD, 0));
maum.setText("AUMENTAR");
maum.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
maumActionPerformed(evt);
}
});
jMenu4.add(maum);

mdis.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_SUBTRACT, 0));
mdis.setText("DISMINUIR");
mdis.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mdisActionPerformed(evt);
}
});
jMenu4.add(mdis);

mher.add(jMenu4);

jMenu5.setText("GROSOR");

mgr1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_1, 0));
buttonGroup1.add(mgr1);
mgr1.setSelected(true);
mgr1.setText("1 PTO");
mgr1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mgr1ActionPerformed(evt);
}
});
jMenu5.add(mgr1);

mg3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_2, 0));
buttonGroup1.add(mg3);
mg3.setText("3 PTO");
mg3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mg3ActionPerformed(evt);
}
});
jMenu5.add(mg3);
mgr6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_3, 0));
buttonGroup1.add(mgr6);
mgr6.setText("6 PTO");
mgr6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mgr6ActionPerformed(evt);
}
});
jMenu5.add(mgr6);

mher.add(jMenu5);

jMenu6.setText("ESTILO");

mesv.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_4, 0));
buttonGroup2.add(mesv);
mesv.setText("VERTICE");
mesv.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mesvActionPerformed(evt);
}
});
jMenu6.add(mesv);

meli.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_5, 0));
buttonGroup2.add(meli);
meli.setSelected(true);
meli.setText("LINEAL");
meli.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
meliActionPerformed(evt);
}
});
jMenu6.add(meli);

mesr.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_6, 0));
buttonGroup2.add(mesr);
mesr.setText("RELLENO");
mesr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mesrActionPerformed(evt);
}
});
jMenu6.add(mesr);

mher.add(jMenu6);

menuFigura.add(mher);

mtra.setText("TRASLADAR");

mizq.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_LEFT, 0));
mizq.setText("IZQUIERDA");
mizq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mizqActionPerformed(evt);
}
});
mtra.add(mizq);

mder.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_RIGHT, 0));
mder.setText("DERECHA");
mder.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mderActionPerformed(evt);
}
});
mtra.add(mder);

marr.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_UP, 0));
marr.setText("ARRIBA");
marr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
marrActionPerformed(evt);
}
});
mtra.add(marr);

maba.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_DOWN, 0));
maba.setText("ABAJO");
maba.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mabaActionPerformed(evt);
}
});
mtra.add(maba);

matr.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK
_DELETE, 0));
matr.setText("ATRAS");
matr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
matrActionPerformed(evt);
}
});
mtra.add(matr);

made.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_END, 0));
made.setText("ADELANTE");
made.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
madeActionPerformed(evt);
}
});
mtra.add(made);

menuFigura.add(mtra);

mrot.setText("ROTAR");

mrizq.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_A, 0));
mrizq.setText("ROTAR IZQUIERDA");
mrizq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrizqActionPerformed(evt);
}
});
mrot.add(mrizq);

mrder.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_D, 0));
mrder.setText("ROTAR DERECHA");
mrder.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrderActionPerformed(evt);
}
});
mrot.add(mrder);

mrarr.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_W, 0));
mrarr.setText("ROTAR ARRIBA");
mrarr.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrarrActionPerformed(evt);
}
});
mrot.add(mrarr);

mraba.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.V
K_S, 0));
mraba.setText("ROTAR ABAJO");
mraba.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrabaActionPerformed(evt);
}
});
mrot.add(mraba);

jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyE
vent.VK_Q, 0));
jMenuItem5.setText("ROTAR ATRAS");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem5ActionPerformed(evt);
}
});
mrot.add(jMenuItem5);

jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyE
vent.VK_E, 0));
jMenuItem6.setText("ROTAR ADELANTE");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
mrot.add(jMenuItem6);

menuFigura.add(mrot);

jMenuBar1.add(menuFigura);
setJMenuBar(jMenuBar1);

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

// IMPLEMENTAMOS SUS ACCIONES, ASIGNANDOLES VALORES POR CADA


ACCION EFECTUADA EN EL COMPONENTE

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {


opc=1;
activo=true;
}

private void mtriActionPerformed(java.awt.event.ActionEvent evt) {


opc=2;
activo=true;
}

private void mcuaActionPerformed(java.awt.event.ActionEvent evt) {


opc=3;
activo=true;
}

private void mrecActionPerformed(java.awt.event.ActionEvent evt) {


opc=4;
activo=true;
}

private void mpolActionPerformed(java.awt.event.ActionEvent evt) {


opc=5;
activo=true;
}

private void mlinActionPerformed(java.awt.event.ActionEvent evt) {


opc=0;
activo=true;
}

private void marrActionPerformed(java.awt.event.ActionEvent evt) {


trasY+=.1;
}

private void mabaActionPerformed(java.awt.event.ActionEvent evt) {


trasY-=.1;
}

private void mizqActionPerformed(java.awt.event.ActionEvent evt) {


trasX-=.1;
}
private void mderActionPerformed(java.awt.event.ActionEvent evt) {
trasX+=.1;
}

private void mrarrActionPerformed(java.awt.event.ActionEvent evt) {


rotX+=5.0f;
}

private void mrabaActionPerformed(java.awt.event.ActionEvent evt) {


rotX-=5.0f;
}

private void mrizqActionPerformed(java.awt.event.ActionEvent evt) {


rotY-=5.f;
}

private void mrderActionPerformed(java.awt.event.ActionEvent evt) {


rotY+=5.f;
}

private void mgr1ActionPerformed(java.awt.event.ActionEvent evt) {


vgr=4.0f;
}

private void mg3ActionPerformed(java.awt.event.ActionEvent evt) {


vgr=8.0f;
}

private void mgr6ActionPerformed(java.awt.event.ActionEvent evt) {


vgr=12.0f;
}

private void mesvActionPerformed(java.awt.event.ActionEvent evt) {


estilo=0;
}

private void meliActionPerformed(java.awt.event.ActionEvent evt) {


estilo=1;
}

private void mesrActionPerformed(java.awt.event.ActionEvent evt) {


estilo=2;
}

private void matrActionPerformed(java.awt.event.ActionEvent evt) {


trasZ-=.1;
}

private void madeActionPerformed(java.awt.event.ActionEvent evt) {


trasZ+=.1;
}

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {


rotZ-=5.0f;
}

private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {


rotZ-=5.0f;
}

private void maumActionPerformed(java.awt.event.ActionEvent evt) {


if(escX<1)
{
escX+=.25f;
escY+=.25f;
escZ+=.25f;
}
else
{
JOptionPane.showMessageDialog(this, "AUMENTO DE ESCALA SUPERADO");
}
}

private void mdisActionPerformed(java.awt.event.ActionEvent evt) {


if(escX>0.25f)
{
escX-=.25f;
escY-=.25f;
escZ-=.25f;
}
else
{
JOptionPane.showMessageDialog(this, "DISMINUCION DE ESCALA
SUPERADO");
}
}

private void bcrMActionPerformed(java.awt.event.ActionEvent evt) {


vcr=0;
vcg=1;
vcb=1;
}

private void bcrAActionPerformed(java.awt.event.ActionEvent evt) {


vcr=0;
vcg=0;
vcb=1;
}
private void bcrVActionPerformed(java.awt.event.ActionEvent evt) {
vcr=0;
vcg=1;
vcb=0;
}

private void bcBActionPerformed(java.awt.event.ActionEvent evt) {


vcr=1;
vcg=1;
vcb=1;
}

private void bcRActionPerformed(java.awt.event.ActionEvent evt) {


vcr=1;
vcg=0;
vcb=0;
}

//METODO PRINCIPAL EN JAVA


public static void main(String args[]) {
FramePintorOpenGL ven = new FramePintorOpenGL();
}

//VARIABLES DE LOS COMPONENTE

// Variables declaration - do not modify


private javax.swing.JButton bcB;
private javax.swing.JButton bcR;
private javax.swing.JButton bcrA;
private javax.swing.JButton bcrM;
private javax.swing.JButton bcrV;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenu jMenu5;
private javax.swing.JMenu jMenu6;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JMenuItem maba;
private javax.swing.JMenuItem made;
private javax.swing.JMenuItem marr;
private javax.swing.JMenuItem matr;
private javax.swing.JMenuItem maum;
private javax.swing.JMenuItem mcua;
private javax.swing.JMenuItem mder;
private javax.swing.JMenuItem mdis;
private javax.swing.JCheckBoxMenuItem meli;
private javax.swing.JMenu menuFigura;
private javax.swing.JCheckBoxMenuItem mesr;
private javax.swing.JCheckBoxMenuItem mesv;
private javax.swing.JRadioButtonMenuItem mg3;
private javax.swing.JRadioButtonMenuItem mgr1;
private javax.swing.JRadioButtonMenuItem mgr6;
private javax.swing.JMenu mher;
private javax.swing.JMenuItem mizq;
private javax.swing.JMenuItem mlin;
private javax.swing.JMenuItem mpol;
private javax.swing.JMenuItem mraba;
private javax.swing.JMenuItem mrarr;
private javax.swing.JMenuItem mrder;
private javax.swing.JMenuItem mrec;
private javax.swing.JMenuItem mrizq;
private javax.swing.JMenu mrot;
private javax.swing.JMenu mtra;
private javax.swing.JMenuItem mtri;
private javax.swing.JPanel pan1;
// End of variables declaration

//VARIABLES DE LAS ACCIONES


//Iniciamos el valor de la traslacion en 0
//Para que cuando inicie el programa, la figura aun
//No se traslade en la ventana
float trasX=0;
float trasY=0;
float trasZ=0;

/*Iniciamos el valor en la escala en 1


* para que cuando el objeto inicie el programa aun no se escale*/
float escX=.25f;
float escY=.25f;
float escZ=.25f;

//Hacemos lo mismo para la rotacion sobre los ejes x,y


float rotX=0;
float rotY=0;
float rotZ=0;

//variable de operacion
int opc=0;
float vcr=1,vcg=0,vcb=0;
float vgr=4.0f;
int estilo=1;
boolean activo=true;

public class GraphicListener implements GLEventListener{


// indica que figura dibujar

public GraphicListener(int op) {


opc=op;
}

//método que inicializa las variables


private void limpiarVariables() {
trasX=0;
trasY=0;
trasZ=0;
rotX=0;
rotY=0;
rotZ=0;
escX=.25f;
escY=.25f;
escZ=.25f;
}

// Metodo Inicial de OpenGL


public void init(GLAutoDrawable drawable) {

//METODO DE EJECUCION CONSTANTE


// EN ESTE METODO VERIFICAMOS LAS ACCIONES REALIZADAS EN
// LA VENTANA
public void display(GLAutoDrawable drawable) {

GL gl = drawable.getGL();
GLU glu = new GLU();

//limpiamos el color e indicamos el color


gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//especifica el tamaño y la forma en la que se proyecta la escena para luego pasarla
a la pantalla.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
//glu.gluPerspective(45.0f, 1.0, 1.0, 20.0);
gl.glOrtho(-1.0, 1.0, 1.0, -1.0, -1.0, 1.0);

//estilo de pintado de la figura


if(estilo==0)
{
gl.glPolygonMode(GL.GL_FRONT, GL.GL_POINT);
}
else
{
if(estilo==1)
{
gl.glPolygonMode(GL.GL_FRONT, GL.GL_LINE);
}
else
{
gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
}
}

//Matriz identidad
gl.glLoadIdentity();

gl.glColor3f(1, 1, 0);
gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0, 1.f,0);
gl.glVertex3f(0, 0,0);
gl.glEnd();

gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0,0,0);
gl.glVertex3f(1.f,0,0);
gl.glEnd();

gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0,0,0);
gl.glVertex3f(-1.f,-1.f,0);
gl.glEnd();

//Indicamos el tamaño de los puntos en pixeles


gl.glPointSize(8.0f);
//Indicamos el grosor de las lineas...
gl.glLineWidth(vgr);

//Metodo para trasladar


gl.glTranslatef(trasX, trasY, trasZ);
/*De igual forma , escalamos nuestros objetos dependiendo de los valores que
les sea asignado en cada tecla */
gl.glScalef(escX , escY, escZ);
//Indicamos que vamos a rotar "x" grados sobre el eje x
gl.glRotatef(rotX, 1, 0, 0f);
//Indicamos que vamos a rotar en "y" grados sobre el eje y
gl.glRotatef(rotY, 0, 1, 0);
gl.glRotatef(rotZ, 0, 0, 1);

//Definimos el color
gl.glColor3f(vcr, vcg, vcb);
if(opc==0)
{
gl.glBegin(GL.GL_POLYGON);
for(int i=0;i<100;i++)
{
float x = (float)Math.cos(i*2*Math.PI/100);
float y = (float)Math.sin(i*2*Math.PI/100);
gl.glVertex3f(0.2f*x, 0.2f*y,0);
}
gl.glEnd();

}
else
{
if(opc==1)
{
if(activo)
{
limpiarVariables();
activo=false;
}
gl.glBegin(GL.GL_LINES);
gl.glVertex3f(0, .25f,0);
gl.glVertex3f(0, -.25f,0);
gl.glEnd();
}
else
{
if(opc==2)
{
if(activo)
{
limpiarVariables();
activo=false;
}
gl.glBegin(GL.GL_TRIANGLES);
gl.glVertex3f(0, .25f,0);
gl.glVertex3f(-.25f, -.25f,0);
gl.glVertex3f(.25f, -.25f,0);
gl.glEnd();
}
else
{
if(opc==3)
{
if(activo)
{
limpiarVariables();
activo=false;
}
gl.glBegin(GL.GL_QUADS);
gl.glVertex3f(-.25f, -.25f,0);
gl.glVertex3f(.25f,-.25f,0);
gl.glVertex3f(.25f, .25f,0);
gl.glVertex3f(-.25f,.25f,0);
gl.glEnd();
}
else
{
if(opc==4)
{
if(activo)
{
limpiarVariables();
activo=false;
}
gl.glBegin(GL.GL_QUADS);
gl.glVertex3f(-.25f, -.5f,0);
gl.glVertex3f(.25f,-.5f,0);
gl.glVertex3f(.25f, .5f,0);
gl.glVertex3f(-.25f,.5f,0);
gl.glEnd();
}
else
{
if(activo)
{
limpiarVariables();
activo=false;
}
gl.glBegin(GL.GL_POLYGON);
gl.glVertex3f(-.25f,-.5f,0);
gl.glVertex3f(.25f,-.5f,0);
gl.glVertex3f(.5f,0,0);
gl.glVertex3f(.25f,.5f,0);
gl.glVertex3f(-.25f,.5f,0);
gl.glVertex3f(-.5f,0,0);
gl.glEnd();
}
}
}
}
}
gl.glFlush();
}

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

GL gl = drawable.getGL();
GLU glu = new GLU();
if(height<=0)
{
height=1;
}
float h= (float)width/ (float)height;

gl.glViewport(0, 0, width, height);


gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();

glu.gluPerspective(45.0f,h, 1.0, 20.0);


gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}

public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,


boolean deviceChanged) {

}
}
RESULTADOS DE LA APLICACIÓN

Al ejecutar la aplicación muestra la interfaz siguiente, en la cual indica el plano en 3D y


dibuja una imagen central

La cual cambiara de color si seleccionamos en cualquier botón de abajo


Si presionamos la tecla “+”, aumentara su tamaño de escala

Y presionando la tecla “-“, disminuirá su tamaño de escala


Si presionamos las teclas “Numero 1”: Grosor Fino, “Numero 2”: Grosor Medio.
“Numero 3”: Grosor Grueso

Podemos pintar la imagen presionando la tecla “Numero 6”


Presionando las teclas
Flecha Arriba: traslada arriba, Flecha Abajo: traslada abajo, Flecha Izquierda: traslada
Izquierda, Flecha Derecha: traslada a la Derecha, Sup: Traslada Adelante, Fin: Traslada
Atrás
Letra “W”: rota hacia arriba, Letra “S”: rota hacia abajo, Letra “A”: rota hacia izquierda,
Letra “D”: rota hacia la derecha, Letra “Q”; rota hacia adelante, Letra “E” rota hacia atrás.

S-ar putea să vă placă și