source: opengl-game/new-game.cpp@ 772d8c7

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

Add fullscreen and anti-aliasing support and some more logging

  • Property mode set to 100644
File size: 3.2 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
11const bool FULLSCREEN = true;
12
13void glfw_error_callback(int error, const char* description) {
14 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description);
15}
16
17int main(int argc, char* argv[]) {
18 cout << "New OpenGL Game" << endl;
19
20 if (!restart_gl_log()) {}
21 gl_log("starting GLFW\n%s\n", glfwGetVersionString());
22
23 glfwSetErrorCallback(glfw_error_callback);
24 if (!glfwInit()) {
25 fprintf(stderr, "ERROR: could not start GLFW3\n");
26 return 1;
27 }
28
29#ifdef __APPLE__
30 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
31 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
32 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
33 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
34#endif
35
36 glfwWindowHint(GLFW_SAMPLES, 4);
37
38 GLFWwindow* window = NULL;
39
40 if (FULLSCREEN) {
41 GLFWmonitor* mon = glfwGetPrimaryMonitor();
42 const GLFWvidmode* vmode = glfwGetVideoMode(mon);
43 window = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init", mon, NULL);
44 } else {
45 window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
46 }
47
48 if (!window) {
49 fprintf(stderr, "ERROR: could not open window with GLFW3\n");
50 glfwTerminate();
51 return 1;
52 }
53 glfwMakeContextCurrent(window);
54 glewExperimental = GL_TRUE;
55 glewInit();
56
57 const GLubyte* renderer = glGetString(GL_RENDERER);
58 const GLubyte* version = glGetString(GL_VERSION);
59 printf("Renderer: %s\n", renderer);
60 printf("OpenGL version supported %s\n", version);
61 glEnable(GL_DEPTH_TEST);
62 glDepthFunc(GL_LESS);
63
64 GLfloat points[] = {
65 0.0f, 0.5f, 0.0f,
66 0.5f, -0.5f, 0.0f,
67 -0.5f, -0.5f, 0.0f,
68 };
69
70 GLuint vbo = 0;
71 glGenBuffers(1, &vbo);
72 glBindBuffer(GL_ARRAY_BUFFER, vbo);
73 glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
74
75 GLuint vao = 0;
76 glGenVertexArrays(1, &vao);
77 glBindVertexArray(vao);
78 glEnableVertexAttribArray(0);
79 glBindBuffer(GL_ARRAY_BUFFER, vbo);
80 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
81
82 const char* vertex_shader =
83 "#version 410\n"
84 "in vec3 vp;"
85 "void main() {"
86 " gl_Position = vec4(vp, 1.0);"
87 "}";
88
89 GLuint vs = glCreateShader(GL_VERTEX_SHADER);
90 glShaderSource(vs, 1, &vertex_shader, NULL);
91 glCompileShader(vs);
92
93 const char* fragment_shader =
94 "#version 410\n"
95 "out vec4 frag_color;"
96 "void main() {"
97 " frag_color = vec4(0.5, 0.0, 0.5, 1.0);"
98 "}";
99
100 GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
101 glShaderSource(fs, 1, &fragment_shader, NULL);
102 glCompileShader(fs);
103
104 GLuint shader_program = glCreateProgram();
105 glAttachShader(shader_program, vs);
106 glAttachShader(shader_program, fs);
107 glLinkProgram(shader_program);
108
109 while (!glfwWindowShouldClose(window)) {
110 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111 glUseProgram(shader_program);
112 glBindVertexArray(vao);
113 glDrawArrays(GL_TRIANGLES, 0, 3);
114
115 glfwPollEvents();
116 glfwSwapBuffers(window);
117
118 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) {
119 glfwSetWindowShouldClose(window, 1);
120 }
121 }
122
123 glfwTerminate();
124 return 0;
125}
Note: See TracBrowser for help on using the repository browser.