🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Using second UV map in OpenGL

Started by
6 comments, last by frob 1 year, 7 months ago

I am triing to write some kind of 3D viewer for some models from diferent 3d games. For visualisation I am using good old OpenGL. To represent parts of 3D geometry I´ve writen some generic Mesh class which looks like this:

//Vector3F is float[3], Vector2F is float[2] and Triangle is unsigned short[3]
    class Mesh{
        Vector3F* positions;
        Vector3F* normals;
        Vector2F* uvMap1;
        Vector2F* uvMap2;
        GLuint tex1; //texture for uvMap1
        GLuint tex2; //texture for uvMap2
        Triangle* trangles;
        unsigned int numTriangles;
        
        public:
        
        void draw();
        
    };
    
    
    void Mesh::draw()
    {
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, positions);
        if(normals) glNormalPointer(3, GL_FLOAT, 0, normals);
        if(uvMap1)
        {
            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, tex1);
            glTexCoordPointer(2, GL_FLOAT, 0, uvMap1);
            /*
            if(uvMap2)
            {
                here should be code for uvMap2
            }
            */
            glDisable(GL_TEXTURE_2D);
        }
        glDrawElements(GL_TRIANGLES, numTriangles*3, GL_UNSIGNED_SHORT, triangles);
        glDisableClientState(GL_VERTEX_ARRAY);
    }

This is not complete code, just the most important part for my question. As can be seen in the code would like tu use uvMap2 with the coresponding texture if it´s present. Need a solution with standard OpenGL, no wired external APIs.

Advertisement

You can look at ‘Texture Units’ at the bottom: https://learnopengl.com/Getting-started/Textures

JoeJ said:

You can look at ‘Texture Units’ at the bottom: https://learnopengl.com/Getting-started/Textures

I have read that long ago, but it´s not helpful for the problem I have.

convert said:
I have read that long ago, but it´s not helpful for the problem I have.

What precisely is your problem?
If it's not how to use multiple textures at once, maybe it is for what do we need multiple textures?

JoeJ said:

What precisely is your problem?

Its about second UV map, or if tallking about code this part:

if(uvMap2)
            {
                here should be code for uvMap2
            }

So while uvMap1 uses tex1, uvMap2 should use tex2.

convert said:
So while uvMap1 uses tex1, uvMap2 should use tex2.

I see the article handles most of that (binding 2 textures to 2 different texture units), but it does not handle multiple UVs.

Copying and editing a random shader from the tutorial to add this:

#version 330 core
out vec4 FragColor;
  
in vec3 ourColor;
in vec2 TexCoord1;

in vec2 TexCoord2;

uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
    FragColor = mix(texture(texture1, TexCoord1), texture(texture2, TexCoord2), 0.2);
}

Problem is i have forgotten everything about OpenGL, so i can not help about details.
But assume with some trial and error you should get it to work.

You're really going back into really old versions of OpenGL from two decades ago. Basically the transition era from the fixed pipeline to the earliest forms of shaders. That was when people were doing multitexturing to accomplish things but before they were doing it in programmable ways with shaders.

You MIGHT be able to do something like this:

glActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2, GL_FLOAT, 0, uvmap1);
glActiveTexture(GL_TEXTURE1);
glTexCoordPointer(2, GL_FLOAT, 0, uvmap2);

The various GL_TEXTURE# constants are sequential, so if that works you can also do it in a loop:

glActiveTexture(GL_TEXTURE0 + currentIdx);
glTexCoordPointer(2, GL_FLOAT, 0, uvmaps[currentIdx]);

These days – meaning anytime past 2004 or so – use shaders. Put them in your buffer data, use glBufferData() to transfer them, use glVertexAttribPointer() to indicate where they are in the buffer, and use glEnableVertexAttribArray() to enable the data. There's no need to indicate it is a specific texture any more, they've become arbitrary data arrays that you can use for whatever purpose you want, including texturing. Indicate the layout of your data array so the shader knows what location to look for the data, and you can include whatever data arrays you want.

This topic is closed to new replies.

Advertisement