   Open Gl 3d help
  Hi, I am pretty new to open gl and i have recently got a tutorial kind of working for me.  The program builds and debugs but the sides of the object overlap weirdly.  I understand whats happening but i don't get why the people who made the tutorial didn't have the same problem if there's some stupid thing i forgot to do i'd like to know. Here is a link to a picture of the result and my source code.click here for the pic #include <windows.h>#include <gl\gl.h>#include <gl\glu.h>#include <gl\glaux.h>#pragma comment( lib, "opengl32.lib" )// Search For OpenGL32.lib While Linking ( NEW )#pragma comment( lib, "glu32.lib" )// Search For GLu32.lib While Linking    ( NEW )HDChDC=NULL;HGLRChRC=NULL;HWNDhWnd=NULL;HINSTANCEhInstance;boolkeys[256];boolactive = TRUE;boolfullscreen = TRUE;float rtri = 0;LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);GLvoid ReSizeGLScene(GLsizei width, GLsizei height){glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}int InitGL(GLvoid){glShadeModel(GL_SMOOTH);glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// Black glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);return TRUE;}int DrawGLScene(GLvoid){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);rtri += .1;glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f);glRotatef(rtri,0.0f,1.0f,0.0f); glBegin(GL_TRIANGLES);/* Front side with Red Color */glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f);glVertex3f(-1.0f,-1.0f, 1.0f);glVertex3f( 1.0f,-1.0f, 1.0f);/* Right side with Blue Color */glColor3f(0.0f,0.0f,1.0f);glVertex3f( 0.0f, 1.0f, 0.0f);glVertex3f( 1.0f,-1.0f, 1.0f);glVertex3f( 1.0f,-1.0f, -1.0f);/* Back side with Red Color */glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f);glVertex3f( 1.0f,-1.0f, -1.0f);glVertex3f(-1.0f,-1.0f, -1.0f);/* Left Side with Green Color */glColor3f(0.0f,1.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);glVertex3f(-1.0f,-1.0f, 1.0f);glEnd(); glLoadIdentity();glTranslatef(1.5f,0.0f,-7.0f);glRotatef(rtri,0.0f,1.0f,1.0f);glBegin(GL_QUADS);/* Top Side of the Cube with different shades of colors */glColor3f(0.0f,1.0f,0.0f);glVertex3f( 1.0f, 1.0f,-1.0f); // Top RightglColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f, 1.0f,-1.0f); // Top LeftglColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom LeftglColor3f(0.0f,1.0f,1.0f);glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right/* Bottom Side of the Cube with different shades of colors */glColor3f(1.0f,0.5f,0.0f);glVertex3f( 1.0f,-1.0f, 1.0f); // Top RightglColor3f(1.0f,0.0f,0.0f);glVertex3f(-1.0f,-1.0f, 1.0f); // Top LeftglColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom LeftglColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right/* Front Side of the Cube with different shades of colors */glColor3f(1.0f,0.0f,0.0f);glVertex3f( 1.0f, 1.0f, 1.0f); // Top RightglColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f, 1.0f, 1.0f); // Top LeftglColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom LeftglColor3f(1.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right/* BackSide of the Cube with different shades of colors */glColor3f(1.0f,1.0f,0.0f);glVertex3f( 1.0f,-1.0f,-1.0f); // Top RightglColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f); // Top LeftglColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom LeftglColor3f(0.0f,1.0f,1.0f);glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right/* LeftSide of the Cube with different shades of colors */glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f, 1.0f, 1.0f); // Top RightglColor3f(0.0f,0.5f,1.0f);glVertex3f(-1.0f, 1.0f,-1.0f); // Top LeftglColor3f(1.0f,0.5f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom LeftglColor3f(0.0f,1.0f,1.0f);glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right/* Right Side of the Cube with different shades of colors */glColor3f(1.0f,0.0f,1.0f);glVertex3f( 1.0f, 1.0f,-1.0f); // Top RightglColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f, 1.0f, 1.0f); // Top LeftglColor3f(0.0f,1.0f,1.0f);glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom LeftglColor3f(1.0f,0.5f,1.0f);glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom RightglEnd();return TRUE; }GLvoid KillGLWindow(GLvoid){if (fullscreen){ChangeDisplaySettings(NULL,0);ShowCursor(TRUE);}if (hRC){if (!wglMakeCurrent(NULL,NULL)){MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);}if (!wglDeleteContext(hRC)){MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);}hRC=NULL;}if (hDC && !ReleaseDC(hWnd,hDC)){MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hDC=NULL;}if (hWnd && !DestroyWindow(hWnd)){MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hWnd=NULL;}if (!UnregisterClass("OpenGL",hInstance)){MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);hInstance=NULL;}} BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag){GLuintPixelFormat;WNDCLASSwc;DWORDdwExStyle;DWORDdwStyle;RECTWindowRect;WindowRect.left=(long)0;WindowRect.right=(long)width;WindowRect.top=(long)0;WindowRect.bottom=(long)height;fullscreen=fullscreenflag;hInstance= GetModuleHandle(NULL);wc.style= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;wc.lpfnWndProc= (WNDPROC) WndProc;wc.cbClsExtra= 0;wc.cbWndExtra= 0;wc.hInstance= hInstance;wc.hIcon= LoadIcon(NULL, IDI_WINLOGO);wc.hCursor= LoadCursor(NULL, IDC_ARROW);wc.hbrBackground= NULL;wc.lpszMenuName= NULL;wc.lpszClassName= "OpenGL";if (!RegisterClass(&wc)){MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}if (fullscreen){DEVMODE dmScreenSettings;memset(&dmScreenSettings,0,sizeof(dmScreenSettings));dmScreenSettings.dmSize=sizeof(dmScreenSettings);dmScreenSettings.dmPelsWidth= width;dmScreenSettings.dmPelsHeight= height;dmScreenSettings.dmBitsPerPel= bits;dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL){if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES){fullscreen=FALSE;}else{MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);return FALSE;}}}if (fullscreen){dwExStyle=WS_EX_APPWINDOW;dwStyle=WS_POPUP;ShowCursor(FALSE);}else{dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;dwStyle=WS_OVERLAPPEDWINDOW;}AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);if (!(hWnd=CreateWindowEx(dwExStyle,"OpenGL",title,dwStyle |WS_CLIPSIBLINGS |WS_CLIPCHILDREN,0, 0,WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,NULL,NULL,hInstance,NULL))){KillGLWindow();MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}staticPIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,bits,0, 0, 0, 0, 0, 0,0,0,0,0, 0, 0, 0,16,0,0,PFD_MAIN_PLANE,0,0, 0, 0};if (!(hDC=GetDC(hWnd))){KillGLWindow();MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))){KillGLWindow();MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}if(!SetPixelFormat(hDC,PixelFormat,&pfd)){KillGLWindow();MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}if (!(hRC=wglCreateContext(hDC))){KillGLWindow();MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}if(!wglMakeCurrent(hDC,hRC)){KillGLWindow();MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}ShowWindow(hWnd,SW_SHOW);SetForegroundWindow(hWnd);SetFocus(hWnd);ReSizeGLScene(width, height);if (!InitGL()){KillGLWindow();MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);return FALSE;}return TRUE;}LRESULT CALLBACK WndProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam){switch (uMsg){case WM_ACTIVATE:{if (!HIWORD(wParam)){active=TRUE;}else{active=FALSE;}return 0;}case WM_SYSCOMMAND:{switch (wParam){case SC_SCREENSAVE:case SC_MONITORPOWER:return 0;}break;}case WM_CLOSE:{PostQuitMessage(0);return 0;}case WM_KEYDOWN:{keys[wParam] = TRUE;return 0;}case WM_KEYUP:{keys[wParam] = FALSE;return 0;}case WM_SIZE:{ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); return 0;}}return DefWindowProc(hWnd,uMsg,wParam,lParam);}int WINAPI WinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){MSGmsg;BOOLdone=FALSE;if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO){fullscreen=FALSE;}if (!CreateGLWindow("Tutorial - OpenGl",640,480,16,fullscreen)){return 0;}while(!done){if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){if (msg.message==WM_QUIT){done=TRUE;}else{TranslateMessage(&msg);DispatchMessage(&msg);}}else{if (active){DrawGLScene();SwapBuffers(hDC);}}}KillGLWindow();return (msg.wParam);} [Edited by - das612 on May 31, 2010 4:49:23 PM]
  I don't see a picture but i don't see glClearDepth set anywhere.  otherwise maybe it's the vertex order which could be fixed by reversing the drawing order or changing from cullfaces back to front though i don't see culling enabled.   ;  Couldn't get it to compile- type LPCWSTR wasn't recognized as being char[x] arrays   ;  I tried enabling culling and clearing the depth buffer but neither changed any thing.  I also added a link to the picture of what's happening in my original post if that will help identify the problem.   ;  The vertices of your faces should be ordered in clockwise direction.   ;Quote:Original post by PrestoChungCouldn't get it to compile- type LPCWSTR wasn't recognized as being char[x] arraysSimple fix: Visual Studio uses 2-byte character strings by default. Which means MessageBoxW is used instead of MessageBoxA.The simple fix:Add an L in front of static strings.LPCWSTR str = L"MyString";You can also disable this behavior, or call MessageBoxA explicitly.   ;  looks like your depth testing isn't working.  At  a quick glance I didn't see glEnable(GL_DEPTH_TEST) anywhere, try enabling that. If that doesn't work you might want to make sure your window actually has a depth buffer.   ;  The object rotates so ordering the verticies wouldnt change much.;  Thanks for all your help it turns out i was missing three lines of codde.glClearDepth(1.0f);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);The problem has been soloved.   