简体   繁体   中英

opengl - black textures, how to use glm::vec* on buffers?

I'm currently learn some opengl stuff. In the past, I stored my vertex data, texture position data and so on, in arrays, created with malloc. Now I'm trying to achieve this with some std::vector. I also created a simple .obj loader with this tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading . But my textures stay black for some reason. I have this stuff in a "object" class.

I will post all the code that I think is important, but if you want to see the whole source code, I will post it.

Object.hpp:

class Object {
public:
  Object(std::string objName);
  ~Object(void);

  bool loadTexture(std::string textureName);

  void setPos(glm::vec3 pos);
  void setDir(glm::vec3 dir);

  void draw(GLfloat time);

private:
  std::vector<glm::vec3> vertexData;
  std::vector<glm::vec3> elementData;
  std::vector<glm::vec2> texturePosData;
  std::vector<glm::vec3> vertexNormalData;

  GLuint vertexArrayObject;
  GLuint vertexBufferObject;
  GLuint elementBufferObject;
  GLuint texturePosBufferObject;

  ShaderProgram *shader;
  GLuint shaderProgram ;

  GLuint textureObject;
  GLuint textureUnit;
  static GLuint globalTextureCount;

  glm::vec3 position;
  glm::vec3 direction;

  //Uniforms
  GLint model, uniView, uniProj, overrideColor;
};

There is currently some unused stuff I will use as soon as the textures work (like setPos() or setDir() )

Object.cpp: I know the constructor and object loader is working correctly, because the vertex data and texture position data is in the vector

bool Object::loadTexture(std::string textureName) {
  textureUnit = globalTextureCount;
  globalTextureCount++;

  glBindVertexArray(vertexArrayObject);

  //Create texuture Pos Buffer
  glGenBuffers(1, &texturePosBufferObject);
  glBindBuffer(GL_ARRAY_BUFFER, texturePosBufferObject);
  glBufferData(GL_ARRAY_BUFFER, texturePosData.size() * sizeof(glm::vec2), &texturePosData[0], GL_STATIC_DRAW);

  glGenTextures(1, &textureObject);
  glActiveTexture(GL_TEXTURE0 + textureUnit);
  glBindTexture(GL_TEXTURE_2D, textureObject);

  int width, height;
  unsigned char *image = NULL;
  image = SOIL_load_image(textureName.c_str(), &width, &height, 0, SOIL_LOAD_RGB);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  SOIL_free_image_data(image);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  glUniform1i(glGetUniformLocation(shaderProgram, "texKitten"), textureUnit);

  GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
  glEnableVertexAttribArray(texAttrib);
  glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

  return true;
}

Is it right way, I am using glBufferData with those vectors ? And what could be the problem with the textures ? glError() don't throw an error. Thank you for your help !

David

The problem is you're calling glBufferData before you loaded the texture into your vector. The call to glBufferData is copying the contents of your vector into the GPU memory, but your vector hasn't got the texture loaded in it yet, so it's probably full of zeros (just initialized).

What I suggest is wait until after SOIL_load_image returns to call glBufferData.

[EDIT]
I was way offbase in my answer there. I also didn't see your comment on your question, that you found the root cause. What threw me off was that you were creating and loading your vertex array buffer inside the loadTexture method. The vertex array buffer doesn't really have anything to do with the texture or loading it. It might make more sense and make your code more readable to deal with the vertex array buffer somewhere other than in loadTexture. It certainly threw me off when reading your code! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM