OpenGL(GLUT)用2D图像纹理化3D物体的一个面C++

koaltpgm  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(130)

我试图从一个bmp加载一个纹理(加载到imgur作为bmp https://imgur.com/a/ZGscbqc,你可以看到它时,下载的imgur不渲染bmp),然后显示在一个对象的正面。当我尝试用一个solidcube时,整个立方体只是变成了一种奇怪的颜色,当相机靠近立方体时,我的fps变得非常低
我不想使用额外的库,这里是加载纹理的函数

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

然后在renderScene函数中调用它

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个问题
1.如何解决这个问题。
1.使用像glRecd()这样的2D对象而不是3D的solidcube会更好吗?
这里是一个最小的repoducable代码(我离开了移动和相机方向的代码,以便更容易看到和接近它)

#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);
}
uklbhaso

uklbhaso1#

编辑:

原文回答:
真的没有理由不使用像下面这样的单头库:https://github.com/nothings/stb/blob/master/stb_image.h
我设法在你的代码中做了一个进步的暗示。它现在显示了一个扭曲的公主在一个四合院。

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

给你个建议使用最新的库并学习如何编写着色器代码。此外,像Python这样的脚本语言也可以提供很大的帮助。如果它的垃圾回收不是一个选项,使用现代C++。

相关问题