Lightweight OpenGL 3D Renderer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Euclid/src/main.cpp

114 lines
2.7 KiB

2 years ago
#include <glad/glad.h>
2 years ago
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
2 years ago
#include <stdio.h>
2 years ago
#include <vector>
2 years ago
#include <math.h>
2 years ago
#include "../headers/renderer.hpp"
2 years ago
#include "glm/trigonometric.hpp"
// #include "../headers/shaders.hpp"
// #include "../headers/textures.hpp"
2 years ago
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
2 years ago
#define VERT_SHADER_SRC_FILE "shaders/vertex.glsl"
#define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl"
2 years ago
2 years ago
#define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg"
2 years ago
void framebuffer_size_callback(GLFWwindow* win, int w, int h) {
glViewport(0, 0, w, h);
}
void processInput(GLFWwindow *win) {
int action = glfwGetKey(win, GLFW_KEY_ESCAPE);
if (action == GLFW_PRESS) {
glfwSetWindowShouldClose(win, true);
}
}
void renderCallback() {
// Make background
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
2 years ago
glClear(GL_COLOR_BUFFER_BIT);
}
2 years ago
int main() {
glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
2 years ago
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Hohmann", NULL, NULL);
if (win == NULL) {
2 years ago
printf("Failed to create a window.\n");
2 years ago
glfwTerminate();
return 1;
}
2 years ago
Renderer::Renderer3D renderer(win);
2 years ago
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
2 years ago
printf("Failed to init GLAD.\n");
2 years ago
return 1;
}
2 years ago
std::vector<float> verts({
2 years ago
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f
2 years ago
});
2 years ago
// Vert struc: x y z r g b tx ty
2 years ago
std::vector<unsigned int> indices({
0, 1, 3,
1, 2, 3
2 years ago
});
2 years ago
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
2 years ago
Renderer::RenderObject ro(verts, indices);
renderer.spawnObject(ro);
// ro.setTexture(RUSTY_METAL_TEXTURE);
2 years ago
2 years ago
// Window width & height
2 years ago
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);
// rendering
renderCallback();
/* OBJECT RENDERING */
float time = glfwGetTime();
float gVal = (sin(time) / 1.5f) + 0.5f;
// Transformation
float rotang = time;
glm::mat4 T = glm::mat4(1.0f);
2 years ago
T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0));
T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5));
ro.transform(T);
2 years ago
renderer.render();
2 years ago
// glfw
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
return 0;
}