Jump to content
  • Advertisement
Sign in to follow this  
MickDuprez

OpenGL drawing problem

This topic is 4508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't for the life of me find what's wrong with my code and I just know it will be something simple, if you could have a look that would be great. The problem - After creating a standard windows application in VS2002 I change a few necessary style settings such as WS_CLIPSIBLING,WS_CLIPCHILDREN and it has its CS_OWNDC. That's the window out of the way, then I set up a palette etc and start to draw a simple auxWireCube and all I get is a pixel (or 2) in the bottom left corner of the client area. I've tried different viewing projections (ortho and projection), different shapes and sizes/locations but I can't get it to move into the screen. Anyway, here's the code: My winproc -
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	static HDC hDC = NULL;
	static HGLRC hRC = NULL;
	float range = 250.0f;

	switch (message) 
	{
	case WM_CREATE:
		// store the device context:
		hDC = GetDC(hWnd);
		// select the pixel format:
		EnableOpenGL(hWnd, &hDC, &hRC);
		// create the RC and make it current:
		SetUpRC();
		SetTimer(hWnd,33,1,NULL);
		break;

	case WM_TIMER:
		InvalidateRect(hWnd,NULL,FALSE);
		break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		// our rendering function:
		DrawScene();
		// and swap the buffers:
		SwapBuffers(hDC);
		// validate the newly painted client area:
		ValidateRect(hWnd,NULL);
		break;
	case WM_SIZE:
		// call our resizing handler:
		OnResize(LOWORD(lParam),HIWORD(wParam),range);
		PostMessage(hWnd, WM_PAINT, 0, 0);
		break;
	case WM_DESTROY:
		KillTimer(hWnd,101);
		DisableOpenGL(hWnd,hDC,hRC);
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

My rendering set up and drawing code -
// GLMain.cpp
// main file to handle the interface with windows events for our application.

#include "stdafx.h"

void SetUpRC()// called from WM_CREATE
{
	glShadeModel(GL_FLAT);		// Enables Flat Shading
	// set background color:
	//			  R     G    B    A
	glClearColor(0.f,0.0f,0.5f,0.0f);// Dark Blue background

}

void DrawScene()// called from WM_PAINT
{	
	// set matrix mode to modelview ready to apply 
	// camera and modeling transformations to the model:
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT);
	glTranslatef(0.0f,0.0f,-150.0f); //xform out a bit
	glColor3f(0.0, 1.0, 0.0); 
	auxWireCube(50);
	glFlush();
}
void OnResize(GLsizei w, GLsizei h, GLfloat range)// called from WM_SIZE
{
	// prevent divide by zero;
	if(h == 0)
		h = 1;
	// reset coordinate system:
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// create clipping volume - left,right,bottom,top,near,far:
	// range = 250.0f for now giving a cube of 500 units
	if(w <= h)
		glOrtho(-range, range, -range, range*h/w, range, -range);
	else
		glOrtho(-range, range*w/h, -range, range, range, -range);
	// set viewport to window dimensions:
	glViewport(0,0,w,h);
	
}

void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
    PIXELFORMATDESCRIPTOR pfd;
    int iFormat;

    // get the device context (DC)
    *hDC = GetDC( hWnd );

    // set the pixel format for the DC
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
                  PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    iFormat = ChoosePixelFormat( *hDC, &pfd );
    SetPixelFormat( *hDC, iFormat, &pfd );

    // create and enable the render context (RC)
    *hRC = wglCreateContext( *hDC );
    wglMakeCurrent( *hDC, *hRC );
}

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent( NULL, NULL );
    wglDeleteContext( hRC );
    ReleaseDC( hWnd, hDC );
}

Thankyou, Mick.

Share this post


Link to post
Share on other sites
Advertisement
I fixed it (of sorts), I created yet another project and used the same rendering code and it works??
I can't really see any difference so it must be something really subtle in the setting up of the DC and RC, this was the only thing I changed really. But I was getting the correct back colour and at least a couple of pixels for my drawing so I assumed it was ok...
I'll let you know if I ever find out.
Thanks for looking,
Mick.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!