prog executing extremely slowly

Started by
4 comments, last by Fruny 18 years, 2 months ago
I have been looking at this code for the last couple of weeks and can't seem to figure out why it is running so slowly. It is one of the basic tutorials (NeHe Lesson 6) what it is supposed to do is spin a cube with a testure mapped to each face. The code works fine when the texture is not mapped, but as soon as I map it on the block it is practically at a stand still. Any insight would be awesome. just to clear some things up I am using Visual studio Visual C++ Sorry I just posted the whole code to make sure all information was available

// NeHeLesson5.cpp : Defines the entry point for the application.
// sort of it is actually lesson 6
#include "stdafx.h"
#include <stdio.h>
#pragma comment( lib, "opengl32.lib" )					// Search For OpenGL32.lib While Linking
#pragma comment( lib, "glu32.lib" )					// Search For GLu32.lib While Linking
#pragma comment( lib, "vfw32.lib" )	
#include "NeHeLesson6.h"
#include "scrnsave.h"								//contains hMainInstance

#define BMP_ID 0x4D42					// bmp id

//Globals
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

GLfloat xrot;									// x axis rotation
GLfloat yrot;									// y axis rotation
GLfloat zrot;									// guess

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);				// Declaration For WndProc

void LoadBMPTexture(int id) 
{	
	HBITMAP hBmp = NULL;
	 
	hBmp = (HBITMAP) ::LoadImage(hInstance, 
		MAKEINTRESOURCE(id), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);

	//get info about the bitmap
	BITMAP BM;
	::GetObject(hBmp, sizeof(BM), &BM);

	//tell OpenGL to ignore padding at ends of rows
	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, BM.bmWidth, BM.bmHeight,
		0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BM.bmBits);

	DeleteObject((HGDIOBJ) hBmp);  //avoid memory leak (Windows)
	DeleteObject(&BM);
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)				// Resize And Initialize The GL Window
{
	if (height==0){								// Prevent A Divide By Zero By
		height=1;							// Making 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);
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix
	glLoadIdentity();							// Reset The Modelview Matrix
}

// All Setup For OpenGL Goes Here
int InitGL(GLvoid)								
{
	glEnable(GL_TEXTURE_2D);
	LoadBMPTexture(IDB_buther);
	//	if(!LoadGLTextures())
		//return false;
	
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	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);					// enable 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;
}

// Drawrings go here
int DrawGLScene(GLvoid)							
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	//Clear the Screen and the depth 
	glLoadIdentity();							// Reset the current modelview matrix
	
	glTranslatef(0.0f, 0.0f, -5.0f);			// move the cursor to the left 1 and into the screen 6
	
	glRotatef(xrot, 1.0f, 0.0f, 0.0f);			// rotate on the x-axis
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);			
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	glBegin(GL_QUADS);							// quadrangle drawings
		// Front Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
	
	glEnd();

	xrot+=0.3f;								// X Axis Rotation
	yrot+=0.2f;								// Y Axis Rotation
	zrot+=0.4f;								// Z Axis Rotation
	return TRUE;								//  Everything went ok 
}

GLvoid KillGLWindow(GLvoid){					// kills the window
	if(fullscreen){								//fullscreen?
		ChangeDisplaySettings(NULL,0);			// if yes switch back to the desktop
		ShowCursor(TRUE);						//  Illuminate
	}
	if(hRC){									//Rendering context?
		if(!wglMakeCurrent(NULL,NULL)){			// Did the RC detach from the DC
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);	// if not shit out a msg
		}
		if (!wglDeleteContext(hRC)){				//  did the RC delete
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); // dido
		}
		hRC=NULL;								// set RC to NULL
	}
	if (hDC && !ReleaseDC(hWnd,hDC)){			//  did the DC release
		MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hDC=NULL;								// Set D to NULL
	}

	if(hWnd && !DestroyWindow(hWnd)){			// did the window die
		MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hWnd=NULL;								// Set hWnd to NULL
	}
	if (!UnregisterClass("OpenGl",hInstance)){	// Did we unreg our window
		MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hInstance=NULL;							// die hInstance, die
	}
}

BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag){

	GLuint	PixelFormat;						// holds the results after searching for a match

	WNDCLASS wc;								// window class structure
	DWORD dwExStyle;							// window extended style
	DWORD dwStyle;								// window style
	
	RECT WindowRect;							// grabs rectangle upper left / lower right vals
	WindowRect.left=(long)0;					//set left val to 0
	WindowRect.right=(long)width;				// set right val to width requested
	WindowRect.top=(long)0;						// set top val to 0
	WindowRect.bottom=(long)height;				// set bottom val to height requested

	fullscreen=fullscreenflag;					// set global fullscreen flag

	hInstance = GetModuleHandle(NULL);			// grab an instacne for our window
	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw on move, and own DC for window
	wc.lpfnWndProc = (WNDPROC) WndProc;			// WndProc handles message
	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 an icon from windows
	wc.hCursor = LoadCursor(NULL, IDC_CROSS);	// load the arrow pointer
	wc.hbrBackground = NULL;					//  no background required
	wc.lpszMenuName = NULL;						// we don't want a menu
	wc.lpszClassName = "OpenGL";				// Set class name

	if(!RegisterClass(&wc)){					// register deez
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;							// Exit And Return FALSE
	}

	if (fullscreen){							// attemp fullscreen mode?
		DEVMODE dmScreenSettings;				// Device Mode
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	//makes sure mem is cleared
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);	// Size of Devmode structure
		dmScreenSettings.dmPelsWidth = width;	// selected screen width
		dmScreenSettings.dmPelsHeight = height; // selected height
		dmScreenSettings.dmBitsPerPel = bits;	// bits per pixel
		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 fails, offer quit or run in a window
			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;				// Select windowes mode
			}
			else{
				return FALSE;					// exit and falsify
			}
		}
	}
	if(fullscreen){								// we still go on fullscreen?
		dwExStyle=WS_EX_APPWINDOW;				// window extended style
		dwStyle = WS_POPUP;						// window style
		ShowCursor(FALSE);						//  guess
	}else {
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;	// window extended style
		dwStyle = WS_OVERLAPPEDWINDOW;			// windows style
	}

	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);	// adjust window to true requested size 
	
	if (!(hWnd = CreateWindowEx( dwExStyle, "OpenGL", title,	// ext style, class name, wind title
		WS_CLIPSIBLINGS |						//required window style
		WS_CLIPCHILDREN |						// dido
		dwStyle,								// dido again
		0, 0,									// position
		WindowRect.right - WindowRect.left,		// calculate width
		WindowRect.bottom - WindowRect.top,		// calculate height
		NULL,									// no parent window
		NULL,									// no menu
		hInstance,								// Instance
		NULL))){								// DO NOT pass anything to WM_CREATE or Hannible the Cannible
			
			KillGLWindow();						//reset display
			MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;
		}
		static PIXELFORMATDESCRIPTOR pfd =		// pfd tells windows how we want things
		{
			sizeof(PIXELFORMATDESCRIPTOR),		// its the size
			1,									// wersion num
			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 a rgba format
			bits,								// select our color depth
			0, 0, 0, 0, 0, 0,					// color bits ignored
			0,									// no alpha buffer
			0,									// shift bit ignored
			0,									// no accumulation bits ignored
			0, 0, 0, 0,							// accumulation bits ignored
			16,									// 16bit z buffer(depth buffer)
			0,									// no stencil buffer
			0,									// no aux buffer
			PFD_MAIN_PLANE,						// main drawring layer
			0,									// reserved
			0, 0, 0								// layer masks ignored
		};

		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))){	// did Windows find a matching pixel format
			KillGLWindow();
			MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;
		}

		if(!SetPixelFormat(hDC,PixelFormat, &pfd)){		// Set the pixel format
			KillGLWindow();
			MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;
		}

		if(!(hRC=wglCreateContext(hDC))){			// did we get a RC
			KillGLWindow();	
			MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			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);					// raises priority
		SetFocus(hWnd);								// sets keyboard focus to the Window
		ReSizeGLScene(width,height);				// set up our perspective gl screen 

		if(!InitGL()){
			KillGLWindow();
			MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return FALSE;
		}

		return true;							// holy crap we did it
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){		//  this where we deal with all win messages
	switch (msg){			// check for a message
		case WM_ACTIVATE:
			if(!HIWORD(wParam))
				active = true;
			else
				active = false;

			return 0;

		case WM_DESTROY:
		{
			switch(wParam){
				case SC_SCREENSAVE:				// screw screensaver
				case SC_MONITORPOWER:			// halt monitor powersaver
				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, msg, wParam, lParam);
}


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

	cout << "test msg\n";

	MSG msg;							// Win message
	BOOL done = false;					// exit cond

	// 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
	}

	if(!CreateGLWindow("Not2's OpenGL 3d Polygon Color Rotate Kraftwerk", 1280, 768, 32, fullscreen)){
		return 0;
	}

	while(!done){
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){		//message?
			if(msg.message == WM_QUIT){						//is it a quit message?
				done = true;
			}else{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}else{												//no message do your shit
			if(active){
				if(keys[VK_ESCAPE]){
					done = true;
				}else{
					DrawGLScene();
					SwapBuffers(hDC);
				}
			}
			if(keys[VK_F1]){							// F1 is being pressed
				keys[VK_F1] = false;
				KillGLWindow();
				fullscreen = !fullscreen;

				if (!CreateGLWindow("Not2's 3d OpenGL Polygon Color Rotate Kraftwerk", 800, 600, 32, fullscreen)){
					return 0;
				}
			}
		}
	}
	KillGLWindow();
	return (msg.wParam);
}




Merp
Advertisement
i dont see anything other than your casrd not having GL Compatability, one thing is you should set it to link to those libs in your settings not using #pragma
Quote:Original post by C plus noob
i dont see anything other than your casrd not having GL Compatability, one thing is you should set it to link to those libs in your settings not using #pragma


Either way is fine and I have had no trouble with #pragma in the past. Don't quote me but I think a lot of other people actually prefer #pragma to setting it in the linker options.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
The card is fine. It runs other gl apps without a problem.
Merp
What gfx card do you have? The code looks ok but the resolution you are trying to use is kind of high try using a lower res and switching to 16 bits may make a difference...
Quote:Original post by u235
Either way is fine and I have had no trouble with #pragma in the past. Don't quote me but I think a lot of other people actually prefer #pragma to setting it in the linker options.


And I prefer to add libraries to the project as dependences/'source' files.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement