I recently decided to make my own voxel game with opengl in C (using glfw).
I'm looking to add textures to the blocs (I already done the chunk system). To do this, I add for every bloc added to the vertices array and ID for the texture (1 = cobblestone, 2 = dirt, ...) and I take the ID back directly in the shader to apply the texture.
When I launch my game with only cobblestone, it works perfectly :
Image link : https://www.mediafire.com/view/xh28aq5z1vcsacl/COBBLE.png/file
But the problem is when I use dirt blocs :
Image link : https://www.mediafire.com/view/6lkacdmdkb1fibd/DIRT.png/file
Here is my vertex shader :
#version 330 core
layout (location = 0) in vec3 aPos ;
layout (location = 1) in vec3 aNormal ;
layout (location = 2) in vec2 aTexCoords ;
layout (location = 3) in float type ; //I am importing here the ID
uniform mat4 model ;
uniform mat4 view ;
uniform mat4 projection ;
out vec3 Normal ;
out vec3 FragPos ;
out vec2 TexCoords ;
out float BlockType ;
void main(){
gl_Position = projection * view * model * vec4(aPos, 1.0f) ;
FragPos = vec3(model * vec4(aPos, 1.0f)) ;
Normal = aNormal;
TexCoords = aTexCoords ;
BlockType = type ; //I send it to the fragment shader
//I have already tried to send a constant value :
//BlockType = 2 ;
//To be sure that the problem does not come from the vertices array
//And It changes nothing so the problem is after sending
}
And the fragment shader :
#version 330 core
#define NR_POINT_LIGHTS 1
#define NB_TEXTURES_TYPE 3
out vec4 FragColor ;
struct Material {
sampler2D diffuse[NB_TEXTURES_TYPE]; //Those sampler2D contains the textures
sampler2D specular[NB_TEXTURES_TYPE];
float shininess;
};
in vec2 TexCoords;
in vec3 FragPos;
in float BlockType ;
uniform Material material; //I create the uniform that permit to import the textures here
void main(){
//Here I convert the ID into integer to use it for an array
int btype = int(BlockType) ;
if(btype > 0){
btype -= 1 ; //I reduce by 1 the ID because 0 is equivalent to air from my C program but here this is the cobble (because an array start by 0).
}
FragColor = vec4(vec3(texture(material.diffuse[btype], TexCoords)), 1.0) ; //And finally I send the result.
//I already done to add a constant instead of the btype variable :
//FragColor = vec4(vec3(texture(material.diffuse[1], TexCoords)), 1.0) ;
//And when I do this, it works, I obtain the dirt texture.
}
I have the impression that the value of the ID of the bloc changes for no reason between the vertex shader and the fragment shader I don't know why.
I can eventually send my entire code to github if you need it.
I spent a lot of time to resolve this and I am stuck so a little help would be appreciated ;)
Thanks in advance !
CodePudding user response:
"I have the impression that the value of the ID of the bloc changes for no reason between the vertex shader and the fragment shader"
Of course, the value of the fragment shader input is interpolated between the outputs of the vertex shader associated with the primitive. If you don't want the input to be interpolated, you must use the flat
Interpolation qualifiers:
vertex shader
flat out float BlockType;
fragment shader
flat in float BlockType;