Mesa 01
Mesa 01
Mesa 01
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLJPanel;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.awt.ImageUtil;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
};
// forma
private float[] polygonCoords = {
// cuadrado
-1f, 0.2f, 0f, -1f, 1f, 0f, -0.6f, 1f, 0f, -0.6f, 0.2f, 0f,
// fantastma anaranjado
0.0f, 0.0f, 0, 0.2f, 0.0f, 0, 0.2f, 0.2f, 0, 0.0f, 0.2f, 0,
// fantasma rojo
0.0f, 0.0f, 0, 0.2f, 0.0f, 0, 0.2f, 0.2f, 0, 0.0f, 0.2f, 0,
};
// Textura de los cuadrados
private float[] polygonTexture = {
// cuadrado
0.017f, 0.386f, 0f, 0.017f, 0.984f, 0f, 0.409f, 0.9846f, 0f,
0.409f, 0.386f, 0f,
// fantastma anaranjado
0.55f, 0.39f, 0, 0.82f, 0.39f, 0, 0.82f, 0.68f, 0, 0.55f, 0.68f,
0,
// fantasma rojo
0.56f, 0.7f, 0, 0.83f, 0.7f, 0, 0.83f, 0.98f, 0, 0.56f, 0.98f, 0,
};
// Color de los poligonos
private float[] polygonFaceColors = {
// cuadsrado
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
// fantasma
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
};
// naranja 1
ghost g3 = new ghost(-0.4f, 1.5f);
// naranja 2
ghost g4 = new ghost(-0.4f, 1.2f);
public Principal() {
super(new GLCapabilities(null)); // Makes a panel with default OpenGL
"capabilities".
setDoubleBuffered(true);
setPreferredSize(new Dimension(720, 480));
addGLEventListener(this); // A listener is essential! The listener is
where the OpenGL programming lives.
addKeyListener(this);
@Override
public void display(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
GL2 gl = drawable.getGL().getGL2();
// gl.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
// gl.glEnable(GL2.GL_LIGHT0);
// gl.glEnable(GL2.GL_LIGHTING);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// cuadrados
textures[0].bind(gl);
gl.glPushMatrix();
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0.8f, 0, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(0.8f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(1.6f, 0f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(1.6f, -1.2f, 0);
gl.glDrawArrays(GL2.GL_QUADS, 0, 4);
gl.glPopMatrix();
gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
textures[0].bind(gl);
gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g3.getX(), -(0.2f) / 2 + g3.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 4, 4);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g1.getX(), -(0.2f) / 2 + g1.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
gl.glPopMatrix();
gl.glPushMatrix();
gl.glTranslatef(-(0.2f) / 2 + g2.getX(), -(0.2f) / 2 + g2.getY(), 0);
gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
gl.glPopMatrix();
// gl.glPushMatrix();
// gl.glTranslatef(-(0.2f) / 2 + g4.getX(), -(0.2f) / 2 + g4.getY(), 0);
// gl.glDrawArrays(GL2.GL_POLYGON, 8, 4);
// gl.glPopMatrix();
if (g1.getY() >= -1.2f) {
if (g1.y <= 0f) {
if (g1.x > -0.4f) {
g1.x -= g1.movx;
} else {
g1.y -= g1.movy;
}
} else {
g1.y -= g1.movy;
}
} else {
g1.x = 0.4f;
g1.y = 1.2f;
} else {
g2.y -= g2.movy;
}
} else {
g2.x = 0.4f;
g2.y = 1.2f;
} else {
g3.y -= g3.movy;
}
} else {
g3.x = -0.4f;
g3.y = 1.2f;
}
if (g4.getY() >= -1.2f) {
if (g4.y <= 0f) {
if (g4.x < 0.4f) {
g4.x += g4.movx;
} else {
g4.y -= g4.movy;
}
} else {
g4.y -= g4.movy;
}
} else {
g4.x = -0.4f;
g4.y = 1.2f;
}
colision();
gl.glDisable(GL2.GL_LIGHTING);
gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL2.GL_COLOR_ARRAY);
gl.glFlush();
void colision() {
colisionfantastamas(g1, g2);
colisionfantastamas(g1, g3);
colisionfantastamas(g1, g4);
colisionfantastamas(g2, g3);
colisionfantastamas(g2, g4);
colisionfantastamas(g3, g4);
}
void colisionfantastamas(ghost g1, ghost g2) {
if (g1.getY() >= -1.2f) {
if (g1.getY() <= 0f) {
if (g1.getX() > -0.4f) {
g1.setX(g1.getX() - g1.movx);
} else {
g1.setY(g1.getY() + g1.movy);
}
} else {
g1.setY(g1.getY() + g1.movy);
}
} else {
g1.setX(0.4f);
g1.setY(1.2f);
}
double boca = 0;
int rotar = 0;
boolean abierto = true;
float xpacma = 0.5f;
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
// arriba
if (key == KeyEvent.VK_UP) {
}
// abajo
if (key == KeyEvent.VK_DOWN) {
}
// Derecha
if (key == 39) {
// Izquierda
if (key == 37) {
rotar = 180;
if (xpacma >= -1.1f) {
xpacma -= 0.01f;
} else {
xpacma = 1.1f;
}
}
// espacio
if (key == 32) {
@Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
@Override
public void init(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
GL2 gl2 = drawable.getGL().getGL2();
gl2.glClearColor(1f, 1f, 1f, 1.0f);
@Override
public void reshape(GLAutoDrawable drawable, int arg1, int arg2, int arg3,
int arg4) {
// TODO Auto-generated method stub
GL2 gl = drawable.getGL().getGL2();
}
@Override
public void keyTyped(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
@Override
public void keyReleased(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
public ghost() {
}
}