Hi !
My project includes OpenGL API and most recently shader programming.
The thing is: I need to know world coordinates of vertexes in my Vertex shader. So I do this:
vec4 pos= gl_ModelViewMatrix * gl_Vertex;
Then I can do whatever I want to “pos” which is the world coordinates of my Vertex. And at the end, I do this:
gl_Position = gl_ProjectionMatrix * pos;
But whenever I decide to move the camera, it changes world coordinates !
This is my rendering process:
public void draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear The Screen And The Depth Buffer
shaders.use();
set3D();
camera();
Map.draw();
shaders.release();
set2D();
//... 2D GUI
}
public void camera(){
glRotated(-avatar.getYangle(),1,0,0);
glRotated(-avatar.getViewangle(),0,1,0);
glTranslated(-avatar.getX(), -avatar.getY(), -avatar.getZ());
}
To solve the problem, I created an uniform variable in my shader which corresponds to camera position, and I set this variable in my camera() function so I can substract camera position to “pos”. But I 'm not sure this is the best thing to do as I should do the same thing for rotation and it can be time consuming… So is it the best way to deal with this problem or is there a better way ?
Thanks in advance for your help