so I'm having a problem with what it seems to be shadow acne (as far as I can tell), but the thing is that I have not yet implemented shadows of any type. I have implemented Phong Lighting model (following learnopengl tutorials).
enter image description here enter image description here
I'm using Windows 11, but also happens on any other platform that I tried (Mac, Linux and Android).
I'm using SDL (2.26.5) + OpenGL (OpenGL Version: 4.0.0 NVIDIA 537.58, Vendor: NVIDIA Corporation, GPU: NVIDIA GeForce RTX 3060/PCIe/SSE2, GLSL: 4.00 NVIDIA via Cg compiler).
Depth Testing (I have also tried with GL_LEQUAL and has same effect, someone said that could solve the problem)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
And those are the Vertex and Fragment shaders:
#version 330 core
layout(location = 0) in vec3 in_pos;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec2 in_text_coord;
layout(location = 3) in mat4 in_model;
out vec3 normal;
out vec2 text_coord;
out vec3 frag_pos;
out mat4 model_matrix;
uniform mat4 view_projection_matrix;
void main(){
mat4 _model = in_model;
gl_Position = view_projection_matrix * _model * vec4(in_pos, 1);
normal = in_normal;
frag_pos = in_pos;
text_coord = in_text_coord;
model_matrix = _model;
}
#version 330 core
const int RDE_MAX_POINT_LIGHTS = 10;
const int RDE_MAX_SPOT_LIGHTS = 10;
in vec3 normal;
in vec2 text_coord;
in vec3 frag_pos;
out vec4 color_out;
struct rde_material {
float shininess;
vec3 Ka;
vec3 Kd;
vec3 Ks;
int using_render_texture;
};
struct rde_directional_light {
vec3 direction;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
};
struct rde_point_light {
vec3 position;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
float constant;
float linear;
float quadratic;
int used;
};
struct rde_spot_light {
vec3 position;
vec3 direction;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
float constant;
float linear;
float quadratic;
float cut_off;
float outer_cut_off;
int used;
};
uniform vec3 camera_pos;
uniform rde_directional_light directional_light;
uniform rde_point_light point_lights[RDE_MAX_POINT_LIGHTS];
uniform rde_spot_light spot_lights[RDE_MAX_SPOT_LIGHTS];
uniform rde_material material;
uniform sampler2D tex_ka;
uniform sampler2D tex_kd;
uniform sampler2D tex_ks;
uniform sampler2D tex_bump;
uniform sampler2D render_texture;
vec3 directional_light_calc() {
vec3 _ambient = material.Ka * directional_light.ambient_color * texture(tex_kd, text_coord).rgb;
vec3 _norm = normalize(normal);
vec3 _light_dir = normalize(-directional_light.direction);
float _diff = max(dot(_norm, _light_dir), 0.0);
vec3 _diffuse = material.Kd * directional_light.diffuse_color * _diff * texture(tex_kd, text_coord).rgb;
vec3 _view_dir = normalize(camera_pos + frag_pos);
vec3 _reflect_dir = reflect(-_light_dir, _norm);
float _spec = pow(max(dot(_view_dir, _reflect_dir), 0.0), material.shininess);
vec3 _specular = material.Ks * directional_light.specular_color * _spec * texture(tex_ks, text_coord).rgb;
vec3 _final_light = _ambient + _diffuse + _specular;
return _final_light;
}
vec3 point_light_calc(int _i) {
rde_point_light _light = point_lights[_i];
if(_light.used < 0) return vec3(0.0, 0.0, 0.0);
vec3 _ambient = material.Ka * _light.ambient_color * texture(tex_kd, text_coord).rgb;
vec3 _norm = normalize(normal);
vec3 _light_dir = normalize(_light.position - frag_pos);
float _diff = max(dot(_norm, _light_dir), 0.0);
vec3 _diffuse = material.Kd * _light.diffuse_color * _diff * texture(tex_kd, text_coord).rgb;
vec3 _view_dir = normalize(camera_pos + frag_pos);
vec3 _reflect_dir = reflect(-_light_dir, _norm);
float _spec = pow(max(dot(_view_dir, _reflect_dir), 0.0), material.shininess);
vec3 _specular = material.Ks * _light.specular_color * _spec * texture(tex_ks, text_coord).rgb;
float _distance = length(_light.position - frag_pos);
float _attenuation = 1.0 / (_light.constant + _light.linear * _distance + _light.quadratic * (_distance * _distance));
_ambient *= _attenuation;
_diffuse *= _attenuation;
_specular *= _attenuation;
return (_ambient + _diffuse + _specular);
}
vec3 spot_light_calc(int _i) {
rde_spot_light _light = spot_lights[_i];
if(_light.used < 0) return vec3(0.0, 0.0, 0.0);
vec3 _light_dir = normalize(_light.position - frag_pos);
float _theta = dot(_light_dir, normalize(-_light.direction));
float _epsilon = (_light.cut_off - _light.outer_cut_off);
float _intensity = clamp((_theta - _light.outer_cut_off) / _epsilon, 0.0, 1.0);
vec3 _ambient = material.Ka * _light.ambient_color * texture(tex_kd, text_coord).rgb;
vec3 _norm = normalize(normal);
float _diff = max(dot(_norm, _light_dir), 0.0);
vec3 _diffuse = material.Kd * _light.diffuse_color * _diff * texture(tex_kd, text_coord).rgb;
vec3 _view_dir = normalize(camera_pos + frag_pos);
vec3 _reflect_dir = reflect(-_light_dir, _norm);
float _spec = pow(max(dot(_view_dir, _reflect_dir), 0.0), material.shininess);
vec3 _specular = material.Ks * _light.specular_color * _spec * texture(tex_ks, text_coord).rgb;
float _distance = length(_light.position - frag_pos);
float _attenuation = 1.0 / (_light.constant + _light.linear * _distance + _light.quadratic * (_distance * _distance));
_ambient *= _attenuation;
_diffuse *= _attenuation * _intensity;
_specular *= _attenuation * _intensity;
return (_ambient + _diffuse + _specular);
}
void normal_rendering() {
// This is for textures with some kind of transparency
if(texture(tex_kd, text_coord).a < 0.05) discard;
vec3 _final_light = vec3(0.0);
_final_light += directional_light_calc();
for(int _i = 0; _i < RDE_MAX_POINT_LIGHTS; _i++) {
_final_light += point_light_calc(_i);
}
for(int _i = 0; _i < RDE_MAX_SPOT_LIGHTS; _i++) {
_final_light += spot_light_calc(_i);
}
color_out = vec4(_final_light, 1.0);
}
// This can be ignored
void render_texture_rendering() {
color_out = texture(render_texture, text_coord);
}
void main(){
if(material.using_render_texture == 0) {
// This is how it is rendered
normal_rendering();
} else {
// This can be ignored
render_texture_rendering();
}
}
I basically followed the steps on the learnopengl tutorials, but there they only use cubes and simple textures, which for those ones, my engine works well also, there is no such effect, but suddenly with some textures it happens badly. I don't know if I'm missing something on the fragment shader or if there is any other GL function/paramenter I'm not setting up correctly.
I'm not using Anti-aliasing yet, as I use a custom Framebuffer (this already happened with the default one) and as far as I could see, with custom framebuffer you need to implement manually anti-aliasing. Could this be the problem? Not having it?
Also tried with this fragment shader:
#version 330 core
const int RDE_MAX_POINT_LIGHTS = 10;
const int RDE_MAX_SPOT_LIGHTS = 10;
in vec3 normal;
in vec2 text_coord;
in vec3 frag_pos;
out vec4 color_out;
struct rde_material {
float shininess;
vec3 Ka;
vec3 Kd;
vec3 Ks;
int using_render_texture;
};
struct rde_directional_light {
vec3 direction;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
};
struct rde_point_light {
vec3 position;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
float constant;
float linear;
float quadratic;
int used;
};
struct rde_spot_light {
vec3 position;
vec3 direction;
vec3 ambient_color;
vec3 diffuse_color;
vec3 specular_color;
float constant;
float linear;
float quadratic;
float cut_off;
float outer_cut_off;
int used;
};
uniform vec3 camera_pos;
uniform rde_directional_light directional_light;
uniform rde_point_light point_lights[RDE_MAX_POINT_LIGHTS];
uniform rde_spot_light spot_lights[RDE_MAX_SPOT_LIGHTS];
uniform rde_material material;
uniform sampler2D tex_ka;
uniform sampler2D tex_kd;
uniform sampler2D tex_ks;
uniform sampler2D tex_bump;
uniform sampler2D render_texture;
void main(){
color_out = vec4(texture(tex_kd, text_coord).rgb, 1.0);
}
But the error rendering those textures presists, there is just no lighting (well everything is super bright, but thats it).
Please any extra info needed, just ask on the comments and I'll provide it.
I followed all of the learnopengl steps and tutorials until Phong Lighting, which makes some textures look good and not others. I've tried to look for my problem but only found similar errors when implementing shadows, so I don't know how to fix this.
So the problem was that I did not use Mipmapping. I did not quite understand it, so I did not want to implement it right away, and that was causing this error. There is also other problem people with this issue can look into and it is called anisotropic filtering https://www.khronos.org/opengl/wiki/Sampler_Object#Filtering