Created
March 13, 2017 01:28
-
-
Save mathewmariani/f301eb8cf3f4156055c2ad6e5674a5c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C/C++ | |
#include <fstream> | |
#include <iostream> | |
#include <math.h> | |
// OpenGL | |
#include "opengl.h" | |
// utility | |
#include "matrix4.h" | |
#include "shader.h" | |
// constant variables | |
constexpr int width = 800; | |
constexpr int height = 800; | |
// buffer objects | |
GLuint vao, vbo; | |
// variables | |
int pixelWidth, pixelHeight; | |
std::vector<Vertex> user_points; | |
// vertex shader | |
std::string std_vertex_shader = | |
"#version 330 core\n" \ | |
"layout(location = 0) in vec3 in_Vertex;" \ | |
"uniform mat4 uf_Projection;" \ | |
"uniform mat4 uf_Model;" \ | |
"uniform vec3 uf_Color;" \ | |
"out vec4 var_Color;" \ | |
"void main() {" \ | |
"var_Color = vec4(uf_Color.xyz, 1.0);" \ | |
"gl_Position = (uf_Projection * uf_Model) * vec4(in_Vertex.xyz, 1.0);" \ | |
"}"; | |
// fragment shader | |
std::string std_fragment_shader = | |
"#version 330 core\n" \ | |
"in vec4 var_Color;" \ | |
"out vec4 frag_color;" \ | |
"void main() {" \ | |
"frag_color = var_Color;" \ | |
"}"; | |
// managed resources | |
GLFWwindow* window; | |
// callback forward declarations | |
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode); | |
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); | |
void setFramebufferSizeCallback(GLFWwindow* window, int w, int h); | |
int main() { | |
// initialize GLFW | |
glfwInit(); | |
// set window flags | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); | |
// create window | |
window = glfwCreateWindow(width, height, "Assignment 2", nullptr, nullptr); | |
if (window == nullptr) { | |
printf("Failed to create GLFW window\n"); | |
glfwTerminate(); | |
return -1; | |
} | |
glfwMakeContextCurrent(window); | |
// initializw gl3w | |
gl3wInit(); | |
// set callbacks | |
glfwSetKeyCallback(window, keyCallback); | |
glfwSetMouseButtonCallback(window, mouseButtonCallback); | |
glfwSetFramebufferSizeCallback(window, setFramebufferSizeCallback); | |
// define the viewport dimensions | |
glfwGetFramebufferSize(window, &pixelWidth, &pixelHeight); | |
gl.setViewport(0, 0, pixelWidth, pixelHeight); | |
// make stuff more easily visible | |
glPointSize(10); | |
// compile shader | |
Shader defaultShader({ | |
std_vertex_shader, std_fragment_shader | |
}); | |
// modern OpenGL only requires a single vao | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glGenBuffers(1, &vbo); | |
// main loop | |
while (!glfwWindowShouldClose(window)) { | |
// poll for events | |
glfwPollEvents(); | |
// clear the buffer | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
// attach shader | |
defaultShader.attach(); | |
defaultShader.setMatrix4("uf_Projection", gl.matrices.projection.back()); | |
defaultShader.setMatrix4("uf_Model", Matrix4()); | |
if (!user_points.empty()) { | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * user_points.size(), &user_points[0], GL_STATIC_DRAW); | |
// setup attribute buffer | |
glEnableVertexAttribArray(0); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); | |
// draw our points | |
glDrawArrays(GL_POINTS, 0, user_points.size()); | |
glDisableVertexAttribArray(0); | |
} | |
// swap the screen buffers | |
glfwSwapBuffers(window); | |
} | |
// terminate glfw, and managed resources | |
glfwTerminate(); | |
return 0; | |
} | |
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode) { | |
switch (key) { | |
case GLFW_KEY_ESCAPE: | |
glfwSetWindowShouldClose(window, GL_TRUE); | |
break; | |
default: | |
break; | |
} | |
} | |
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { | |
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { | |
double xpos, ypos; | |
glfwGetCursorPos(window, &xpos, &ypos); | |
user_points.push_back({ (float)xpos, (float)ypos, 0.0f }); | |
} | |
} | |
void setFramebufferSizeCallback(GLFWwindow* window, int w, int h) { | |
glfwGetFramebufferSize(window, &pixelWidth, &pixelHeight); | |
gl.setViewport(0, 0, pixelWidth, pixelHeight); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment