-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
197 lines (154 loc) · 4.71 KB
/
main.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
#include <GLFW/glfw3.h>
#include <OpenGL/glu.h>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#define CheckGL do { \
GLenum e = glGetError(); \
if (e != GL_NO_ERROR) \
fprintf ( stderr \
, "in ['%s', L %d, %s()]: %s\n" \
, __FILE__, __LINE__, __FUNCTION__ \
, gluErrorString(e) ); \
} while (false)
const char *src_fs1 = (
#include "gen/fs1.c"
);
const char *src_vs1 = (
#include "gen/vs1.c"
);
GLuint createShader(const char *code, GLenum type) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &code, nullptr);
glCompileShader(shader);
char *strLog = new char[4096];
GLsizei length = 0;
glGetShaderInfoLog(shader, 4096, &length, strLog);
fprintf(stderr, "%s\n", strLog);
delete [] strLog;
return shader;
}
GLint aVertex, aColor;
GLint uRand;
GLuint prog;
GLuint g_buf = 0;
void initPrograms() {
GLuint shader_v = createShader(src_vs1, GL_VERTEX_SHADER);
GLuint shader_f = createShader(src_fs1, GL_FRAGMENT_SHADER);
prog = glCreateProgram();
CheckGL;
glAttachShader(prog, shader_v);
glAttachShader(prog, shader_f);
glLinkProgram(prog);
glUseProgram(prog);
aVertex = 0;
glEnableVertexAttribArray(aVertex);
aColor = 1;
glEnableVertexAttribArray(aColor);
uRand = glGetUniformLocation(prog, "uRandom");
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
fprintf ( stderr
, "Error creating program: %s\n"
, gluErrorString(error) );
}
}
static void initBuffers() {
initPrograms();
GLfloat pos_color[] = {
/* x y z r g b a */
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0,
-1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0,
1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 1.0,
1.0, 1.0, -1.0, 1.0, 1.0, 0.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0, 1.0, 1.0,
1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 1.0,
-1.0, -1.0, -1.0, 0.5, 0.5, 0.5, 1.0,
};
glGenBuffers(1, &g_buf);
glBindBuffer(GL_ARRAY_BUFFER, g_buf);
glBufferData( GL_ARRAY_BUFFER
, sizeof(pos_color), pos_color
, GL_STATIC_DRAW );
glUseProgram(prog);
glBindBuffer(GL_ARRAY_BUFFER, g_buf);
glVertexAttribPointer
( aVertex, 3, GL_FLOAT, GL_FALSE
, sizeof(GLfloat)*7
, (void*)(sizeof(GLfloat)*0) );
glVertexAttribPointer
( aColor, 4, GL_FLOAT, GL_FALSE
, sizeof(GLfloat)*7
, (void*)(sizeof(GLfloat)*3) );
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
fprintf ( stderr
, "Error creating buffer: %s\n"
, gluErrorString(error) );
}
}
static void onDraw(GLFWwindow *window) {
double newTime = glfwGetTime();
int width, height;
glfwGetFramebufferSize(window, &width, &height);
float ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(prog);
glUniform2f ( uRand
, (rand()%4096)/4096.0f
, (rand()%4096)/4096.0f );
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glScalef(0.5, 0.5, 0.5);
glTranslatef(-0.8, 0.0, 0.0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
glPopMatrix();
glPushMatrix();
{
glScalef(0.3, 0.3, 0.3);
glTranslatef(1.0, 0.0, 0.0);
glDrawArrays(GL_TRIANGLE_STRIP, 3, 4);
}
glPopMatrix();
glFlush();
glfwSwapBuffers(window);
}
static void onWindowSize(GLFWwindow *window, int width, int height) {
//onDraw(window);
}
void reportGl(GLFWwindow* window) {
int v0 = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
int v1 = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
fprintf(stderr, "OpenGL API: %d.%d\n", v0, v1);
}
int main(int argc, char** argv) {
if (!glfwInit()) { return -1; }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window =
glfwCreateWindow( 640, 480
, "Hello World"
, NULL, NULL );
if (!window) {
fprintf(stderr, "Failed! Zhazha!\n");
glfwTerminate();
return -1;
}
reportGl(window);
glfwSwapInterval(1);
glfwMakeContextCurrent(window);
initBuffers();
glfwSetWindowSizeCallback(window, onWindowSize);
glfwSetWindowRefreshCallback(window, onDraw);
while (!glfwWindowShouldClose(window)) {
onDraw(window);
glfwPollEvents();
usleep(1);
}
glfwTerminate();
return 0;
}