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

121 lines
2.9 KiB

2 years ago
#include <glad/glad.h>
2 years ago
#include <GLFW/glfw3.h>
2 years ago
#include <stdio.h>
2 years ago
#include <vector>
#include "shaders.cpp"
2 years ago
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
2 years ago
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\0";
2 years ago
static float verts[] = {
2 years ago
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
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);
}
}
2 years ago
void preRenderCallback(GLFWwindow *win) {
2 years ago
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
2 years ago
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
2 years ago
// Compile a bunch of shaders etc
unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource);
unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource);
unsigned int shaders[2] = {vertShader, fragShader};
// Create a shader program & link the shaders etc
unsigned int shaderProg;
shaderProg = Shader::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) );
// Copy verts into buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
// Set attrib pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Use the shader program whenever we want to render
glUseProgram(shaderProg);
glDeleteShader(vertShader);
glDeleteShader(vertShader);
}
void renderCallback(GLFWwindow *win) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
2 years ago
}
int main() {
2 years ago
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
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;
}
glfwMakeContextCurrent(win);
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) {
2 years ago
printf("Failed to init GLAD.\n");
2 years ago
return 1;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
2 years ago
preRenderCallback(win);
2 years ago
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);
// rendering
2 years ago
verts[1] += 0.0001f;
printf("f=%f\n", verts[1]);
2 years ago
renderCallback(win);
// glfw
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
return 0;
}