Problem rendering to a child window

So yeah. I’m trying to render to a child window in my win32 application. I get the glClearColor properly, but whenever I try to render any polygons, I get nothing. Here’s my render code:

void Render(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(0.2f, 0.3f, 1.0f);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(0.5f, 0.7f, 1.0f);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(0.8f, 0.3f, 1.0f);
	glEnd();
	SwapBuffers(rHdc);
}

And the code in the WM_SIZE and WM_PAINT messages:


case WM_SIZE:
		{
			RECT wndRect, toolRect;
			GetWindowRect(hwnd, &wndRect);
			GetWindowRect(toolRender, &toolRect);
			int ToolHeight = toolRect.bottom - toolRect.top;

			float width = wndRect.right - wndRect.left;

			float height = wndRect.bottom - ToolHeight;
			if (height == 0)
				height = 1;

			glViewport(wndRect.left, wndRect.top + ToolHeight, wndRect.right - wndRect.left, wndRect.bottom - ToolHeight);
			gluPerspective(75.0, width/height, 0.1, 100.0);
		}
		//WM_SIZE

	case WM_PAINT:
		Render();
		break; //WM_PAINT

(There’s no break; in WM_SIZE on purpose, so either way it calls the Render() in WM_PAINT)

The child window shows the color I define in glClearColor, but absolutely nothing else. What’s the problem here?

Before rendering to a child window, make sure it works fine in a single window first. You have not setup the projection and modelview matrix properly. Usually, we would call glMatrixMode before we call the projection or modelview matrix functions. So your resize code should be this,

 
case WM_SIZE:
{
   RECT wndRect, toolRect;
   GetWindowRect(hwnd, &wndRect);
   GetWindowRect(toolRender, &toolRect);
   int ToolHeight = toolRect.bottom - toolRect.top;
   float width = wndRect.right - wndRect.left;
   float height = wndRect.bottom - ToolHeight;
   if (height == 0)
      height = 1;
   glViewport(wndRect.left, wndRect.top + ToolHeight, wndRect.right - wndRect.left, wndRect.bottom - ToolHeight);
   glMatrixMode(GL_PROJECTION); 
   glLoadIdentity();
   gluPerspective(75.0, width/height, 0.1, 100.0);
   glMatrixMode(GL_MODELVIEW); 
} 
//WM_SIZE

And then ur display function should be this,


void Render(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
   //move the camera back a bit
   glTranslatef(0,0,-5);

   glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.2f, 0.3f, 1.0f);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3f(0.5f, 0.7f, 1.0f);
	glColor3f(0.0f, 0.0f, 1.0f);
   	glVertex3f(0.8f, 0.3f, 1.0f);
   glEnd();
   SwapBuffers(rHdc);
}

See if this helps.

Okay, well, progress. At first it looked like nothing was being rendered, but now I move the splitter bar, making my child window rreeeeeeeeeeeeaaaaaaally wide, I can see the triangle on the right side of the window.

Why isn’t it rendering in the middle like it’s supposed to…?

I think your viewport is not setup properly. To test it, just give it a size equal to the client area of the parent window. This should show you the whole triangle. Then adjust the viewport based on the size of the child window.

Why isn’t it rendering in the middle like it’s supposed to…?

Because the coordinates that u r passing in are not centered. For centered triangle try this.


void Render(void)
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(0.0, 0.0, -1.0);
  glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(-0.5f, -0.5f);
    glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(0.5f, -0.5f);
    glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(0.0f, 0.5f);
  glEnd();
}

Solved! For glViewport, I put 0 for x, and ToolHeight for y, and it works perfectly now. Also, yes, I found that (0, 0) is the center of the screen. Thanks a ton!!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.