PDA

View Full Version : Simple (?) problem OGL/win32



micko
08-13-2006, 08:16 PM
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 changed my range to be *h/w for left/right bottom top etc. it moved the pixels to the middle bottom.
It does clear the screen and paint it with the appropriate background color etc, just can't get my cube drawn properly.

Thanks for any help,
Mick.

The 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;
} the opengl setup and rendering code -

#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( &amp;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, &amp;pfd );
SetPixelFormat( *hDC, iFormat, &amp;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 );
}

micko
08-13-2006, 10:04 PM
Don't worry too much unless you can see something obvious. I ust started yet another project and created it from scratch and all is well??
I have done this plenty of times before in both C# and C but this one threw me.
When I get time I'll see if I can find what it was and let you know.
Thanks,
Mick.