forked from fogleman/Craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
165 lines (153 loc) · 4.57 KB
/
util.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "lodepng.h"
#include "matrix.h"
#include "util.h"
int rand_int(int n) {
int result;
while (n <= (result = rand() / (RAND_MAX / n)));
return result;
}
double rand_double() {
return (double)rand() / (double)RAND_MAX;
}
void update_fps(FPS *fps, int show) {
fps->frames++;
double now = glfwGetTime();
double elapsed = now - fps->since;
if (elapsed >= 1) {
int result = fps->frames / elapsed;
fps->frames = 0;
fps->since = now;
if (show) {
printf("%d\n", result);
}
}
}
char *load_file(const char *path) {
FILE *file = fopen(path, "rb");
fseek(file, 0, SEEK_END);
int length = ftell(file);
rewind(file);
char *data = calloc(length + 1, sizeof(char));
fread(data, 1, length, file);
fclose(file);
return data;
}
void malloc_buffers(
int components, int faces,
GLfloat **position_data, GLfloat **normal_data, GLfloat **uv_data)
{
if (position_data) {
*position_data = malloc(sizeof(GLfloat) * faces * 6 * components);
}
if (normal_data) {
*normal_data = malloc(sizeof(GLfloat) * faces * 6 * components);
}
if (uv_data) {
*uv_data = malloc(sizeof(GLfloat) * faces * 6 * 2);
}
}
GLuint gen_buffer(GLenum target, GLsizei size, const void *data) {
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(target, buffer);
glBufferData(target, size, data, GL_STATIC_DRAW);
glBindBuffer(target, 0);
return buffer;
}
void gen_buffers(
int components, int faces,
GLfloat *position_data, GLfloat *normal_data, GLfloat *uv_data,
GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer)
{
if (position_buffer) {
glDeleteBuffers(1, position_buffer);
*position_buffer = gen_buffer(
GL_ARRAY_BUFFER,
sizeof(GLfloat) * faces * 6 * components,
position_data
);
free(position_data);
}
if (normal_buffer) {
glDeleteBuffers(1, normal_buffer);
*normal_buffer = gen_buffer(
GL_ARRAY_BUFFER,
sizeof(GLfloat) * faces * 6 * components,
normal_data
);
free(normal_data);
}
if (uv_buffer) {
glDeleteBuffers(1, uv_buffer);
*uv_buffer = gen_buffer(
GL_ARRAY_BUFFER,
sizeof(GLfloat) * faces * 6 * 2,
uv_data
);
free(uv_data);
}
}
GLuint make_shader(GLenum type, const char *source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
GLchar *info = calloc(length, sizeof(GLchar));
glGetShaderInfoLog(shader, length, NULL, info);
fprintf(stderr, "glCompileShader failed:\n%s\n", info);
free(info);
}
return shader;
}
GLuint load_shader(GLenum type, const char *path) {
char *data = load_file(path);
GLuint result = make_shader(type, data);
free(data);
return result;
}
GLuint make_program(GLuint shader1, GLuint shader2) {
GLuint program = glCreateProgram();
glAttachShader(program, shader1);
glAttachShader(program, shader2);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
GLint length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
GLchar *info = calloc(length, sizeof(GLchar));
glGetProgramInfoLog(program, length, NULL, info);
fprintf(stderr, "glLinkProgram failed: %s\n", info);
free(info);
}
glDetachShader(program, shader1);
glDetachShader(program, shader2);
glDeleteShader(shader1);
glDeleteShader(shader2);
return program;
}
GLuint load_program(const char *path1, const char *path2) {
GLuint shader1 = load_shader(GL_VERTEX_SHADER, path1);
GLuint shader2 = load_shader(GL_FRAGMENT_SHADER, path2);
GLuint program = make_program(shader1, shader2);
return program;
}
void load_png_texture(const char *file_name) {
unsigned error;
unsigned char *image;
unsigned width, height;
error = lodepng_decode32_file(&image, &width, &height, file_name);
if (error) {
fprintf(stderr, "error %u: %s\n", error, lodepng_error_text(error));
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
free(image);
}