1 | #include "logger.h"
|
---|
2 |
|
---|
3 | #include <GL/glew.h>
|
---|
4 | #include <GLFW/glfw3.h>
|
---|
5 |
|
---|
6 | #include <cstdio>
|
---|
7 | #include <iostream>
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | int main(int argc, char* argv[]) {
|
---|
12 | cout << "New OpenGL Game" << endl;
|
---|
13 |
|
---|
14 | restart_gl_log();
|
---|
15 |
|
---|
16 | if (!glfwInit()) {
|
---|
17 | fprintf(stderr, "ERROR: could not start GLFW3\n");
|
---|
18 | return 1;
|
---|
19 | }
|
---|
20 |
|
---|
21 | GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
|
---|
22 | if (!window) {
|
---|
23 | fprintf(stderr, "ERROR: could not open window with GLFW3\n");
|
---|
24 | glfwTerminate();
|
---|
25 | return 1;
|
---|
26 | }
|
---|
27 | glfwMakeContextCurrent(window);
|
---|
28 | glewExperimental = GL_TRUE;
|
---|
29 | glewInit();
|
---|
30 |
|
---|
31 | const GLubyte* renderer = glGetString(GL_RENDERER);
|
---|
32 | const GLubyte* version = glGetString(GL_VERSION);
|
---|
33 | printf("Renderer: %s\n", renderer);
|
---|
34 | printf("OpenGL version supported %s\n", version);
|
---|
35 | glEnable(GL_DEPTH_TEST);
|
---|
36 | glDepthFunc(GL_LESS);
|
---|
37 |
|
---|
38 | GLfloat points[] = {
|
---|
39 | 0.0f, 0.5f, 0.0f,
|
---|
40 | 0.5f, -0.5f, 0.0f,
|
---|
41 | -0.5f, -0.5f, 0.0f,
|
---|
42 | };
|
---|
43 |
|
---|
44 | GLuint vbo = 0;
|
---|
45 | glGenBuffers(1, &vbo);
|
---|
46 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
47 | glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
|
---|
48 |
|
---|
49 | GLuint vao = 0;
|
---|
50 | glGenVertexArrays(1, &vao);
|
---|
51 | glBindVertexArray(vao);
|
---|
52 | glEnableVertexAttribArray(0);
|
---|
53 | glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
---|
54 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
---|
55 |
|
---|
56 | const char* vertex_shader =
|
---|
57 | "#version 410\n"
|
---|
58 | "in vec3 vp;"
|
---|
59 | "void main() {"
|
---|
60 | " gl_Position = vec4(vp, 1.0);"
|
---|
61 | "}";
|
---|
62 |
|
---|
63 | GLuint vs = glCreateShader(GL_VERTEX_SHADER);
|
---|
64 | glShaderSource(vs, 1, &vertex_shader, NULL);
|
---|
65 | glCompileShader(vs);
|
---|
66 |
|
---|
67 | const char* fragment_shader =
|
---|
68 | "#version 410\n"
|
---|
69 | "out vec4 frag_color;"
|
---|
70 | "void main() {"
|
---|
71 | " frag_color = vec4(0.5, 0.0, 0.5, 1.0);"
|
---|
72 | "}";
|
---|
73 |
|
---|
74 | GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
75 | glShaderSource(fs, 1, &fragment_shader, NULL);
|
---|
76 | glCompileShader(fs);
|
---|
77 |
|
---|
78 | GLuint shader_program = glCreateProgram();
|
---|
79 | glAttachShader(shader_program, vs);
|
---|
80 | glAttachShader(shader_program, fs);
|
---|
81 | glLinkProgram(shader_program);
|
---|
82 |
|
---|
83 | while (!glfwWindowShouldClose(window)) {
|
---|
84 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
85 | glUseProgram(shader_program);
|
---|
86 | glBindVertexArray(vao);
|
---|
87 | glDrawArrays(GL_TRIANGLES, 0, 3);
|
---|
88 | glfwPollEvents();
|
---|
89 | glfwSwapBuffers(window);
|
---|
90 | }
|
---|
91 |
|
---|
92 | glfwTerminate();
|
---|
93 | return 0;
|
---|
94 | }
|
---|