208 lines
6.9 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;
vec2 direction = {0};
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);
direction[0] = 0;
direction[1] = 0;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
direction[0] = 1;
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
direction[0] = -1;
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
direction[1] = -1;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
direction[1] = 1;
}
}
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);
glEnable(GL_DEPTH_TEST);
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(cubeVerts), cubeVerts, 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, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * 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 view;
glm_mat4_identity(view);
glm_translate(view, (vec3){0.0f, 0.0f, -0.6f});
mat4 projection;
glm_mat4_identity(projection);
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
glm_perspective(45.0f, aspectRatio, 0.1f, 100.0f, projection);
int cubeCount = 10;
vec3 cubePositions[cubeCount];
srand(arc4random());
for (int i = 0; i < cubeCount; i++) {
float x = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
float y = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
float z = ((float)rand()/(float)(RAND_MAX)) * 5.0f - 2.5f;
cubePositions[i][0] = x;
cubePositions[i][1] = y;
cubePositions[i][2] = z;
}
float lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) {
float currentFrame = (float)glfwGetTime();
float dt = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// float spin = (sin(currentFrame) / 2.0f) + 0.5f;
// printf("%f\n", deltaTime);
float speed = 0.5 * dt;
glm_translate(view, (vec3){direction[0] * speed, 0.0f, direction[1] * speed});
int matrixLocation;
matrixLocation = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, view[0]);
matrixLocation = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, projection[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);
for (int i = 0; i < cubeCount; i++) {
mat4 model;
glm_mat4_identity(model);
glm_scale(model, (vec3){0.1f, 0.1f, 0.1f});
glm_translate(model, cubePositions[i]);
glm_rotate(model, (i+1) * currentFrame, (vec3){1.0f, 1.0f, 0.0f});
matrixLocation = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(matrixLocation, 1, GL_FALSE, model[0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(2, vaos);
glDeleteBuffers(2, vbos);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}