-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.cpp
More file actions
239 lines (188 loc) · 6.38 KB
/
Copy pathmain.cpp
File metadata and controls
239 lines (188 loc) · 6.38 KB
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
#define GLEW_STATIC
#include "hacklib/Main.h"
#include "hacklib/WindowOverlay.h"
#include <chrono>
#include <cstdio>
#include <GL/glew.h>
#define GLSL_SRC(src) "#version 150 core\n" #src
static hl::StaticInit<class ExampleMain> g_main;
static const GLchar* vertexSource =
GLSL_SRC(in vec2 position; uniform mat4 world; uniform mat4 view; uniform mat4 proj;
void main() { gl_Position = proj * view * world * vec4(position, 0.0, 1.0); });
static const GLchar* fragmentSource = GLSL_SRC(out vec4 outColor;
void main() { outColor = vec4(1.0, 1.0, 1.0, 1.0); });
class GLDTest
{
public:
/*
cleanup:
glDeleteProgram(m_program);
glDeleteShader(m_fragShader);
glDeleteShader(m_vertShader);
glDeleteBuffers(1, &m_vbTest);
glDeleteVertexArrays(1, &m_vaTest);
*/
bool init()
{
glewExperimental = GL_TRUE;
const GLenum result = glewInit();
if (result != GLEW_OK)
{
printf("glew init failed: %s\n", glewGetErrorString(result));
return false;
}
if (glGenVertexArrays == nullptr)
{
printf("glew didnt do its thing\n");
return false;
}
glGenVertexArrays(1, &m_vaTest);
glBindVertexArray(m_vaTest);
glGenBuffers(1, &m_vbTest);
GLfloat verts[] = { -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f };
glBindBuffer(GL_ARRAY_BUFFER, m_vbTest);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glGenBuffers(1, &m_ibTest);
GLuint inds[] = { 0, 1, 2, 2, 3, 0 };
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibTest);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(inds), inds, GL_STATIC_DRAW);
m_vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(m_vertShader, 1, &vertexSource, NULL);
glCompileShader(m_vertShader);
GLint vertStatus = 0;
glGetShaderiv(m_vertShader, GL_COMPILE_STATUS, &vertStatus);
if (vertStatus != GL_TRUE)
{
char buffer[512];
glGetShaderInfoLog(m_vertShader, 512, NULL, buffer);
printf("Shader compile output:\n%s\n", buffer);
return false;
}
m_fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(m_fragShader, 1, &fragmentSource, NULL);
glCompileShader(m_fragShader);
GLint fragStatus = 0;
glGetShaderiv(m_fragShader, GL_COMPILE_STATUS, &fragStatus);
if (fragStatus != GL_TRUE)
{
char buffer[512];
glGetShaderInfoLog(m_fragShader, 512, NULL, buffer);
printf("Shader compile output:\n%s\n", buffer);
return false;
}
m_program = glCreateProgram();
glAttachShader(m_program, m_vertShader);
glAttachShader(m_program, m_fragShader);
glBindFragDataLocation(m_program, 0, "outColor");
glLinkProgram(m_program);
glUseProgram(m_program);
const GLint posAttr = glGetAttribLocation(m_program, "position");
glEnableVertexAttribArray(posAttr);
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, 0, 0);
// GLint uniModel = glGetUniformLocation(m_program, "model");
// GLint uniView = glGetUniformLocation(m_program, "view");
// GLint uniProj = glGetUniformLocation(m_program, "proj");
return true;
}
void draw()
{
/*
/// multiple bufs:
// bind once
glBindVertexArray(ssquareVAOid);
glUseProgram(shaderProgId);
glActiveTexture(GL_TEXTURE0);
glBindTexture(catTextureId);
glUniformMatrix4fv(localToWorldId, 1, GL_FALSE, pointerOnFloatArray);
glDrawArrray(GL_TRIANGLE, numVertToSkip, numVertToDraw);
// second primitive:
glBindTexture(dogTextureId);
glUniformMatrix4fv(localToWorldId, 1, GL_FALSE, pointerOnFloatArrayForSecondSquareMatrix);
glDrawArrray(GL_TRIANGLE, numVertToSkip, numVertToDraw);
-> glDrawArrays(GL_TRIANGLES, 0, 3);
*/
}
private:
GLuint m_vaTest = 0;
GLuint m_vbTest = 0;
GLuint m_ibTest = 0;
GLuint m_vertShader = 0;
GLuint m_fragShader = 0;
GLuint m_program = 0;
};
class ExampleMain : public hl::Main
{
public:
bool init() override
{
printf("hl::Main::init\n");
m_started = std::chrono::system_clock::now();
if (m_overlay.create() != hl::WindowOverlay::Error::Okay)
{
printf("could not create overlay\n");
}
// need to have context.
m_overlay.resetContext();
GLDTest test;
if (!test.init())
{
printf("test init failed\n");
return false;
}
printf("done\n");
return true;
}
bool step() override
{
// printf("hl::Main::step\n");
if (std::chrono::system_clock::now() - m_started > std::chrono::seconds(5))
return false;
GLuint vao{};
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
m_overlay.beginDraw();
glViewport(0, 0, m_overlay.getWidth(), m_overlay.getHeight());
// orthographic projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, m_overlay.getWidth(), m_overlay.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(50, 50, 0);
glBegin(GL_QUADS);
glColor4f(1, 0, 0, 0.5);
glVertex2f(0, 0);
glVertex2f(0, 100);
glVertex2f(200, 100);
glVertex2f(200, 0);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(80, 80, 0);
glBegin(GL_QUADS);
glColor4f(0, 1, 0, 0.5);
glVertex2f(0, 0);
glVertex2f(0, 100);
glVertex2f(200, 100);
glVertex2f(200, 0);
glEnd();
glPopMatrix();
glLineWidth(3.0);
glColor4f(0.5, 1.0, 0, 0.5);
glBegin(GL_LINES);
glVertex2f(100, 100);
glVertex2f(200, 200);
glEnd();
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(0.5, 0.5);
glEnd();
m_overlay.swapBuffers();
return true;
}
void shutdown() override { printf("hl::Main::shutdown\n"); }
private:
std::chrono::system_clock::time_point m_started;
hl::WindowOverlay m_overlay;
};