Reputation: 13
I worked through lot of tutorials and finally created a nice cube simulator.
Now I want to implement ray picking. For this I created a simple GL_LINE_STRIP to visualize my ray. Unfortunately when I check near and far point values they make no sense or I interprete them wrong. I would expect near point close to camera origin, but it is close to 0, therefore my ray starts in the middle of my scene.
Projectionmatrix:
Matrix.perspectiveM(projectionmatrix, 0, 20.0f, (float) mWidth / (float) mHeight, 0.1f, 300.0f);
ViewMatrix:
camX = (float) (Math.sin(Math.toRadians(phi)) * Math.cos(Math.toRadians(counter)) * radius);
camY = (float) (Math.sin(Math.toRadians(phi)) * Math.sin(Math.toRadians(counter)) * radius);
camZ = (float) Math.cos(Math.toRadians(phi)) * radius;
Matrix.setLookAtM(ViewMatrix, 0, camX, camY, camZ, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
Ray Pick:
float winx = xTouch, winy =(float)viewport[3] - yTouch;
int result = GLU.gluUnProject(winx, winy, 1.0f, ViewMatrix, 0, projectionmatrix, 0, viewport, 0, temp, 0);
Matrix.multiplyMV(temp2, 0, ViewMatrix, 0, temp, 0);
if(result == GL10.GL_TRUE){
near[0] = temp2[0] / temp2[3];
near[1] = temp2[1] / temp2[3];
near[2] = temp2[2] / temp2[3];
}
result = GLU.gluUnProject(winx, winy, 0.0f, ViewMatrix, 0, projectionmatrix, 0, viewport, 0, temp3, 0);
Matrix.multiplyMV(temp2,0,ViewMatrix, 0, temp3, 0);
if(result == GL10.GL_TRUE){
far[0] = temp2[0] / temp2[3];
far[1] = temp2[1] / temp2[3];
far[2] = temp2[2] / temp2[3];
}
P0 = near;
P1 = far;
float lineVerticesData[] =
{
// positions // colors // texture
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
};
lineVerticesData[0] = P0[0];
lineVerticesData[1] = P0[1];
lineVerticesData[2] = P0[2];
lineVerticesData[9] = P1[0];
lineVerticesData[10] = P1[1];
lineVerticesData[11] = P1[2];
VBO_BindBufferFloat(VBOIds[3],Float_to_FloatBuffer(lineVerticesData),lineVerticesData.length);
VAO_BindBuffer(VAOId[2],VBOIds[3],0);
Log: lineVerticsData: [8.532211, 49.11434, -50.042286, 1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0054870476, -0.0148345595, -0.09998755, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0
I also followed the solution from here, but I was not able to get a proper ray: https://antongerdelan.net/opengl/raycasting.html
int[] viewport = {0, 0, width, height};
float x = (2.0f * xTouch) / width - 1.0f;
float y = 1.0f - (2.0f * yTouch) / height;
Matrix.setIdentityM(inverseview,0);
Matrix.invertM(inverseview,0,ViewMatrix,0);
Matrix.invertM(inverseproj,0, projectionmatrix,0);
Matrix.setIdentityM(initcoords,0);
Matrix.translateM(initcoords,0,x,y,0);
Matrix.multiplyMM(helpm,0, inverseproj,0,initcoards,0);
Matrix.multiplyMM(tmodelmatrix,0, inverseview,0,helpm,0);
private static float DefaultShapeVertices[] =
{
// positions // colors // texture coords
0.0f, 0.0f, -100.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f,
0.0f, 0.0f, 100.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, }
Shaderprog1.setMat4("model", tmodelmatrix);
Upvotes: 0
Views: 26