Archived

This topic is now archived and is closed to further replies.

colinisinhere

textures all blue?

Recommended Posts

my code is:
  
#include <windows.h>		
#include <math.h>			
#include <stdio.h>			
#include <stdarg.h>			
#include <gl\gl.h>			
#include <gl\glu.h>			
#include <gl\glaux.h>			

HDC			hDC=NULL;		
HGLRC		hRC=NULL;		
HWND		hWnd=NULL;		
HINSTANCE	hInstance;		

int width = 1024;
int height = 768;

GLfloat	yrot;

bool	keys[256];			
bool	active=TRUE;		
bool	fullscreen=TRUE;

GLuint	base;
GLuint	base2;
GLuint	texture[2];

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image

{
	FILE *File=NULL;									// File Handle


	if (!Filename)										// Make Sure A Filename Was Given

	{
		return NULL;									// If Not Return NULL

	}

	File=fopen(Filename,"r");							// Check To See If The File Exists


	if (File)											// Does The File Exist?

	{
		fclose(File);									// Close The Handle

		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer

	}

	return NULL;										// If Load Failed Return NULL

}

int LoadGLTextures()									// Load Bitmaps And Convert To Textures

{
	int Status=FALSE;									// Status Indicator


	AUX_RGBImageRec *TextureImage[2];					// Create Storage Space For The Texture


	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL


	// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit

	if (TextureImage[0]=LoadBMP("Data/ball.bmp"))
	{
		Status=TRUE;									// Set The Status To TRUE


		glGenTextures(1, &texture[0]);					// Create The Texture


		// Typical Texture Generation Using Data From The Bitmap

		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 Texture Exists

	{
		if (TextureImage[0]->data)							// If Texture Image Exists

		{
			free(TextureImage[0]->data);					// Free The Texture Image Memory

		}

		free(TextureImage[0]);								// Free The Image Structure

	}
	
	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL


	// Load The Bitmap, Check For Errors, If Bitmap''s Not Found Quit

	if (TextureImage[1]=LoadBMP("Data/paddle.bmp"))
	{
		Status=TRUE;									// Set The Status To TRUE


		glGenTextures(1, &texture[1]);					// Create The Texture


		// Typical Texture Generation Using Data From The Bitmap

		glBindTexture(GL_TEXTURE_2D, texture[1]);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	if (TextureImage[1])									// If Texture Exists

	{
		if (TextureImage[1]->data)							// If Texture Image Exists

		{
			free(TextureImage[1]->data);					// Free The Texture Image Memory

		}

		free(TextureImage[1]);								// Free The Image Structure

	}

	return Status;										// Return The Status

}


GLvoid BuildFont_bigRed(GLvoid)								
{
	HFONT	font;									
	HFONT	oldfont;								

	base = glGenLists(96);								

	font = CreateFont(	-100,							
						0,							
						0,							
						0,							
						0,					
						FALSE,						
						FALSE,							
						FALSE,						
						ANSI_CHARSET,					
						OUT_TT_PRECIS,					
						CLIP_DEFAULT_PRECIS,			
						1000,		
						FF_DONTCARE|DEFAULT_PITCH,		
						"RedAlert");				

	oldfont = (HFONT)SelectObject(hDC, font);           
	wglUseFontBitmaps(hDC, 32, 96, base);				
	SelectObject(hDC, oldfont);							
	DeleteObject(font);									
}

GLvoid KillFont_bigRed(GLvoid)								
{
	glDeleteLists(base, 96);						
}

GLvoid glPrint_bigRed(const char *fmt, ...)				
{
	char		text[256];								
	va_list		ap;										

	if (fmt == NULL)								
		return;										

	va_start(ap, fmt);								
	    vsprintf(text, fmt, ap);					
	va_end(ap);										

	glPushAttrib(GL_LIST_BIT);						
	glListBase(base - 32);							
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	
	glPopAttrib();									
}	

GLvoid BuildFont_small(GLvoid)								
{
	HFONT	font;									
	HFONT	oldfont;								

	base2 = glGenLists(96);								

	font = CreateFont(	-21,							
						0,							
						0,							
						0,							
						0,					
						FALSE,						
						FALSE,							
						FALSE,						
						ANSI_CHARSET,					
						OUT_TT_PRECIS,					
						CLIP_DEFAULT_PRECIS,			
						1000,		
						FF_DONTCARE|DEFAULT_PITCH,		
						"Ariel MT");				

	oldfont = (HFONT)SelectObject(hDC, font);           
	wglUseFontBitmaps(hDC, 32, 96, base2);				
	SelectObject(hDC, oldfont);							
	DeleteObject(font);									
}

GLvoid KillFont_small(GLvoid)								
{
	glDeleteLists(base2, 96);						
}

GLvoid glPrint_small(const char *fmt, ...)				
{
	char		text[256];								
	va_list		ap;										

	if (fmt == NULL)								
		return;										

	va_start(ap, fmt);								
	    vsprintf(text, fmt, ap);					
	va_end(ap);										

	glPushAttrib(GL_LIST_BIT);						
	glListBase(base2 - 32);							
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	
	glPopAttrib();									
}	

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		
{
	if (height==0)										
	{
		height=1;										
	}

	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())								// Jump To Texture Loading Routine ( NEW )

	{
		return FALSE;									// If Texture Didn''t Load Return FALSE

	}
	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);
	BuildFont_bigRed();
	BuildFont_small();
	return TRUE;										
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int DrawGLScene(GLvoid)									
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glColor3f(1.0f, 0.0f, 0.0f);
    glTranslatef(0.0f, 0.0f, -2.5f);		//Text closes position to camera

	glRasterPos2f(-1.01f, 0.8f);
    glPrint_bigRed("X-treme Pong");
	glLoadIdentity();
	glColor3f(0.0f, 0.0f, 0.8f);
    glTranslatef(0.0f, 0.0f, -2.5f);		//Text closes position to camera

	glRasterPos2f(-1.27f, 0.5f);
    glPrint_small("Blue:12");
	//Draw Ball

	glEnable(GL_TEXTURE_2D);
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -10.0f);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
		// Front Face

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f,  0);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.1f, -0.1f,  0);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.1f,  0.1f,  0);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1f,  0.1f,  0);
	glEnd();
	//Draw left paddle

	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -9.0f);
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glBegin(GL_QUADS);
		// Front Face

		glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f,  0);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.1f, -0.1f,  0);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.1f,  0.1f,  0);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1f,  0.1f,  0);
	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 (!CreateGLWindow("X-treme Pong", width, height, 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()) || keys[VK_ESCAPE])	
			{
				done=TRUE;						
			}
			else								
			{
				SwapBuffers(hDC);				
			}

			if (keys[VK_F1])						
			{
				keys[VK_F1]=FALSE;					
				KillGLWindow();					
				fullscreen=!fullscreen;				
			
				if (!CreateGLWindow("X-treme Pong", width, height, 16, fullscreen))
				{
					return 0;						
				}
			}
		}
	}


	KillGLWindow();								
	return (msg.wParam);							
}
  
My textures are all blue!!! please help

Share this post


Link to post
Share on other sites
OpenGL is a state machine, once you set something it stays that way until you change it. When you set the color to blue (glColor3f(0.0f, 0.0f, 0.8f);), it stays that way until you change it. By default the color affects the rendering of textured primitives too. Setting the color to white would make the texture appear normal.

Share this post


Link to post
Share on other sites