shadowmapping #2

Merged
Anri merged 8 commits from shadowmapping into main 2023-05-27 04:22:33 +02:00
3 changed files with 15 additions and 14 deletions
Showing only changes of commit ddbd1f9ae5 - Show all commits

View file

@ -2,7 +2,7 @@
in vec4 pos_model; in vec4 pos_model;
in vec3 vec_normal; in vec3 vec_normal;
in vec4 smcoord; in vec4 shadowMapCoord;
out vec4 fragColor; out vec4 fragColor;
@ -12,7 +12,7 @@ uniform vec4 couleur;
uniform vec4 lumPos[max]; uniform vec4 lumPos[max];
uniform mat4 view; uniform mat4 view;
uniform vec4 lumColor[max]; uniform vec4 lumColor[max];
uniform sampler2D sm; uniform sampler2D shadowmap;
void main() { void main() {
const vec3 vue = vec3(0, 0, -1); const vec3 vue = vec3(0, 0, -1);
@ -22,8 +22,8 @@ void main() {
vec3 torche = normalize(pos_model.xyz - lumPos[i].xyz); vec3 torche = normalize(pos_model.xyz - lumPos[i].xyz);
float intensite_lumineuse = clamp(dot(vec_normal, -torche), 0., 1.); float intensite_lumineuse = clamp(dot(vec_normal, -torche), 0., 1.);
vec3 smpcoord = smcoord.xyz / smcoord.w; vec3 shadowMapPcoord = shadowMapCoord.xyz / shadowMapCoord.w;
if(texture(sm, smpcoord.xy).r < smpcoord.z) { if(texture(shadowmap, shadowMapPcoord.xy).r < shadowMapPcoord.z) {
intensite_lumineuse = 0; intensite_lumineuse = 0;
} }

View file

@ -5,7 +5,7 @@ layout(location = 1) in vec3 normal;
out vec4 pos_model; out vec4 pos_model;
out vec3 vec_normal; out vec3 vec_normal;
out vec4 smcoord; out vec4 shadowMapCoord;
uniform mat4 proj, model, view; uniform mat4 proj, model, view;
uniform mat4 lightView, lightProjection; uniform mat4 lightView, lightProjection;
@ -16,6 +16,6 @@ void main() {
pos_model = model * vec4(pos, 1); pos_model = model * vec4(pos, 1);
vec_normal = normalize(transpose(inverse(model)) * vec4(normal, 0)).xyz; vec_normal = normalize(transpose(inverse(model)) * vec4(normal, 0)).xyz;
smcoord = bias * lightProjection * lightView * pos_model; shadowMapCoord = bias * lightProjection * lightView * pos_model;
gl_Position = proj * view * pos_model; gl_Position = proj * view * pos_model;
} }

View file

@ -7,7 +7,7 @@ static GLuint _planId = 0;
static struct manifestant _herosId[HEROS_NUMBER]; static struct manifestant _herosId[HEROS_NUMBER];
#define SHADOW_MAP_SIDE 1024 #define SHADOW_MAP_SIDE 1024
static GLuint _fbo = 0, _smTex = 0; static GLuint _fbo = 0, _shadow_map_tex = 0;
static const char *matrix_proj = "proj"; static const char *matrix_proj = "proj";
static const char *matrix_model = "model"; static const char *matrix_model = "model";
@ -62,8 +62,8 @@ static void init(void) {
gl4duGenMatrix(GL_FLOAT, matrix_lproj); gl4duGenMatrix(GL_FLOAT, matrix_lproj);
// Création et paramétrage de la Texture de shadow map // Création et paramétrage de la Texture de shadow map
glGenTextures(1, &_smTex); glGenTextures(1, &_shadow_map_tex);
glBindTexture(GL_TEXTURE_2D, _smTex); glBindTexture(GL_TEXTURE_2D, _shadow_map_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_MAP_SIDE, glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_MAP_SIDE,
@ -93,7 +93,7 @@ static void draw(void) {
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
glBindFramebuffer(GL_FRAMEBUFFER, _fbo); glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
_smTex, 0); _shadow_map_tex, 0);
glViewport(0, 0, SHADOW_MAP_SIDE, SHADOW_MAP_SIDE); glViewport(0, 0, SHADOW_MAP_SIDE, SHADOW_MAP_SIDE);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < HEROS_NUMBER; ++i) { for (int i = 0; i < HEROS_NUMBER; ++i) {
@ -148,16 +148,17 @@ static void draw(void) {
drawManifestant(matrix_model, &_herosId[i], deplacement, GL_TRUE); drawManifestant(matrix_model, &_herosId[i], deplacement, GL_TRUE);
} }
int max_gpu = 3; // HEROS_NUMBER;
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _smTex); glBindTexture(GL_TEXTURE_2D, _shadow_map_tex);
int max_gpu = 3; // HEROS_NUMBER;
glUniform4fv(glGetUniformLocation(_pId[0], "lumPos"), max_gpu, glUniform4fv(glGetUniformLocation(_pId[0], "lumPos"), max_gpu,
(float *)lumpos); (float *)lumpos);
glUniform4fv(glGetUniformLocation(_pId[0], "lumColor"), max_gpu, glUniform4fv(glGetUniformLocation(_pId[0], "lumColor"), max_gpu,
(float *)lumcolor); (float *)lumcolor);
glUniform1i(glGetUniformLocation(_pId[0], "sm"), 0); glUniform1i(glGetUniformLocation(_pId[0], "shadowmap"), 0);
deplacement += (float)(0.4 * M_PI * dt); deplacement += (float)(0.4 * M_PI * dt);
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0); glUseProgram(0);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
@ -166,7 +167,7 @@ static void draw(void) {
static void deinit(void) { static void deinit(void) {
if (_fbo) { if (_fbo) {
glDeleteTextures(1, &_smTex); glDeleteTextures(1, &_shadow_map_tex);
glDeleteFramebuffers(1, &_fbo); glDeleteFramebuffers(1, &_fbo);
_fbo = 0; _fbo = 0;
} }