diff --git a/main.c b/main.c index c94b212..19412fd 100644 --- a/main.c +++ b/main.c @@ -2,10 +2,13 @@ #include static void init(void); +static void resize(int, int); static void draw(void); static void sortie(void); static GLuint _wW = 1280, _wH = 720; +static GLuint _quadId = 0; +static GLuint _pId = 0; int main(int argc, char **argv) { if (!gl4duwCreateWindow(argc, argv, "Demo API8 2023", GL4DW_POS_CENTERED, @@ -17,14 +20,67 @@ int main(int argc, char **argv) { init(); atexit(sortie); + gl4duwResizeFunc(resize); gl4duwDisplayFunc(draw); gl4duwMainLoop(); return 0; } -void init(void) {} +void init(void) { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + _quadId = gl4dgGenQuadf(); + _pId = gl4duCreateProgram("shaders/lights.vs", "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); } diff --git a/shaders/lights.fs b/shaders/lights.fs new file mode 100644 index 0000000..9fee1c8 --- /dev/null +++ b/shaders/lights.fs @@ -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; +} diff --git a/shaders/lights.vs b/shaders/lights.vs new file mode 100644 index 0000000..a2e10de --- /dev/null +++ b/shaders/lights.vs @@ -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; +}