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: How to add two texture to two different objects

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    3

    How to add two texture to two different objects

    Current, I am able to add one texture to one object using imagemagic.

    Code cpp:
    gSampler = m_shader->GetUniformLocation("gSampler");
      if (gSampler == INVALID_UNIFORM_LOCATION) 
      {
        printf("gSample not found\n");
        return false;
      }
     
     
     
      //assert(glGetError() == GL_NO_ERROR);

    in initialization:

    Code cpp:
      try
      {
            m_image.read("../src/test/checker.jpg");
            m_image.write(&m_blob, "RGBA");
     
      }
      catch (Magick::Error& Error) 
      {
            std::cout << "Error loading texture"<< std::endl;
            return false;
      }
     
      glGenTextures(1, &m_textureObj);
     
      //glActiveTexture(GL_TEXTURE0);
     
     
      glBindTexture(GL_TEXTURE_2D, m_textureObj);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_image.columns(), m_image.rows(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob.data());
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    
      glBindTexture(GL_TEXTURE_2D, 0);

    In render():
    Code cpp:
    void Object::Render(GLuint m_textureObj)
    {
     
      glEnableVertexAttribArray(0);
      glEnableVertexAttribArray(1);
     
     
     
      for (unsigned int i = 0 ; i < m_Entries.size() ; i++) 
      {
        glBindBuffer(GL_ARRAY_BUFFER, m_Entries[i].VB);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex,uv));
     
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Entries[i].IB);
     
     
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_textureObj);
     
        glDrawElements(GL_TRIANGLES, m_Entries[i].NumIndices, GL_UNSIGNED_INT, 0);
      }
     
     
      glDisableVertexAttribArray(0);
      glDisableVertexAttribArray(1);
    }

    fragment shader:
    Code glsl:
    #version 330
              smooth in vec2 TexCoord0; 
              out vec4 frag_color;
              uniform sampler2D gSampler; 
              void main(void) 
              { 
                 frag_color = texture2D(gSampler, TexCoord0.xy); 
              }

    vertex shader:

    Code glsl:
    #version 330
              layout (location = 0) in vec3 v_position; 
              layout (location = 1) in vec2 TexCoord;
              smooth out vec2 TexCoord0; 
              uniform mat4 projectionMatrix; 
              uniform mat4 viewMatrix; 
              uniform mat4 modelMatrix; 
              void main(void) 
              { 
                vec4 v = vec4(v_position, 1.0); 
     
                gl_Position =  (projectionMatrix*viewMatrix*modelMatrix)*v; 
                TexCoord0 = TexCoord; 
              }

    How can I add two texture to two different objects(which are stored in the same obj file)?
    Last edited by Dark Photon; 10-03-2017 at 04:56 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
  •