pull/2/head
E. Almqvist 2 years ago
parent 9eb68445e2
commit b63391ff86
  1. BIN
      Hohmann
  2. 44
      src/main.cpp
  3. 8
      src/shaders.cpp

Binary file not shown.

@ -19,7 +19,7 @@ const char *fragShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n" "out vec4 FragColor;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" " FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
"}\0"; "}\0";
static float verts[] = { static float verts[] = {
@ -39,14 +39,13 @@ void processInput(GLFWwindow *win) {
} }
} }
void preRenderCallback(GLFWwindow *win) { struct RenderObj {
unsigned int VBO; unsigned int VBO;
glGenBuffers(1, &VBO); unsigned int VAO;
unsigned int shaderProg;
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);
RenderObj preRenderCallback() {
// Compile a bunch of shaders etc // Compile a bunch of shaders etc
unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource); unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource);
unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource); unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource);
@ -56,6 +55,19 @@ void preRenderCallback(GLFWwindow *win) {
unsigned int shaderProg; unsigned int shaderProg;
shaderProg = Shader::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) ); shaderProg = Shader::createProgram( std::vector<unsigned int> (std::begin(shaders), std::end(shaders)) );
// Vertex buffer object
unsigned int VBO;
glGenBuffers(1, &VBO);
// Vertex Array Object
unsigned int VAO;
glGenVertexArrays(1, &VAO); // gen the VAO
glBindVertexArray(VAO); // bind it
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);
// Copy verts into buffer // Copy verts into buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
@ -64,16 +76,16 @@ void preRenderCallback(GLFWwindow *win) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// Use the shader program whenever we want to render return RenderObj {VBO, VAO, shaderProg};
glUseProgram(shaderProg);
glDeleteShader(vertShader);
glDeleteShader(vertShader);
} }
void renderCallback(GLFWwindow *win) { void renderCallback(RenderObj ro) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(ro.shaderProg);
glBindVertexArray(ro.VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
} }
int main() { int main() {
@ -99,16 +111,14 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
preRenderCallback(win); RenderObj ro = preRenderCallback();
while (!glfwWindowShouldClose(win)) { while (!glfwWindowShouldClose(win)) {
// Handle input // Handle input
processInput(win); processInput(win);
// rendering // rendering
verts[1] += 0.0001f; renderCallback(ro);
printf("f=%f\n", verts[1]);
renderCallback(win);
// glfw // glfw
glfwSwapBuffers(win); glfwSwapBuffers(win);

@ -15,8 +15,6 @@ namespace Shader {
if (!success) { if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infolog); glGetShaderInfoLog(shader, 512, NULL, infolog);
printf("Shader Compilation Error:\n%s\n", infolog); printf("Shader Compilation Error:\n%s\n", infolog);
} else {
printf("Compiled shader %u!\n", shadertype);
} }
return shader; return shader;
@ -39,10 +37,12 @@ namespace Shader {
if (!success) { if (!success) {
glGetProgramInfoLog(shaderProg, 512, NULL, infolog); glGetProgramInfoLog(shaderProg, 512, NULL, infolog);
printf("Shader Program Linking Error:\n%s\n", infolog); printf("Shader Program Linking Error:\n%s\n", infolog);
} else {
printf("Linked shaders & created program: %u\n", shaderProg);
} }
// Delete the shaders when we are done
for(unsigned int s : shaders)
glDeleteShader(s);
return shaderProg; return shaderProg;
} }
} }

Loading…
Cancel
Save