pull/2/head
E. Almqvist 2 years ago
parent 8f560810fe
commit 9eb68445e2
  1. BIN
      Hohmann
  2. 52
      src/main.cpp
  3. 2
      src/shader_loader.cpp
  4. 48
      src/shaders.cpp

Binary file not shown.

@ -1,10 +1,27 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <vector>
#include "shaders.cpp"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
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";
static float verts[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
@ -22,16 +39,41 @@ void processInput(GLFWwindow *win) {
}
}
void renderCallback(GLFWwindow *win) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
void preRenderCallback(GLFWwindow *win) {
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); // for moving stuff
//glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
// 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);
}
int main() {
@ -57,6 +99,8 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
preRenderCallback(win);
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);

@ -1,2 +0,0 @@
#include <fstream>

@ -0,0 +1,48 @@
#include <fstream>
#include <glad/glad.h>
#include <vector>
namespace Shader {
unsigned int compile(GLenum shadertype, const char* shaderSource) {
unsigned int shader;
shader = glCreateShader(shadertype);
glShaderSource(shader, 1, &shaderSource, NULL);
glCompileShader(shader);
int success;
char infolog[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infolog);
printf("Shader Compilation Error:\n%s\n", infolog);
} else {
printf("Compiled shader %u!\n", shadertype);
}
return shader;
}
unsigned int createProgram(std::vector<unsigned int> shaders) {
unsigned int shaderProg;
shaderProg = glCreateProgram();
// Link the shaders
for(unsigned int s : shaders)
glAttachShader(shaderProg, s);
glLinkProgram(shaderProg);
// Check for errors
int success;
char infolog[512];
glGetProgramiv(shaderProg, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProg, 512, NULL, infolog);
printf("Shader Program Linking Error:\n%s\n", infolog);
} else {
printf("Linked shaders & created program: %u\n", shaderProg);
}
return shaderProg;
}
}
Loading…
Cancel
Save