-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopengl.cpp
181 lines (158 loc) · 5.33 KB
/
opengl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <cstring>
#include <cstdio>
#include <thread>
#include <vector>
#include <GL/glew.h>
#include <GL/gl.h>
#include "simple/graphical/initializer.h"
#include "simple/interactive/initializer.h"
#include "simple/interactive/event.h"
#include "simple/graphical/software_window.h"
#include "simple/graphical/gl_window.h"
#include "simple/graphical/algorithm.h"
using namespace std::chrono_literals;
using namespace simple;
using namespace graphical::color_literals;
using graphical::int2;
using graphical::range2D;
using graphical::float2;
using graphical::anchored_rect;
using graphical::gl_window;
int main() try
{
graphical::initializer graphics;
interactive::initializer input;
gl_window::global.require<gl_window::attribute::major_version>(2);
gl_window::global.require<gl_window::attribute::minor_version>(1);
gl_window::global.request<gl_window::attribute::stencil>(8);
gl_window win("opengl", int2(600, 600), gl_window::flags::borderless);
win.require_vsync(gl_window::vsync_mode::enabled);
float2 win_size(win.size());
glewInit();
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
// Compile Vertex Shader
char const * VertexSourcePointer = R"shader(
#version 120
#extension GL_ARB_explicit_attrib_location : require
layout(location = 0) in vec3 vertexPosition_modelspace;
void main()
{
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
)shader";
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , nullptr);
glCompileShader(VertexShaderID);
// Check Vertex Shader
GLint Result = GL_FALSE;
int InfoLogLength;
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, VertexShaderErrorMessage.data());
printf("%s\n", VertexShaderErrorMessage.data());
}
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Compile Fragment Shader
char const * FragmentSourcePointer = R"shader(
#version 120
#extension GL_ARB_explicit_attrib_location : require
out vec3 color;
void main()
{
color = vec3(1,0,0);
}
)shader";
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, FragmentShaderErrorMessage.data());
printf("%s\n", FragmentShaderErrorMessage.data());
}
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if ( InfoLogLength > 0 ){
std::vector<char> ProgramErrorMessage(InfoLogLength+1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ProgramErrorMessage.data());
printf("%s\n", ProgramErrorMessage.data());
}
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
glClearColor(0,0,0.4,0);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(ProgramID);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
auto vertex_data = geom::vector {
geom::vector(-1.0f, -1.0f, 0.0f),
geom::vector(1.0f, -1.0f, 0.0f),
geom::vector(0.0f, 1.0f, 0.0f),
};
vertex_data /= 5.f;
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
bool done = false;
const auto x = geom::vector(1.f, 0.f, 0.f);
while(!done)
{
using namespace interactive;
while(auto e = next_event())
{
std::visit(support::overloaded
{
[&](quit_request){ done = true; },
[](auto&&){}
}, *e);
}
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), &vertex_data, GL_STATIC_DRAW);
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
if(vertex_data[2] >= x)
{
vertex_data -= x * 2;
}
vertex_data += x * 0.03;
win.update();
// std::this_thread::sleep_for(16ms);
}
glDeleteBuffers(1, &vertexbuffer);
glDeleteVertexArrays(1, &VertexArrayID);
return 0;
}
catch(...)
{
if(errno)
std::puts(std::strerror(errno));
throw;
}