Texture Loading

Started by
4 comments, last by das612 13 years, 9 months ago
I have been working on my first Open GL program and i followed a tutorial to add a texture to the 3d object that i created but I keep getting errors that I dont understand and I'm kind of lost if anyone understands this please help.

#define _CRT_SECURE_NO_WARNINGS#include <windows.h>		#include <gl\gl.h>			#include <gl\glu.h>			#include <gl\glaux.h>		#include<stdio.h>#include<conio.h>HDC			hDC=NULL;		HGLRC		hRC=NULL;		HWND		hWnd=NULL;		HINSTANCE	hInstance;		bool	keys[256];			bool	active=TRUE;		bool	fullscreen=TRUE;	GLuint texture[1];float rtri = 0;AUX_RGBImageRec *LoadBMP(char *Filename){FILE *File=NULL;if (!Filename){return NULL;}File=fopen(Filename,"r");if (File){fclose(File);return auxDIBImageLoad(Filename);}return NULL;}int LoadGLTextures(){int Status=FALSE;AUX_RGBImageRec *TextureImage[1];memset(TextureImage,0,sizeof(void *)*1);if (TextureImage[0]=LoadBMP(Pictures/fire.bmp));{Status=TRUE;glGenTextures(1, &texture[0]);glBindTexture(GL_TEXTURE_2D, texture[0]);glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);}if (TextureImage[0]){if (TextureImage[0]->data){free(TextureImage[0]->data);}free(TextureImage[0]);}return Status;}LRESULT	CALLBACK 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)										{	if (!LoadGLTextures()){return FALSE;}glEnable(GL_TEXTURE_2D);	glShadeModel(GL_SMOOTH);		glClearColor(0.0f, 0.0f, 0.0f, 0.5f);					glClearDepth(1.0f);										glEnable(GL_DEPTH_TEST);								glDepthFunc(GL_LEQUAL);									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(0.0f,0.0f,-20.0f);glRotatef(rtri,1.0f,0.0f,0.0f);glBindTexture(GL_TEXTURE_2D, texture[0]);glBegin(GL_TRIANGLES);	/* 1*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -3.0f, 0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( 1.0f, -1.0f, 0.0f);/* 2*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -3.0f, 0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( -1.0f, -1.0f, 0.0f);/* 3*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -3.0f, 0.0f);glVertex3f( 0.0f, -2.0f, -1.0f);glVertex3f( 1.0f, -1.0f, 0.0f);/* 4*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -3.0f, 0.0f);glVertex3f( 0.0f, -2.0f, -1.0f);glVertex3f( -1.0f, -1.0f, 0.0f);/* 5*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( -1.0f, -1.0f, 0.0f);glVertex3f( -0.5f, -1.0f, 1.0f);/* 6*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( 1.0f, -1.0f, 0.0f);glVertex3f( 0.5f, -1.0f, 1.0f);/* 7*/glColor3f(0.0f,0.0f,0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( -0.5f, -1.0f, 1.0f);/* 8*/glColor3f(0.0f,0.0f,0.0f);glVertex3f( 0.0f, -2.0f, 1.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( 0.5f, -1.0f, 1.0f);/* 9*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( -1.0f, -1.0f, 0.0f);glVertex3f( -4.0f, 0.0f, 0.0f);glVertex3f( -1.0f, 1.0f, 1.0f);/* 10*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( -4.0f, 2.0f, 0.0f);glVertex3f( -4.0f, 0.0f, 0.0f);glVertex3f( -1.0f, 1.0f, 1.0f);/* 11*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( -1.0f, -1.0f, 0.0f);glVertex3f( -4.0f, 0.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);/* 12*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( -4.0f, 2.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);glVertex3f( -1.0f, 1.0f, 1.0f);/* 13*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( -4.0f, 0.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);glVertex3f( -1.0f, 1.0f, 1.0f);/* 14*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( 1.0f, -1.0f, 0.0f);glVertex3f( 4.0f, 0.0f, 0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);/* 15*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( 4.0f, 2.0f, 0.0f);glVertex3f( 4.0f, 0.0f, 0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);/* 16*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( 1.0f, -1.0f, 0.0f);glVertex3f( 4.0f, 0.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);/* 17*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( 4.0f, 2.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);glVertex3f( 1.0f, 1.0f, 1.0f);/* 18*/glColor3f(1.0f,1.0f,0.0f);glVertex3f( 4.0f, 0.0f, 0.0f);glVertex3f( 0.0f, 1.0f, -1.0f);glVertex3f( 1.0f, 1.0f, 1.0f);/* 19*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( -1.0f, -1.0f, 0.0f);glVertex3f( -0.5f, -1.0f, 1.0f);/* 20*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( 1.0f, -1.0f, 0.0f);glVertex3f( 0.5f, -1.0f, 1.0f);/* 21*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( -1.0f, -1.0f, 0.0f);glVertex3f( -1.0f, 1.0f, 1.0f);/* 22*/glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f);glVertex3f( 1.0f, -1.0f, 0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f); glVertex3f(-1.0f, 1.0f, 1.0f);glVertex3f( 0.0f, 1.0f, 2.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f( 0.0f, 0.0f, 2.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( 0.0f, 1.0f, 2.0f); ///////glColor3f(0.0f,1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-0.5f, 3.0f, 0.0f); glVertex3f( 0.0f, 1.0f, 2.0f);glColor3f(0.0f,1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( 0.5f, 3.0f, 0.0f);glVertex3f( 0.0f, 1.0f, 2.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f( 0.0f, 1.0f, 2.0f); glVertex3f( 0.0f, 3.0f, 1.0f);glVertex3f( 0.5f, 3.0f, 0.0f);glColor3f(0.0f,1.0f,0.0f);glVertex3f( 0.0f, 1.0f, 2.0f); glVertex3f( 0.0f, 3.0f, 1.0f); glVertex3f(-0.5f, 3.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-0.5f, 3.0f, 0.0f); glVertex3f( 0.0f, 3.0f, 1.0f); glVertex3f( 0.0f, 4.0f, 0.0f);glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.5f, 3.0f, 0.0f); glVertex3f( 0.0f, 3.0f, 1.0f);glVertex3f( 0.0f, 4.0f, 0.0f);glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.0f,-2.0f,-1.0f); glVertex3f(-1.0f,-1.0f, 0.0f);glVertex3f( 0.0f, 1.0f,-1.0f);glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.0f,-2.0f,-1.0f); glVertex3f( 1.0f,-1.0f, 0.0f);glVertex3f( 0.0f, 1.0f,-1.0f);///////glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 1.0f,-1.0f); glVertex3f(-1.0f, 1.0f, -0.5f);glVertex3f(-0.5f, 3.0f, 0.0f); glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.0f, 1.0f,-1.0f); glVertex3f( 1.0f, 1.0f, -0.5f);glVertex3f( 0.5f, 3.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.0f, 1.0f,-1.0f); glVertex3f( -0.5f, 3.0f, 0.0f);glVertex3f( 0.0f, 4.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 0.0f, 1.0f,-1.0f); glVertex3f( 0.5f, 3.0f, 0.0f);glVertex3f( 0.0f, 4.0f, 0.0f);glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.1f, 3.0f, 1.0f); glVertex3f( 0.0f, 4.0f, 0.0f);glVertex3f( 0.0f, 4.0f, 2.0f);glColor3f(0.0f,1.0f,1.0f); glVertex3f( -0.1f, 3.0f, 1.0f); glVertex3f( 0.0f, 4.0f, 0.0f);glVertex3f( 0.0f, 4.0f, 2.0f);glColor3f(0.0f,1.0f,1.0f); glVertex3f( 0.1f, 3.0f, 1.0f); glVertex3f( -0.1f, 3.0f, 1.0f);glVertex3f( 0.0f, 4.0f, 2.0f);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 1.0f, 1.0f, 1.0f);glVertex3f( 0.5f, 3.0f, 0.0f);glVertex3f( 1.0f, 1.0f, -0.5f); glColor3f(1.0f,0.0f,0.0f);glVertex3f( -1.0f, 1.0f, 1.0f);glVertex3f( -0.5f, 3.0f, 0.0f);glVertex3f( -1.0f, 1.0f, -0.5f);glEnd();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){	GLuint		PixelFormat;				WNDCLASS	wc;							DWORD		dwExStyle;					DWORD		dwStyle;					RECT		WindowRect;					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;				}	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											};		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(	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			nCmdShow)			{	MSG		msg;										BOOL	done=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);							} 


and here is the errors

1>c:\users\drew\documents\visual studio 2008\projects\space age\space age\wds1.cpp(48) : error C2065: 'Pictures' : undeclared identifier
1>c:\users\drew\documents\visual studio 2008\projects\space age\space age\wds1.cpp(48) : error C2065: 'fire' : undeclared identifier
1>c:\users\drew\documents\visual studio 2008\projects\space age\space age\wds1.cpp(48) : error C2228: left of '.bmp' must have class/struct/union
Advertisement
What is the argument type for LoadBMP()? I would assume it's a string, which means you need to put the file location/name in quotes, as in:

if (TextureImage[0]=LoadBMP("Pictures/fire.bmp"))

Also, get rid of the semicolon after the if statement, it ends the entire if block.

edit: Double-take, shouldn't that be a == in the boolean statement there? Did you copy + paste this code verbatim?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

That line of code now looks like this
if (TextureImage[0]==LoadBMP("Pictures/fire.bmp"))
but I'm still getting errors.
1>wds1.obj : error LNK2019: unresolved external symbol _auxDIBImageLoadA@4 referenced in function "struct _AUX_RGBImageRec * __cdecl LoadBMP(char *)" (?LoadBMP@@YAPAU_AUX_RGBImageRec@@PAD@Z)
1> fatal error LNK1120: 1 unresolved externals
So I'll ask again: was this code just copied straight out of the tutorial or book you're learning from? Are you jumping directly into 3D or have you already done some console/2D programming in C++?

If you don't know what's actually going on with the code, you can't learn much by copying someone else's code and then posting every error that you get. The first one was solved with a quick read-through of the line that the error referred to (48), and I would expect someone "ready" to try learning OpenGL to be able to catch that.

Most linker errors (starting with LNK) stem from not including or linking an associated library (either .lib or .dll depending on if you're linking statically or dynamically). Which IDE are you using to build this? Never mind, Visual Studio 2008. Have you set up the project's dependencies so that it knows to reference the OpenGL libraries?
What tutorial are you following?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Starting out with OpenGL in a Windows system can be a bit confusing. As BCullis mentioned, a link error usually indicates you need to link to additional libraries which contain code not found during the linking process.

That's not a "learning opengl" problem, that's learning how to setup your project.

Because you include gl.h, glu.h, and glaux.h - you should ensure your project settings for the linker input include opengl32.lib, glu32.lib and glaux.lib. Hopefully, that should resolve the externals.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Nevermind I have solved my own problem, I simply replaced glaux.h with a glaux replacement code, namely bmp.h and bmp.cpp so that it could load the bitmap. Thankyou for your help.

This topic is closed to new replies.

Advertisement