OpenGL纹理未显示(我不使用着色器)

时间:2018-08-20 07:17:28

标签: c++ opengl textures

我正在尝试在项目中构造我的模型,但没有正常工作。我不知道怎么了我的代码:

  //loading the texture:

  unsigned int textureWalls;
  int widthWalls, heightWalls, nrChannelsWalls;
  unsigned char *dataWalls = stbi_load("walls.png", &widthWalls, &heightWalls, &nrChannelsWalls, 0);

  glGenTextures(1, &textureWalls);
  glBindTexture(GL_TEXTURE_2D, textureWalls);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widthWalls, heightWalls, 0, GL_RGB, GL_UNSIGNED_BYTE, dataWalls);
  glGenerateMipmap(GL_TEXTURE_2D);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_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);

  // drawing:

  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, textureWalls);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_NORMALIZE);

  glBindTexture(GL_TEXTURE_2D,tex);
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  glVertexPointer(4,GL_FLOAT,0,vertices);
  glNormalPointer(GL_FLOAT,sizeof(float)*4,vertexNormals);
  glTexCoordPointer(2,GL_FLOAT,0,texCoords);
  glDrawArrays(GL_TRIANGLES,0,vertexCount);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  glDisable(GL_TEXTURE_2D);

我尝试以此方式为一些模型绘制纹理(每个模型都有其自己的纹理),但是纹理不显示。是否需要使用着色器?

0 个答案:

没有答案