PDA

View Full Version : Wants to use GL_TEXTURE_2D_ARRAY



koker_007
07-12-2016, 04:39 PM
Hello, I'm directly sorry for my bad english, not found answers in my mother tongue how to properly use GL_TEXTURE_2D_ARRAY.
Most recently I managed to impose a first texture using GL_TEXTURE_2D. But to my surprise it was imposed absolutely on all polygons. ;)
Now I understand why did it happen.
Studying GL_TEXTURE_2D_ARRAY I find it very convenient to use.
I want to download it a certain number of textures and then, using the texture coordinates select what kind of texture should be imposed on the polygon.
I have code which must doing it. However, due to of my inability this does not happen.
Please explain what I'm doing wrong.

Program


//Тест загрузки нескольких текстур
void LoadTexture_in_OpenGL() {
wchar_t FileName1[] = L"textures/Test1.jpg";
wchar_t FileName2[] = L"textures/test2.jpg";
LoadTexture(FileName1, &Textures_Mass[0]);
LoadTexture(FileName2, &Textures_Mass[1]);

glGenTextures(1, &TextureID); //создаем ID описатель текстуры.. лиш для одной текстуры (это будет 2D Array)

glBindTexture(GL_TEXTURE_2D_ARRAY, TextureID); //Говорим использовать этот описатель текстуры

GLuint depth_texture_array = 20; //Задаем количество слоев в нашей текстуре.. под 20 будуших текстур
glTexImage3D(GL_TEXTURE_2D_ARRAY,0, GL_RGB8, Textures_Mass[0].width, Textures_Mass[0].height, depth_texture_array, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); //создана "пустышка" неизменяемой текстуры

//добавление 1-ой текстуры в текстурный массив, 0-вым слоем
glTexSubImage3D(GL_TEXTURE_2D_ARRAY,0, 0, 0, 0, Textures_Mass[0].width, Textures_Mass[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, Textures_Mass[0].imageData);

//добавление 2-ой текстуры в текстурный массив, 1-вым слоем
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, Textures_Mass[1].width, Textures_Mass[1].height, 1, GL_RGB, GL_UNSIGNED_BYTE, Textures_Mass[1].imageData);

glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

}



Shader


const GLchar* VertShaderCode_VeryLow = R"END(

#version 430

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
layout (location = 2) in vec3 VertexTexCoord;
layout (location = 3) in int VertexTexID;

out vec3 Color;
out vec3 TexCoord;
out int TexID;

uniform mat4 MVPMatrix;

void main()
{
TexCoord = VertexTexCoord;
TexID = VertexTexID;
Color = VertexColor;

gl_Position = MVPMatrix * vec4(VertexPosition, 1.0);
gl_PointSize = 40; //Не проверенно
}

)END";


//Frag Shader Very Low
const GLchar* FragShaderCode_VeryLow = R"END(

#version 430

in vec3 TexCoord;
in vec3 Color;
in int TexID;

uniform sampler2D Tex1;

layout (location = 0) out vec4 FragColor;

void main()
{
//FragColor = texture2D(Tex1, TexCoord.st);
FragColor = texture(Tex1, TexCoord.st, 1.0);
}

)END";

john_connor
07-12-2016, 05:26 PM
did you check for any errors ?
are all textures of the same size ?
i think you need also some more glTexParameteri(...) to set: GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T

koker_007
07-13-2016, 02:52 AM
did you check for any errors ?
are all textures of the same size ?
i think you need also some more glTexParameteri(...) to set: GL_TEXTURE_WRAP_S, GL_TEXTURE_WRAP_T
1) No, I did not check for errors. You want to me to use glGetError? Or for texture has its own command?
2) Yes, both of have 400x400 texture size. The function LoadTexture() memorizes both sizes are for struct Textures_Mass[].
3) Added. But there is no visible changes.

I changed the code, it must be closer to getting result through GL_TEXTURE_2D_ARRAY.

Program


//Тест загрузки нескольких текстур
void LoadTexture_in_OpenGL() {
wchar_t FileName1[] = L"textures/Test1.jpg";
wchar_t FileName2[] = L"textures/test2.jpg";
LoadTexture(FileName1, &Textures_Mass[0]);
LoadTexture(FileName2, &Textures_Mass[1]);

glGenTextures(1, &TextureID); //создаем ID описатель текстуры.. лиш для одной текстуры (это будет 2D Array)

glBindTexture(GL_TEXTURE_2D_ARRAY, TextureID); //Говорим использовать этот описатель текстуры

GLuint depth_texture_array = 20; //Задаем количество слоев в нашей текстуре.. под 20 будуших текстур
glTexStorage3D(GL_TEXTURE_2D_ARRAY,0, GL_RGB8, Textures_Mass[0].width, Textures_Mass[0].height, depth_texture_array); //создана "пустышка" неизменяемой текстуры

//добавление 1-ой текстуры в текстурный массив, 0-вым слоем
glTexSubImage3D(GL_TEXTURE_2D,0, 0, 0, 0, Textures_Mass[0].width, Textures_Mass[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, Textures_Mass[0].imageData);

//добавление 2-ой текстуры в текстурный массив, 1-вым слоем
glTexSubImage3D(GL_TEXTURE_2D, 0, 0, 0, 1, Textures_Mass[1].width, Textures_Mass[1].height, 1, GL_RGB, GL_UNSIGNED_BYTE, Textures_Mass[1].imageData);

glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_LINEAR);

}


Shader


const GLchar* VertShaderCode_VeryLow = R"END(

#version 430

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
layout (location = 2) in vec3 VertexTexCoord;
layout (location = 3) in int VertexTexID;

out vec3 Color;
out vec3 TexCoord;
out int TexID;

uniform mat4 MVPMatrix;

void main()
{
TexCoord = VertexTexCoord;
TexID = VertexTexID;
Color = VertexColor;

gl_Position = MVPMatrix * vec4(VertexPosition, 1.0);
gl_PointSize = 40; //Не проверенно
}

)END";


//Frag Shader Very Low
const GLchar* FragShaderCode_VeryLow = R"END(

#version 430

in vec3 TexCoord;
in vec3 Color;
in int TexID;

//uniform sampler2D Tex1;
uniform sampler2DArray Textur_Array;

layout (location = 0) out vec4 FragColor;

void main()
{
//FragColor = texture2D(Tex1, TexCoord.st);
FragColor = texture(Textur_Array, TexCoord);
}

)END";


At this point, one can see the contour the figure. But it has all color black.
I think the problem must be somewhere in the loading textures for OpenGL.
P.S. I can answer for a long time due to the fact I have the different time zone.