forked from azekillDIABLO/omicron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.c
244 lines (220 loc) · 8.47 KB
/
rendering.c
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
#include "rendering.h"
#include <string.h>
#include "cube.h"
#include "matrix.h"
#include "util.h"
GLuint gen_plant_buffer(float x, float y, float z, float n, int w) {
GLfloat *data = malloc_faces(10, 4);
float ao = 0;
float light = 1;
make_plant(data, ao, light, x, y, z, n, w, 45);
return gen_faces(10, 4, data);
}
GLuint gen_player_buffer(float x, float y, float z, float rx, float ry) {
GLfloat *data = malloc_faces(10, 6);
make_player(data, x, y, z, rx, ry);
return gen_faces(10, 6, data);
}
GLuint gen_text_buffer(float x, float y, float n, char *text) {
int length = strlen(text);
GLfloat *data = malloc_faces(4, length);
for (int i = 0; i < length; i++) {
make_character(data + i * 24, x, y, n / 2, n, text[i]);
x += n;
}
return gen_faces(4, length, data);
}
GLuint gen_ui_buffer(float x, float y, float n, char spritesheet_index) {
/** Thanks CraftNG :D **/
GLfloat *data = malloc_faces(4, 1);
//make_character(data, x, y, n, n * 2, spritesheet_index);
make_ui_quad(data, x, y, n, n, spritesheet_index);
return gen_faces(4, 1, data);
}
GLuint gen_logo_buffer(float x, float y, float n, char spritesheet_index) {
GLfloat *data = malloc_faces(4, 1);
//make_character(data, x, y, n, n * 2, spritesheet_index);
make_logo_quad(data, x, y, n, n, spritesheet_index);
return gen_faces(4, 1, data);
}
/*GLuint gen_background_buffer(float x, float y, float n, char spritesheet_index) {
GLfloat *data = malloc_faces(4, 1);
//make_character(data, x, y, n, n * 2, spritesheet_index);
make_logo_quad(data, x, y, n, n, spritesheet_index);
return gen_faces(4, 1, data);
}*/
void draw_triangles_3d_ao(Attrib *attrib, GLuint buffer, int count) {
glEnable(GL_BLEND); //*
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glBlendEquation(GL_FUNC_ADD);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->normal);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, 0);
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(attrib->uv, 4, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 10, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->normal);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
void draw_triangles_3d_text(Attrib *attrib, GLuint buffer, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 5, 0);
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 5, (GLvoid *)(sizeof(GLfloat) * 3));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_triangles_3d(Attrib *attrib, GLuint buffer, int count) {
glEnable(GL_BLEND); //*
glEnable(GL_CULL_FACE);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->normal);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, 0);
glVertexAttribPointer(attrib->normal, 3, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 3));
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 8, (GLvoid *)(sizeof(GLfloat) * 6));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->normal);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
void draw_triangles_2d(Attrib *attrib, GLuint buffer, int count) {
glEnable(GL_BLEND); //*
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glEnableVertexAttribArray(attrib->uv);
glVertexAttribPointer(attrib->position, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, 0);
glVertexAttribPointer(attrib->uv, 2, GL_FLOAT, GL_FALSE,
sizeof(GLfloat) * 4, (GLvoid *)(sizeof(GLfloat) * 2));
glDrawArrays(GL_TRIANGLES, 0, count);
glDisableVertexAttribArray(attrib->position);
glDisableVertexAttribArray(attrib->uv);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisable(GL_BLEND);
}
void draw_lines(Attrib *attrib, GLuint buffer, int components, int count) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glEnableVertexAttribArray(attrib->position);
glVertexAttribPointer(
attrib->position, components, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINES, 0, count);
glDisableVertexAttribArray(attrib->position);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw_chunk(Attrib *attrib, Chunk *chunk) {
draw_triangles_3d_ao(attrib, chunk->buffer, chunk->faces * 6);
}
void draw_item(Attrib *attrib, GLuint buffer, int count) {
draw_triangles_3d_ao(attrib, buffer, count);
}
void draw_text(Attrib *attrib, GLuint buffer, int length) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_triangles_2d(attrib, buffer, length * 6);
glDisable(GL_BLEND);
}
void draw_signs(Attrib *attrib, Chunk *chunk) {
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-8, -1024);
draw_triangles_3d_text(attrib, chunk->sign_buffer, chunk->sign_faces * 6);
glDisable(GL_POLYGON_OFFSET_FILL);
}
void draw_sign(Attrib *attrib, GLuint buffer, int length) {
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-8, -1024);
draw_triangles_3d_text(attrib, buffer, length * 6);
glDisable(GL_POLYGON_OFFSET_FILL);
}
void draw_cube(Attrib *attrib, GLuint buffer) {
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendEquation(GL_FUNC_ADD);
draw_item(attrib, buffer, 36);
//glDisable(GL_BLEND);
}
void draw_plant(Attrib *attrib, GLuint buffer) {
draw_item(attrib, buffer, 24);
}
void draw_player(Attrib *attrib, Player *player) {
draw_cube(attrib, player->buffer);
}
void draw_ui(Attrib *attrib, GLuint buffer) {
glEnable(GL_BLEND); //*
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_triangles_2d(attrib, buffer, 6); //wtf is 6.
glDisable(GL_BLEND);
}
void draw_logo(Attrib *attrib, GLuint buffer) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glUseProgram(attrib->program);
//glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
//glUniform1i(attrib->sampler, 1);
//glUniform1i(attrib->extra1, 0);
draw_triangles_2d(attrib, buffer, 6); //wtf is 6.
glDisable(GL_BLEND);
}
GLuint gen_crosshair_buffer(int width, int height, int scale) {
int x = width / 2;
int y = height / 2;
int p = 11 * scale;
float data[] = {
x, y - p, x, y + p,
x - p, y, x + p, y
};
return gen_buffer(sizeof(data), data);
}
void render_text(
Attrib *attrib, int justify, float x, float y, float n, char *text,
int win_width, int win_height)
{
float matrix[16];
set_matrix_2d(matrix, win_width, win_height);
glUseProgram(attrib->program);
glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
glUniform1i(attrib->sampler, 1);
glUniform1i(attrib->extra1, 0);
int length = strlen(text);
x -= n * justify * (length - 1) / 2;
GLuint buffer = gen_text_buffer(x, y, n, text);
draw_text(attrib, buffer, length);
del_buffer(buffer);
}
void render_crosshairs(Attrib *attrib, int width, int height, int scale) {
float matrix[16];
set_matrix_2d(matrix, width, height);
glUseProgram(attrib->program);
glLineWidth(4 * scale);
glEnable(GL_COLOR_LOGIC_OP);
glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
GLuint crosshair_buffer = gen_crosshair_buffer(width, height, scale);
draw_lines(attrib, crosshair_buffer, 2, 4);
del_buffer(crosshair_buffer);
glDisable(GL_COLOR_LOGIC_OP);
}