PDA

View Full Version : OpenGl background and part model proble



Digga
12-01-2009, 10:50 AM
Hello,
I have started a project and Im using openGl to render a 3D partview.
Ive drawn a quad with the color gradient that I want to use as the background. And I used some existing code the draw a box with XYZ postion indicatiors.

Here is the code:
void CAvalonView::DrawBackGround(void)
{
//glMatrixMode( GL_MODELVIEW );
// glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glBegin(GL_QUADS); // Start Drawing A Quad
glColor4f(0.2f,0.2f,0.6f,1.0f);
glVertex3f(-1.0f, 1.0f, -2.0f);// Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, -2.0f); // Top Right Of The Quad
glColor4f(0.9f,0.9f,1.0f,1.0f);
glVertex3f( 1.0f,-1.0f, -2.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, -2.0f); // Bottom Left Of The Quad
glEnd();

}

void CAvalonView::oglDrawScene(void)
{


DrawBackGround();

// Wireframe Mode


glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
glColor3f(0.f,0.f,1.f);
// Front Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);

// Back Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);

// Top Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);

// Bottom Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

// Right Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);

// Left Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

// draw carthesian axes
glBegin(GL_LINES);
// red x axis
glColor3f(1.f,0.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,-0.1f,0.0f);
// green y axis
glColor3f(0.f,1.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.1f,0.9f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
// blue z axis
glColor3f(0.f,0.f,1.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.1f,0.9f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,-0.1f,0.9f);
glEnd();

What am I doing wrong?

Thanks
Derrek





}

AdrianPi
12-01-2009, 12:04 PM
I need more information. What specific problem do you have?

Digga
12-01-2009, 12:45 PM
Thanks for the reply,
In DrawBackground() if I enable GL(MODELVIEW) and Load Identity
I get the background the way I like it but nothing from oglDrawScene()

If I disable GL(MODELVIEW) and LoadIdentity I get oglDrawScene drawing correctly but the background quad is just a extra piece of the drawing and rotates and zooms along with the model.

Basically I want DrawBackground to not move and stay in the back ground and the " model" the draw infront of the background.

I hope that clearer

Derrek

AdrianPi
12-01-2009, 01:12 PM
Well, you should set the modelview matrix to identity when drawing the background and to whatever transform you want for the cube. You can safely accomplish this by pushing and popping:


glMatrixMode(GL_MODELVIEW); // maybe not needed
glPushMatrix();
glLoadIdentity();
DrawBackGround();
glPopMatrix();


Also I would suggest to disable z writing and testing when drawing the background:

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

// Then as before
glMatrixMode(GL_MODELVIEW); // maybe not needed
glPushMatrix();
glLoadIdentity();
DrawBackGround();
glPopMatrix();

// And reenable those
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);

Digga
12-01-2009, 01:15 PM
Thanks so much

that fixed it!!!!!

Thanks again
Derrek