Jump to content

  • Log In with Google      Sign In   
  • Create Account

Varien

Member Since 20 May 2012
Offline Last Active May 29 2012 06:03 PM

Topics I've Started

Objects only appearing white in OpenGL?

20 May 2012 - 11:09 AM

I've just gotten started in 3D programming, and have tried learning as much as I can, and it's been working pretty well. The issue I'm having is, as the title suggests, any objects I try and instantiate in my test environment are only appearing white. This isn't an issue I used to have. In the last tutorial I followed, it covered texture mapping, and everything worked fine, I had a cube that had textures mapped to its' surfaces, and all was right with the world. Only after I followed the next tutorial in the series, here: http://nehe.gamedev...._control/15002/, did I have this problem (NOTE: the "LoadGLTextures()" int in the tutorial is outdated! Using SOIL is a much easier alternative that the person who wrote the tutorial even asks you to use in the texture mapping tutorial). I'm not sure what I added to cause the objects to appear white, but it has to be something in that tutorial I linked.

Now, I've looked all through that tutorial, and I can't see anything that I added that would have caused this. I even copy/pasted the tutorials' code into my project to test it, then deleted it afterwards, and there was still nothing but a white cube being displayed.

Does ANYONE know how to fix this?

Code:
#include <windows.h>
#include <stdio.h>
#include <SDL\gl.h>  
#include <SDL\glu.h>
#include <SDL\glaux.h>
#include <SDL\SOIL.h>
HDC   hDC=NULL;
HGLRC  hRC=NULL;
HWND  hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;
bool light;
bool lp, fp;
GLfloat xrot;
GLfloat yrot;
GLfloat xspeed;
GLfloat yspeed;
GLfloat z=-5.0f;
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};
GLuint filter;
GLuint texture[3];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int LoadGLTextures(){
	texture[0]=SOIL_load_OGL_texture("Resources/Crate.bmp",SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
	if(texture[0]==0){return false;}
	glGenTextures(3,&texture[0]);
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);
  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);

  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);

  
	return true;
	}
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()){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);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
return true;		
}
int DrawGLScene(GLvoid){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
	glTranslatef(0.0f,0.0f,z);
	glRotatef(xrot,1.0f,0.0f,0.0f);
	glRotatef(yrot,0.0f,1.0f,0.0f);
  
	glBindTexture(GL_TEXTURE_2D, texture[filter]);
	glBegin(GL_QUADS);
	  glNormal3f( 0.0f, 0.0f, 1.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
  // Back Face
  glNormal3f( 0.0f, 0.0f,-1.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  // Top Face
  glNormal3f( 0.0f, 1.0f, 0.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
  // Bottom Face
  glNormal3f( 0.0f,-1.0f, 0.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
  // Right face
  glNormal3f( 1.0f, 0.0f, 0.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
  // Left Face
  glNormal3f(-1.0f, 0.0f, 0.0f);
  glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
  glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
  glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();
  
	xrot+=xspeed;
	yrot+=yspeed;
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);
// Create The Window
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),	// 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 Our 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))){
  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("OpenGL Game Testing Framework",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){if (keys[VK_ESCAPE]){
	 done=TRUE;}
   else{DrawGLScene();  
		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['Q']){xspeed=0.0f;
							   yspeed=0.0f;}
				 }}
   if (keys[VK_F1])	
   {keys[VK_F1]=FALSE;  
	KillGLWindow();	
	fullscreen=!fullscreen;  
	if (!CreateGLWindow("OpenGL Game Testing Framework",640,480,16,fullscreen)){return 0;}}}}
KillGLWindow();		
return (msg.wParam);	  
}

PARTNERS