Trouble Creating a Texture Array
Posted by
andromda on
Nov 24, 2023; 5:47am
URL: https://forum.jogamp.org/Trouble-Creating-a-Texture-Array-tp4043154.html
Hey all, I have been learning OpenGL in one of my college courses and have decided to try to make a very simple/dumb Minecraft clone as a learning exercise.
At first to load textures I was using a texture atlas and while I had that working as expected, it resulted in textures bleeding over the edge. I read that using a texture array has more benefits/less drawbacks so I have decided to switch to that.
Vertex Shader
#version 410
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNorm;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in float aDepth;
out vec2 TexCoords;
out vec3 Normal;
out vec3 FragPos;
out float vDepth;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float aTexID;
void main()
{
TexCoords = aTexCoords;
vDepth = aDepth;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
Fragment Shader
#version 410
out vec4 FragColor;
in vec2 TexCoords;
in float vDepth;
uniform sampler2DArray textureArray;
void main()
{
FragColor = texture(textureArray, vec3(TexCoords, vDepth));
}
Creating the texture array
public class TextureArray {
private static int imageIndex = 0;
public final IntBuffer textureID = Buffers.newDirectIntBuffer(1);
public TextureArray(GL4 gl) {
try {
TextureData data = TextureIO.newTextureData(gl.getGLProfile(), new File("src/main/resources/textures/atlas.png"), true, "png");
gl.glGenTextures(1, textureID);
gl.glBindTexture(GL_TEXTURE_2D_ARRAY, textureID.get(0));
gl.glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 128, 128, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.getBuffer());
} catch (IOException ignored) {
System.out.println("Texture Array Failed");
}
}
}
Draw function for cube
gl.glUseProgram(shaderProgram);
gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_2D_ARRAY, Main.textureArray.textureID.get(0));
... send model/view/projection matrices
// draw cubes
gl.glBindVertexArray(vao[0]);
gl.glDrawArrays(GL_TRIANGLES, 0, numVertices);
gl.glBindVertexArray(0);
I am pretty confident that my issue lies in how I am creating the texture array,
as when I draw the blocks everyone of them is black. I recognize that I am
not sending the vDepth value to the vertex/fragment shader yet,
however as I understand it defaults to 0, I am just trying to get a
texture showing and then configure that properly later on.
Thank you for your help and time.