Advertisement

Initialization Failed!

Started by October 28, 2001 08:18 AM
8 comments, last by Pagan 22 years, 11 months ago
I''ve got the NeHe tutorials code in my VC++, like this, the lesson 7: /* Lesson 7: Texture Filters, Lighting & Keyboard Control * 2001, From NeHe OpenGL Tutorials * */ #include #include #include #include #include // Global Variables HGLRC hRC = NULL; HDC hDC = NULL; HWND hWnd = NULL; HINSTANCE hInstance; bool keys[256]; bool active = TRUE; bool fullscreen = TRUE; // Lighting on / off, L / F pressed? bool light; bool lp; bool fp; // x, y rotations and speeds GLfloat xrot; GLfloat yrot; GLfloat xspeed; GLfloat yspeed; // Depth into the screen GLfloat z = -5.0f; // Ambient, diffuse light values, and light position GLfloat LightAmbient[] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat LightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f}; GLfloat LightPosition[] = {0.0f, 0.0f, 2.0f, 1.0f}; // Filter to use and storage for 3 textures GLuint filter; GLuint texture[3]; // Forward declarations LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Load a bitmap image AUX_RGBImageRec *LoadBMP(char *Filename) { // File handle FILE *File = NULL; // Make sure a filename was given if (!Filename) { return NULL; } // Check to see if the file exists File = fopen(Filename, "r"); // File exists? if (File) { fclose(File); return auxDIBImageLoad(Filename); } return NULL; } // Load bitmaps and convert to textures int LoadGLTextures() { // Status indicator int Status = FALSE; // Create storage for the texture AUX_RGBImageRec *TextureImage[1]; memset(TextureImage, 0, sizeof(void *)*1); // Load the bitmap, check for errors, if bitmap''s not found, quit if (TextureImage[0] = LoadBMP("Setup.bmp")) { Status = TRUE; // Create 3 textures glGenTextures(3, &texture[0]); // Create nearest filtered texture glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // Create linear filtered texture glBindTexture(GL_TEXTURE_2D, texture[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // Create mipmapped texture glBindTexture(GL_TEXTURE_2D, texture[2]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } // Texture exists, free texture image memory if (TextureImage[0]) { if (TextureImage[0]->data) { free(TextureImage[0]->data); } free(TextureImage[0]); } return Status; } // Resize the OpenGL scene GLvoid ReSizeGLScene(GLsizei width, GLsizei height) { // Prevent a divide by zero if (height == 0) { height = 1; } // Reset the current viewport glViewport(0, 0, width, height); // Select the projection matrix and reset glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Calculate the aspect ratio of the window gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f); // Select the modelview matrix and reset glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int InitGL(GLvoid) { if (!LoadGLTextures()) { return FALSE; } // Enable texture mapping glEnable(GL_TEXTURE_2D); // Enable smooth shading glShadeModel(GL_SMOOTH); // Clear the background glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Depth testing, buffer, enabling and type glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); // Best perspective correction glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Setup the ambient, diffuse light, and light position glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); // Enable light 1 glEnable(GL_LIGHT1); return TRUE; } int DrawGLScene(GLvoid) { // Clear the screen and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Reset the modelview matrix glLoadIdentity(); // Translate into/out of the screen by z glTranslatef(0.0f, 0.0f, z); // Rotate on the x, y axises glRotatef(xrot, 1.0f, 0.0f, 0.0f); glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Select a texture based on filter glBindTexture(GL_TEXTURE_2D, texture[filter]); // Start drawing quads glBegin(GL_QUADS); // Front Face glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Point 1 (Front) glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Point 2 (Front) glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Front) glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Point 4 (Front) // Back Face glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Back) glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Point 2 (Back) glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Point 3 (Back) glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Point 4 (Back) // Top Face glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Point 1 (Top) glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Point 2 (Top) glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Top) glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Point 4 (Top) // Bottom Face glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Bottom) glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Point 2 (Bottom) glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Point 3 (Bottom) glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Point 4 (Bottom) // Right face glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Point 1 (Right) glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Point 2 (Right) glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Point 3 (Right) glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Point 4 (Right) // Left Face glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Left) glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Point 2 (Left) glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Point 3 (Left) glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Point 4 (Left) glEnd(); // Done Drawing Quads // Rotate speeds xrot += xspeed; yrot += yspeed; return TRUE; } GLvoid KillGLWindow(GLvoid) { if (fullscreen) { // Switch back to the desktop and show mouse pointer ChangeDisplaySettings(NULL, 0); ShowCursor(TRUE); } if (hRC) { // Are we able to release the DC and RC contexts? if (!wglMakeCurrent(NULL, NULL)) { MessageBox(NULL, "Release of DC and RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } // Are we able to delete the RC context? if (!wglDeleteContext(hRC)) { MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); } hRC = NULL; } // Are we able to release the DC if (hDC && !ReleaseDC(hWnd, hDC)) { MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hDC = NULL; } // Are we able to destroy the window if (hWnd && !DestroyWindow(hWnd)) { MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hWnd = NULL; } // Are we able the unregister the class if (!UnregisterClass("OpenGL", hInstance)) { MessageBox(NULL, "Coule Not Unregister Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION); hInstance = NULL; } } BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { // Holds the result after searching for a match GLuint PixelFormat; DWORD dwExStyle; DWORD dwStyle; // Grabs rectangle upper left / lower right values RECT rt; rt.left = (long)0; rt.right = (long)width; rt.top = (long)0; rt.bottom = (long)height; // Set the global fullscreen flag fullscreen = fullscreenflag; // Grab an instance for our window hInstance = GetModuleHandle(NULL); WNDCLASS wc; 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"; // Attempt to register the window class if (!RegisterClass(&wc)) { MessageBox(NULL, "Failed to Register the Window Class", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // Attempt fullscreen mode 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; // Try to set selected mode and get results if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { // If the mode fials, offer two options. Quit or run in a window if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "FULLSCREEN ERROR", MB_YESNO | MB_ICONEXCLAMATION) == IDYES) { fullscreen = FALSE; } else { // Tell you the the program is closing MessageBox(NULL, "Program Is Closing Now.", "ERROR", MB_OK | MB_ICONSTOP); return FALSE; } } } // Are we still in fullscreen mode? if (fullscreen) { dwExStyle = WS_EX_APPWINDOW; dwStyle = WS_POPUP; ShowCursor(FALSE); } else { dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle = WS_OVERLAPPEDWINDOW; } // Adjust window to true requested size AdjustWindowRectEx(&rt, dwStyle, FALSE, dwExStyle); if (!(hWnd = CreateWindowEx(dwExStyle, "OpenGL", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, 0, 0, rt.right - rt.left, rt.bottom - rt.top, NULL, NULL, hInstance, NULL))) { KillGLWindow(); MessageBox(NULL, "Window Creation Error.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // pdf tells windows how we want things to be static PIXELFORMATDESCRIPTOR 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 }; // Did we get a device context? if (!(hDC = GetDC(hWnd))) { KillGLWindow(); MessageBox(NULL, "Can''t Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // Did windows find a matching pixel format? if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) { KillGLWindow(); MessageBox(NULL, "Can''t Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // Are we able to set the pixel format? if (!SetPixelFormat(hDC, PixelFormat, &pfd)) { KillGLWindow(); MessageBox(NULL, "Can''t Set the PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // Are we able to get a redering context? if (!(hRC = wglCreateContext(hDC))) { KillGLWindow(); MessageBox(NULL, "Can''t Create a GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } // Try to activate the rendering context 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); // Initialize our newly created GL window if (!InitGL()) { KillGLWindow(); MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION); return FALSE; } return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { 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(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nshowCmd) { MSG msg; BOOL done = FALSE; // Ask user which screen mode the prefer if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO) { fullscreen = FALSE; } // Create our OpenGL window if (!CreateGLWindow("Lesson 7: Texture Filters, Lighting & Keyboard Control", 640, 480, 16, fullscreen)) { return 0; } while (!done) { // A message waiting? if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { done = TRUE; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { // Draw the scene. Watch for ESC key and quit message from DrawGLScene() if (active && !DrawGLScene() || keys[VK_ESCAPE]) { done = TRUE; } else { SwapBuffers(hDC); if (keys[''L''] && !lp) { lp = TRUE; light = !light; if (!light) { glDisable(GL_LIGHTING); } else { glEnable(GL_LIGHTING); } } if (!keys[''L'']) { lp = FALSE; } if (keys[''F''] && !fp) { fp = TRUE; filter += 1; if (filter > 2) { filter = 0; } } if (!keys[''F'']) { fp = FALSE; } if (keys[VK_PRIOR]) { z -= 0.02f; } if (keys[VK_NEXT]) { z += 0.02f; } if (keys[VK_UP]) { xspeed -= 0.01f; } if (keys[VK_DOWN]) { xspeed += 0.01f; } if (keys[VK_RIGHT]) { yspeed += 0.01f; } if (keys[VK_LEFT]) { yspeed -= 0.01f; } if (keys[VK_F1]) { keys[VK_F1] = FALSE; KillGLWindow(); fullscreen = !fullscreen; // Recreate our OpenGL window if (!CreateGLWindow("Lesson 7: Texture Filters, Lighting & Keyboard Control", 640, 480, 16, fullscreen)) { return 0; } } } } } // Shutdown KillGLWindow(); return (msg.wParam); } compiling is okay, but when it is executed, it said ''Initialization Failed'', could you help me to solve this problem? thanks in advance.
This post removed by me as I hit submit twice...


Edited by - cyberdrek on October 28, 2001 9:54:35 AM
[Cyberdrek | ]
Advertisement
First things first, when posting a snipet, you shouldn't post that much code, second, try debugging on your own first and then asking why it's not working with a small piece of code that shows where you think the problem might be. Don't put the complete sources. Most people on this board are able to get the nehe tuts. Now, on to your question, here's the snippet that returns the error you're seeing:

    // Initialize our newly created GL windowif (!InitGL()){   KillGLWindow();   MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK |              MB_ICONEXCLAMATION);   return FALSE;}    


Now try and figure out what the problem is and why it's giving you that error when you run. If you can't figure it out, you didn't try hard enough. Try again. If after about 1 week of debugging you can't figure it out, then ask for help here. Now, I know from downloading that code and compiling it myself that it should work. Did you make any modifications to the InitGL function??? Anyhow, try solving it with that info.

"And that's the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP's Invasive Production Activation Technology!

"gitty up" -- Kramer

Edited by - cyberdrek on October 28, 2001 9:58:25 AM
[Cyberdrek | ]
That is a classic mistake, no offence meant .
The first thing I said to myself was "texture problem". Check that the texture is there and formatted correctly. Also try putting MessageBox() calls in the texture Functions with numbers like:

MessageBox(NULL, "1", "DEBUG", MB_OK);

//Some code

MessageBox(NULL, "2", "DEBUG", MB_OK);

If the "1" shows, but the "2" doesn''t. then your problem is somewhere between those 2 calls.

Welcome to the wonderful world of Debugging!

------------------------------
Baldur K
I agree with the two previous posts that you should have a go at debugging it yourself, but because I''m nice I''ll suggest one possible problem (the one that I had when compiling one of the NeHe tutorials) - is your texture located in the folder that the program looks for it in? (Your code tries to load "Setup.bmp", my copy loads "data/crate.bmp")
Here''s some advice for you. Post code in happy little code boxes. As can been seen in the example below, their much neater. I found it hard to read the code in your post.

  void Example(int mode){   If(mode==1)      Function1();   else      End();}  


All you need to do is place "source" in "][" brackets before the code. And "/source" in "][" brackets after the code.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
Advertisement
Thank you for your advice.
This is a test, Note, this is only a test, in the event of a real emergency I''ll post a new message for all to see

  code  


HardSniper
HardSniper
I was just beating my head against the wall trying to figure out this same error message in tut #25. Using Dev-C++ the code would compile fine(after adding "#include <stdlib.h>"), but when I did compile&execute to run the program, I always got the "Initialization Falied!" error. I tracked down the error to this:

  FILE *file = fopen(filename, "rb");						// Open The TGA File	if(	file==NULL ||								// Does File Even Exist?		fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?		memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0		||	// Does The Header Match What We Want?		fread(header,1,sizeof(header),file)!=sizeof(header))			// If So Read Next 6 Header Bytes	{		if (file == NULL)							// Did The File Even Exist? *Added Jim Strong*			return false;							// Return False		else		{			fclose(file);							// If Anything Failed, Close The File			return false;							// Return False		}	}  


It turns out the fopen() was failing, so (file==null) was true and it was returning false. I''m still not sure exactly what caused it to not open, but if I run the .exe file from outside the Dev-C++ environment, then I no longer get the error! So, now I compile in Dev-C++, and then run the program via windows explorer.

Incidentally, I just went back to try tutorial 6 again since I had gotten the same "Initialization Failed" error in that one. Same thing - as long as I don''t run the compiled executable directly from Dev-C++, it works.
---------------Delphi 6 Personal Edition, free for non-commercial use.
This relates to the directory your exe is executed in. You can modify this environment variable from within the Dev-C++ IDE, though I can''t tell you where because I''m on Bloodshed strike until Dev-C++ v.5 comes out.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement