3 Replies - 940 Views - Last Post: 21 May 2012 - 02:48 PM Rate Topic: -----

#1 VarienNexus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-May 12

Objects only appearing white in OpenGL 3D programming

Posted 20 May 2012 - 10:23 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/NeHe.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);      
}



Is This A Good Question/Topic? 0
  • +

Replies To: Objects only appearing white in OpenGL 3D programming

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Objects only appearing white in OpenGL 3D programming

Posted 20 May 2012 - 04:04 PM

Can you post an image of the issue? It could give us valuable clues.
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Objects only appearing white in OpenGL 3D programming

Posted 21 May 2012 - 02:34 AM

What did you add between the previous tutorial and this one that caused the issue?
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 560
  • View blog
  • Posts: 1,262
  • Joined: 24-April 12

Re: Objects only appearing white in OpenGL 3D programming

Posted 21 May 2012 - 02:48 PM

I'm not sure exactly how to fix the issue. But I've encountered some similar issues elsewhere. I'm going to guess that you have either one of two problems.

Either, one - you're drawing models without their textures and the default shader color is white.

Or two - you're "blowing out" the color.

That
LightDiffuse[]={1.0f,1.0f,1.0f,1.0f}; 
looks pretty suspicious. I don't code in OpenGl or whatever you're using here, but I'm going to guess that that's RGB+A color. I'm betting that's "white" right there.

Ambient light is going to color everything in your scene usually. The light is probably "additive" but it still probably has a max value of 1.0f for every color. So, once this light goes in the scene any other lights are going to just shine more light.

This actually happens in the real world believe it or not. There's a point where your eyes and brain can't process the color information that they are seeing and they just turn the color to white in your brain. Imagine a blue light that just keeps getting brighter and brighter, without changing color, until it just looks white.

Or think about looking into the sun, which is obviously yellow but appears white in the brightest spots.

That's basically what I think is going on here. The color values are adding together and if it goes over 1.0f the result is 1.0f because that's as high as it can go.

Keep in mind that you're not "lighting" anything, but rather "drawing pictures" using light.

Or look at it this way. We have a pixel that we're "lighting". We have 0f,0f,0f for red, green, and blue. One light adds 0.3f, 0.3f, 0.3f to the pixel. The next adds 5.0f,0f,0f and the pixel is now 0.8f, 0.3f, 0.3f. Then another light adds 1f,1f,1f and the color "should" be 1.8f,1.3f,1.3f but maximum light is 1.0f,1.0f,1.0f which is what you get - white.

Try backing your lights WAY down. Change that diffuse to a nice 0.1f,0.1f,0.1f (I don't know about that 4th, probably alpha, value). And try to make sure that all the lights added together never go over 1.0f,1.0f,1.0f or even get close to it. Especially, watch the intensity of lights that cover everything in the scene, like an ambient light.

So, I hope that makes sense and solves your problem. Good luck!

This post has been edited by BBeck: 22 May 2012 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1