forked from AdamSimpson/PiBrot
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmulti_tex.c
325 lines (268 loc) · 11 KB
/
multi_tex.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
The MIT License (MIT)
Copyright (c) 2014 Adam Simpson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <assert.h>
#include "multi_tex.h"
#include "ogl_utils.h"
#include "stdlib.h"
#include "fractal.h"
#define USE_MIPMAP 1
void create_textures(texture_t *state, FRAC_INFO *frac_left, FRAC_INFO *frac_right)
{
int i,j,k;
GLubyte *pixels;
state->tex_width[LEFT] = frac_left->num_cols;
state->tex_height[LEFT] = frac_left->num_rows;
state->tex_width[RIGHT] = frac_right->num_cols;
state->tex_height[RIGHT] = frac_right->num_rows;
size_t pixel_bytes;
pixel_bytes = state->tex_width[LEFT]*state->tex_height[LEFT]*sizeof(GLubyte)*frac_left->channels;
// Left fractal
pixels = malloc(pixel_bytes);
// Pixel packing
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Generate texture
glGenTextures(NUM_TEXTURES, state->textures);
// Set sampler
glActiveTexture(GL_TEXTURE0);
// Initialize left texture to black
for(i=0; i<state->tex_height[LEFT]; i++) {
for(j=0; j<state->tex_width[LEFT]; j++) {
for(k=0; k<frac_left->channels; k++) {
pixels[(i*state->tex_width[LEFT] + j)*frac_left->channels + k] = 255;
}
}
}
// Bind texture
glBindTexture(GL_TEXTURE_2D, state->textures[LEFT]);
// Load texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, state->tex_width[LEFT], state->tex_height[LEFT], 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Set filtering modes
#if !(USE_MIPMAP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#endif
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#if USE_MIPMAP
// Generate mipmap
glGenerateMipmap(GL_TEXTURE_2D);
#endif
free(pixels);
// Right fractal
pixel_bytes = state->tex_width[RIGHT]*state->tex_height[RIGHT]*sizeof(GLubyte)*frac_right->channels;
pixels = malloc(pixel_bytes);
// Initialize right texture to white
for(i=0; i<state->tex_height[RIGHT]; i++) {
for(j=0; j<state->tex_width[RIGHT]; j++) {
for(k=0; k<frac_right->channels; k++) {
pixels[(i*state->tex_width[RIGHT] + j)*frac_right->channels + k] = 0;
}
}
}
// Bind texture
glBindTexture(GL_TEXTURE_2D, state->textures[1]);
// Load texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, state->tex_width[RIGHT], state->tex_height[RIGHT], 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
#if USE_MIPMAP
// Generate mipmap
glGenerateMipmap(GL_TEXTURE_2D);
#endif
// Set filtering modes
#if !(USE_MIPMAP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#endif
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#if USE_MIPMAP
// Generate mipmap
glGenerateMipmap(GL_TEXTURE_2D);
#endif
// Free pixels
free(pixels);
}
void update_texture_rows(texture_t *state, int fractal, GLsizei start_row, GLuint num_rows, GLubyte *row_pixels)
{
glBindTexture(GL_TEXTURE_2D, state->textures[fractal]);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, start_row, state->tex_width[fractal], num_rows, GL_RGB, GL_UNSIGNED_BYTE, row_pixels);
#if USE_MIPMAP
glGenerateMipmap(GL_TEXTURE_2D);
#endif
}
// GLUE between outside world and textures - still requires state which I don't like
void update_fractal_rows(texture_t *state, int fractal, unsigned int start_row, unsigned int num_rows, unsigned char *row_pixels)
{
update_texture_rows(state, fractal, (GLsizei)start_row, (GLsizei)num_rows, (GLubyte*)row_pixels);
// Draw textures
draw_textures(state);
}
void create_vertices(texture_t *state)
{
// Vertices: Pos(x,y) Tex(x,y)
// For simplicity only single vbo is generated and offset used as needed
float vertices[] = {
// Image 0 vertices
-1.0f, 1.0f, 0.0f, 0.0f, // Top left
-0.005f, 1.0f, 1.0f, 0.0f, // Top right
-0.005f,-1.0f, 1.0f, 1.0f, // Bottom right
-1.0f, -1.0f, 0.0f, 1.0f, // Bottom left
// Image 1 vertices
0.005f, 1.0f, 0.0f, 0.0f, // Top left
1.0f, 1.0f, 1.0f, 0.0f, // Top right
1.0f, -1.0f, 1.0f, 1.0f, // Bottom right
0.005f,-1.0f, 0.0f, 1.0f, // Bottom left
// Full screen vertices
-1.0f, 1.0f, 0.0f, 0.0f, // Top left
1.0f, 1.0f, 1.0f, 0.0f, // Top right
1.0f, -1.0f, 1.0f, 1.0f, // Bottom right
-1.0f, -1.0f, 0.0f, 1.0f // Bottom left
};
// VAO is required for OpenGL 3+ when using VBO I believe
#ifndef RASPI
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
#endif
// Generate vertex buffer
glGenBuffers(1, &state->vbo);
// Set buffer
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
// Fill buffer
glBufferData(GL_ARRAY_BUFFER, 3*4*4*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
// Elements
GLubyte elements[] = {
2, 3, 0,
0, 1, 2
};
// Generate element buffer
glGenBuffers(1, &state->ebo);
// Set buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->ebo);
// Fill buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*3*sizeof(GLubyte), elements, GL_STATIC_DRAW);
}
void create_shaders(texture_t *state)
{
// Compile vertex shader
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
#ifdef RASPI
compile_shader(vertex_shader, "PiBrot/shaders/texture_es.vert");
#else
compile_shader(vertex_shader, "shaders/texture.vert");
#endif
// Compile fragment shader
GLuint frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
#ifdef RASPI
compile_shader(frag_shader, "PiBrot/shaders/texture_es.frag");
#else
compile_shader(frag_shader, "shaders/texture.frag");
#endif
// Create shader program
state->program = glCreateProgram();
glAttachShader(state->program, vertex_shader);
glAttachShader(state->program, frag_shader);
// Link and use program
glLinkProgram(state->program);
glUseProgram(state->program);
check();
// Get position location
state->position_location = glGetAttribLocation(state->program, "position");
// Get tex_coord location
state->tex_coord_location = glGetAttribLocation(state->program, "tex_coord");
// Get tex uniform location
state->tex_location = glGetUniformLocation(state->program, "tex");
}
/*
void show_left_tex_fullscreen(texture_t *state)
{
// Size of each vertex in bytes
size_t vert_size = 4*sizeof(GL_FLOAT);
// Offset in our vertices array that the full screen vertices start
size_t offset = 2*4*vert_size;
// Draw left fractal
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, vert_size, (void*)offset);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->tex_coord_location, 2, GL_FLOAT, GL_FALSE, vert_size,(void*)offset+2*sizeof(GL_FLOAT));
glEnableVertexAttribArray(state->tex_coord_location);
glBindTexture(GL_TEXTURE_2D, state->textures[LEFT]);
glUniform1i(state->tex_location, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
// Swap buffers
swap_ogl(state->gl_state);
}
void show_right_tex_fullscreen(texture_t *state)
{
// Size of each vertex in bytes
size_t vert_size = 4*sizeof(GL_FLOAT);
// Offset in our vertices array that the full screen vertices start
size_t offset = 2*4*vert_size;
// Draw left fractal
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, vert_size, (void*)offset);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->tex_coord_location, 2, GL_FLOAT, GL_FALSE, vert_size,(void*)offset+2*sizeof(GL_FLOAT));
glEnableVertexAttribArray(state->tex_coord_location);
glBindTexture(GL_TEXTURE_2D, state->textures[RIGHT]);
glUniform1i(state->tex_location, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
// Swap buffers
swap_ogl(state->gl_state);
}
void show_both_textures(texture_t *state)
{
draw_textures(state);
swap_ogl(state->gl_state);
}
*/
void draw_textures(texture_t *state)
{
// Setup program
glUseProgram(state->program);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Size of each vertex in bytes
size_t vert_size = 4*sizeof(GL_FLOAT);
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->ebo);
// Draw left fractal
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, vert_size, 0);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->tex_coord_location, 2, GL_FLOAT, GL_FALSE, vert_size,(void*)(2*sizeof(GL_FLOAT)));
glEnableVertexAttribArray(state->tex_coord_location);
glBindTexture(GL_TEXTURE_2D, state->textures[LEFT]);
glUniform1i(state->tex_location, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
// Draw right fractal
// Offset in vertices array that right texture will use
size_t offset = 4*vert_size;
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, vert_size, (void*)offset);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->tex_coord_location, 2, GL_FLOAT, GL_FALSE, vert_size,(void*)(offset+2*sizeof(GL_FLOAT)));
glEnableVertexAttribArray(state->tex_coord_location);
glBindTexture(GL_TEXTURE_2D, state->textures[RIGHT]);
glUniform1i(state->tex_location, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
}