basic light

This commit is contained in:
Mylloon 2023-05-01 01:00:50 +02:00
parent 3ea25f83ad
commit 747b4d1e86
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 97 additions and 2 deletions

60
main.c
View file

@ -2,10 +2,13 @@
#include <stdio.h> #include <stdio.h>
static void init(void); static void init(void);
static void resize(int, int);
static void draw(void); static void draw(void);
static void sortie(void); static void sortie(void);
static GLuint _wW = 1280, _wH = 720; static GLuint _wW = 1280, _wH = 720;
static GLuint _quadId = 0;
static GLuint _pId = 0;
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (!gl4duwCreateWindow(argc, argv, "Demo API8 2023", GL4DW_POS_CENTERED, if (!gl4duwCreateWindow(argc, argv, "Demo API8 2023", GL4DW_POS_CENTERED,
@ -17,14 +20,67 @@ int main(int argc, char **argv) {
init(); init();
atexit(sortie); atexit(sortie);
gl4duwResizeFunc(resize);
gl4duwDisplayFunc(draw); gl4duwDisplayFunc(draw);
gl4duwMainLoop(); gl4duwMainLoop();
return 0; return 0;
} }
void init(void) {} void init(void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
_quadId = gl4dgGenQuadf();
_pId = gl4duCreateProgram("<vs>shaders/lights.vs", "<fs>shaders/lights.fs",
NULL);
glEnable(GL_DEPTH_TEST);
gl4duGenMatrix(GL_FLOAT, "proj");
gl4duGenMatrix(GL_FLOAT, "mod");
gl4duGenMatrix(GL_FLOAT, "view");
resize(_wW, _wH);
}
void draw(void) {} static void resize(int width, int height) {
GLfloat ratio;
_wW = width;
_wH = height;
glViewport(0, 0, _wW, _wH);
ratio = (GLfloat)_wW / (GLfloat)_wH;
gl4duBindMatrix("proj");
gl4duLoadIdentityf();
gl4duFrustumf(-1 * ratio, 1 * ratio, -1, 1, 2, 100);
}
void draw(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Vue
gl4duBindMatrix("view");
gl4duLoadIdentityf();
gl4duLookAtf(0, 0, 4.f, 0, 0, 0, 0.f, 1.f, 0);
// Scene
// clang-format off
GLfloat rouge[] = {1.f, 0.f, 0.f, 1.f},
blanc[] = {1.f, 1.f, 1.f, 1.f},
lumpos0[] = {0.f, 0.f, 1.1f, 1.f},
lumpos[4],
*mat;
// clang-format on
static GLdouble x = 0.;
lumpos0[0] = 4.f * (float)sin(x);
gl4duBindMatrix("view");
mat = (GLfloat *)gl4duGetMatrixData();
MMAT4XVEC4(lumpos, mat, lumpos0);
gl4duBindMatrix("mod");
gl4duLoadIdentityf();
glUseProgram(_pId);
gl4duSendMatrices();
glUniform4fv(glGetUniformLocation(_pId, "scolor"), 1, rouge);
glUniform4fv(glGetUniformLocation(_pId, "lcolor"), 1, blanc);
glUniform4fv(glGetUniformLocation(_pId, "lumpos"), 1, lumpos);
gl4dgDraw(_quadId);
glUseProgram(0);
}
void sortie(void) { gl4duClean(GL4DU_ALL); } void sortie(void) { gl4duClean(GL4DU_ALL); }

22
shaders/lights.fs Normal file
View file

@ -0,0 +1,22 @@
#version 330
in vec4 mvpos;
in vec3 N;
out vec4 fragColor;
uniform vec4 scolor;
uniform vec4 lcolor;
uniform vec4 lumpos;
void main() {
vec4 ambient = 0.15f * lcolor * scolor;
vec3 Ld = normalize(mvpos.xyz - lumpos.xyz);
float ild = clamp(dot(normalize(N), -normalize(Ld)), 0, 1);
vec4 diffus = (ild * lcolor) * scolor;
vec3 R = reflect(Ld, N);
vec3 Vue = vec3(0, 0, -1);
float ils = pow(clamp(dot(R, -Vue), 0, 1), 10);
vec4 spec = ils * lcolor;
fragColor = ambient + diffus + spec;
}

17
shaders/lights.vs Normal file
View file

@ -0,0 +1,17 @@
#version 330
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
uniform mat4 proj;
uniform mat4 mod;
uniform mat4 view;
out vec4 mvpos;
out vec3 N;
void main() {
N = (inverse(transpose(mod)) * vec4(normal, 0)).xyz;
mvpos = view * mod * vec4(pos, 1.0);
gl_Position = proj * mvpos;
}