简体   繁体   中英

Multiple texture on same object opengl4csharp

I got ac# program with opengl4csharp library, which creates a 3D cube, movable in space with the mouse and keyboard.

Actually, I apply only one texture to the cube uniformly. My problem is that I want to apply a different texture to each face.

I tried to initiate a texture array and a textureId array as following :

 diceTextures = new Texture[6];
        diceTextures[0] = new Texture("top.jpg");
        diceTextures[1] = new Texture("bottom.jpg");
        diceTextures[2] = new Texture("left.jpg");
        diceTextures[3] = new Texture("right.jpg");
        diceTextures[4] = new Texture("front.jpg");
        diceTextures[5] = new Texture("back.jpg");

        diceUint = new uint[diceTextures.Length];

        for (uint ui = 0; ui < diceTextures.Length; ui++)
        {
            diceUint[ui] = diceTextures[ui].TextureID;
        }

Then in the OnRenderFrame method, to bind them with :

        Gl.UseProgram(program);
        Gl.ActiveTexture(TextureUnit.Texture0);
        Gl.BindTextures(0, diceTextures.Length, diceUint);

But nothing changes, only the first texture of the array is displayed on the cube, as previously when binding only one texture.

How can I achieve that the textures are applied to the faces ?

Gl.BindTextures(0, diceTextures.Length, diceUint);

This binds 6 textures to 6 separate texture units, 0 through diceTextures.Length - 1 . Indeed, if you're going to use glBindTexture , you don't need the glActiveTexture call. 你不需要glActiveTexture电话。

In any case, if your goal is to give each face a different texture, you first have to be able to identify a specific face from your shader. That means each face needs to be given a per-vertex value which is separate from those for the vertices for other faces. This also means that such faces cannot share positions with other faces, since one of their attributes is not shared from face to face.

So you need a new vertex attribute which contains the index for the texture you want that face to use.

From there, you can employ array textures . These are single textures which contain an array of images. When sampling from array textures, you can specify (as part of the texture coordinate) which index in the array to sample from.

Of course, this changes your texture building code, as you must use GL_TEXTURE_2D_ARRAY for your texture type, and allocate multiple array layers for each mipmap face.

Overall, the shader code would look something like this:

#version 330

layout(location = 0) in vec3 position;
layout(location = 2) in vec2 vertTexCoord;
layout(location = 6) in float textureLayer;

out vec2 texCoord;
flat out float layer;

void main()
{
  gl_Position = //your usual stuff.
  texCoord = vertTexCoord;
  layer = textureLayer;
}

Fragment shader:

#version 330

in vec2 texCoord;
flat in float layer;

uniform sampler2DArray arrayTexture;

out vec4 outColor;

void main()
{
  outColor = texture(arrayTexture, vec3(texCoord, layer));
}

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