Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Incorrectly textured cube

  1. #1
    Newbie Newbie
    Join Date
    Jul 2013
    Posts
    1

    Incorrectly textured cube

    Hello people!

    I have program loading cube (model from obj.) and texturing it with bitmap (.bmp). It is textured incorrectly, three of its faces are textured with part of bitmap, but rest of cube is black. I'm pretty sure that model (vertices and uv's) and bitmap data are prepared and loaded correctly, cause I've tested all these data with another program with similar code (the same purpose, but another structure).

    My questions:
    Have somebody ever had similar problem?
    What reason could it be (if somebody had similar problem)?
    • Incorrect state of texture?
    • Bad parameter of texture
    • Bad operation on buffer for texture data?
    • Bad operation on VBO?



    Below I put some code of program:

    IDs of buffers and states (for example texture state) are in Texture or Model Classes scope.

    Most important texture class code (not putting code freeing resources):

    Code :
    bool Texture::LoadTexture(string filename)
    {
      int width;
      int height;
      int channels;
      unsigned char* data = SOIL_load_image(filename.c_str(),&width,&height,&channels,SOIL_LOAD_RGB);
      if(!data)
        return false;
     
      // vector<unsigned char> dane(data,data+width*height*3);
      glGenTextures(1,&texID);
      if(!texID)
        return false;
     
     
      GLenum blad = glGetError();
      glBindTexture(GL_TEXTURE_2D,texID);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER);
      glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,(GLvoid*)data);
      glGenerateMipmap(GL_TEXTURE_2D);
      SOIL_free_image_data(data);
      data = NULL;
      return true;
    }
     
    bool Texture::CreateTextureBuffer(vector<vec2> uv)
    {
      if(uv.empty())
        return false;
      uvCoords = uv;
      glGenBuffers(1,&texBufferID);
      glBindBuffer(GL_ARRAY_BUFFER,texBufferID);
      glBufferData(GL_ARRAY_BUFFER,uvCoords.size()*sizeof(vec2),&uvCoords[0],GL_STATIC_DRAW);
      return true;
    }
     
    bool Texture::ActivateTexture(GLuint texUnit)
    {
      if(!texID)
        return false;
      glActiveTexture(GL_TEXTURE0+texUnit);
      glBindTexture(GL_TEXTURE_2D,texID);
      return true;
    }
     
    bool Texture::SetShaderTextureParameter(string name, GLuint value)
    {
      if(!programID)
        return false;
      GLuint textureParamLocation = glGetUniformLocation(programID,name.c_str());
      if(textureParamLocation==-1)
        return false;
      glUniform1i(textureParamLocation,value);
      return true;
    }
     
    bool Texture::EnableTextureAttrib(GLuint attribID)
    {
      if(!texBufferID || !texID)
        return false;
      if(uvCoords.empty())
        return false;
      glEnableVertexAttribArray(attribID);
      glBindBuffer(GL_ARRAY_BUFFER,texBufferID);
      glVertexAttribPointer(attribID,2,GL_FLOAT,GL_FALSE,0,(void*)0);
      return true;
    }

    Most important Model class code:

    Code :
    bool Model::CreateVertexArray()
    {
      glGenVertexArrays(1,&vaoID);
      glBindVertexArray(vaoID);
      return true;
    }
     
    bool Model::CreateVertexBuffer(vector<vec3> vertices)
    {
      if(vertices.empty())
        return false;
      verticesData = vertices;
      glGenBuffers(1,&vboID);
      glBindBuffer(GL_ARRAY_BUFFER,vboID);
      glBufferData(GL_ARRAY_BUFFER,verticesData.size()*sizeof(vec3),&verticesData[0],GL_STATIC_DRAW);
      return true;
    }
     
    bool Model::SetVertexShaderParameter(GLuint shaderID, string name, GLint value)
    {
      if(!shaderID)
        return false;
      GLuint vertexParamLocation = glGetUniformLocation(shaderID,name.c_str());
      if(vertexParamLocation==-1)
        return false;
      glUniform1f(vertexParamLocation,value);
      return true;
    }
     
    void Model::SetPrimitiveParameters(GLenum primitive, GLint first, GLsizei count)
    {
      primitiveType = primitive;
      firstVertex = first;
      countOfVertices = count;
    }
     
    bool Model::EnableVertexAttrib(GLuint attribID)
    {
      if( !vaoID || !vboID )
        return false;
      if(verticesData.empty())
        return false;
      glEnableVertexAttribArray(attribID);
      glBindBuffer(GL_ARRAY_BUFFER,vboID);
      glVertexAttribPointer(attribID,3,GL_FLOAT,GL_FALSE,0,(void*)0);
     
      return true;
    }
     
    void Model::DrawModel()
    {
      glDrawArrays(GL_TRIANGLES,0,verticesData.size());
    }

    Code preparing data:

    Code :
    bool Program::InitModelResources(string vsPath, string fsPath, string meshPath, string texturePath)
    {
      bool result;
      Shader* shader = new Shader;
      result = shader->Init(vsPath,fsPath);
      if(!result)
        return false;
      modelRes.shader = shader;
     
      ObjLoader loader;
      if(loader.OpenObj(meshPath)==OL_ERROR)
        return false;
      if(loader.ReadObj()==OL_ERROR)
        return false;
      if(loader.ExtractObjData()==OL_ERROR)
        return false;
      if(loader.PrepareOutputData()==OL_ERROR)
        return false;
      loader.CloseObj();
     
      Model* model = new Model;
      model->CreateVertexArray();
      model->CreateVertexBuffer(loader.GetVertices());
      modelRes.model = model;
     
      Texture* texture = new Texture;
      texture->LoadTexture(texturePath);
      texture->CreateTextureBuffer(loader.GetTexCoords());
      texture->SetShaderProgramID(shader->GetProgramID());
      modelRes.texture = texture;
      return true;
    }
     
    bool Program::InitModelInstances()
    {
      modelInst.modelResources = &modelRes;
      modelInst.modelTransformation = mat4(1.0);
      return true;
    }

    I observed that textured faces are culling faces.

    I think that bug is in one of snippets. Can somebody help?
    Last edited by cpp_someone; 07-15-2013 at 05:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •