Code not working with Ortho2D and pixelFormatDescriptor

Started by
1 comment, last by dschmitt423 18 years, 7 months ago
I am trying to figure out why if I un-comment the 2 lines marked, and comment out the gluOrtho2D line I see the sample picture, but if I leave it as is, I do not. For this project the openGL image will only EVER be a 2D image, and I need the origin to be centered at the bottom left hand corner. Yet I don't see anything. I am not used to using the pixelFormatDescriptor, do I perhaps have a value wrong? Here is the necessary part of the code... /** Resize And Initialize The GL Window */ GLvoid COpenGLWrapper::Set2DMatrix(GLsizei width, GLsizei height) { if ( height == 0 ) { // Prevent A Divide By Zero By Making Height Equal One height = 1; } glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Sets the (0,0) coordinates to the bottom left side of the screen - what I want! gluOrtho2D( 0, width, 0, height ); // Calculate The Aspect Ratio Of The Window // gluPerspective( 45.0f, (GLfloat)width/(GLfloat)height, 0.1f,100.0f); -- LINE 1 to uncomment glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /** All Setup For OpenGL Goes Here */ int COpenGLWrapper::InitGL(GLvoid) { glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(BACKGROUND_COLOR); // Set Background Color to stored constant 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 return TRUE; } /** Here's where we do all the drawing for this window! */ void COpenGLWrapper::Draw() { glLoadIdentity(); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Clear Buffer & Fill with Background Color SamplePicture(); SwapBuffers( m_hDC ); // Don't forget to swap the buffers! =) } void COpenGLWrapper::SamplePicture() { // glTranslatef( 0.0f,0.0f,-6.0f); // Move Into The Screen 6.0 Units (Z-azis) -- LINE 2 to uncomment glBegin(GL_LINE_STRIP); // Drawing Using Connected Lines glColor3f(1.0f,0.0f,0.0f); // Red glVertex2f( 0.0f, 1.0f); // Top glColor3f(1.0f,1.0f,0.0f); // Yellow glVertex2f( 0.0f, 0.0f); // Bottom Left glColor3f(1.0f,0.0f,1.0f); // Purple glVertex2f( 1.0f, 0.0f); // Bottom Right glColor3f(0.0f,1.0f,0.0f); // Green glVertex2f( 1.0f, 1.0f); // Bottom Right glEnd(); // Finished Drawing The Lines } /** Creates the Window Device Content for OpenGL and begins drawing onto it */ BOOL COpenGLWrapper::CreateWindowWrapper() { GLuint PixelFormat; // Holds The Results After Searching For A Match static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Enables drawing to a window or device surface PFD_DRAW_TO_BITMAP | // Enables drawing to a bitmap in memory PFD_SUPPORT_OPENGL | // Enables OpenGL calls PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 16, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 32, // 32 bit Z-Buffer (Depth Buffer) - only want window to be able to do 2D 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if( !(m_hDC) ) // Did We Get A Device Context? { DWORD error = GetLastError(); KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } if( !(PixelFormat=ChoosePixelFormat( m_hDC,&pfd )) ) // Did Windows Find A Matching Pixel Format? { KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } if(!SetPixelFormat( m_hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format? { KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } if (!(m_hRC=wglCreateContext(m_hDC))) // Are We Able To Get A Rendering Context? { KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } if(!wglMakeCurrent( m_hDC,m_hRC)) // Try To Activate The Rendering Context { KillGLWindow(); // Reset The Display return FALSE; // Return FALSE } // Initially display the window ProcessWindow(); // Let our timers know this window has been created, so we can start refreshing it! winCreated = true; return TRUE; } /** Necessary steps to initially display the window */ void COpenGLWrapper::ProcessWindow() { if ( !InitGL() ) // Initialize Our Newly Created GL Window { // Initialization failed! KillGLWindow(); // Reset The Display return; // Return FALSE } Set2DMatrix(width, height); Draw(); }
Advertisement
gluOrtho2D( 0, width, 0, height );
You're drawing the sprite with a width & height of 1
but you're setting the 2d scale to match the viewport size

I think gluOrtho2D(0,1,0,1) is what you're after
The parameters you pass to gluortho just set the relative draw scale, they have nothing to do with the window pixel size

It was being draw.. just very very tiny >_< (a single pixel in size, infact)
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Actually, I did mean width and height (believe it or not I do need to do know exact pixel by pixel -- my program is a simulation of hardware screens), but thank you that helped me realize I had already fixed the real problem! Thanks!

I had presumed (obviously incorrectly) that I didn't need a depth buffer since I am only doing a 2D screen. However, with pixelFormatDescriptor, this is obviously untrue!

This topic is closed to new replies.

Advertisement