OpenGL & 2d - Example given does not work, any ideas ?

Started by
3 comments, last by Bagpuss 21 years, 6 months ago
Hi, I have kept quiet for a week or 2 now, and think I have learnt a fair amount of the basic stuff I need to know. Now, I am stuck again, and it has to be something really simple, probably a case of not seeing the wood for the trees ! I am trying to set up my drawing area for a coordinate based system that I can relate to (i.e. 640 * 480), and not just the seemingly abitrary opengl units. (I got teh example from someone on heres source code, just to see how it was done) My code below however does not work, when I think it should. I even copied someone elses code that does work using a similar method, and can''t get that to work either. Here is my initialisation function.
  

	PIXELFORMATDESCRIPTOR pfd;
    int         n;
	HGLRC		hrc;
	GLfloat     fMaxObjSize, fAspect;
	GLfloat     fNearPlane, fFarPlane;

    ASSERT(m_pDC != NULL);

    if (!bSetupPixelFormat(m_pDC))
        return;

// Interrogates the Pixel Format to see if a Palette is needed.

    n = ::GetPixelFormat(m_pDC->GetSafeHdc());
    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);

//If a Palette is needeed, then create one.(256 Colours Only)

//Detect if Needed by Anding the Value in the Data flags and the Need Palette Flag

	if ((pfd.dwFlags & PFD_NEED_PALETTE)) 
	{
								
		CreateRGBPalette(m_pDC);
	}
	
    // create a rendering context and make it current

        
    hrc = wglCreateContext(m_pDC->GetSafeHdc());
    wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);

	//Clear the Depth Buffer

    glClearDepth(1.0f);
	//Enable OGL using the Depth Buffer for comparisons

    glEnable(GL_DEPTH_TEST);


//Initialise OpenGL for 2d


	glViewport(0,0,640,480);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f,640.0f,0.0f,480.0f,-10.0f,10.0f);
	
	glMatrixMode(GL_MODELVIEW);

	glShadeModel(GL_SMOOTH);							
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);			
	glClearDepth(1.0f);									
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	
	glEnable(GL_TEXTURE_2D);

	//Select the GL Projection Matrix (Stack)

    glMatrixMode(GL_PROJECTION);
	//Reset Identity Matrix

    glLoadIdentity();	
	
}
  
and my draw scene function...
  
int CMyOgl::DrawGLScene()
{
	
    static BOOL     bBusy = FALSE;	
	if(bBusy)
        return TRUE;

    bBusy = TRUE;

	//Setup Drawing Area Back Colour

	glClearColor(m_BackColour.fRed, m_BackColour.fGreen, m_BackColour.fBlue, m_BackColour.fAlpha);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//Reset the Drawing Point to the Centre of the screen

	glLoadIdentity();	
	

  glColor3f(0.5f,0.5f,0.5f);
	glBegin(GL_LINES);
		glVertex3f(160.0f,120.0f,0.0f);
		glVertex3f(480.0f,120.0f,0.0f);

		glVertex3f(480.0f,120.0f,0.0f);
		glVertex3f(480.0f,360.0f,0.0f);

		glVertex3f(480.0f,360.0f,0.0f);
		glVertex3f(160.0f,360.0f,0.0f);

		glVertex3f(160.0f,360.0f,0.0f);
		glVertex3f(160.0f,120.0f,0.0f);
	glEnd();
	
	
	
	SwapBuffers(wglGetCurrentDC());

    bBusy = FALSE;
	
	glFlush();	

	return TRUE;
}
  
If anyone can offer me any hints or even fix my probelm I''ll be very greatful. As an aside, if I set my system up in this manner and draw in teh z plane at 0, is there an easy way of finding out how the coordinates I have set will change if I draw in another z plane, or have I misunderstood something fundamental ? TYVMIA, Bp.
Advertisement
at least part of your mistake is in your initialization.

you setup a nice viewport and projection.
then you setup some model view stuff.

then you clobber your projection!

<code>
glClearDepth(1.0f);
//Enable OGL using the Depth Buffer for comparisons
glEnable(GL_DEPTH_TEST);
//Initialise OpenGL for 2d
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f,640.0f,0.0f,480.0f,-10.0f,10.0f);

glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f); // note -- you already did this.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);

//Select the GL Projection Matrix (Stack)
glMatrixMode(GL_PROJECTION);
//Reset Identity Matrix -- clobbers projection!
glLoadIdentity();

</code>

i don''t know why you''re selecting the projection matrix at the end. after you''ve initialized, everything you do will be (probably) in the modelview matrix.

step 1) don''t clobber your projection matrix.

another note. depending on whether you use single or double buffering, you maybe should call glFlush() before the SwapBuffers. it makes more sense that way.

i''m not a windows guy, really, so i don''t know if you''re doing it on purpose. (calling glFlush after SwapBuffers) but in general i don''t think it makes sense. you may need it before Swap, though, to get your card to actually draw (since you''re using just a few vertices.)

thanks,
ryan.

Thanks Ryan, I Have fixed those, but I still get a white screen with no lines on it. Any more takers ?

Bp.
OK, so following Ap''s comment, I now have the following...


      PIXELFORMATDESCRIPTOR pfd;    int         n;    HGLRC		hrc;    ASSERT(m_pDC != NULL);    if (!bSetupPixelFormat(m_pDC))        return;// Interrogates the Pixel Format to see if a Palette is needed.    n = ::GetPixelFormat(m_pDC->GetSafeHdc());    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);//If a Palette is needeed, then create one.(256 Colours Only)//Detect if Needed by Anding the Value in the Data flags and the Need Palette Flag	if ((pfd.dwFlags & PFD_NEED_PALETTE)) 	{										CreateRGBPalette(m_pDC);	}	    // create a rendering context and make it current            hrc = wglCreateContext(m_pDC->GetSafeHdc());    wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);	//Enable OGL using the Depth Buffer for comparisons    glEnable(GL_DEPTH_TEST);    //Initialise OpenGL for 2d	glViewport(0,0,640,480);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glOrtho(0.0f,640.0f,0.0f,480.0f,-10.0f,10.0f);    glMatrixMode(GL_MODELVIEW);	glShadeModel(GL_SMOOTH);								glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				glClearDepth(1.0f);										glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);		glEnable(GL_TEXTURE_2D);	//Select the GL Projection Matrix (Stack)		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//	glLoadIdentity();	//Set up the viewing angle//    gluPerspective(0.0f, fAspect, fNearPlane, fFarPlane);	//Setup Perspective Matrix		//Initialise Matrix Mode    glMatrixMode(GL_MODELVIEW);			glLoadIdentity();///Temporary code   // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		glColor3f(1.0f, 0.0f, 0.0f);	glBegin(GL_LINES);		glVertex3f(160.0f,120.0f,0.0f);		glVertex3f(480.0f,120.0f,0.0f);		glVertex3f(480.0f,120.0f,0.0f);		glVertex3f(480.0f,360.0f,0.0f);		glVertex3f(480.0f,360.0f,0.0f);		glVertex3f(160.0f,360.0f,0.0f);		glVertex3f(160.0f,360.0f,0.0f);		glVertex3f(160.0f,120.0f,0.0f);	glEnd();	glFlush();		SwapBuffers(wglGetCurrentDC());	  


And still just the white screen, no lines.

This is driving me nuts, it has to be obvious, so what am I missing ?

TIA,

Bp.
Not certain about this .. but try drawing the lines at a +ve Z coord, rather than 0.

glBegin(GL_LINES);
glVertex3f(160.0f,120.0f,5.0 f);
glVertex3f(480.0f,120.0f,5.0 f);
glVertex3f(480.0f,120.0f,5.0 f);
glVertex3f(480.0f,360.0f,5.0 f);
glVertex3f(480.0f,360.0f,5.0 f);
glVertex3f(160.0f,360.0f,5.0 f);
glVertex3f(160.0f,360.0f,5.0 f);
glVertex3f(160.0f,120.0f,5.0 f);
glEnd();

I think the problem is that the camera is at Z=0 and so are the lines, so nothing gets drawn.

Not 100% certain, tho, so E&OE

Edit: Ignore me. I had a play around with some glOrtho stuff and anything within Z=-10 -> Z=10 should get drawn fine. sorry for being no help

[edited by - SimbobX on October 1, 2002 7:21:51 AM]

This topic is closed to new replies.

Advertisement