learn opengl

This commit is contained in:
Debucquoy Anthony 2023-05-31 23:01:19 +02:00
parent 763df573d8
commit 56daae2c9e
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
8 changed files with 8077 additions and 13 deletions

View File

@ -2,8 +2,8 @@
set -xe set -xe
CFLAGS="-Wall -Wextra $(pkg-config --cflags sdl2 glew)" CFLAGS="-g -Wall -Wextra $(pkg-config --cflags sdl2 glew)"
LIBS="$(pkg-config --libs sdl2 glew)" LIBS="$(pkg-config --libs sdl2 glew) -lm"
# gcc -Wall -Wextra -o gameoflife_term gameoflife_term.c # gcc -Wall -Wextra -o gameoflife_term gameoflife_term.c
gcc ${CFLAGS} -o gameoflife_graph gameoflife_graph.c ${LIBS} gcc ${CFLAGS} -o opengl_train opengl_train.c ${LIBS}

BIN
main

Binary file not shown.

View File

@ -1,11 +1,12 @@
#include <SDL2/SDL_events.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h> #include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_opengl_glext.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define FACTOR 80 #define FACTOR 80
#define WIDTH ((FACTOR) * 16) #define WIDTH ((FACTOR) * 16)
#define HEIGHT ((FACTOR) * 9) #define HEIGHT ((FACTOR) * 9)
@ -13,9 +14,16 @@
SDL_Event e; SDL_Event e;
float vertices[] = { float vertices[] = {
0.0f, 0.5f, // Positions Color TexCoords
0.5f, -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
-0.5f, -0.5f 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left
};
GLuint elements[] = {
0, 1, 2,
2, 3, 0,
}; };
char * read_file(const char* path){ char * read_file(const char* path){
@ -88,26 +96,76 @@ int main(void)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
// vectors
GLuint vertexBuffer; GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer); glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// reuse vectors
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
GLuint vertexShader = CompileShader(vs, GL_VERTEX_SHADER); GLuint vertexShader = CompileShader(vs, GL_VERTEX_SHADER);
GLuint fragmentShader = CompileShader(fs, GL_FRAGMENT_SHADER); GLuint fragmentShader = CompileShader(fs, GL_FRAGMENT_SHADER);
GLuint shaderProgram = LinkShader(vertexShader, fragmentShader); GLuint shaderProgram = LinkShader(vertexShader, fragmentShader);
// Textures
int width, height;
unsigned char *image, *image2;
GLuint tex[2];
glGenTextures(2, tex);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex[0]);
//Load image
image = stbi_load("./sample.png", &width, &height, NULL, 3);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
stbi_image_free(image);
glGenerateMipmap(GL_TEXTURE_2D);
glUniform1i(glGetUniformLocation(shaderProgram, "kitty"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex[1]);
//Load image
image2 = stbi_load("./sample2.png", &width, &height, NULL, 3);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
stbi_image_free(image2);
glGenerateMipmap(GL_TEXTURE_2D);
glUniform1i(glGetUniformLocation(shaderProgram, "puppy"), 1);
GLint posAttrib = glGetAttribLocation(shaderProgram, "positions"); GLint posAttrib = glGetAttribLocation(shaderProgram, "positions");
glEnableVertexAttribArray(posAttrib); glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, 0);
GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (void*)(2*sizeof(float)));
GLint texCoordsAttrib = glGetAttribLocation(shaderProgram, "texCoords");
glEnableVertexAttribArray(texCoordsAttrib);
glVertexAttribPointer(texCoordsAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (void*)(5*sizeof(float)));
glUseProgram(shaderProgram);
int i = 0;
bool should_close = false; bool should_close = false;
while(!should_close){ while(!should_close){
i++;
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUniform1i(glGetUniformLocation(shaderProgram, "triangleColor"), i);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/* glDrawArrays(GL_TRIANGLES, 0, 3); */
SDL_GL_SwapWindow(win); SDL_GL_SwapWindow(win);
@ -121,8 +179,6 @@ int main(void)
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
glDeleteBuffers(1, &vertexBuffer);
SDL_GL_DeleteContext(con); SDL_GL_DeleteContext(con);
SDL_DestroyWindow(win); SDL_DestroyWindow(win);
SDL_Quit(); SDL_Quit();

BIN
sample.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

BIN
sample2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

View File

@ -1,7 +1,19 @@
#version 330 core #version 330 core
in vec3 Color;
in vec2 TexCoords;
uniform int triangleColor;
uniform sampler2D kitty;
uniform sampler2D puppy;
out vec4 outColor; out vec4 outColor;
void main(){ void main(){
outColor = vec4(1.0, 1.0, 1.0, 1.0); vec4 kit = texture(kitty, TexCoords);
vec4 pup = texture(puppy, TexCoords);
outColor = pup;
} }
/* vim: set ft=glsl: */

View File

@ -1,7 +1,16 @@
#version 330 core #version 330 core
in vec2 positions; in vec2 positions;
in vec3 color;
in vec2 texCoords;
out vec3 Color;
out vec2 TexCoords;
void main(){ void main(){
Color = color;
TexCoords = texCoords;
gl_Position = vec4(positions, 0.0, 1.0); gl_Position = vec4(positions, 0.0, 1.0);
} }
// vim: set ft=glsl:

7987
stb_image.h Normal file

File diff suppressed because it is too large Load Diff