source: opengl-game/new-game.cpp@ 644a2e4

feature/imgui-sdl points-test
Last change on this file since 644a2e4 was 644a2e4, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Finish the triangle drawing program

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