mightymandel v16

GPU-based Mandelbrot set explorer

fp32_colour.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
5 #include "fp32_colour.h"
6 #include "shader.h"
7 #include "logging.h"
8 #include "texture.h"
9 
10 void fp32_colour_begin(struct fp32_colour *s, GLuint rgb_tex) {
12  s->ida0 = glGetUniformLocation(s->program, "ida0");D;
13  s->slice_number = glGetUniformLocation(s->program, "slice_number");D;
14  s->slice_coords = glGetUniformLocation(s->program, "slice_coords");D;
15  s->update = glGetUniformLocation(s->program, "update");D;
16  s->update2 = glGetUniformLocation(s->program, "update2");D;
17  s->errs = glGetUniformLocation(s->program, "errs");D;
18  s->thickness = glGetUniformLocation(s->program, "thickness");D;
19  s->slicing = glGetUniformLocation(s->program, "slicing");D;
20  s->pt = glGetAttribLocation(s->program, "pt");D;
22  s->rgb2 = glGetUniformLocation(s->program2, "rgb");D;
23  s->pt2 = glGetAttribLocation(s->program2, "pt");D;
24  glGenVertexArrays(1, &s->vao);D;
25  glGenVertexArrays(1, &s->vao2);D;
26  glGenBuffers(1, &s->vbo);D;
27  GLfloat quad[] =
28  { -1, -1, 0, 0
29  , 1, -1, 1, 0
30  , -1, 1, 0, 1
31  , 1, 1, 1, 1
32  };
33  glBindBuffer(GL_ARRAY_BUFFER, s->vbo);D;
34  glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), &quad, GL_STATIC_DRAW);D;
35  glBindVertexArray(s->vao);D;
36  glVertexAttribPointer(s->pt, 4, GL_FLOAT, GL_FALSE, 0, 0);D;
37  glEnableVertexAttribArray(s->pt);D;
38  glBindVertexArray(s->vao2);D;
39  glVertexAttribPointer(s->pt2, 4, GL_FLOAT, GL_FALSE, 0, 0);D;
40  glEnableVertexAttribArray(s->pt2);D;
41  glBindVertexArray(0);D;
42  glBindBuffer(GL_ARRAY_BUFFER, 0);D;
43  glUseProgram(s->program);D;
44  glUniform1i(s->ida0, TEX_RAW);D;
45  glUniform1i(s->slice_number, TEX_SLICE_NUMBER);D;
46  glUniform1i(s->slice_coords, TEX_SLICE_COORDS);D;
47  glUseProgram(s->program2);D;
48  glUniform1i(s->rgb2, TEX_RGB);D;
49  glUseProgram(0);D;
50  glGenFramebuffers(1, &s->fbo);D;
51  glBindFramebuffer(GL_FRAMEBUFFER, s->fbo);D;
52  glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, rgb_tex, 0);D;
53  GLenum buffers[1] = { GL_COLOR_ATTACHMENT0 };
54  glDrawBuffers(1, buffers);D;
55  glBindFramebuffer(GL_FRAMEBUFFER, 0);D;
56 }
57 
58 void fp32_colour_end(struct fp32_colour *s) {
59  glDeleteProgram(s->program);D;
60  glDeleteProgram(s->program2);D;
61  glDeleteVertexArrays(1, &s->vao);D;
62  glDeleteVertexArrays(1, &s->vao2);D;
63  glDeleteBuffers(1, &s->vbo);D;
64  glDeleteFramebuffers(1, &s->fbo);D;
65 }
66 
72 void fp32_colour_do(struct fp32_colour *s, int win_width, int win_height, int width, int height, bool update, bool update2, GLuint errs, float thickness, int slice, int slice_n) {
73  // render to texture
74  glBindFramebuffer(GL_FRAMEBUFFER, s->fbo);D;
75  glViewport(0, 0, width, height);D;
76  glUseProgram(s->program);D;
77  glUniform1i(s->update, update);D;
78  glUniform1i(s->update2, update2);D;
79  glUniform1i(s->errs, errs);D;
80  glUniform1f(s->thickness, thickness);D;
81  glUniform2i(s->slicing, slice_n, slice);
82  glBindVertexArray(s->vao);D;
83  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);D;
84  glBindVertexArray(0);D;
85  glUseProgram(0);D;
86  glBindFramebuffer(GL_FRAMEBUFFER, 0);D;
87  glActiveTexture(GL_TEXTURE0 + TEX_RGB);D;
88  glGenerateMipmap(GL_TEXTURE_2D);D;
89  // draw to screen
90  glViewport(0, 0, win_width, win_height);D;
91  glUseProgram(s->program2);D;
92  glBindVertexArray(s->vao2);D;
93  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);D;
94  glBindVertexArray(0);D;
95  // reset state
96  glViewport(0, 0, width, height);D;
97 }
98 
99 void fp32_colour_clear(struct fp32_colour *s, int width, int height) {
100  glBindFramebuffer(GL_FRAMEBUFFER, s->fbo);D;
101  glViewport(0, 0, width, height);D;
102  glClearColor(1.0, 0.7, 0.0, 1.0);D;
103  glClear(GL_COLOR_BUFFER_BIT);D;
104  glBindFramebuffer(GL_FRAMEBUFFER, 0);D;
105 }