2023-09-08 01:11:35 +02:00
|
|
|
#include <stdio.h>
|
2023-09-08 16:38:14 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#include <glad/gl.h>
|
|
|
|
|
#include "3rdparty/stb/stb_image.h"
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
#include "shader.h"
|
|
|
|
|
|
|
|
|
|
static const float quad_vertices[] = {
|
2023-09-08 16:38:14 +02:00
|
|
|
// positions // texture coords
|
|
|
|
|
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top left
|
|
|
|
|
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top right
|
|
|
|
|
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom left
|
|
|
|
|
1.0f, -1.0f, 0.0f, 1.0f, 0.0f // bottom right
|
2023-09-03 00:51:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const int quad_indices[] = {
|
|
|
|
|
0, 1, 2,
|
|
|
|
|
1, 2, 3
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
void set_scale_factor(int width, int height, float *x, float *y) {
|
|
|
|
|
if (width > height) {
|
|
|
|
|
float ratio = (float)height / (float)width;
|
|
|
|
|
*x = ratio;
|
|
|
|
|
*y = 1.f;
|
|
|
|
|
} else {
|
|
|
|
|
float ratio = (float)width / (float)height;
|
|
|
|
|
*x = 1.f;
|
|
|
|
|
*y = ratio;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int prepare_bg(bool use_texture) {
|
2023-09-03 00:51:45 +03:00
|
|
|
unsigned int vbo, vao, ebo;
|
|
|
|
|
glGenVertexArrays(1, &vao);
|
|
|
|
|
glGenBuffers(1, &vbo);
|
|
|
|
|
glGenBuffers(1, &ebo);
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(vao);
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_indices), quad_indices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
2023-09-08 16:38:14 +02:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
|
|
|
|
|
|
|
|
|
if (use_texture) {
|
|
|
|
|
unsigned int texture;
|
|
|
|
|
int tex_width = 0, tex_height = 0;
|
|
|
|
|
unsigned char *tex_data = NULL;
|
|
|
|
|
|
|
|
|
|
glGenTextures(1, &texture);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
|
|
tex_data = stbi_load("assets/sudoku-grid.png", &tex_width, &tex_height, NULL, 0);
|
|
|
|
|
if (tex_data) {
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data);
|
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
|
|
stbi_image_free(tex_data);
|
|
|
|
|
} else {
|
|
|
|
|
printf("[ERROR] could not load texture image\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(0);
|
2023-09-03 00:51:45 +03:00
|
|
|
|
|
|
|
|
return vao;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
void draw_bg_grid_shader(Shader shader, int vao, float* transform, float resolution) {
|
2023-09-03 00:51:45 +03:00
|
|
|
use_shader(shader);
|
|
|
|
|
|
2023-09-08 01:51:09 +02:00
|
|
|
set_mat4(shader, "transform", transform);
|
2023-09-08 16:38:14 +02:00
|
|
|
set_float(shader, "resolution", resolution);
|
2023-09-04 01:57:39 +03:00
|
|
|
|
2023-09-03 00:51:45 +03:00
|
|
|
glBindVertexArray(vao);
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
2023-09-08 01:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
void draw_bg_grid_texture(Shader shader, int vao, float* transform) {
|
2023-09-08 01:11:35 +02:00
|
|
|
use_shader(shader);
|
2023-09-08 01:51:09 +02:00
|
|
|
set_mat4(shader, "transform", transform);
|
2023-09-08 01:11:35 +02:00
|
|
|
|
2023-09-08 16:38:14 +02:00
|
|
|
glBindVertexArray(vao);
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
2023-09-03 00:51:45 +03:00
|
|
|
}
|