user2176681
user2176681

Reputation: 176

OpenGL displaying white textures

I have trouble displaying a texture using OpenGL and C++. I am loading the texture from a RGB Raw file, binding it to a texture, but when I am trying to display the texture on a quad, the texture is white. I am using glGetError and it catches no errors. Here is my code:

#include <iostream>
#include <windows.h>       // Standard header for MS Windows applications
#include <GL/gl.h>         // Open Graphics Library (OpenGL) header
#include <GL/glut.h>       // The GL Utility Toolkit (GLUT) Header
using namespace std;

typedef struct {
    int width;
    int height;
    char* title;
    float field_of_view_angle;
    float z_near;
    float z_far;
} glutWindow;
glutWindow win;
float rotation, distance1;
bool light=TRUE;
bool trans=FALSE;
GLuint tex;

void drawFlatFace(float size)
{
    glBegin(GL_QUADS);
        glTexCoord2f (0, 0);
        glVertex3f(-size, -2*size, size);
        glTexCoord2f (1.0, 0);
        glVertex3f(size, -2*size, size);
        glTexCoord2f (1.0, 1.0);
        glVertex3f(size, 2*size, size);
        glTexCoord2f (0, 1.0);
        glVertex3f(-size, 2*size, size);
    glEnd();
}

void drawBlock(float size){
    glColor3f(1.0f,1.0f,1.0f);
    glPushMatrix();
        drawFlatFace (size);
        glRotatef (90, 0, 1, 0);
        drawFlatFace (size);
        glRotatef (90, 0, 1, 0);
        drawFlatFace (size);
        glRotatef (90, 0, 1, 0);
        drawFlatFace (size);
    glPopMatrix();
}

GLuint loadTexture(const char * filename) {
    GLuint text[1];
    GLubyte* bits = new GLubyte[512*256*3];
    FILE *file;
    fopen_s(&file,filename,"rb");
    fread(&bits,(512*256*3),1,file);
    fclose(file);
    glGenTextures(1,&text[0]); 
    glBindTexture(GL_TEXTURE_2D,text[0]);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    glTexImage2D(GL_TEXTURE_2D, 0, 3,256,512,0,GL_RGB,GL_UNSIGNED_BYTE,bits);
    free(bits);
    return text[0];
}

void display() 
{
    GLenum err;
    err = glGetError();
    if (err != GL_NO_ERROR)
    {
        cerr << "OpenGL error: " << gluErrorString(err) << endl;
    }

    tex=loadTexture("tex.raw");
    glBindTexture(GL_TEXTURE_2D,tex);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);          // Clear Screen and Depth Buffer
    glLoadIdentity();
    gluLookAt( 10+distance1 ,1.4,0, 0,0,0, 0,1,0);                    // Define a viewing transformation
    glRotatef(rotation, 0.f, 1.f, 0.f);
    drawBlock(3);
    if (light)  glEnable(GL_LIGHTING);      // Enable Lighting
    else glDisable(GL_LIGHTING);      // Disable Lighting
    if (trans) glEnable (GL_BLEND);  // Enable Transparency
    else glDisable(GL_BLEND);      // Disable Transparency
    glutSwapBuffers();
}


void initialize () 
{
    glMatrixMode(GL_PROJECTION);                                                // select projection matrix
    glViewport(0, 0, win.width, win.height);                                    // set the viewport
    glLoadIdentity();                                                           // reset projection matrix
    GLfloat aspect = (GLfloat) win.width / win.height;
    gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);     // set up a perspective projection matrix
    glMatrixMode(GL_MODELVIEW);                                                 // specify which matrix is the current matrix
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );                        // specify implementation-specific hints
    GLfloat amb_light[] = { 0.3, 0.3, 0.3, 1.0 };
    GLfloat diffuse[] = { 0.4, 0.4, 0.4, 1.0 };
    GLfloat specular[] = { 0.5, 0.5, 0.5, 1.0 };
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
    glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
    glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
    GLfloat light_position[] = { 1.0, 1.0, 0, 0.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable( GL_LIGHT0 );
    glEnable( GL_COLOR_MATERIAL );
    glEnable( GL_LIGHTING );
    //glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );

    glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping
    glShadeModel(GL_SMOOTH);                        // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                   // Black Background
    glClearDepth(1.0f);                         // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                        // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                         // The Type Of Depth Testing To Do
    glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}


void keyboard ( int key, int mousePositionX, int mousePositionY )       
{ 
    switch ( key ) 
  {

    case GLUT_KEY_DOWN:
        if (distance1<9){
        distance1=distance1+.3;
        }
        break;      

    case GLUT_KEY_UP:
        if (distance1>-9){
        distance1=distance1-.3;
        }
        break;      

    case GLUT_KEY_LEFT:
        rotation=rotation-2;
        break;      

    case GLUT_KEY_RIGHT:
        rotation=rotation+2;
        break; 

    case GLUT_KEY_PAGE_UP:
        light=TRUE;
        break; 

    case GLUT_KEY_PAGE_DOWN:
        light=FALSE;
        break; 

    case GLUT_KEY_HOME:
        trans=TRUE;
        break; 

    case GLUT_KEY_END:
        trans=FALSE;
        break; 


    default:      
      break;
  }
}

int main(int argc, char **argv) 
{
    // set window values
    win.width = 800;
    win.height = 600;
    win.title = "Test Texture";
    win.field_of_view_angle = 45;
    win.z_near = 1.0f;
    win.z_far = 500.0f;

    // initialize and run program
    glutInit(&argc, argv);                                      // GLUT initialization
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );  // Display Mode
    glutInitWindowSize(win.width,win.height);                   // set window size
    glutCreateWindow(win.title);                                // create Window
    // Callback functions
    glutDisplayFunc(display);                                   // register Display Function
    glutIdleFunc( display );                                    // register Idle Function
    glutSpecialFunc( keyboard );                                // register Keyboard Handler
    initialize();
    glutMainLoop();                                             // run GLUT mainloop
    return 0;
}

I want to add that the Raw file is displayed correctly when loaded in IrfanView as 256x512, 24BPP, RGB (the size is 393,216 bytes or 3*512*256)

Upvotes: 1

Views: 1770

Answers (1)

JasonD
JasonD

Reputation: 16582

This line:

glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Shouldn't be in your main intialisation, but instead needs to be done when setting up the texture itself (i.e. after binding).

You may also want to set the MAG_FILTER, and the texture coordinate wrapping modes, as a general rule.

Also, you appear to be loading the texture every time you render. Don't do that, performance will be terrible and you'll run out of memory as GL will keep allocating new textures.

Upvotes: 3

Related Questions