Estaba viendo un libro de Programación para gráficos 3D en Java con JOGL y JML, y el ejercicio que propone el libro se trata de animar una pirámide y dos cubos, como si la pirámide fuese el sol y los cubos la tierra y la luna respectivamente utilizando Matrix Stack.
He probado a animar sin Matrix Stack y me ha funcionado bien, pero con Matrix Stack no logro hacer que se mueva, si alguien puede echar un vistazo al programa y ver que falla.
Pongo el código completo para que se pueda probar.
Gracias de antemano.
package code;
import javax.swing.*;
import java.lang.Math;
import java.nio.*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GLContext;
import static com.jogamp.opengl.GL4.*;
import static com.jogamp.opengl.GL.GL_ARRAY_BUFFER;
import static com.jogamp.opengl.GL.GL_COLOR_BUFFER_BIT;
import static com.jogamp.opengl.GL.GL_DEPTH_BUFFER_BIT;
import static com.jogamp.opengl.GL.GL_DEPTH_TEST;
import static com.jogamp.opengl.GL.GL_LEQUAL;
import static com.jogamp.opengl.GL.GL_STATIC_DRAW;
import static com.jogamp.opengl.GL2ES2.GL_FRAGMENT_SHADER;
import static com.jogamp.opengl.GL2ES2.GL_VERTEX_SHADER;
import com.jogamp.opengl.util.Animator;
import org.joml.*;
public class Code extends JFrame implements GLEventListener
{
/**
* Atributos privados
*/
private GLCanvas myCanvas;
private int renderingProgram;
private int vao[] = new int[1];
private int vbo[] = new int[2];
private float cameraX, cameraY, cameraZ;
// allocate variables used in display() function, so that they won’t need to be allocated during rendering
private FloatBuffer vals = Buffers.newDirectFloatBuffer(16); // utility buffer for transferring matrices
private Matrix4f pMat = new Matrix4f(); // perspective matrix
private Matrix4fStack mvStack = new Matrix4fStack(5);
private int mvLoc, projLoc;
private float aspect;
private final float startTime;
/**
* Constructor
*/
public Code()
{
setTitle("Chapter 4 - Matrix Stack");
setSize(600, 600);
setLocation(400, 50);
myCanvas = new GLCanvas();
myCanvas.addGLEventListener(this);
this.add(myCanvas);
Animator animtr = new Animator(myCanvas);
animtr.start();
startTime = System.currentTimeMillis();
setVisible(true);
}
/**
* Métodos públicos
*/
@Override public void init(GLAutoDrawable drawable)
{
GL4 gl = (GL4) GLContext.getCurrentGL();
renderingProgram = createShaderProgram("vertShader.glsl", "fragShader.glsl");
setupVertices();
cameraX = 0.0f;
cameraY = 0.0f;
cameraZ = 10.0f;
}
@Override public void display(GLAutoDrawable drawable)
{
GL4 gl = (GL4) GLContext.getCurrentGL();
gl.glClear(GL_DEPTH_BUFFER_BIT);
gl.glClear(GL_COLOR_BUFFER_BIT);
gl.glUseProgram(renderingProgram);
// get references to the uniform variables for the MV and projection matrices
mvLoc = gl.glGetUniformLocation(renderingProgram, "mv_matrix");
projLoc = gl.glGetUniformLocation(renderingProgram, "proj_matrix");
// build perspective matrix. This one has fovy = 60, aspect ratio matches the screen window.
// Values for near and far clipping planes can vary as discussed in Section 4.9
aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
pMat.setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);
gl.glUniformMatrix4fv(projLoc, 1, false, pMat.get(vals));
double elapsedTime = System.currentTimeMillis() - startTime;
double tf = elapsedTime / 1000.0;
// push view matrix onto the stack
mvStack.pushMatrix();
mvStack.translate(-cameraX, -cameraY, -cameraZ);
// ---------- Pyramid == Sun ------------------
mvStack.pushMatrix();
mvStack.translate(0.0f, 0.0f, 0.0f); // Sun position
mvStack.pushMatrix();
mvStack.rotate((float)tf, 1.0f, 0.0f, 0.0f); // sun’s rotation on its axis
gl.glUniformMatrix4fv(mvLoc, 1, false, mvStack.get(vals));
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(0);
gl.glEnable(GL_DEPTH_TEST);
gl.glDrawArrays(GL_TRIANGLES, 0, 18); // draw the sun
mvStack.popMatrix(); // remove the sun’s axial rotation from the stack
//------------ cube == planet ------------------------
mvStack.pushMatrix();
mvStack.translate((float)Math.sin(tf)*4.0f, 0.0f, (float)Math.cos(tf)*4.0f); // planet moves around sun
mvStack.pushMatrix();
mvStack.rotate((float)tf, 0.0f, 1.0f, 0.0f); // planet axis rotation
mvStack.scale(0.50f, 0.50f, 0.50f);
gl.glUniformMatrix4fv(mvLoc, 1, false, mvStack.get(vals));
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(0);
gl.glDrawArrays(GL_TRIANGLES, 0, 36); // draw the planet
mvStack.popMatrix(); // remove the planet’s axial rotation from the stack
//--------- smaller cube == moon ---------
mvStack.pushMatrix();
mvStack.translate(0.0f, (float)Math.sin(tf)*2.0f, (float)Math.cos(tf)*2.0f); // moon moves around planet
mvStack.rotate((float)tf, 0.0f, 0.0f, 1.0f); // moon’s rotation on its axis
mvStack.scale(0.15f, 0.15f, 0.15f); // make the moon smaller
gl.glUniformMatrix4fv(mvLoc, 1, false, mvStack.get(vals));
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(0);
gl.glDrawArrays(GL_TRIANGLES, 0, 36); // draw the moon
// remove moon scale/rotation/position, planet position, sun position, and view matrices from stack
mvStack.popMatrix(); mvStack.popMatrix(); mvStack.popMatrix(); mvStack.popMatrix();
}
public static void main(String[] args)
{
new Code();
}
@Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
@Override public void dispose(GLAutoDrawable drawable) { }
/**
* Métodos privados
*/
private void setupVertices()
{
GL4 gl = (GL4) GLContext.getCurrentGL();
// 36 vertices of the 12 triangles making up a 2 x 2 x 2 cube centered at the origin
float[ ] cubePositions =
{
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f
};
// pyramid with 18 vertices, comprising 6 triangles (four sides, and two on the bottom)
float[ ] pyramidPositions =
{
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // front face
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // right face
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // back face
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // left face
-1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, // base – left front
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f // base – right back
};
gl.glGenVertexArrays(vao.length, vao, 0);
gl.glBindVertexArray(vao[0]);
gl.glGenBuffers(vbo.length, vbo, 0);
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // cubePositions
FloatBuffer cubeBuf = Buffers.newDirectFloatBuffer(cubePositions);
gl.glBufferData(GL_ARRAY_BUFFER, cubeBuf.limit()*4, cubeBuf, GL_STATIC_DRAW);
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // pyramidPositions
FloatBuffer pyrBuf = Buffers.newDirectFloatBuffer(pyramidPositions);
gl.glBufferData(GL_ARRAY_BUFFER, pyrBuf.limit()*4, pyrBuf, GL_STATIC_DRAW);
}
private int createShaderProgram(String vertFile, String fragFile)
{
GL4 gl = (GL4) GLContext.getCurrentGL();
final String workingPath = "src/code/";
String vshaderSource[ ] = readShaderSource(workingPath + vertFile);
String fshaderSource[ ] = readShaderSource(workingPath + fragFile);
int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0);
gl.glCompileShader(vShader);
int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0);
gl.glCompileShader(fShader);
int vfprogram = gl.glCreateProgram();
gl.glAttachShader(vfprogram, vShader);
gl.glAttachShader(vfprogram, fShader);
gl.glLinkProgram(vfprogram);
gl.glDeleteShader(vShader);
gl.glDeleteShader(fShader);
return vfprogram;
}
private String[] readShaderSource(String filename)
{
ArrayList<String> lines = new ArrayList<String>();
Scanner sc;
try
{
sc = new Scanner(new File(filename));
}
catch (IOException e)
{
System.err.println("IOException reading file: " + e);
return null;
}
while (sc.hasNext())
lines.add(sc.nextLine());
String[ ] program = new String[lines.size()];
for (int i = 0; i < lines.size(); i++)
program[i] = (String) lines.get(i) + "\n";
return program;
}
}
Vertex shader
#version 430
layout (location = 0) in vec3 position;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
out vec4 varyingColor;
void main(void)
{
gl_Position = proj_matrix * mv_matrix * vec4(position, 1.0);
varyingColor = vec4(position, 1.0) * 0.5 + vec4(0.5, 0.5, 0.5, 0.5);
}
Fragment Shader
#version 430
in vec4 varyingColor;
out vec4 color;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{
color = varyingColor;
}