Home > Software engineering >  Texture applied through OpenGL isn't visible when rendering add image directly in question
Texture applied through OpenGL isn't visible when rendering add image directly in question

Time:08-17

I tried to apply a checkerboard styled texture to the model below here using GLubyte and glTexImage2D along with glTexCoord2f.

I previously applied a material to it that works perfectly, but for some reasons my texture won't show up at all. I can't seem to see or figure out why because looking at the examples I'm following everything should be working perfectly.

enter image description here

What I should have

#include <glut.h>


float angle[4];
float LightAngle;
bool LowerFrontLegDown = true;
bool LowerBackLegDown = true;


GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
                        {0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
                        {-0.5,0.5,0.5},{0.5,0.5,0.5},
                        {0.5,-0.5,0.5},{-0.5,-0.5,0.5} };

//Two Dimensional Array for corners

GLfloat normals[][3] = { {0.0,0.0,1.0},
                                      {1.0,0.0,0.0},
                                      {0.0,-1.0,0.0},
                                      {0.0,1.0,0.0},
                                      {0.0,0.0,-1.0},
                                      {-1.0,0.0,0.0} };

typedef struct materialStruct {
    GLfloat ambient[4];
    GLfloat diffuse[4];
    GLfloat specular[4];
    GLfloat shininess;
};

materialStruct brassMaterial = {
       { 0.33, 0.22, 0.03, 1.00 },
       { 0.78, 0.57, 0.11, 1.00 },
       { 0.99, 0.91, 0.81, 1.00 },
       27.80 };

materialStruct redPlasticMaterial = {
       { 0.30, 0.00, 0.00, 1.00 },
       { 0.60, 0.00, 0.00, 1.00 },
       { 0.80, 0.60, 0.60, 1.00 },
       32.00 };


materialStruct* currentMaterial;

void setMaterial(materialStruct* materials) {
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materials->ambient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materials->diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materials->specular);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials->shininess);
}

void drawFace(int a, int b, int c, int d) {
    glBegin(GL_POLYGON);
    glTexCoord2f(0.0, 0.0);
    glVertex3fv(corners[a]);
    glTexCoord2f(0.0, 1.0);
    glVertex3fv(corners[b]);
    glTexCoord2f(1.0, 1.0);
    glVertex3fv(corners[c]);
    glTexCoord2f(1.0, 0.0);
    glVertex3fv(corners[d]);
    glEnd();
} //Turns the corners from the two dimensional array into corner pieces for the model, allowing faces to be drawn

void ArrayCube() {
    glNormal3fv(normals[0]);
    drawFace(0, 3, 2, 1);
    glNormal3fv(normals[1]);
    drawFace(3, 0, 4, 7);
    glNormal3fv(normals[2]);
    drawFace(2, 3, 7, 6);
    glNormal3fv(normals[3]);
    drawFace(1, 2, 6, 5);
    glNormal3fv(normals[4]);
    drawFace(4, 5, 6, 7);
    glNormal3fv(normals[5]);
    drawFace(5, 4, 0, 1);
}
//Draws the faces of the model and creates a cube we can call later for the individual parts of the model.

void rotate() {

    angle[0]  = 1.0;
    if (angle[0] > 360) angle[0] -= 360;

    if (LowerFrontLegDown) angle[1] -= 0.2;
    else angle[1]  = 0.2;

    if (angle[1] < 315) LowerFrontLegDown = false;
    if (angle[1] > 360) LowerFrontLegDown = true;

    angle[0]  = 1.0;
    if (angle[0] > 360) angle[0] -= 360;

    if (LowerBackLegDown) angle[1] -= 0.2;
    else angle[1]  = 0.2;

    if (angle[1] < 315) LowerBackLegDown = false;
    if (angle[1] > 360) LowerBackLegDown = true;

    glutPostRedisplay();
}






void MainBody()
{
    glPushMatrix();
    glScalef(1.25, 0.25, 0.5);
    ArrayCube();
    glPopMatrix();
}

void LowerNeck()
{
    glPushMatrix();
    glTranslatef(0.5, 0.25, 0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void UpperNeck()
{
    glPushMatrix();
    glTranslatef(0.5, 0.75, 0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void Head()
{
    glPushMatrix();
    glRotatef(90, 0.0, 0.0,1.0);
    glTranslatef(1, -0.6, 0);
    glScalef(0.1, 0.4, 0.15);
    ArrayCube();
    glPopMatrix();
}

void RightHorn()
{
    glPushMatrix();
    glRotatef(0, 0.0, 0.0,1);
    glTranslatef(0.5, 1.15, 0.035);
    glScalef(0.05, 0.15, 0.05);
    ArrayCube();
    glPopMatrix();
}

void LeftHorn()
{
    glPushMatrix();
    glRotatef(0, 0.0, 0.0, 1);
    glTranslatef(0.5, 1.15, -0.035);
    glScalef(0.05, 0.15, 0.05);
    ArrayCube();
    glPopMatrix();
}

void FrontUpperRightLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.35, 0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontLowerRightLeg()
{
    glPushMatrix();
 
    glTranslatef(0.5, -0.75, 0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontUpperLeftLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.35, -0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void FrontLowerLeftLeg()
{
    glPushMatrix();
    glTranslatef(0.5, -0.75, -0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackUpperRightLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.35, -0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackLowerRightLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.75, -0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackUpperLeftLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.35, 0.15);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void BackLowerLeftLeg()
{
    glPushMatrix();
    glTranslatef(-0.5, -0.75, 0.15);
    glRotatef(angle[1], 0.0, 0.0, 1.0);
    glScalef(0.15, 0.5, 0.15);
    ArrayCube();
    glPopMatrix();
}

void Tail()
{
    glPushMatrix();
    glTranslatef(-0.65, -0.25, 0);
    glScalef(0.05, 0.75, 0.05);
    ArrayCube();
    glPopMatrix();
}

//Each of the below functions draws an individual part of the whole model and places those parts where they need to go once the program runs

void DrawGiraffe()
{
    MainBody();
    LowerNeck();
    UpperNeck();
    Head();
    RightHorn();
    LeftHorn();
    FrontUpperRightLeg();
    FrontLowerRightLeg(); 
    FrontUpperLeftLeg();
    FrontLowerLeftLeg();
    BackUpperRightLeg();
    BackLowerRightLeg();
    BackUpperLeftLeg();
    BackLowerLeftLeg();
    Tail();

}

//Calls the above functions to render the final model

//The rotate function allows the camera to rotate around the model

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    DrawGiraffe();
    glutSwapBuffers();
}


void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    currentMaterial = &redPlasticMaterial;
    setMaterial(currentMaterial);
    glColor3f(1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
    GLfloat light_pos[] = {2.0,2.0,2.0, 1.0};
    glLightfv(GL_LIGHT0, GL_POSITION, light_pos);     
}

int main(int argc, char** argv) 
{
    angle[0] = 0;
    angle[1] = 360;
    angle[2] = 315;
    angle[3] = 0;

        GLubyte image[64][64][3];
    int i, j, r, c;
    for(i = 0;i < 64; i  ){
          for(j = 0;j < 64; j  ){
                c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
                image[i][j][0] = (GLubyte) c;
                image[i][j][1] = (GLubyte) c;
                image[i][j][2] = (GLubyte) c;
          }
    }

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Giraffe");
    glutDisplayFunc(display);
    glutIdleFunc(rotate);
    init();
    glutMainLoop();
}

CodePudding user response:

There are many things wrong with your code:

  1. You are using glTexParameterf instead of glTexParameteri.

  2. You are not binding a texture object, which means that calls to your glTexImage2D function will be useless, because glTexImage2D relies on the bounded texture of GL_TEXTURE_2D.

  3. You are initializing the texture before you call the glut initialization functions. This means that any calls to OpenGL functions will be useless.

You should initialize your texture with something like this

// this should be declared as a global variable
GLuint texture;


glGenTextures(1, &texture); 
glBindTexture(GL_TEXTURE_2D, texture);

GLubyte image[64][64][3];
int i, j, r, c;
for(i = 0;i < 64; i  ){
    for(j = 0;j < 64; j  ){
          c = ((((i&0x8)== 0)^((j&0x8))==0))*255;
          image[i][j][0] = (GLubyte) c;
          image[i][j][1] = (GLubyte) c;
          image[i][j][2] = (GLubyte) c;
    }
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

and move the initialization of the texture to the init function, just after the call to glEnable(GL_TEXTURE_2D);

Of course, you will need to bind this texture when you render by calling glBindTexture(GL_TEXTURE_2D, texture);

  • Related