Left is right?
Here''s the situation that I don''t understand.
I''m trying to draw a spinning triangle on the left, a square on the right (exactly like on Nehe''s lesson 3)
But, when I port the code onto my MFC child window, I get the triangle on the right, square on the left. The y and z axis are both correct. It''s just seems the x-axis is flipped around. All the code is for the OpenGL bit is the same as from Nehe''s lesson 3, so I don''t understand what''s wrong...
void CGlView::vDrawGLScene()
{
static GLfloat rtri;
static GLfloat rquad;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,0.1f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
glBegin(GL_TRIANGLES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Set Top Point Of Triangle To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // First Point Of The Triangle
glColor3f(0.0f,1.0f,0.0f); // Set Left Point Of Triangle To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Second Point Of The Triangle
glColor3f(0.0f,0.0f,1.0f); // Set Right Point Of Triangle To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Third Point Of The Triangle
glEnd(); // Done Drawing The Triangle
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0
glRotatef(rquad,1.0f,0.0f,0.0f); // Rotate The Quad On The X axis ( NEW )
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad
rtri+=1.2f; // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=1.15f; // Decrease The Rotation Variable For The Quad ( NEW )
// swap the buffer
SwapBuffers(m_hDC);
}
CGlView::CGlView(CWnd *pclWnd)
{
m_pclWnd = pclWnd;
m_hDC = ::GetDC(pclWnd->m_hWnd); // Get Handle of Static Box
BOOL bSuccess;
RECT rect;
m_pclWnd->GetClientRect(&rect); // Get dimensions of box
int iWidth = rect.right - rect.left;
int iHeight = rect.top - rect.bottom;
bSuccess = bSetWindowPixelFormat(m_hDC); // Set Pixel Format in that box
ASSERT(&bSuccess);
bSuccess = bCreateViewGLContext(m_hDC); // Create a OpenGL view in Box
ASSERT(&bSuccess);
vInitGl(iWidth, iHeight); // Call our initialization routine
g_pclGLView = this;
}
void CGlView::vInitGl(int iWidth, int iHeigth)
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glViewport(0,0,iWidth,iHeigth); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)iWidth/(GLfloat)iHeigth,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
Try using the matrix stacks with glPushMatrix() and glPopMatrix(). Try it like this:
// Push scene into view
glTranslatef( 0.0f, 0.0f, -6.0f );
glPushMatrix();
glRotatef(blah);
DrawShape1();
glPopMatrix();
glPushMatrix();
glRotatef(blah);
DrawShape();
glPopMatrix();
IncrementRotation();
SwapBuffers();
-SirKnight
// Push scene into view
glTranslatef( 0.0f, 0.0f, -6.0f );
glPushMatrix();
glRotatef(blah);
DrawShape1();
glPopMatrix();
glPushMatrix();
glRotatef(blah);
DrawShape();
glPopMatrix();
IncrementRotation();
SwapBuffers();
-SirKnight
Thanks for replying, but unfortunately, I tried what you suggested (Push/Pop) and it didn''t work. The x-axis is still flipped. (triangle on right, square on left)
you can test this by downloading this source code from OpenGL''s website:
http://www.opengl.org/developers/faqs/technical/mswindows.htm#mswi0160
and then paste this little code from NeHe''s Lesson 4
//====================================================
void CGlView::vDrawGLScene()
{
static GLfloat rtri;
static GLfloat rquad;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f); //<--- should move left
glRotatef(rtri,0.0f,0.1f,0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glTranslatef(3.0f,0.0f,0.0f); // //<--- should move right
glRotatef(rquad,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
rtri+=1.2f;
rquad-=1.15f;
// swap the buffer
SwapBuffers(m_hDC);
}
//====================================================
I also discovered a couple things. If you run their program under "release" mode instead if "debug" mode, all the colors disappear and shows an ugly red. But if you close and run it again immediately, sometimes the color will show up again. VERY strange.
Can anyone solve this problem or is MFC+OpenGL doomed?
you can test this by downloading this source code from OpenGL''s website:
http://www.opengl.org/developers/faqs/technical/mswindows.htm#mswi0160
and then paste this little code from NeHe''s Lesson 4
//====================================================
void CGlView::vDrawGLScene()
{
static GLfloat rtri;
static GLfloat rquad;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f); //<--- should move left
glRotatef(rtri,0.0f,0.1f,0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glTranslatef(3.0f,0.0f,0.0f); // //<--- should move right
glRotatef(rquad,1.0f,0.0f,0.0f);
glColor3f(0.5f,0.5f,1.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
rtri+=1.2f;
rquad-=1.15f;
// swap the buffer
SwapBuffers(m_hDC);
}
//====================================================
I also discovered a couple things. If you run their program under "release" mode instead if "debug" mode, all the colors disappear and shows an ugly red. But if you close and run it again immediately, sometimes the color will show up again. VERY strange.
Can anyone solve this problem or is MFC+OpenGL doomed?
A bit of a strange suggestion, maybe, but try adding glMatrixMode (GL_MODELVIEW) to the start of your scene drawing code.
Also, check whether iWidth is actually positive.
It might not do any good because of what you describe as happening in a release build. Stuff like that is usually indicative of members not being initialised correctly (although I don''t see any of that in your program) or writing at an incorrect location in memory (apparently without triggering a GPF).
Oh, and you should also make sure that the window has the WS_CLIPCHILDREN and WS_CLIPSIBLINGS and that the window class has CS_OWNDC set.
Also, check whether iWidth is actually positive.
It might not do any good because of what you describe as happening in a release build. Stuff like that is usually indicative of members not being initialised correctly (although I don''t see any of that in your program) or writing at an incorrect location in memory (apparently without triggering a GPF).
Oh, and you should also make sure that the window has the WS_CLIPCHILDREN and WS_CLIPSIBLINGS and that the window class has CS_OWNDC set.
Well, Kippesoep, you are now officially my hero of the day.
It wasn''t the GL_MODELVIEW or iWidth that was messed up. However, while checking iWidth, I noticed iHeight was negative. Fixing it and making it positive did the job. I''m not really sure why the height affects the x-axis, but anyway, it''s working.
Now, my only problem is that the release version doesn''t show color when running most of the time (run 5 times, 1 will show the correct color). However, I noticed something that might be an important clue. Running on two of my desktop computers with TNT2 3D cards will results the above situation. But, if I run it on my notebook that has no 3D graphics card, it shows the correct colors 100% everytime I it (but turning really slow, of course). Could it be some OpenGL init routine might be skipped or something?
It wasn''t the GL_MODELVIEW or iWidth that was messed up. However, while checking iWidth, I noticed iHeight was negative. Fixing it and making it positive did the job. I''m not really sure why the height affects the x-axis, but anyway, it''s working.
Now, my only problem is that the release version doesn''t show color when running most of the time (run 5 times, 1 will show the correct color). However, I noticed something that might be an important clue. Running on two of my desktop computers with TNT2 3D cards will results the above situation. But, if I run it on my notebook that has no 3D graphics card, it shows the correct colors 100% everytime I it (but turning really slow, of course). Could it be some OpenGL init routine might be skipped or something?
I''ll bet your negative height contributed to the problem because you''re using iHeight in your call to gluPerspective().
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement