HELP! Im a newbie...

Started by
6 comments, last by Moogle 22 years, 8 months ago
Whats messed up with this piece of junk? //////////////////////////////////////////////////////////// #define WIN32_LEAN_AND_MEAN //Chop away some stuff I wont be using #include //Standard windows application include #include #include #include float angle = 0.0f; HDC g_HDC; void SetupPixelFormat(HDC hDC) { int nPixelFormat; //the pixel format index 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 message, WPARAM wParam, LPARAM lParam) { static HGLRC hRC; //Rendering context static HDC hDC; //Device context int height,width; switch(message) { case WM_CREATE: //Window is being created hDC = GetDC(hwnd); g_HDC = hDC; SetupPixelFormat(hDC); hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); return 0; break; case WM_CLOSE: //The window is being closed wglMakeCurrent(hDC, NULL); wglDeleteContext(hRC); PostQuitMessage(0); return 0; break; case WM_SIZE: height = HIWORD(lParam); width = LOWORD(lParam); if (height==0) { height=1; } glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; break; default: break; } return DefWindowProc(hwnd, message, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX windowClass; //Window class HWND hwnd; //Window handle MSG msg; //Message bool done; //Flag to determine if program is complete //Now lets fill out the windows class structure windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.style = CS_HREDRAW | CS_VREDRAW; windowClass.lpfnWndProc = WndProc; windowClass.cbClsExtra = 0; windowClass.cbWndExtra = 0; windowClass.hInstance = hInstance; windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); windowClass.hCursor = LoadCursor(NULL, IDC_CROSS); windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); windowClass.lpszMenuName = NULL; windowClass.lpszClassName = "MyClass"; windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //And register it if (!RegisterClassEx(&windowClass)) return 0; //And finally create window hwnd = CreateWindowEx(NULL, "MyClass", "Gösta glares at you what would you like your tombstone to say", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU, 100, 100, 800, 640, NULL, NULL, hInstance, NULL); //Check if window creation did fail if (!hwnd) return 0; ShowWindow(hwnd,SW_SHOW); UpdateWindow(hwnd); done = false; //Initializate loop variable //And here is the main message loop while (!done) { PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE); if (msg.message == WM_QUIT) //Did we recive a quit message? { done = true; //If thats the case, QUIT! } else { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); angle = angle + 0.25f; if (angle >= 360.0f) angle = 0.0f; glTranslatef(0.0f,0.0f,-5.0f); glRotatef(angle,0.0f,0.0f,1.0f); glRotatef(angle,0.0f,1.0f,0.0f); glRotatef(angle,1.0f,0.0f,0.0f); glBegin(GL_QUADS); glColor3f(0.0,0.1,0.0); glVertex3f(-1.0f, 1.0f,0.0f); glColor3f(0.0,0.2,0.0); glVertex3f(-1.0f,-1.0f,0.0f); glColor3f(0.0,0.3,0.0); glVertex3f(1.0f,-1.0f,0.0f); glColor3f(0.0,0.4,0.0); glVertex3f(1.0f, 1.0f,0.0f); glEnd(); SwapBuffers(g_HDC); glBegin(GL_QUADS); glColor3f(0.0,0.1,0.0); glVertex3f(-1.0f, 1.0f,0.0f); glColor3f(0.0,0.2,0.0); glVertex3f(-1.0f,-1.0f,0.0f); glColor3f(0.0,0.3,0.0); glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0,0.4,0.0); glVertex3f(-1.0f, 1.0f,-1.0f); glEnd(); SwapBuffers(g_HDC); glBegin(GL_QUADS); glColor3f(0.0,0.0,1.0); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f(1.0f,-1.0f,0.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glEnd(); SwapBuffers(g_HDC); glBegin(GL_QUADS); glColor3f(1.0,0.0,0.0); glVertex3f(-1.0f,1.0f,0.0f); glVertex3f(1.0f,1.0f,0.0f); glVertex3f(1.0f,1.0f,-1.0f); glVertex3f(-1.0f,1.0f,-1.0f); glEnd(); SwapBuffers(g_HDC); glBegin(GL_QUADS); glColor3f(0.0,0.0,1.0); glVertex3f(-1.0f,1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(1.0f,1.0f,-1.0f); glEnd(); SwapBuffers(g_HDC); glBegin(GL_QUADS); glColor3f(0.0,1.0,0.0); glVertex3f(-1.0f,-1.0f,0.0f); glVertex3f(1.0f,-1.0f,0.0f); glVertex3f(1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glEnd(); SwapBuffers(g_HDC); TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam; } //////////////////////////////////////////////////////////// The problem is the cube is flickering! Edited by - ShiningKnight on August 19, 2001 10:02:17 PM
-=Moogle=-
Advertisement
only use swapbuffers once, at the end of each frame.
Thanks for that ... flickering is gone but polygons are for some reason semi transparent ... thank you anyway
-=Moogle=-
Well, I'm a newbie as well but I think I can help.

One very important thing in 3D is depth testing. I fixed your problem by adding one line:

...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glEnable(GL_DEPTH_TEST); // add this here!

angle = angle + 0.25f;
...

and you should really use an initialize function to set this only once... =)

Good luck!
- Mike

Edit:

Also, don't forget that the quads must be drawn in a specific order to make them face the front. The default is CounterClockWise by default. So drawn the vertex's for the quads in a CCW order to make them face forward!

Edited by - mkaltner on August 19, 2001 9:16:45 PM
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."
Thanks a lot Mike! Its weird that they dont have this in the OpenGL Game Programming book!!
-=Moogle=-
Thanks a lot Mike! Its weird that they dont have this in the OpenGL Game Programming book!!
Thanks a lot Mike! Its weird that they dont have this in the OpenGL Game Programming book!!
-=Moogle=-
Actually, they do. Well, at least that''s where I got it from. But don''t fear, ''cause I know I''ve had to re-read a few things because there''s just so much info to soak in. I''m on chapter 8 now. =) Good luck! There''s some good stuff coming up!

- Mike
"The important thing to remember when programming is: When you're 90% complete, there's still 50% more to go."

This topic is closed to new replies.

Advertisement