PDA

View Full Version : how to do a rotated mirror?



noeska
11-01-2004, 05:15 PM
i came up with this. But rotation still is no good:



//create the reflection texture
procedure CreateTexture;
var
CP: array [0..3] of Double;
begin
//set up clipping plane
cp[0]:=0.0; //x
cp[1]:=0.0; //y
cp[2]:=0.000001; //z has to be somewhat larger then 0
cp[3]:=0.0; //w

//set up the viewport
glViewport(0, 0, M_SIZE, M_SIZE);

//clear the buffers
glClearColor(0.0, 0.0, 0.1, 0.0); // Background color
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

//set the camera pos
glLoadIdentity;
glLoadmatrixd(@matM);

// gltranslatef(mirrorpos.x,mirrorpos.y,mirrorpos.z);
// glrotatef(mirrorrot.x,1,0,0);
// glrotatef(mirrorrot.y,0,1,0);
// glrotatef(mirrorrot.z,0,0,1);


//mirror it
glScalef(1.0, 1.0, -1.0);

//enable clipping plane
glClipPlane(GL_CLIP_PLANE0, @CP);
glEnable(GL_CLIP_PLANE0);

//translate the mirror from its position back to 0
//actualy the scene is rotated with the reverse matrix of the mirror?
glrotatef(-mirrorrot.y,0,1,0);
glrotatef(-mirrorrot.x,1,0,0);
glrotatef(-mirrorrot.z,0,0,1);
gltranslatef(-mirrorpos.x,-mirrorpos.y,-mirrorpos.z);

//draw the scene...
DrawObjects;

//disable the clipping plane
glDisable(GL_CLIP_PLANE0);

//copy the color buffer to a texture creating a new texture if necessary
if &#40;Reflection <> 0&#41; then
begin
glBindTexture&#40;GL_TEXTURE_2D, Reflection&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP&#41;;
glCopyTexSubImage2D&#40;GL_TEXTURE_2D, 0, 0, 0, 0, 0,
M_SIZE, M_SIZE&#41;;
end else
begin
glGenTextures&#40;1, @Reflection&#41;;
glBindTexture&#40;GL_TEXTURE_2D, Reflection&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP&#41;;
glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP&#41;;
glCopyTexImage2D&#40;GL_TEXTURE_2D, 0, GL_RGB, 0, 0,
M_SIZE, M_SIZE, 0&#41;;
end;

//restore the viewport
glViewport&#40;0, 0, WND_XSIZE, WND_YSIZE&#41;;
end;

//calculate the texture coordinates for a vertex and send them to OpenGL
procedure RenderVertex&#40;vX, vY, vZ&#58; Single&#41;;
var
tX, tY, tZ&#58; Double;
const
vp&#58; TGLVectori4 = &#40;0,0,1,1&#41;; //dummy viewport
begin
//calculate the window coordinates of the vertex
gluProject&#40;vX, vY, vZ, matM, matP, vp, @tX, @tY, @tZ&#41;;
//use the window coords as texture coords
glTexCoord2f&#40;tX, tY&#41;;
glVertex3f&#40;vX, vY, vZ&#41;;
end;

// Function to draw the actual scene
procedure glDraw&#40;&#41;;
var
i,j&#58; integer;
begin

Time &#58;= GetTickCount / 1000.0;

//set mirror pos...
// mirrorpos.x&#58;=Sin&#40;Time * 1.3&#41;;
// mirrorpos.y&#58;=Sin&#40;Time * 1.3&#41;;
// mirrorpos.z&#58;=Sin&#40;Time * 1.3&#41;;

mirrorrot.x&#58;=Sin&#40;Time * 1.3&#41; * 60;
mirrorrot.y&#58;=Sin&#40;Time * 1.4&#41; * 60;
mirrorrot.z&#58;=Sin&#40;Time * 1.5&#41; * 60;

glLoadIdentity;

//set camera pos...
gltranslatef&#40;0,-5,-20&#41;;
// glrotatef&#40;90,0,1,0&#41;;

//Get the modelview and projection matrices
glGetDoublev&#40;GL_MODELVIEW_MATRIX, @matM&#41;;
glGetDoublev&#40;GL_PROJECTION_MATRIX, @matP&#41;;

CreateTexture; //create the mirror texture

glClearColor&#40;0.0, 0.0, 0.2, 0.0&#41;; // Background color
glClear&#40;GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT&#41;; //comment this line out the see the last renderend mirror scene
glLoadIdentity&#40;&#41;;
glLoadmatrixD&#40;@matM&#41;;

glLightfv&#40;GL_LIGHT0, GL_POSITION, @lp&#41;; //position light

//render the mirror
glColor4f&#40;1, 1, 1, 0.8&#41;; // make the water transparent.
gldisable&#40;GL_LIGHTING&#41;;
glenable&#40;GL_TEXTURE_2D&#41;;
glBindTexture&#40;GL_TEXTURE_2D, Reflection&#41;; //use calculated mirror texture
glpushmatrix&#40;&#41;;
//place the mirror
gltranslatef&#40;mirrorpos.x,mirrorpos.y,mirrorpos.z&#41;;

glrotatef&#40;mirrorrot.z,0,0,1&#41;;
glrotatef&#40;mirrorrot.x,1,0,0&#41;;
glrotatef&#40;mirrorrot.y,0,1,0&#41;;


//do the actual rendering
//by using RenderVertex the texture coords are recalculated
for I &#58;= -&#40;mirrorsize div 2&#41; to &#40;mirrorsize div 2&#41; - 1 do
begin
glBegin&#40;GL_QUAD_STRIP&#41;;
for J &#58;= -&#40;mirrorsize div 2&#41; to &#40;mirrorsize div 2&#41; do
begin
RenderVertex&#40;I / M_DETAIL - 0.5,
J / M_DETAIL - 0.5, 0.0&#41;;
RenderVertex&#40;&#40;I + 1&#41; / M_DETAIL - 0.5,
J / M_DETAIL - 0.5, 0.0&#41;;
end;
glEnd;

end;
glpopmatrix&#40;&#41;;
gldisable&#40;GL_TEXTURE_2D&#41;;
glenable&#40;GL_LIGHTING&#41;;

//draw the scene...
drawobjects&#40;&#41;;

// Flush the OpenGL Buffer
glFlush&#40;&#41;;

end;
[/code]

Alimonster
14-01-2004, 10:09 PM
I'll see if I can come up with something for you tomorrow.