-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMinGL.cpp
295 lines (255 loc) · 7.07 KB
/
MinGL.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "MinGL.h"
#include <glad/glad.h> // OpenGL functions
#include <GLFW/glfw3.h> // windows, contexts, input and events
#include <algorithm>
#include <iostream>
#include <vector>
const char* vertexShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource =
"#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec4 inColor;\n"
"void main()\n"
"{\n"
" FragColor = inColor;\n"
"}\0";
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
bool MinGL::init(unsigned width, unsigned height, const char* title)
{
// GLFW
//
// Setup window
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return false;
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!m_window)
{
fprintf(stderr, "Failed to create GLFW m_window\n");
return false;
}
glfwMakeContextCurrent(m_window);
glfwSwapInterval(1); // Enable vsync
glfwSetFramebufferSizeCallback(m_window, framebuffer_size_callback);
// GLAD
//
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
fprintf(stderr, "Failed to initialize GLAD\n");
return false;
}
float vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f
};
unsigned VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer
(
0, // Location = 0
3, // Size of vertex attribute
GL_FLOAT, // Type of the data
GL_FALSE, // Normalize data?
3 * sizeof(float), // Stride
(void*)0 // Offset
);
glEnableVertexAttribArray(0/*Location*/);
glBindVertexArray(0);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
fprintf(stderr, "%s", infoLog);
return false;
}
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
fprintf(stderr, "%s", infoLog);
return false;
}
m_shaderProgram = glCreateProgram();
glAttachShader(m_shaderProgram, vertexShader);
glAttachShader(m_shaderProgram, fragmentShader);
glLinkProgram(m_shaderProgram);
glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(m_shaderProgram, 512, nullptr, infoLog);
fprintf(stderr, "%s", infoLog);
return false;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUseProgram(m_shaderProgram);
glBindVertexArray(VAO);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
return true;
}
bool MinGL::windowShouldClose() const
{
return (bool)glfwWindowShouldClose(m_window);
}
void MinGL::pollEvents() const
{
glfwPollEvents();
}
void MinGL::processInput() const
{
if (glfwGetKey(m_window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(m_window, true);
}
void MinGL::putPixel(int x, int y, const MinGLColor& color, int width, int height) const
{
GLint transformLoc = glGetUniformLocation(m_shaderProgram, "inColor");
glUniform4f(transformLoc, color.rgba[0], color.rgba[1], color.rgba[2], color.rgba[3]);
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height); /// position of pixel
glDrawArrays(GL_TRIANGLES, 0/*Starting Index*/, 6/*# of vertices*/);
glDisable(GL_SCISSOR_TEST);
}
MinGLColor MinGL::getPixelColor(int x, int y) const{
constexpr int pixelSize = 4; // RGBA has 4 components (R, G, B, A)
std::vector<unsigned char> pixel(pixelSize);
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel.data());
const MinGLColor color{(float)pixel[0]/255, (float)pixel[1]/255, (float)pixel[2]/255, 1.0f};
return color;
}
void MinGL::drawLine(int x0, int y0, int x1, int y1, const MinGLColor& color) const{
// Bresenham's line generation algorithm is used here
bool steep = false;
if (std::abs(x0 - x1) < std::abs(y0 - y1)) {
std::swap(x0, y0);
std::swap(x1, y1);
steep = true;
}
if (x0 > x1) {
std::swap(x0, x1);
std::swap(y0, y1);
}
const int dx = x1 - x0;
const int dy = y1 - y0;
int derror = std::abs(dy) * 2;
int error = 0;
int y = y0;
for (int x = x0; x <= x1; ++x) {
if (steep) {
putPixel(y, x, color);
}
else {
putPixel(x, y, color);
}
error += derror;
if (error > dx) {
y += (y1 > y0 ? 1 : -1);
error -= dx * 2;
}
}
}
void MinGL::drawRectangle(int x0, int y0, int x1, int y1, const MinGLColor& color) const{
// specify any two diagonally opposite points of the rectangle
// draws a rectangle with sides parallel to screen
const int xa = std::min(x0, x1);
const int ya = std::min(y0, y1);
const int xb = std::max(x0, x1);
const int yb = std::max(y0, y1);
for(int i=xa; i<xb; i++){
putPixel(i, ya, color);
}
for(int i=ya; i<yb; i++){
putPixel(xb, i, color);
}
for(int i=xb; i>xa; i--){
putPixel(i, yb, color);
}
for(int i=yb; i>ya; i--){
putPixel(xa, i, color);
}
}
void MinGL::drawCircle(int xc, int yc, int rad, const MinGLColor& color) const{
// specify center (x,y) of circle and its radius
// Bresenham's circle drawing algorithm is used here
auto draw = [this, xc, yc, &color](int x, int y){
putPixel(xc+x, yc+y, color);
putPixel(xc-x, yc+y, color);
putPixel(xc+x, yc-y, color);
putPixel(xc-x, yc-y, color);
putPixel(xc+y, yc+x, color);
putPixel(xc-y, yc+x, color);
putPixel(xc+y, yc-x, color);
putPixel(xc-y, yc-x, color);
};
int x = 0, y = rad;
int d = 3 - 2 * rad;
draw(x, y);
while(y >= x){
x++;
if (d > 0){
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
draw(x, y);
}
}
void MinGL::flush(float r, float g, float b, float a)
{
glfwGetFramebufferSize(m_window, &m_displayW, &m_displayH);
glfwSwapBuffers(m_window);
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
}
void MinGL::flush(const MinGLColor& color)
{
flush(color.rgba[0], color.rgba[1], color.rgba[2], color.rgba[3]);
}
void MinGL::shutdown() const
{
glfwDestroyWindow(m_window);
glfwTerminate();
}
GLFWwindow* MinGL::getWindow()
{
return m_window;
}