Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Can someone compile this?

This topic is 5196 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''m having a problem getting my SetupPixelFormat function to work, i don''t know if i have OGL set up right or not, so can you just plop this into your compiler and tell me if it works or not? if it does''nt work can you take a look at the setupPixelFormat function for me? thanks. /******* *Lib''s *******/ #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "GLAUX.lib") /************* *Includes **************/ #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> #include <gl/glaux.h> /********* *Globals *********/ float angle = 0.0f; HDC g_HDC; bool fullscreen = false; float legAngle[2] = {0.0f, 0.0f}; float armAngle[2] = {0.0f, 0.0f}; void DrawCube(float xPos, float yPos, float zPos) { glPushMatrix(); glTranslatef(xPos,yPos,zPos); glBegin(GL_POLYGON); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,-1.0f); glVertex3f(-1.0f,0.0f,-1.0f); glVertex3f(-1.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,0.0f); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f(0.0f,-1.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,-1.0f,0.0f); glVertex3f(0.0f,-1.0f,-1.0f); glVertex3f(0.0f,0.0f,-1.0f); glVertex3f(-1.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(0.0f,-1.0f,-1.0f); glEnd(); glPopMatrix(); } void DrawArm(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(1.0, 0.0, 0.0); glTranslatef(xPos,yPos,zPos); glScalef(1.0f,4.0f,1.0f); DrawCube(0.0f,0.0f,0.0f); glPopMatrix(); } void DrawHead(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(1.0f,1.0f,1.0f); glTranslatef(xPos,yPos,zPos); glScalef(2.0f,2.0f,2.0f); DrawCube(0.0f,0.0f,0.0f); glPopMatrix(); } void DrawTorso(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(0.0f,0.0f,1.0f); glTranslatef(xPos,yPos,zPos); glScalef(3.0f,5.0f,2.0f); DrawCube(0.0,0.0,0.0); glPopMatrix(); } void DrawLeg(float xPos, float yPos, float zPos) { glPushMatrix(); glColor3f(1.0f, 1.0f,0.0f); glTranslatef(xPos,yPos,zPos); glScalef(1.0f,5.0f,1.0f); DrawCube(0.0f,0.0f,0.0f); } void DrawRobot(float xPos,float yPos,float zPos) { static bool leg1 = true; static bool leg2 = false; static bool arm1 = true; static bool arm2 = false; glPushMatrix(); glTranslatef(xPos,yPos,zPos); DrawHead(1.0f,2.0f,0.0f); DrawTorso(1.5f,0.0f,0.0f); glPushMatrix(); if(arm1) { armAngle[0] += 1.0f; } else { armAngle[0] -= 1.0f; } if(armAngle[0] >=15.0f) { arm1 = false; } if(armAngle[0] <= -15.0f) { arm1 = true; } glTranslatef(0.0f,-0.5f,0.0f); glRotatef(armAngle[0],1.0f,0.0f,0.0f); DrawArm(2.5f,0.0f,-0.5f); glPopMatrix(); glPushMatrix(); if(arm2) { armAngle[1] += 1.0f; } else { armAngle[1] -= 1.0f; } if(armAngle[1] >=15.0f) { arm2 = false; } if(armAngle[1] <= -15.0f) { arm2 = true; } glTranslatef(0.0f,-0.5f,0.0f); glRotatef(armAngle[1],1.0f,0.0f,0.0f); DrawArm(-1.5f,0.0f,-0.5f); glPopMatrix(); glPushMatrix(); if(leg1) { legAngle[0] += 1.0f; } else { legAngle[0] -= 1.0f; } if(legAngle[0] >=15.0f) { arm1 = false; } if(legAngle[0] <= -15.0f) { arm1 = true; } glTranslatef(0.0f,-0.5f,0.0f); glRotatef(legAngle[0],1.0f,0.0f,0.0f); DrawLeg(-0.5f,-5.0f,-0.5f); glPopMatrix(); glPushMatrix(); if(leg2) { legAngle[1] += 1.0f; } else { legAngle[1] -= 1.0f; } if(legAngle[1] >=15.0f) { arm1 = false; } if(legAngle[1] <= -15.0f) { arm1 = true; } glTranslatef(0.0f,-0.5f,0.0f); glRotatef(legAngle[1],1.0f,0.0f,0.0f); DrawLeg(1.5f,-5.0f,-0.5f); glPopMatrix(); glPopMatrix(); } void Render() { glEnable(GL_DEPTH_TEST); glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); angle += 1.0f; if(angle >=360.0f) { angle = 0.0f; } glPushMatrix(); glLoadIdentity(); glTranslatef(0.0f,0.0f,-30.0f); glRotatef(angle, 0.0f,1.0f,0.0f); DrawRobot(0.0f,0.0f,0.0f); glPopMatrix(); glFlush(); SwapBuffers(g_HDC); } void SetupPixelFormat(HDC hdc) { int nPixelFormat; static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0,0,0,0,0,0, 0, 0, 0, 0,0,0,0, 16, 0, 0, PFD_MAIN_PLANE 0, 0,0,0}; nPixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc,nPixelFormat,&pfd); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam) { static HGLRC hRC; static HDC hdc; COORD winsize; switch(msg) { case WM_CREATE: hdc = getDC(hwnd); g_HDC = hdc; SetupPixelFormat(hdc); hRc = wglCreateContext(hdc); wglMakeCurrent(hdc,hRC); return 0; break; case WM_CLOSE: wglMakeCurrent(hdc,NULL); wglDeleteContext(hRC); PostQuitMessage(0); return 0; break; case WM_SIZE: winsize.Y = HIWORD(lParam); winsize.X = LOWORD(lParam); if(winsize.X ==0) { winsize.X = 1; } if(winsize.Y = 0) { winsize.Y = 1; } glViewport(0,0,winsize.X,winsize.Y) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(54.0f,(GLFLOAT)winsize.X/(GLFLOAT)winsize.Y,1.0f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; break; default: break; } return (DefWindowProc(hwnd,msg,wParam,lParam)); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdShow,int nShowCmd) { WNDCLASSEX wc; HWND hwnd; MSG msg; bool done; DWORD dwExStyle; DWORD dwStyle; RECT windowRect; COORD winsize; winsize.X = 800; winsize.Y = 600; int bits = 32; windowRect.left =(long)0; windowRect.right =(long)winsize.X; windowRect.top = (long)winsize.Y; windowRect.bottom =(long)0; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCurson(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = "OGL CLASS 1"; wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); if(!RegisterClassEx(&windowClass)); { return 0; } if(fullscreen) { DEVMODE dmset; memset(&dmset,0,sizeof(dmset)); dmset.dmSize = sizeof(dmset); dmset.dmPelsWidth = winsize.X; dmset.dmPelsHeight = winsize.Y; dmset.dmBitsPerPel = bits; dmset.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; if(ChangeDisplaySettings(&dmset,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){ { MessageBox(NULL,"Display Mode Failed", NULL, MB_OK); fullscreen = false; } } if(fullscreen) { dwExStyle = WS_EX_APPWINDOW; dwStyle=WS_POPUP; ShowCursor(FALSE); } else { dwExStyle=WS_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle=WS_OVERLAPPEDWINDOW; } AdjustWindowRectEx(&windowRect,dwStyle,FALSE,dwExStyle); hwnd = CreateWindow(NULL, "OGL CLASS 1","OGL ROBO",dwStyle |WS_CLIPCHILDReN | WS_CLIPSIBLINGS,0,0,windowRect.right - windowRect.left,windowRect.bottom - windowRect.top, NULL,NULL,hInstance,NULL); if(!hwnd) { return 0; } ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); done = false; while(!done) { PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE); if(msg.message == WM_QUIT) { done = true; } else { Render(); TranslateMessage(&msg); DispatchMessage(&msg); } } if(fullScreen) { ChangeDisplaySettings(NULL,0); ShowCursor(TRUE); } return msg.wParam; }

Share this post

Link to post
Share on other sites
1. comma is missing off PFD_MAIN_PLANE in setting up PIXELFORMATDESCRIPTOR in the SetupPixelFormat routine.
2. In your WndProc routine you have GLFLOAT instead of GLfloat in the gluPerspective routine
3. In WinMain you reference fullScreen when it should be fullscreen
4. In WndProc you call getDC when it should be GetDC
5. Right after 4 you reference hRc when it should be hRC.
6. In WinMain you call LoadCurson when it should be LoadCursor
7. In the RegisterClassEx of WinMain you pass the nonexistant &windowClass reference, when it should be &wc, and also remove the semicolon from end of the line
8. In WinMain again you reference WS_APPWINDOW when it should be WS_EX_APPWINDOW
9. In your CreateWindow call in WinMain you reference WS_CLIPCHILDReN when it should be WS_CLIPCHILDREN
10. glViewport command in WndProc has missing semicolon from end of line
11. In WinMain your wc.style assignment statement has semicolon missing from end of line
12. Remove NULL as the first parameter of CreateWindow in WinMain
13. One too many curly open brackets on the if ChangeDisplaySettings line in WinMain

Those are the syntax errors and will get your code to compile, but it will still not run properly. The window is only the title bar and has to be maunally resized to be viewed. Fullscreen mode does not work. There is nothing displayed even in the window.

I suggest you quickly run through some MFC tutorials on setting up window modes or go through the NeHe base code turorial which explains setting up a window quite thoroughly.

Hope this helps

Ok. I have upset the wife and she's not talking to me now, so I spent some more time on this...

14. In WinMain your windowRect.top and windowRect.bottom assignments are the wrong way around. This results in your window starting up with just the title bar.
15. if(winsize.Y = 0) should be if(winsize.Y == 0) in WndProc. This is a logical error and results in nothing being displayed in the window. Subtle error that.
16. In your if(leg1) code block you set arm1 to true or false. I think this should be leg1 not arm1.
17. Same as above for leg2. A copy/paste mistake by the looks of it.
18. if(angle >=360.0f) should be if(angle >=359.0f). Using >=360 takes you 1 degree past 0 and then the reset of the angle to 0 creates a noticeable glitch in what would otherwise be smooth rotation. 360 degrees is 0 to 359
19. You need to issue the proper commands to release resources back to the system when you get a WM_QUIT/KILL message. At a minimum, put a DestroyWindow call in the WM_CLOSE section.

Good luck. The robot still has a leg that doesnt move.. but then I have to leave something for you to do :-)

[edited by - Fahrenheit451 on March 6, 2004 2:33:19 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!