So I've been trying to do picking code, but I've run across an issue before I even get started. I have a pointer that is rendered as a cube at the camera's position + 10 on the z axis, but when I run the application it's just sitting at 0, 0, 0. I'm completely stumped, because if the camera works so should the pointer. I'm doing this in java with lwjgl.
Here's my Pointer class:
import static org.lwjgl.opengl.GL11.*;
public class Pointer {
public static float[] getPosition(float cameraX, float cameraY, float cameraZ, float cameraXRotation, float cameraYRotation, float cameraZRotation){
float[] coords = new float[3];
coords[0] = cameraX;
coords[1] = cameraY;
coords[2] = cameraZ;
return coords;
}
public static void render(Color color, float x, float y, float z){
glPushMatrix();
{
glTranslatef(x, y, z);
glBegin(GL_LINES);
glColor3f(color.getRed(), color.getGreen(), color.getBlue());
{
glVertex3f(0, 0, 0);
glVertex3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 1);
glVertex3f(1, 0, 0);
glVertex3f(1, 1, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 1, 1);
glVertex3f(1, 1, 0);
glVertex3f(1, 1, 1);
glVertex3f(0, 1, 1);
glVertex3f(1, 1, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(0, 0, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, 0);
glVertex3f(0, 1, 1);
glVertex3f(0, 1, 0);
}
glEnd();
}
glPopMatrix();
}
}
this is the code that does the rendering:
float[] pointer = Pointer.getPosition(world.getCamera().getX(), world.getCamera().getY(), world.getCamera().getZ(), world.getCamera().getXRotation(), world.getCamera().getYRotation(), world.getCamera().getZRotation());
Pointer.render(world.getBasicsColor(), pointer[0], pointer[1], pointer[2] + 10);
and just in case, this is the camera class:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
public class Camera {
private float x;
private float y;
private float z;
private float rx;
private float ry;
private float rz;
private float fov;
private float ar;
private float ncp;
private float fcp;
public Camera(float fieldOfView, float aspectRatio, float nearClippingPlane, float farClippingPlane){
x = 0;
y = 0;
z = 0;
rx = 0;
ry = 0;
rz = 0;
fov = fieldOfView;
ar = aspectRatio;
ncp = nearClippingPlane;
fcp = farClippingPlane;
initProjection();
}
private void initProjection(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, ar, ncp, fcp);
glMatrixMode(GL_MODELVIEW);
}
public void use(){
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glRotatef(rz, 0, 0, 1);
glTranslatef(x, y, z);
}
public void look(char axis, float amount){
if(axis == 'x'){
rx += amount;
}else if(axis == 'y'){
ry += amount;
}else if(axis == 'z'){
rz += amount;
}
}
public void climb(float amount){
y += amount;
}
public void move(char type, float amount){
if(type == 'm'){
x += amount * Math.cos(Math.toRadians(ry + 90));
z += amount * Math.sin(Math.toRadians(ry + 90));
}else if(type == 's'){
x += amount * Math.cos(Math.toRadians(ry));
z += amount * Math.sin(Math.toRadians(ry));
}
}
public float getX(){
return x;
}
public float getY(){
return y;
}
public float getZ(){
return z;
}
public float getXRotation(){
return rx;
}
public float getYRotation(){
return ry;
}
public float getZRotation(){
return rz;
}
public float getFieldOfView(){
return fov;
}
public float getAspectRatio(){
return ar;
}
public float getNearClippingPlane(){
return ncp;
}
public float getFarClippingPlane(){
return fcp;
}
public void setX(float x){
this.x = x;
}
public void setY(float y){
this.y = y;
}
public void setZ(float z){
this.z = z;
}
public void setXRotation(float xRotation){
rx = xRotation;
}
public void setYRotation(float yRotation){
ry = yRotation;
}
public void setZRotation(float zRotation){
rz = zRotation;
}
public void setFieldOfView(float fieldOfView){
fov = fieldOfView;
}
public void setAspectRatio(float aspectRatio){
ar = aspectRatio;
}
public void setNearClippingPlane(float nearClippingPlane){
ncp = nearClippingPlane;
}
public void setFarClippingPlane(float farClippingPlane){
fcp = farClippingPlane;
}
}
Any help is greatly appreciated, thanks in advance!