Hey all, I have been trying to get cubemapping to work for a long time now. I cannot figure it out, and good full source code for it is sparse. This is the only complete example I could find: http://tfc.duke.free.fr/
Does anybody know how to do it properly, my reflection is tied to the camera and I can’t figure out why. Can anybody help me?
Here is my application code if anyone wants to look at it, my shader code is exactly the same as tfc.duke.free.fr/.
for cube mapping u have to send to ur shader the camera position and the model matrix(and not the modelviewmatrix) of ur object that u want to apply the cube mapping .
so in ur GL code u have to separete the viewing transformation from modeling transfromation .
the modeling transformation is when u want to rotate/translate ur 3d model into game world.
the viewing transformation is to transform the world so it appears as if it is being viewed from a certain position in our world. then in ur GL code u will do something like this
//setup viwepoint of the global world
//translate,rotate or glulookat (lets call it matrix V)
//postion model1
//push matrix
//roatate translate model (lets call it matrix M)
//draw ur model : here each vertex of ur model will be multiplied by V*M
//pop matrix
//…
so back to ur shader . as i said u need to send the camera positon and the model matrix of ur object and not the modelview matrix(if u supply the modelview matrix the reflection will remain fix even if ur model moves ( as explained b4))
the question is how u can get the model matrix to send it to ur shader.
here an example :
//Ex,Ey,Ez define ur camera position in ur shader
gluLookAt(Ex,Ey,Ez,…);
//position ur model
glPushMatrix();
//here we will ask OpenGL to generate model matrix for us
glLoadIdentity();//back to identity matrix
glTranslatef(modelpos);//translate ur model
glRoatef(modelrot);//rotate ur model
float M[16];
glGetFloatv(GL_MODELVIEW_MATRIX,M);//M now contain the model
//matrix .this matrix must be sent to ur shader
glPopMatrix();//back to view matrix
glPushMatrix();
glTranslatef(modelpos);
glRoatetf(modelrot);
shader->begin();
shader->setuniform3fv(“camerapos”,vec3(Ez,Ey,Ez));
shader->setuniform4x4f(“modelmatrix”,M);
drawModel();
shader->end();
glPopMatrix();
here we asked OpenGL to generate our Model Matrix. u can always generate it manually ( check opengl red book appendix : modeling and transformations matrix)
hope this help
Your code is quite clear, and I believe I implemented it correctly, but something is still wrong in my code. The reflection is still stuck, and I do not use the ModelViewMatrix anymore. Here are my suspected areas.
For this line, I am not exactly sure what to put for the second and third arguements. I put a “1” there because it is the only thing that works. The boolean value is a mystery to me if I need T or F.
When doing applying the cubemap, I need to use the fourth texture unit right? Using, “glActiveTexture(GL_TEXTURE4);”? I then bind the cubemap using glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, texture[1]);
the application i provided works fine for ozone3d tutorial.
but the cube map texture must be stored in the texture unit 1(GL_TEXTURE1). i dont understand why u need the texture unit 4 (GL_TEXTURE4).
and if u are asking what to put in the texture unit 0(GL_TEXTURE0) u have to put the diffuse texture of ur model .if ur model is not textured then u have to modify ur shader code to accept the cube map texture as texture unit 0 (replace gl_TexCoord[1]).
so back to ozone3d application. u have to setup ur Opengl application to something like this :
glActiveTexture(GL_TEXTURE0);
glBindTexture( GL_TEXTURE_2D, diffuse_texture->GetID() );
glActiveTexture(GL_TEXTURE1);
glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, skybox->GetID() );
//activate shader
//send camera pos and model matrix
//…
//send textures uniforms
shader->setuniform1i(“colorMap”,0);
shader->setuniform1i(“cubeMap”,1);
…
last thing to say that the parameters u are sending to glUniformMatrix4fv function are correct as u are sending 1 matrix and u dont want the transpose matrix( third parameter=false)
last question is why ur reflections are stuck ? can u send an image of ur output shader ?
Here is a video of what happens. Disregard the fact that the multi-texturing does not work on the sphere and torus, I know how to fix that and I am concerned about the reflection
The reflected image is stuck, it appears to work on the other shapes, but the sphere makes it clear that the camera is still stuck right? I believe I have been coding this right, I am probably just making a noob mistake somewhere…
Can anybody help me? I appreciate the help so far, but can anyone help me through to the finish and not disappear. I feel like I am going to be stuck on this problem forever. I’ve been stuck for 3 weeks so far