diff --git a/cosets/src/main.cpp b/cosets/src/main.cpp index e807fe6..692075b 100644 --- a/cosets/src/main.cpp +++ b/cosets/src/main.cpp @@ -1,11 +1,62 @@ -#include -#include +#include +#include -#include -#include +#include +#include +#include + +#include +#include +#include +#include using namespace std; +GLint build_shader(GLenum type, const string &name, const string &src) { + const char *c_str = src.c_str(); + + auto s = glCreateShader(type); + glShaderSource(s, 1, &c_str, nullptr); + glCompileShader(s); + + GLint stat; + glGetShaderiv(s, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLint log_len; + glGetShaderiv(s, GL_INFO_LOG_LENGTH, &log_len); + char log[log_len]; + glGetShaderInfoLog(s, log_len, nullptr, log); + fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); + + glDeleteShader(s); + return 0; + } + + return s; +} + +GLint build_program(const string &name, GLint vs, GLint fs) { + GLint p = glCreateProgram(); + glAttachShader(p, vs); + glAttachShader(p, fs); + glLinkProgram(p); + + GLint stat; + glGetProgramiv(p, GL_LINK_STATUS, &stat); + if (!stat) { + GLint log_len; + glGetShaderiv(p, GL_INFO_LOG_LENGTH, &log_len); + char log[log_len]; + glGetProgramInfoLog(p, log_len, nullptr, log); + fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); + + glDeleteProgram(p); + return 0; + } + + return p; +} + class Window { protected: GLFWwindow *_window = nullptr; @@ -62,61 +113,19 @@ public: class CosetsWindow : public Window { GLint program; - GLuint vao; - - GLint build_shader(GLenum type, const string &name, const string &src) { - const char *c_str = src.c_str(); - - auto s = glCreateShader(type); - glShaderSource(s, 1, &c_str, nullptr); - glCompileShader(s); - - GLint stat; - glGetShaderiv(s, GL_COMPILE_STATUS, &stat); - if (!stat) { - GLint log_len; - glGetShaderiv(s, GL_INFO_LOG_LENGTH, &log_len); - char log[log_len]; - glGetShaderInfoLog(s, log_len, nullptr, log); - fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); - - glDeleteShader(s); - return 0; - } - - return s; - } - - GLint build_program(const string &name, GLint vs, GLint fs) { - GLint p = glCreateProgram(); - glAttachShader(p, vs); - glAttachShader(p, fs); - glLinkProgram(p); - - GLint stat; - glGetProgramiv(p, GL_LINK_STATUS, &stat); - if (!stat) { - GLint log_len; - glGetShaderiv(p, GL_INFO_LOG_LENGTH, &log_len); - char log[log_len]; - glGetProgramInfoLog(p, log_len, nullptr, log); - fprintf(stderr, "SHADER LOG (%s):\n%s", name.c_str(), log); - - glDeleteProgram(p); - return 0; - } - - return p; - } + GLuint tri; public: void init() override { auto vs = build_shader(GL_VERTEX_SHADER, "vertex", "#version 400\n" + "" + "uniform mat4 proj;" + "" "void main(){" " float x = gl_VertexID % 2;" " float y = gl_VertexID / 2;" - " gl_Position = vec4(x, y, 0, 1);" + " gl_Position = proj * vec4(x, y, 0, 1);" "}"); auto fs = build_shader(GL_FRAGMENT_SHADER, "fragment", @@ -128,24 +137,41 @@ public: program = build_program("main", vs, fs); - glGenVertexArrays(1, &vao); + glGenVertexArrays(1, &tri); } void render() override { int w, h; getBounds(w, h); glViewport(0, 0, w, h); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + const glm::mat4 id = glm::mat4(1); + const glm::vec3 ax = glm::vec3(0, 0, 1); + double t = glfwGetTime(); + float angle = (float) t; + + float ar = (float) w / (float) h; + + const glm::mat4 proj = glm::ortho(-ar, ar, -1.f, 1.f, -1.f, 1.f); + const glm::mat4 view = glm::rotate(id, angle, ax); + const glm::mat4 mat = proj * view; - glBindVertexArray(vao); glUseProgram(program); - glDrawArrays(GL_TRIANGLES, 0, 3); + glUniformMatrix4fv(0, 1, false, glm::value_ptr(mat)); + glEnable(GL_DEPTH_TEST); + glEnable(GL_POINT_SMOOTH); + glPointSize(10.f); + + glBindVertexArray(tri); + glDrawArrays(GL_POINTS, 0, 3); swapbuffers(); } void deinit() override { glDeleteProgram(program); - glDeleteVertexArrays(1, &vao); + glDeleteVertexArrays(1, &tri); } };