I’ve rendered a textured 3D cube on screen with glDrawElements. Pretty much built upon the OpenGL tuts on the lwjgl wiki.
Now I want to add cubes to the sides of an existing cube by clicking on the sides, I figured I’d at least need some 3D coordinates for that. Now there’s some great posts to find on the internet about using gluUnProject, but they seem to use matrices for immediate rendering, and not the matrices I’ve been using. (Stuff about Uniform, shader programs, location ints, etc. I only grasp half of it, but keep learning bit by bit)
Getting the 3D position
public static FloatBuffer getOGLPos(int x, int y){
IntBuffer viewport = BufferUtils.createIntBuffer(16);
FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
FloatBuffer projection = BufferUtils.createFloatBuffer(16);
FloatBuffer winZ = BufferUtils.createFloatBuffer(3);
float winX, winY;
FloatBuffer position = BufferUtils.createFloatBuffer(16);
/* This doesn't seem to be the right thing to do, since I use newer openGL and all...
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
*/
GL20.glGetUniform(renderer.pId, renderer.modelMatrixLocation, modelview); // This is what I came up with after some google-ing am I doing this right?
GL20.glGetUniform(renderer.pId, renderer.projectionMatrixLocation, projection);
GL20.glGetUniform(renderer.pId, renderer.viewMatrixLocation, viewport);
winX = (float)x;
winY = (float)viewport.get(3) - (float)y;
GL11.glReadPixels(x, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);
if (winZ.get(0) == 1) {
return null;
}
GLU.gluUnProject(winX, winY, winZ.get(), modelview, projection, viewport, position);
return position;
}
Printing the info in the console
while (Mouse.next()) {
if (Mouse.isButtonDown(1)) {
FloatBuffer posBuffer = Utils.getOGLPos(Mouse.getX(), Mouse.getY());
System.out.println("Clicked @ x: " + posBuffer.get(0) + " y: " + posBuffer.get(1) + " z: " + posBuffer.get(2));
}
}
As output I get NaN’s for the x, y and z. I’m doing something wrong, somewhere. Do I really need to use glGetUniform? Because using glGetFloat seemed to f**k with my matrices (I get the 3D position in the console, but an openGL error right after that). I figured that was because I was using GL_MODELVIEW_MATRIX, something I only used in openGL examples/tuts using immediate mode.
Please note, I’ve only used Java (and serious programming altogether) since October and OpenGL since a few weeks. I’m rushing head over heels into game programming to get a portfolio set up before/during the summer break. I’ll make loads of mistakes, sorry. But at least I’m learning, and fast too.
Long story short: Look at code, facepalm, cuss, give link/corrected code/stuff I need to look up on.