Trouble Creating a Texture Array

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble Creating a Texture Array

andromda
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.
Reply | Threaded
Open this post in threaded view
|

Re: Trouble Creating a Texture Array

andromda
I was able to solve this myself. I was correct that it was with how I was setting up the texture array. Solution:

Solution public class TextureArray { private static int imageIndex = 0; public TextureArray(GL4 gl, int shaderProgram) { try { gl.glUseProgram(shaderProgram); TextureData data1 = TextureIO.newTextureData(gl.getGLProfile(), new File("src/main/resources/textures/grass.png"), true, "png"); TextureData data2 = TextureIO.newTextureData(gl.getGLProfile(), new File("src/main/resources/textures/stone.png"), true, "png"); IntBuffer texture = IntBuffer.allocate(16); int textureUnit = 5; // arbitrary gl.glActiveTexture(GL_TEXTURE0 + textureUnit); gl.glGenTextures(1, texture); gl.glBindTexture(GL_TEXTURE_2D_ARRAY, texture.get()); gl.glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 128, 128, 16); gl.glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, 128, 128, 1, GL_RGBA, GL_UNSIGNED_BYTE, data1.getBuffer()); gl.glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, 128, 128, 1, GL_RGBA, GL_UNSIGNED_BYTE, data2.getBuffer()); gl.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gl.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gl.glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 4); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); int textureArrayLoc = gl.glGetUniformLocation(shaderProgram, "textureArray"); gl.glUniform1i(textureArrayLoc, textureUnit); } catch (IOException ignored) { System.out.println("Texture Array Failed"); } } }