PDA

View Full Version : Basic question / 2D - 3D sequence



TUAWhite
02-23-2009, 03:09 AM
Hi,

i just started doing some programming in OpenGL. I looked at several tutorials. I have following scenario.

1.) Draw an image as the background (512x512)
2.) Draw some text somewhere
3.) Draw a sphere in the bottom corner (spining)
4.) Draw a window which is partialy overlapping the sphere. (Blend 50%).

The single pieces are working. So drawing the background and the sphere works etc. But as soon as I add the entire thing (drawing 2D,3D and then 2D again) the 3D stuff is missing. Obviously I missed some fundamentals here. Can somebody please tell me how to solve this ? (I thing it has to do with the switching of the Ortho and perspeictive view)

I have added the part of the code. (Delphi)

procedure TForm1.SceneDraw();
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Poloygon are filled.
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

// View position
gluLookAt(0, 0, 10, 0, 0, 0, 0, 10, 0);

DrawBackground(); // Ortho
DrawText('asdf'); // Ortho
DrawSphere(); // Model
DrawWindow(); // Ortho

SwapBuffers(fDeviceContext);
end;

procedure TForm1.DrawBackground();
begin
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fTexBack);

ViewOrtho();

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex2f(0, 0);
glTexCoord2f(1.0, 0.0);
glVertex2f(Width, 0);
glTexCoord2f(1.0, 1.0);
glVertex2f(Width, Height);
glTexCoord2f(0.0, 1.0);
glVertex2f(0, Height);

glEnd();

glEnable(GL_DEPTH_TEST);
end;

procedure TForm1.DrawText(pText: string);
begin
glDisable(GL_DEPTH_TEST);
ViewOrtho;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
fnt.Draw( 10, 100, 'This is my test text', 0);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
end;

procedure TForm1.DrawWindow();
begin
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, fTexWindow);

ViewOrtho;

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex2f(100, 100);
glTexCoord2f(1.0, 0.0);
glVertex2f(100, 300);
glTexCoord2f(1.0, 1.0);
glVertex2f(200, 300);
glTexCoord2f(0.0, 1.0);
glVertex2f(200, 100);
glEnd();
glEnable(GL_DEPTH_TEST);
end;

procedure TForm1.DrawSphere();
begin
ViewPerspective();
glEnable(GL_TEXTURE_2D);
if blnWireFrame then glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBindTexture(GL_TEXTURE_2D, fTexEarth);
glTranslatef(3.3, -3.3, -1.5);

// Rotate the sphere
glRotatef(-65, 1, 0, 0);
glRotatef(-40, 0, 1, 0);
glRotatef(fRot, 0, 0, 1);

// Push the new matrix.
glPushMatrix();
glCallList(1);
glPopMatrix();

// Increate the rotation.
fRot := fRot + 1;
end;

// Set Up An Ortho View
procedure TForm1.ViewOrtho;
begin
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, Width, 0, Height, 0, 0.9);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
end;

// Set Up A Perspective View
procedure TForm1.ViewPerspective;
begin
glMatrixMode( GL_PROJECTION );
glPopMatrix();

glMatrixMode( GL_MODELVIEW );
glPopMatrix();
end;

Budric
02-23-2009, 02:26 PM
You are essentially pushing 2 othrographic projection matrices onto the stack with VieOrtho. Then your ViewPerspective() pops one off - and you're back to original orthographic projection. That's not how you set perspective projection.

It should look something like:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(degrees, 1.0, zNear, zFar);

So you reset whatever GL_PROJECTION matrix exists and then multiply it by projection matrix generated by gluPerspective().

TUAWhite
02-24-2009, 06:37 AM
Thanks a lot Budric, after some changes in the ViewPerspective and ViewOrtho the stuff is now working as aspected. Again thanks !