Hello. The programs renders a cube. If I set the color in the fragment shader manually, it works, but when I use a texture color, the cube becomes black. I wonder how to fix this?
The cube is rendered using glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
The texture is set up as follows:
GLsizei width = 2;
GLsizei height = 2;
GLsizei layerCount = 1;
GLsizei mipLevelCount = 1;
float * data_test = new float[width * height * 4];
for (int i = 0; i < width*height*4; i+=4)
{
data_test[i + 0] = 0.25f;
data_test[i + 1] = 0.45f;
data_test[i + 2] = 0.95f;
data_test[i + 3] = 1.00f;
}
glGenTextures(1, &game_graphics_admin.texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, game_graphics_admin.texture);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA, width, height, layerCount);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, width, height, layerCount, GL_RGBA, GL_FLOAT, data_test);
delete[] data_test;
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
Vertex shader fetch:
12 triangles x 3 vertices = 36 vertices that are indexed.
36 x 2 uv texture coordinates
Vertex shader code:
#version 430 core
layout(location = 0) in vec4 position;
layout(location = 5) in vec2 texCoord;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
out vec2 TexCoords;
void main(void)
{
gl_Position = proj_matrix * mv_matrix * position;
TexCoords = texCoord;
}
Fragment shader code:
#version 430 core
in vec2 TexCoords;
layout (binding=0) uniform sampler2DArray textureArray;
//layout (location=1) uniform int layer;
out vec4 color;
void main(void)
{
color = vec4(texture2DArray(textureArray, vec3(TexCoords.xy, 0)));
}
Texture UV coordinate attribute is taken from a uv-buffer, and for simplicity is made of repeating the 3 coordinate pairs (0.0f,0.0f),(0.0f,1.0f),(1.0f,1.0f), … . It total, 36 uv-pairs.