简体   繁体   English

opengl-黑色纹理,如何在缓冲区上使用glm :: vec *?

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

I'm currently learn some opengl stuff. 我目前正在学习一些opengl的东西。 In the past, I stored my vertex data, texture position data and so on, in arrays, created with malloc. 过去,我将顶点数据,纹理位置数据等存储在使用malloc创建的数组中。 Now I'm trying to achieve this with some std::vector. 现在,我正在尝试通过一些std :: vector实现这一目标。 I also created a simple .obj loader with this tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading . 我还通过本教程http://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading创建了一个简单的.obj加载程序。 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: 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() ) 当前,一旦纹理起作用,我将使用一些未使用的东西(例如setPos()或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 Object.cpp:我知道构造函数和对象加载器工作正常,因为顶点数据和纹理位置数据在矢量中

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 ? 是正确的方法,我正在将glBufferData与这些向量一起使用吗? And what could be the problem with the textures ? 纹理可能是什么问题? glError() don't throw an error. glError()不会引发错误。 Thank you for your help ! 谢谢您的帮助 !

David 大卫

The problem is you're calling glBufferData before you loaded the texture into your vector. 问题是您在将纹理加载到向量中之前正在调用glBufferData。 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). 对glBufferData的调用是将向量的内容复制到GPU内存中,但是向量尚未加载纹理,因此它可能充满了零(刚刚初始化)。

What I suggest is wait until after SOIL_load_image returns to call glBufferData. 我的建议是等到SOIL_load_image返回调用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. 让我失望的是,您正在loadTexture方法内创建并加载顶点数组缓冲区。 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. 这可能更有意义,并使您的代码更具可读性,以处理除loadTexture之外的其他地方的顶点数组缓冲区。 It certainly threw me off when reading your code! 当您阅读代码时,这肯定使我失望了! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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