source: opengl-game/new-game.cpp@ be246ad

feature/imgui-sdl points-test
Last change on this file since be246ad was be246ad, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 7 years ago

Enable OpenGL 4 on OSX

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