c++openglglutbmptexture-mapping

Opengl(GLUT) Texturing one face of 3d object with 2d image C++


Im trying to load a texture from a bmp (loaded to imgur as bmp https://imgur.com/a/ZGscbqc you can see it when downloaded imgur doesn't render bmp) and then display it on the front side of a object. When I try it with a solidcube the whole cube just goes a weird color and when the camera gets close to the cube my fps gets really low

I dont want to use additional libraries and here is the function to load the texture

GLuint LoadTexture( const char * filename )
{
  GLuint texture;
  int width, height;
  unsigned char * data;

  FILE * file;
  file = fopen( filename, "rb" );

  if ( file == NULL ) return 0;
  width = 238; //Exact dimension of my picture
  height = 419; 
  data = (unsigned char *)malloc( width * height * 3 );
  //int size = fseek(file,);
  fread( data, width * height * 3, 1, file );
  fclose( file );

  for(int i = 0; i < width * height ; ++i)
  {
    int index = i*3;
    unsigned char B,R;
    B = data[index];
    R = data[index+2];

    data[index] = R;
    data[index+2] = B;
  }

  glGenTextures( 1, &texture );
  glBindTexture( GL_TEXTURE_2D, texture );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
  free( data );

  return texture;
}

and then I call it in my renderScene function

GLuint texture;
    texture= LoadTexture( "Princess_Peach_Stock_Art.bmp" );
    glEnable( GL_TEXTURE_2D );

    glBindTexture (GL_TEXTURE_2D, texture);

 //princess
    glPushMatrix();
        glTranslatef(-10,19,-95);
        glColor3f(1.0f,1.0f,1.0f);
        glutSolidCube(10);
    glPopMatrix();

glDisable( GL_TEXTURE_2D );

2 Questions

  1. How to fix this.
  2. Will it be better to use 2D object like glRecd() instead of a solidcube which is 3d.

Here is a minimal repoducable code (I left the moving and the camera orientation code so that it will be easier to see and get close to it)

#include <math.h>
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
#include <cstdint>
#include <iostream>
#define HEIGHT  20
#define WIDTH   20

GLUquadricObj *objCylinder = gluNewQuadric();
float angle=0.0,deltaAngle = 0.0,ratio;
float x=0.0f,y=1.75f,z=75.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
int deltaMove = 0,h,w;

static GLubyte  pattern[WIDTH][HEIGHT][4]; //height, width, RGBA
unsigned char *pattern2;
static GLuint   texName[10];
unsigned long   width, height;

   GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };

   GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
   GLfloat no_shininess[] = { 0.0 };
   GLfloat high_shininess[] = { 100.0 };

GLuint LoadTexture( const char * filename )
{
  GLuint texture;
  int width, height;
  unsigned char * data;

  FILE * file;
  file = fopen( filename, "rb" );

  if ( file == NULL ) return 0;
  width = 238; //Exact dimension of my picture
  height = 419;
  data = (unsigned char *)malloc( width * height * 3 );
  //int size = fseek(file,);
  fread( data, width * height * 3, 1, file );
  fclose( file );

  for(int i = 0; i < width * height ; ++i)
  {
    int index = i*3;
    unsigned char B,R;
    B = data[index];
    R = data[index+2];

    data[index] = R;
    data[index+2] = B;
  }

  glGenTextures( 1, &texture );
  glBindTexture( GL_TEXTURE_2D, texture );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
  gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
  free( data );

  return texture;
}



void changeSize(int w1, int h1){

 if(h1 == 0)
  h1 = 1;
 w = w1;
 h = h1;
 ratio = 1.0f * w / h;
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glViewport(0, 0, w, h);
 gluPerspective(45,ratio,0.1,1000);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);

 }

void setLight(void)
{
    GLfloat L0position[]={30.0, 10.0, 40.0, 1.0};
    GLfloat L1position[]={0.0, 0.0, -100.0, 1.0};
    GLfloat whitelight[]={1.0, 1.0, 1.0, 1.0};
    GLfloat amblight[]={0.2, 0.2, 0.2, 1.0};
    GLfloat blacklight[]={0.0, 0.0, 0.0, 1.0};

    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amblight);
    GLfloat whitelight1[]={0, 0, 0, 1.0};

    glLightfv(GL_LIGHT0, GL_AMBIENT, blacklight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, whitelight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, whitelight);
    glLightfv(GL_LIGHT0, GL_POSITION, L0position);


    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}


void orientMe(float ang) {
 lx = sin(ang);
 lz = -cos(ang);
 glLoadIdentity();
 gluLookAt(x, y, z,
        x + lx,y + ly,z + lz,
     0.0f,1.0f,0.0f);
}


void moveMeFlat(int i) {
 x = x + i*(lx)*0.08;
 z = z + i*(lz)*0.08;
 glLoadIdentity();
 gluLookAt(x, y, z,
        x + lx,y + ly,z + lz,
     0.0f,1.0f,0.0f);
}
void renderScene(void) {

 if (deltaMove)
  moveMeFlat(deltaMove);
 if (deltaAngle) {
  angle += deltaAngle;
  orientMe(angle);
 }
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
setLight();


    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

 glColor3f(0.9f, 0.9f, 0.9f);
 glBegin(GL_QUADS);
   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
   glVertex3f(-500.0f, 0.0f, -500.0f);
   glVertex3f(-500.0f, 0.0f,  500.0f);
   glVertex3f( 500.0f, 0.0f,  500.0f);
   glVertex3f( 500.0f, 0.0f, -500.0f);
 glEnd();



GLuint texture;
    texture= LoadTexture( "Princess_Peach_Stock_Art.bmp" );
    glEnable( GL_TEXTURE_2D );

    glBindTexture (GL_TEXTURE_2D, texture);

 //princess
    glPushMatrix();
        glTranslatef(-10,19,-95);
        glColor3f(1.0f,1.0f,1.0f);
        glutSolidCube(10);
    glPopMatrix();

glDisable( GL_TEXTURE_2D );
glDisable(GL_COLOR_MATERIAL);

 glutSwapBuffers();
}


void pressKey(int key, int x, int y) {

 switch (key) {
  case GLUT_KEY_LEFT : deltaAngle = -0.001f;break;
  case GLUT_KEY_RIGHT : deltaAngle = 0.001f;break;
  case GLUT_KEY_UP : deltaMove = 1;break;
  case GLUT_KEY_DOWN : deltaMove = -1;break;
 }

}

void releaseKey(int key, int x, int y) {

 switch (key) {
  case GLUT_KEY_LEFT : if (deltaAngle < 0.0f)
         deltaAngle = 0.0f;
        break;
  case GLUT_KEY_RIGHT : if (deltaAngle > 0.0f)
         deltaAngle = 0.0f;
        break;
  case GLUT_KEY_UP :  if (deltaMove > 0)
         deltaMove = 0;
        break;
  case GLUT_KEY_DOWN : if (deltaMove < 0)
         deltaMove = 0;
        break;
 }
}


void initWindow() {
 glutIgnoreKeyRepeat(1);


 glutSpecialFunc(pressKey);
 glutSpecialUpFunc(releaseKey);

 glutDisplayFunc(renderScene);
 glutIdleFunc(renderScene);

 glutReshapeFunc(changeSize);
 glShadeModel (GL_SMOOTH);
 glEnable(GL_LIGHTING);
 glEnable(GL_DEPTH_TEST);
 glEnable(GL_NORMALIZE);


}

int main(int argc, char **argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
 glutInitWindowPosition(100,100);
 glutInitWindowSize(640,360);
 glutCreateWindow("2");

 initWindow();

 glutMainLoop();

 return(0);
}


Solution

  • Edit:

    Original answer:

    There is really no reason not to use something like the following single-header library: https://github.com/nothings/stb/blob/master/stb_image.h

    I managed to make a hint of progress in your code. It now shows a distorted princess in a quad.

    #define STB_IMAGE_IMPLEMENTATION
    #include "stb_image.h"
    
    #include <math.h>
    #include <GL/glut.h>
    #include <stdio.h>
    #include <string.h>
    #include <cstdint>
    #include <iostream>
    #define HEIGHT  20
    #define WIDTH   20
    
    GLUquadricObj *objCylinder = gluNewQuadric();
    float angle=0.0,deltaAngle = 0.0,ratio;
    float x=0.0f,y=1.75f,z=75.0f;
    float lx=0.0f,ly=0.0f,lz=-1.0f;
    int deltaMove = 0,h,w;
    
    GLuint princessTexture;
    
    static GLubyte  pattern[WIDTH][HEIGHT][4]; //height, width, RGBA
    unsigned char *pattern2;
    static GLuint   texName[10];
    unsigned long   width, height;
    
       GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
    
       GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
       GLfloat no_shininess[] = { 0.0 };
       GLfloat high_shininess[] = { 100.0 };
    
    GLuint LoadTexture( const char * filename )
    {
      GLuint texture;
    
      int width, height, nrChannels;
      unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
      if (!data) {
        std::cout << "Couldn't load texture " << filename << " :(" << std::endl;
        return 0;
      }
      std::cout << width << " × " << height << ", " << nrChannels << "ch" << std::endl;
    
      glGenTextures( 1, &texture );
      glBindTexture( GL_TEXTURE_2D, texture );
      glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
      //gluBuild2DMipmaps takes care of minification
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
      //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
      //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
    
      gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
    
      //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    
      stbi_image_free(data);
    
      return texture;
    }
    
    
    
    void changeSize(int w1, int h1){
    
     if(h1 == 0)
      h1 = 1;
     w = w1;
     h = h1;
     ratio = 1.0f * w / h;
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glViewport(0, 0, w, h);
     gluPerspective(45,ratio,0.1,1000);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);
    
     }
    
    void setLight(void)
    {
        GLfloat L0position[]={30.0, 10.0, 40.0, 1.0};
        GLfloat L1position[]={0.0, 0.0, -100.0, 1.0};
        GLfloat whitelight[]={1.0, 1.0, 1.0, 1.0};
        GLfloat amblight[]={0.2, 0.2, 0.2, 1.0};
        GLfloat blacklight[]={0.0, 0.0, 0.0, 1.0};
    
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amblight);
        GLfloat whitelight1[]={0, 0, 0, 1.0};
    
        glLightfv(GL_LIGHT0, GL_AMBIENT, blacklight);
        glLightfv(GL_LIGHT0, GL_SPECULAR, whitelight);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, whitelight);
        glLightfv(GL_LIGHT0, GL_POSITION, L0position);
    
    
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
    }
    
    
    void orientMe(float ang) {
     lx = sin(ang);
     lz = -cos(ang);
     glLoadIdentity();
     gluLookAt(x, y, z,
            x + lx,y + ly,z + lz,
         0.0f,1.0f,0.0f);
    }
    
    
    void moveMeFlat(int i) {
     x = x + i*(lx)*0.8;
     z = z + i*(lz)*0.8;
     glLoadIdentity();
     gluLookAt(x, y, z,
            x + lx,y + ly,z + lz,
         0.0f,1.0f,0.0f);
    }
    void renderScene(void) {
    
     if (deltaMove)
      moveMeFlat(deltaMove);
     if (deltaAngle) {
      angle += deltaAngle;
      orientMe(angle);
     }
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    setLight();
    
    
      glEnable(GL_COLOR_MATERIAL);
      glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
      glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
      glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
      glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
      glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
      glColor3f(0.9f, 0.9f, 0.9f);
      glBegin(GL_QUADS);
        glVertex3f(-500.0f, 0.0f, -500.0f);
        glVertex3f(-500.0f, 0.0f,  500.0f);
        glVertex3f( 500.0f, 0.0f,  500.0f);
        glVertex3f( 500.0f, 0.0f, -500.0f);
      glEnd();
    
      //glDisable(GL_COLOR_MATERIAL);
    
      //princess
    
      glEnable( GL_TEXTURE_2D );
      glBindTexture (GL_TEXTURE_2D, princessTexture);
    
    
      glPushMatrix();
      glTranslatef(10,7,-90);
          glColor3f(1.0f,1.0f,1.0f);
      glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex3f(-5.0f, 5.0f, 0.0f);
        glTexCoord2f(1,0);
        glVertex3f(-5.0f,-5.0f, 0.0f);
        glTexCoord2f(1,1);
        glVertex3f( 5.0f,-5.0f, 0.0f);
        glTexCoord2f(0,1);
        glVertex3f( 5.0f, 5.0f, 0.0f);
      glEnd();
      glPopMatrix();
    
          glColor3f(1.0f,1.0f,1.0f);
      glPushMatrix();
          glTranslatef(-5,15,-95);
          glutSolidCube(10);
      glPopMatrix();
    
      glDisable( GL_TEXTURE_2D );
    
     glutSwapBuffers();
    }
    
    
    void pressKey(int key, int x, int y) {
    
     switch (key) {
      case GLUT_KEY_LEFT : deltaAngle = -0.02f;break;
      case GLUT_KEY_RIGHT : deltaAngle = 0.02f;break;
      case GLUT_KEY_UP : deltaMove = 1;break;
      case GLUT_KEY_DOWN : deltaMove = -1;break;
     }
    
    }
    
    void releaseKey(int key, int x, int y) {
    
     switch (key) {
      case GLUT_KEY_LEFT : if (deltaAngle < 0.0f)
             deltaAngle = 0.0f;
            break;
      case GLUT_KEY_RIGHT : if (deltaAngle > 0.0f)
             deltaAngle = 0.0f;
            break;
      case GLUT_KEY_UP :  if (deltaMove > 0)
             deltaMove = 0;
            break;
      case GLUT_KEY_DOWN : if (deltaMove < 0)
             deltaMove = 0;
            break;
     }
    }
    
    
    void initWindow() {
     glutIgnoreKeyRepeat(1);
    
    
     glutSpecialFunc(pressKey);
     glutSpecialUpFunc(releaseKey);
    
     glutDisplayFunc(renderScene);
     glutIdleFunc(renderScene);
    
     glutReshapeFunc(changeSize);
     glShadeModel (GL_SMOOTH);
     glEnable(GL_LIGHTING);
     glEnable(GL_DEPTH_TEST);
     glEnable(GL_NORMALIZE);
    
    
    }
    
    int main(int argc, char **argv)
    {
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowPosition(100,100);
     glutInitWindowSize(640,360);
     glutCreateWindow("2");
    
     initWindow();
    
     princessTexture = LoadTexture("Princess_Peach_Stock_Art.png");
    
     glutMainLoop();
    
     return(0);
    }
    

    A piece of advice: Use up-to-date libraries and learn how to write shader code. Also, a scripting language like Python can help a lot. If its garbage collection is not an option, use modern C++.