screen_shape(const program& prog) { gl.bind(vao); GLfloat position_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, positions); gl.buffer_data(GL.array_buffer, position_data, GL.static_draw); vertex_attrib_location va_p; gl.query_location(va_p, prog, "Position"); gl.vertex_array_attrib_pointer( va_p, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_p); GLfloat coord_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, coords); gl.buffer_data(GL.array_buffer, coord_data, GL.static_draw); vertex_attrib_location va_c; gl.query_location(va_c, prog, "TexCoord"); gl.vertex_array_attrib_pointer( va_c, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_c); }
cube_model(const program& prog) { const GLfloat v[8][3] = { {-0.5f, -0.5f, -0.5f}, {+0.5f, -0.5f, -0.5f}, {-0.5f, +0.5f, -0.5f}, {+0.5f, +0.5f, -0.5f}, {-0.5f, -0.5f, +0.5f}, {+0.5f, -0.5f, +0.5f}, {-0.5f, +0.5f, +0.5f}, {+0.5f, +0.5f, +0.5f} }; const GLint f[6][2][3] = { {{0, 4, 2}, {2, 4, 6}}, {{5, 1, 7}, {7, 1, 3}}, {{0, 1, 4}, {4, 1, 5}}, {{6, 7, 2}, {2, 7, 3}}, {{1, 0, 3}, {3, 0, 2}}, {{4, 5, 6}, {6, 5, 7}} }; gl.bind(vao); const GLuint vertex_count = 6 * 2 * 3; // positions GLfloat vertex_data[vertex_count * 3]; for(GLuint fi=0;fi!=6;++fi) for(GLuint ti=0;ti!=2;++ti) for(GLuint vi=0;vi!=3;++vi) { for(GLuint ci=0;ci!=3;++ci) { vertex_data[fi*2*3*3+ti*3*3+vi*3+ci] = v[f[fi][ti][vi]][ci]; } } gl.bind(GL.array_buffer, positions); gl.buffer_data(GL.array_buffer, vertex_data, GL.static_draw); gl.vertex_array_attrib_pointer( vertex_attrib_location(0), 3, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(vertex_attrib_location(0)); vertex_attrib_location va_p; gl.query_location(va_p, prog, "Position"); gl.vertex_array_attrib_pointer( va_p, 3, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_p); // normals const GLfloat n[6][3] = { {-1.0f, 0.0f, 0.0f}, { 1.0f, 0.0f, 0.0f}, { 0.0f, -1.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, { 0.0f, 0.0f, -1.0f}, { 0.0f, 0.0f, 1.0f} }; for(GLuint fi=0;fi!=6;++fi) for(GLuint vi=0;vi!=6;++vi) { for(GLuint ci=0;ci!=3;++ci) { vertex_data[(fi*6+vi)*3+ci] = n[fi][ci]; } } gl.bind(GL.array_buffer, normals); gl.buffer_data(GL.array_buffer, vertex_data, GL.static_draw); vertex_attrib_location va_n; gl.query_location(va_n, prog, "Normal"); gl.vertex_array_attrib_pointer( va_n, 3, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_n); // tex-coords const GLfloat c[6][2] = { {0.0f, 0.0f}, {1.0f, 0.0f}, {0.0f, 1.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} }; for(GLuint fi=0;fi!=6;++fi) { for(GLuint vi=0;vi!=6;++vi) { for(GLuint ci=0;ci!=2;++ci) { vertex_data[(fi*6+vi)*2+ci] = c[vi][ci]; } } } gl.bind(GL.array_buffer, texcoords); gl.buffer_data( GL.array_buffer, buffer_data_spec{vertex_data, vertex_count * 2}, GL.static_draw ); vertex_attrib_location va_c; gl.query_location(va_c, prog, "TexCoord"); gl.vertex_array_attrib_pointer( va_c, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_c); }
example_mandelbrot(void) : offset_x(-0.5f) , offset_y(0.0f) , scale(1.0f) , aspect(1.0f) { shader vs(GL.vertex_shader); vs.source(glsl_literal( "#version 130\n" "uniform vec2 Offset;\n" "uniform vec2 Scale;\n" "in vec2 Position;\n" "in vec2 Coord;\n" "out vec2 vertCoord;\n" "void main(void)\n" "{\n" " vertCoord = Coord*Scale+Offset;\n" " gl_Position = vec4(Position, 0.0, 1.0);\n" "}\n" )); vs.compile(); shader fs(GL.fragment_shader); fs.source(glsl_literal( "#version 130\n" "uniform sampler1D gradient;\n" "in vec2 vertCoord;\n" "out vec4 fragColor;\n" "void main(void)\n" "{\n" " vec2 z = vec2(0.0, 0.0);\n" " vec2 c = vertCoord;\n" " int i = 0, max = 256;\n" " while((i != max) && (distance(z, c) < 2.0))\n" " {\n" " vec2 zn = vec2(\n" " z.x * z.x - z.y * z.y + c.x,\n" " 2.0 * z.x * z.y + c.y\n" " );\n" " z = zn;\n" " ++i;\n" " }\n" " float a = float(i)/float(max);\n" " fragColor = texture(gradient, a+sqrt(length(c))*0.1);\n" "} \n" )); fs.compile(); prog.attach(vs); prog.attach(fs); prog.link(); gl.use(prog); gl.query_location(offset_loc, prog, "Offset"); gl.query_location(scale_loc, prog, "Scale"); gl.uniform(offset_loc, offset_x, offset_y); gl.bind(vao); GLfloat position_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, positions); gl.buffer_data(GL.array_buffer, position_data, GL.static_draw); vertex_attrib_location va_p; gl.query_location(va_p, prog, "Position"); gl.vertex_array_attrib_pointer( va_p, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_p); GLfloat coord_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, coords); gl.buffer_data(GL.array_buffer, coord_data, GL.static_draw); vertex_attrib_location va_c; gl.query_location(va_c, prog, "Coord"); gl.vertex_array_attrib_pointer( va_c, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_c); GLfloat gradient_data[8*3]; for(int i=0; i<8*3; ++i) { gradient_data[i] = (std::rand() % 10000) / 10000.f; } gl.bind(GL.texture_1d, gradient); gl.texture_min_filter(GL.texture_1d, GL.linear); gl.texture_mag_filter(GL.texture_1d, GL.linear); gl.texture_wrap( GL.texture_1d, GL.texture_wrap_s, GL.repeat ); gl.texture_image_1d( GL.texture_1d, 0, GL.rgb, 8, 0, GL.rgb, GL.float_, const_memory_block{gradient_data} ); gl.disable(GL.depth_test); }