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"
"void main()\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";
static float verts[] = {
@ -39,14 +39,13 @@ void processInput(GLFWwindow *win) {
}
}
void preRenderCallback(GLFWwindow *win) {
struct RenderObj {
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);
unsigned int VAO;
unsigned int shaderProg;
};
RenderObj preRenderCallback() {
// Compile a bunch of shaders etc
unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource);
unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource);
@ -56,6 +55,19 @@ void preRenderCallback(GLFWwindow *win) {
unsigned int shaderProg;
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
glBindBuffer(GL_ARRAY_BUFFER, VBO);
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);
glEnableVertexAttribArray(0);
// Use the shader program whenever we want to render
glUseProgram(shaderProg);
glDeleteShader(vertShader);
glDeleteShader(vertShader);
return RenderObj {VBO, VAO, shaderProg};
}
void renderCallback(GLFWwindow *win) {
void renderCallback(RenderObj ro) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(ro.shaderProg);
glBindVertexArray(ro.VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
int main() {
@ -99,16 +111,14 @@ int main() {
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glfwSetFramebufferSizeCallback(win, framebuffer_size_callback);
preRenderCallback(win);
RenderObj ro = preRenderCallback();
while (!glfwWindowShouldClose(win)) {
// Handle input
processInput(win);
// rendering
verts[1] += 0.0001f;
printf("f=%f\n", verts[1]);
renderCallback(win);
renderCallback(ro);
// glfw
glfwSwapBuffers(win);

@ -15,8 +15,6 @@ namespace Shader {
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, infolog);
printf("Shader Compilation Error:\n%s\n", infolog);
} else {
printf("Compiled shader %u!\n", shadertype);
}
return shader;
@ -39,10 +37,12 @@ namespace Shader {
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);
}
// Delete the shaders when we are done
for(unsigned int s : shaders)
glDeleteShader(s);
return shaderProg;
}
}

Loading…
Cancel
Save