Advertisement

SOS

Started by October 04, 2003 02:17 AM
0 comments, last by sunke 21 years, 5 months ago
hi,i hava some questions in lesson 1 here is my source code, which is the same as in the tutorial unfortunately it doesn''t work well..(BTW, i use VS.NET 2003) who can help me? thanks u very much. ------------------------------------------------ #include <windows.h> //header file for windows #include <gl\gl.h> //header file for the OpenGL32 Library #include <gl\glu.h> //header file for the GLu32 Library #include <gl\glaux.h> //header file for the GLaux Library HGLRC hRC = NULL; //Permanent Rendering Context HDC hDC = NULL; //Private GDI Device Context HWND hWnd = NULL; //Holds Our Window Handle HINSTANCE hInstance;//Holds The Instance of The Application bool keys[256]; //Array Used For The KeyBoard Routine bool active = true; //Window Active Flag Set To True By Default bool fullscreen = true;//Fullscreen Flag Set To Fullscreen Mode By Default LRESULT CALLBACK WndProc(HWND, UINT, WPARAM ,LPARAM); //Declaration For WndProc GLvoid ReSizeGLScene(GLsizei width, GLsizei height) //Resize And Initialize The GL Window { if(height == 0) //Prevent A Divide By Zero By { height = 1; //Mark Height Equal One } glViewport(0, 0, width, height); //Reset The Current Viewport glMatrixMode(GL_PROJECTION); //Select The Projection Matrix glLoadIdentity(); //Reset The Projection Matrix //Calculate The Aspect Ratio Of The Window gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); //Select The Modelview Matrix glLoadIdentity(); //Reset The Modelview Matrix } int InitGL(GLvoid) //All Setup For OpenGL Goes Here { glShadeModel(GL_SMOOTH); //Enables Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//Black Background glClearDepth(1.0f); //Depth Buffer Setup glEnable(GL_DEPTH_TEST); //Enables Depth Testing glDepthFunc(GL_LEQUAL); //The Type Of Depth Test To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Really Nice Perspective Calculations return true; //Initialization Went OK } int DrawGLScene(GLvoid) //Here''s Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear The Screen And The Depth Buffer glLoadIdentity(); //Reset The Current Modelview Matrix //... return true; //Everything Went OK } GLvoid KillGLWindow(GLvoid) //Properly Kill The Window { if(fullscreen) //Are We In Fullscreen Mode? { ChangeDisplaySettings(NULL, 0); //If So Switch Back To The Desktop ShowCursor(true); //Show Mouse Pointer } if(hRC) //Do We Have A Rendering Context? { if(!wglMakeCurrent(NULL, NULL)) //Are We Able To Release The DC And RC Contexts? { MessageBox(NULL, "Release of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } if(!wglDeleteContext(hRC)) //Are We Able To Delete The RC? { MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } hRC = NULL; //Set RC To NULL } if(hDC && !ReleaseDC(hWnd, hDC)) //Are We Able To Release The DC { MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hDC = NULL; //Set DC To NULL } if(hWnd && !DestroyWindow(hWnd)) //Are We Able To Destroy The Window? { MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hWnd = NULL; //Set hWnd To NULL } if(!UnregisterClass("OpenGL", hInstance)) //Are We Able To Unregister Class { MessageBox(NULL, "Could Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hInstance = NULL; //Set hInstance To NULL } } BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { GLuint PixelFormat; //Holds The Results After Searching For A Match WNDCLASS wc; //Windows Class Structure DWORD dwExStyle; //Window Extended Style DWORD dwStyle; //Window Style RECT WindowRect; //Grabs Rectangle Upper Left / Lower Right Values WindowRect.left = (long)0; //Set Left Value to 0 WindowRect.right = (long)width; //set right value to requested width WindowRect.top = (long)0; //Set top value to 0 WindowRect.bottom = (long)height; //Set bottom value to requested height fullscreen = fullscreenflag; //Set the global fullscreen flag hInstance = GetModuleHandle(NULL); //Grab An instance for out window wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; //redraw on move, and own DC for window wc.lpfnWndProc = (WNDPROC)WndProc; //WndProc Handles Messages wc.cbClsExtra = 0; //No extra window data wc.cbWndExtra = 0; //No extra window data wc.hInstance = hInstance; //Set the instance wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); //Load the default icon wc.hCursor = LoadCursor(NULL, IDC_ARROW); //Load the arrow pointer wc.hbrBackground = NULL; //No background Required for GL wc.lpszMenuName = NULL; //We don''t want a menu wc.lpszClassName = "openGL"; //Set The class Name if(!RegisterClass(&wc)) //Attempt to register the window class { MessageBox(NULL, "Failed To Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //exit and return false } if(fullscreen) //attempt fullscreen mode? { DEVMODE dmScreenSettings; //Device Mode memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); //Make sure memory''s cleared dmScreenSettings.dmSize = sizeof(dmScreenSettings); //Size of the devmode structure dmScreenSettings.dmPelsWidth = width; //Selected screen width dmScreenSettings.dmPelsHeight = height; //Selected Screen height dmScreenSettings.dmBitsPerPel = bits; //Selected Bits Per Pixel dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; //try to set selected mode and Get results. NOTE: CDS_FULLSCREEN Gets Rid of Start Bar if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { //if the mode fails, offer two options, quit or run in a window if(MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By \n Your Video Card.Use Windowed Mode Instead?", "NeHe GL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES) { fullscreen = FALSE; //Select windowed mode(Fullscreen = FALSE) } else { //Pop up a message box letting Use Know The Program is Closing MessageBox(NULL, "Program Will Now Close.", "ERROR", MB_OK | MB_ICONSTOP); return false; //exit and return false } } } if(fullscreen) //are we still in FullScreen Mode? { dwExStyle = WS_EX_APPWINDOW; //Window Extended Style dwStyle = WS_POPUP; //Windows Style ShowCursor(false); //Hide Mouse Pointer } else { dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; //Window extended style dwStyle = WS_OVERLAPPEDWINDOW; //Window style } AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle); //Adjust Window To true requested size if(!(hWnd = CreateWindowEx(dwExStyle, //Extended style for the window "OpenGL", //Class Name title, //Window title WS_CLIPSIBLINGS | //Required Window Style WS_CLIPCHILDREN | //Required Window Style dwStyle, //Selected Window Style 0, 0, //Window Position WindowRect.right - WindowRect.left, //Calculate Adjusted Window Width WindowRect.bottom - WindowRect.top, //Calculate Adjusted Window Height NULL, //No parent window NULL, //No Menu hInstance, //Instance NULL))) //Don''t Pass Anything To WM_CREATE { MessageBox(NULL, "Window Creation Error.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } static PIXELFORMATDESCRIPTOR pfd = //pfd Tells windows how we want things to be { sizeof(PIXELFORMATDESCRIPTOR), //Size of This Pixel Format Descriptor 1, //Version Number PFD_DRAW_TO_WINDOW | //format must support window PFD_SUPPORT_OPENGL | //format must support openGL PFD_DOUBLEBUFFER, //Must support Double Buffering PFD_TYPE_RGBA, //Request An RGBA Format bits, //Select Out Color Depth 0, 0, 0, 0, 0, 0, //Color bits ignored 0, //No Alpha Buffer 0, //Shift Bit Ignored 0, //No Accumulation BUffer 0, 0, 0, 0, //Accumulation Bits Ignored 16, //16Bit Z-Buffer(Depth Buffer) 0, //No Stencil Buffer 0, //No Auxiliary Buffer PFD_MAIN_PLANE, //Main Drawing Layer 0, //Reserved 0, 0, 0 //Layer Masks Ignored }; if(!(hDC = GetDC(hWnd))) //Did We Get A Device Context? { KillGLWindow(); //Reset The Display MessageBox(NULL, "Can''t Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } DWORD error = GetLastError(); if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) // Did Windows Find A Matching Pixel Format? { KillGLWindow(); // Reset The Display MessageBox(NULL, "Can''t Find A Suitable PixelFormat.", "ERROR", MB_OK|MB_ICONEXCLAMATION); return false; // Return FALSE } error = GetLastError(); if(!SetPixelFormat(hDC, PixelFormat, &pfd)) //are we able to set the pixel format? { KillGLWindow(); //Reset the Display MessageBox(NULL, "Can''t Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } error = GetLastError(); if(!(hRC = wglCreateContext(hDC))) //are we able to Get a rendering context? { error = GetLastError(); KillGLWindow(); //Reset the Display MessageBox(NULL, "Can''t Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } if(!wglMakeCurrent(hDC, hRC)) //try to Activate the rendering context { KillGLWindow(); //Reset the Display MessageBox(NULL, "Can''t Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } ShowWindow(hWnd, SW_SHOW); //Show the Window SetForegroundWindow(hWnd); //Slightly Higher Priority SetFocus(hWnd); //Set Keyboard Focus To The Window ReSizeGLScene(width, height); //Set Up Our Perspective GL Screen if(!InitGL()) //Initialize Our Newly Created GL Window { KillGLWindow(); //Reset the display MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return false; //return false } return true; } LRESULT CALLBACK WndProc(HWND hWnd, //Handle For this Window UINT uMsg, //Msg For this Window WPARAM wParam, //Additional Message Information LPARAM lParam) //Additional Message Information { switch(uMsg) //Check For Window Message { case WM_ACTIVATE: //Watch for Window Activate Message { if(!HIWORD(wParam)) //Check Minimizaion State { active = true; //Program is active } else { active = false; //Program is no longer Active } return 0; //return to the message loop } case WM_SYSCOMMAND: //Intercept System Commands { switch(wParam) //Check System Calls { case SC_SCREENSAVE: //Screensaver Trying to start? case SC_MONITORPOWER://Monitor trying to Enter Powersave? return 0; } break; //exit } case WM_CLOSE: //Did we receive a close Message? { PostQuitMessage(0); //Send A quit message return 0; //jump back } case WM_KEYDOWN: //Is a key being Held Down? { keys[wParam] = true; //if so, mark it as true return 0; //jump back } case WM_KEYUP: //Has a key been released? { keys[wParam] = false; //if so, mark it as false return 0; //jump back } case WM_SIZE: //Resize the openGL Window { ReSizeGLScene(LOWORD(lParam), HIWORD(lParam)); //LoWord = Width, HiWord = Height return 0; //jump back } } //Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd, uMsg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, //Instance HINSTANCE hPrevInstance, //Previous Instance LPSTR lpCmdLine, //Command Line Parameters int nCmdShow) //Window Show State { MSG msg; //Windows Message Structure BOOL done = false; //Bool Variable to exit loop //ask the user which screen mode they prefer if(MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO) { fullscreen = false; //Windowed Mode } //Create Our OpenGL Window if(!CreateGLWindow("Nehe''s OpenGL Framework", 640, 480, 16, fullscreen)) { return 0; //Quit if window Was Not Created } while(!done) //Loop That Runs Until done = true { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) //Is There A Message Waiting? { if(msg.message == WM_QUIT) //Have We Received A Quit Message? { done = true;//if so done = true } else //if not, deal with Window Messages { TranslateMessage(&msg); //Translate the Message DispatchMessage(&msg); //Dispatch the Message } } else // if there are no messages { //draw the scene.watch for ESC key and quit messages From DrawGLScene() if(active) //Program active? { if(keys[VK_ESCAPE]) //Was ESC Pressed? { done = true; //ESC Signalled A quit } else //not time to quit, update screen { DrawGLScene(); //Draw The Scene SwapBuffers(hDC); //Swap Buffers(Double Buffering) } } if(keys[VK_F1]) //is F1 Being Pressed? { keys[VK_F1] = false; //if so make key false KillGLWindow(); //Kill Our Current Window fullscreen = !fullscreen; //Toggle Fullscreen / Windowed Mode //Recreate Our OpenGL Window if(!CreateGLWindow("Nehe''s OpenGL FrameWork", 640, 480, 16, fullscreen)) { return 0; //Quit if Window Was not Created } } } } //Shutdown KillGLWindow(); //Kill The Window return (msg.wParam); //exit the program }
freshman in OpenGL^_^
oh, sorry, i forgot to tell u what the complier said
the programme could be complied, but when it started to run
the message box with text "Can''t Create A GL Rendering Context" jumped out, when i check the error with function GetLastError(), the error number would become 1114, which''s not in MSDN at all

the deadline''s coming, i''m nealy crazy@#..
thanks for your kindly help!
freshman in OpenGL^_^

This topic is closed to new replies.

Advertisement