186 lines
6.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include "lib.h"
#include "libs/cglm/cglm.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#include "stb_image.h"
int SCREEN_WIDTH = 1024;
int SCREEN_HEIGHT = 768;
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
(void)window;
glViewport(0, 0, width, height);
SCREEN_WIDTH = width;
SCREEN_HEIGHT = height;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main(void) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window =
checkPtr(glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Learn OpenGL", NULL, NULL),
"Error creating GLFW window");
if (window == NULL) glfwTerminate();
glfwMakeContextCurrent(window);
checkCode(gladLoadGLLoader((GLADloadproc)glfwGetProcAddress), "Error initializing GLAD");
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
GLuint shaderProgram =
compileShaderProgram("shaders/main.vert", "shaders/main.frag", NULL);
stbi_set_flip_vertically_on_load(true);
GLuint texture1, texture2;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
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);
int w1, h1, c1;
unsigned char* container = stbi_load("container.jpg", &w1, &h1, &c1, 0);
if (container == NULL) {
fprintf(stderr, "Failed to load image\n");
exit(-1);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w1, h1, 0, GL_RGB, GL_UNSIGNED_BYTE, container);
glGenerateMipmap(GL_TEXTURE_2D);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
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);
int w2, h2, c2;
unsigned char* smiley = stbi_load("awesomeface.png", &w2, &h2, &c2, 0);
if (smiley == NULL) {
fprintf(stderr, "Failed to load image\n");
exit(-1);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, smiley);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(container);
stbi_image_free(smiley);
float v1[] = {
// Position // Color // UV
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
GLuint vaos[2];
GLuint vbos[2];
unsigned int EBO;
glGenVertexArrays(2, vaos);
glGenBuffers(1, &EBO);
glGenBuffers(2, vbos);
glBindVertexArray(vaos[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(v1), v1, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6*sizeof(float)));
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(shaderProgram);
int t1Loc = glGetUniformLocation(shaderProgram, "t1");
glUniform1i(t1Loc, 0);
int t2Loc = glGetUniformLocation(shaderProgram, "t2");
glUniform1i(t2Loc, 1);
mat4 trans;
glm_mat4_identity(trans);
// glm_rotate(trans, GLM_PI_2, (vec3){0.0f, 0.0f, 1.0f});
glm_scale(trans, (vec3){0.5f, 0.5f, 0.5f});
mat4 trans2;
glm_mat4_identity(trans2);
glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(trans2, (vec3){-5.1f, 1.1f, 0.0f});
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) {
float currentFrame = (float)glfwGetTime();
float deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// float spin = (sin(timeValue) / 2.0f) + 0.5f;
printf("%f\n", deltaTime);
glm_rotate(trans, (float)deltaTime * -1.0f, (vec3){0.0f, 0.0f, 1.0f});
int transLoc = glGetUniformLocation(shaderProgram, "transform");
glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans[0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glBindVertexArray(vaos[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
mat4 trans2;
glm_mat4_identity(trans2);
glm_scale(trans2, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(trans2, (vec3){cos(currentFrame * 4.0f) * 5.0f,
sin(currentFrame * 4.0f) * 5.0f,
0.0f});
glUniformMatrix4fv(transLoc, 1, GL_FALSE, trans2[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(2, vaos);
glDeleteBuffers(2, vbos);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}