-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAngel.cpp
125 lines (104 loc) · 3.15 KB
/
Angel.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
#include "includes/Angel.hpp"
#include <glad/glad.h>
#include <iostream>
int Angel::m_width = 1280;
int Angel::m_height = 720;
unsigned int Angel::m_ID = 0;
unsigned int Angel::m_shader_ID = 0;
const char *vShaderSource = R"glsl(
#version 330 core
layout (location = 0) in vec3 aPos;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
)glsl";
const char *fShaderSource = R"glsl(
#version 330 core
out vec4 FragColor;
uniform vec4 color;
void main(){
FragColor = vec4(color);
}
)glsl";
void Angel::init(unsigned int width = Angel::m_width,
unsigned int height = Angel::m_height) {
const float vertices[] = {
-1.0f, -1.0f, 0.0f, // bottom left
1.0f, -1.0f, 0.0f, // bottom right
1.0f, 1.0f, 0.0f, // top right
1.0f, 1.0f, 0.0f, // top right
-1.0f, 1.0f, 0.0f, // top left
-1.0f, -1.0f, 0.0f, // bottom left
};
unsigned int VAO;
unsigned int VBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float),
(void *)0);
m_ID = VAO;
m_width = width;
m_height = height;
glBindVertexArray(0);
// setup vertex shader
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vShaderSource, nullptr);
glCompileShader(vertexShader);
// check compilation status of vertex shader
int success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
throw "Failed to compile Verted Shader";
}
// setup fragment shader
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fShaderSource, nullptr);
glCompileShader(fragmentShader);
// check compilation status of the fragment shader
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
throw "Failed to compile fragment shader!";
}
m_shader_ID = glCreateProgram();
glAttachShader(m_shader_ID, vertexShader);
glAttachShader(m_shader_ID, fragmentShader);
glLinkProgram(m_shader_ID);
glGetProgramiv(m_shader_ID, GL_LINK_STATUS, &success);
if (!success) {
throw "Unable to link shader program!";
}
}
void Angel::enable() {
glBindVertexArray(m_ID);
glUseProgram(m_shader_ID);
}
void Angel::disable() {
glUseProgram(0);
glBindVertexArray(0);
}
unsigned int Angel::getWidth() { return m_width; }
void Angel::setWidth(unsigned int width) { m_width = width; }
unsigned int Angel::getHeight() { return m_height; }
void Angel::setHeight(unsigned int height) { m_height = height; }
void Angel::putPixel(float x, float y, int thickness, Color c) {
enable();
int vertexColorLocation = glGetUniformLocation(m_shader_ID, "color");
glUniform4f(vertexColorLocation, c.r, c.g, c.b, c.a);
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, thickness, thickness);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisable(GL_SCISSOR_TEST);
disable();
}
void Angel::drawAxes(Color c) {
for (float i = -1.0f; i <= 1.0f; i += 0.001f) {
putPixel(0, i, 1, c);
putPixel(i, 0, 1, c);
}
}