3#include <cgv_gl/gl/gl.h>
4#include <cgv/render/frame_buffer.h>
5#include <cgv/render/attribute_array_binding.h>
6#include <cgv/math/ftransform.h>
7#include <cgv/render/shader_program.h>
10#ifndef GL_CLAMP_TO_EDGE
11#define GL_CLAMP_TO_EDGE 0x812F
22 static unsigned cf_to_gl[] = {
40 static unsigned cf_to_gl_integer[] = {
46 GL_LUMINANCE_INTEGER_EXT,
47 GL_LUMINANCE_INTEGER_EXT,
48 GL_LUMINANCE_ALPHA_INTEGER_EXT,
49 GL_LUMINANCE_ALPHA_INTEGER_EXT,
61 if (cii == CII_INTEGER)
62 return cf_to_gl_integer[cf];
68 static unsigned ti_to_gl[] = {
89 if (ti_to_gl[ti] == 0) {
90 std::cerr <<
"could not map component type " << ti <<
" to gl type" << std::endl;
91 return GL_UNSIGNED_BYTE;
98 GLint gl_cube_map_target[] = {
99 GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT,
100 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT,
101 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT,
102 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT,
103 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT,
104 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT
106 return gl_cube_map_target[side];
109GLuint gl_tex_dim[] = { GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_1D_ARRAY, GL_TEXTURE_2D_ARRAY, GL_TEXTURE_CUBE_MAP_EXT };
111bool generate_mipmaps(
unsigned int dim,
bool is_cubemap,
bool is_array, std::string* last_error)
113 if (dim == 0 || dim > 3) {
115 *last_error =
"wrong dimension of texture";
118 if(is_array && (dim < 2 || dim > 3)) {
120 *last_error =
"wrong dimension for array texture";
123 if(is_cubemap && dim != 2) {
125 *last_error =
"wrong dimension for cubemap texture";
130 *last_error =
"automatic generation of mipmaps not supported";
137 glGenerateMipmap(gl_tex_dim[dim-1]);
141static const GLenum gl_std_texture_format_ids[] =
156 GL_LUMINANCE4_ALPHA4,
157 GL_LUMINANCE6_ALPHA2,
158 GL_LUMINANCE8_ALPHA8,
159 GL_LUMINANCE12_ALPHA4,
160 GL_LUMINANCE12_ALPHA12,
161 GL_LUMINANCE16_ALPHA16,
188static const char* std_texture_formats[] = {
215 "uint8[R:3,G:3,B:2]",
227 "uint8[R:5,G:5,B:5,A:1]",
229 "uint16[R:10,G:10,B:10,A:2]",
230 "uint16:12[R,G,B,A]",
234static const GLenum gl_float_texture_format_ids[] =
241 GL_LUMINANCE_ALPHA32F_ARB,
248 GL_LUMINANCE_ALPHA16F_ARB,
252static const char* float_texture_formats[] = {
269static const GLenum gl_snorm_texture_format_ids[] =
277 GL_LUMINANCE_ALPHA_SNORM,
285 GL_LUMINANCE8_ALPHA8_SNORM,
292 GL_LUMINANCE16_SNORM,
293 GL_LUMINANCE16_ALPHA16_SNORM,
297static const char* snorm_texture_formats[] = {
325static const GLenum gl_int_texture_format_ids[] =
330 GL_INTENSITY32UI_EXT,
331 GL_LUMINANCE32UI_EXT,
332 GL_LUMINANCE_ALPHA32UI_EXT,
337 GL_INTENSITY16UI_EXT,
338 GL_LUMINANCE16UI_EXT,
339 GL_LUMINANCE_ALPHA16UI_EXT,
346 GL_LUMINANCE_ALPHA8UI_EXT,
353 GL_LUMINANCE_ALPHA32I_EXT,
360 GL_LUMINANCE_ALPHA16I_EXT,
367 GL_LUMINANCE_ALPHA8I_EXT
370static const char* int_texture_formats[] = {
415static const GLenum gl_depth_format_ids[] =
418 GL_DEPTH_COMPONENT16_ARB,
419 GL_DEPTH_COMPONENT24_ARB,
420 GL_DEPTH_COMPONENT32_ARB
423static const char* depth_formats[] =
432static const GLenum gl_rg_texture_format_ids[] =
468static const char* rg_texture_formats[] = {
513 cf = *palettes->at(0).get_format();
515 std::cout <<
"find best match in std_texture_formats:" << std::endl;
519 unsigned gl_format = gl_std_texture_format_ids[i];
523 std::cout <<
"find best match in depth_formats:" << std::endl;
528 gl_format = gl_depth_format_ids[i];
533 std::cout <<
"find best match in snorm_texture_formats:" << std::endl;
538 gl_format = gl_snorm_texture_format_ids[i];
543 std::cout <<
"find best match in int_texture_formats:" << std::endl;
548 gl_format = gl_int_texture_format_ids[i];
553 std::cout <<
"find best match in float_texture_formats:" << std::endl;
558 gl_format = gl_float_texture_format_ids[i];
563 std::cout <<
"find best match in rg_texture_formats:" << std::endl;
568 gl_format = gl_rg_texture_format_ids[i];
575unsigned configure_src_format(
const cgv::data::const_data_view& data, GLuint& src_type, GLuint& src_fmt,
const std::vector<data_view>* palettes)
589 src_fmt = GL_COLOR_INDEX;
592 nr_comp = pf.get_nr_components();
593 unsigned comp_size = pf.get_entry_size() / pf.get_nr_components();
594 std::vector<float> tmp;
595 tmp.resize(pf.get_width());
596 for (
unsigned ci=0; ci<nr_comp; ++ci) {
597 glPixelTransferf(GL_RED_BIAS, 0.000001f);
598 for (
unsigned i=0; i<pf.get_width(); ++i)
599 tmp[i] = pf.get<
unsigned char>(ci, pv.step_i(pv.get_ptr<
unsigned char>(), i))/255.0f;
600 switch (pf.get_component_name(ci)[0]) {
601 case 'R' : glPixelMapfv(GL_PIXEL_MAP_I_TO_R, GLsizei(pf.get_width()), &tmp.front());
break;
602 case 'G' : glPixelMapfv(GL_PIXEL_MAP_I_TO_G, GLsizei(pf.get_width()), &tmp.front());
break;
603 case 'B' : glPixelMapfv(GL_PIXEL_MAP_I_TO_B, GLsizei(pf.get_width()), &tmp.front());
break;
604 case 'A' : glPixelMapfv(GL_PIXEL_MAP_I_TO_A, GLsizei(pf.get_width()), &tmp.front());
break;
606 glPixelMapfv(GL_PIXEL_MAP_I_TO_R, GLsizei(pf.get_width()), &tmp.front());
607 glPixelMapfv(GL_PIXEL_MAP_I_TO_G, GLsizei(pf.get_width()), &tmp.front());
608 glPixelMapfv(GL_PIXEL_MAP_I_TO_B, GLsizei(pf.get_width()), &tmp.front());
620 const unsigned char* data_ptr = data.
get_ptr<
unsigned char>();
622 bool cube_map = (nr_dim == 2) && (cube_side != -1);
623 bool texture_array = (nr_dim > 0) && (nr_dim < 4) && !cube_map && num_array_layers != 0;
626 texture_array =
false;
628 GLuint src_type, src_fmt;
629 unsigned nr_comp = configure_src_format(data, src_type, src_fmt, palettes);
631 bool gen_mipmap = level == -1;
637 glTexImage2D(GL_TEXTURE_1D_ARRAY, level, gl_tex_format, w, 1, 0, src_fmt, src_type, data_ptr);
639 glTexImage1D(GL_TEXTURE_1D, level, gl_tex_format, w, 0, src_fmt, src_type, data_ptr);
644 if(num_array_layers < 0) {
645 glTexImage2D(GL_TEXTURE_1D_ARRAY, level, gl_tex_format, w, GLsizei(data.
get_format()->
get_height()), 0, src_fmt, src_type, data_ptr);
648 glTexImage3D(GL_TEXTURE_2D_ARRAY, level, gl_tex_format, w, GLsizei(data.
get_format()->
get_height()), 1, 0, src_fmt, src_type, data_ptr);
660 if(num_array_layers > 0)
663 glTexImage3D(GL_TEXTURE_2D_ARRAY, level, gl_tex_format, w, GLsizei(data.
get_format()->
get_height()),
664 num_layers, 0, src_fmt, src_type, data_ptr);
675 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
683 const unsigned char* data_ptr = data.
get_ptr<
unsigned char>();
685 bool cube_map = (nr_dim == 2) && (z != -1);
687 GLuint src_type, src_fmt;
688 unsigned nr_comp = configure_src_format(data, src_type, src_fmt, palettes);
690 bool gen_mipmap = level == -1;
695 glTexSubImage1D(GL_TEXTURE_1D, level, x, w, src_fmt, src_type, data_ptr);
702 glTexSubImage2D(GL_TEXTURE_2D, level, x, y, w,
706 glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z, w,
712 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
718 return create_texture(dv,mipmap?(
unsigned)-1:0, (unsigned)-1, palettes, tex_id);
724 glGenTextures(1,&tex_id);
726 if ((nr_dim == 2) && (cube_side != -1))
727 glBindTexture(gl_tex_dim[3], tex_id);
729 glBindTexture(gl_tex_dim[nr_dim-1], tex_id);
733 if (
load_texture(dv, gl_tex_format, level, cube_side,
false, palettes))
734 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
736 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
738 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
739 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
740 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
751 unsigned char* data_ptr = data.
get_ptr<
unsigned char>();
753 GLuint src_type, src_fmt;
754 unsigned nr_comp = configure_src_format(data, src_type, src_fmt,
nullptr);
756 glGetTexImage(gl_tex_dim[nr_dim-1], level, src_fmt, src_type, data_ptr);
760bool cover_screen(
context& ctx,
shader_program* prog_ptr,
bool flip_tex_v_coord,
float xmin,
float ymin,
float xmax,
float ymax,
float umin,
float vmin,
float umax,
float vmax)
772 int pos_idx = prog.get_position_index();
773 int tex_idx = prog.get_texcoord_index();
775 ctx.
error(
"cgv::render::gl::render_2d_texture_to_screen() passed program does not have position vertex attributes", &prog);
785 vec4 positions[4] = {
786 vec4(xmin,ymin, 0, 1),
787 vec4(xmax,ymin, 0, 1),
788 vec4(xmin,ymax, 0, 1),
789 vec4(xmax,ymax, 0, 1)
791 vec2 texcoords[8] = {
808 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
823void gl_texture_to_screen(
float xmin,
float ymin,
float xmax,
float ymax,
float umin,
float vmin,
float umax,
float vmax)
826 glGetIntegerv(GL_MATRIX_MODE, &mm);
828 glMatrixMode(GL_MODELVIEW);
832 glMatrixMode(GL_PROJECTION);
837 glTexCoord2f(umin,vmin);
838 glVertex2f(xmin, ymin);
840 glTexCoord2f(umax,vmin);
841 glVertex2f(xmax, ymin);
843 glTexCoord2f(umax,vmax);
844 glVertex2f(xmax, ymax);
846 glTexCoord2f(umin,vmax);
847 glVertex2f(xmin, ymax);
852 glMatrixMode(GL_MODELVIEW);
861 glGetIntegerv(GL_MATRIX_MODE, &mm);
863 glMatrixMode(GL_MODELVIEW);
867 glMatrixMode(GL_PROJECTION);
873 glVertex2f(xmin, ymin);
875 glTexCoord1f(vary_along_x ? 1.0f : 0.0f);
876 glVertex2f(xmax, ymin);
879 glVertex2f(xmax, ymax);
881 glTexCoord1f(vary_along_x ? 0.0f : 1.0f);
882 glVertex2f(xmin, ymax);
887 glMatrixMode(GL_MODELVIEW);
895 const char* vertex_shader_source =
"\
898uniform float slice_coord;\n\
899layout(location = 0) in vec3 position;\n\
900layout(location = 3) in vec2 texcoord;\n\
901out vec3 tex_coord;\n\
905 tex_coord.xy = texcoord;\n\
906 tex_coord.z = slice_coord;\n\
907 gl_Position = vec4(position,1.0);\n\
910 if (!prog.
attach_code(ctx, vertex_shader_source, cgv::render::ST_VERTEX)) {
912 *error_message =
"could not attach vertex shader source";
915 if (!prog.
link(ctx)) {
917 *error_message =
"could not link render to texture 3D program";
932 std::cerr <<
"ERROR in cgv:render::gl::render_to_texture3D: texture resolution of target_tex2 does not match resolution of target_tex" << std::endl;
938 std::cerr <<
"ERROR in cgv:render::gl::render_to_texture3D: texture resolution of target_tex3 does not match resolution of target_tex" << std::endl;
944 std::cerr <<
"ERROR in cgv:render::gl::render_to_texture3D: texture resolution of target_tex4 does not match resolution of target_tex" << std::endl;
950 fbo.
create(ctx,
int(tex_res[0]),
int(tex_res[1]));
951 fbo.
attach(ctx, target_tex, 0, 0, 0);
953 std::cerr <<
"fbo to update volume gradient not complete" << std::endl;
956 static float V[4 * 3] = {
957 -1, -1, 0, +1, -1, 0,
960 static int F[1 * 4] = {
968 T[0] = T[6] = float(-0.5 / tex_res[0]);
969 T[2] = T[4] = float(1.0 + 0.5 / tex_res[0]);
970 T[1] = T[3] = float(-0.5 / tex_res[1]);
971 T[5] = T[7] = float(1.0 + 0.5 / tex_res[1]);
977 for (
int i = 0; i < (int) tex_res[2]; i++) {
979 if (slice_coord_loc != -1) {
980 float slice_coord = (texture_sampling ==
TS_CELL) ? (i + 0.5f) / tex_res[2] : (float)i / (tex_res[2] - 1);
981 prog.
set_uniform(ctx, slice_coord_loc, slice_coord);
984 fbo.
attach(ctx, target_tex, i, 0, 0);
986 fbo.
attach(ctx, *target_tex2, i, 0, 1);
988 fbo.
attach(ctx, *target_tex3, i, 0, 2);
990 fbo.
attach(ctx, *target_tex4, i, 0, 3);
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
const data_format * get_format() const
return the component format
cgv::type::func::transfer_const< P, S * >::type get_ptr() const
return a data pointer to type S
the data view gives access to a data array of one, two, three or four dimensions.
static bool enable_global_array(const context &ctx, int loc)
enable attribute array of given location
static bool set_global_attribute_array(const context &ctx, int loc, const vertex_buffer &vbo, type_descriptor td, size_t size, size_t offset, unsigned stride=0)
point array of vertex attribute at location loc to vertex buffer array array stored in CPU memory; in...
static bool disable_global_array(const context &ctx, int loc)
disable attribute array of given location
base class for all drawables, which is independent of the used rendering API.
void push_window_transformation_array()
push a copy of the current viewport and depth range arrays defining the window transformations
virtual void set_color(const rgba &clr)
set the current color
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
virtual shader_program & ref_default_shader_program(bool texture_support=false)=0
return a reference to a shader program used to render without illumination
virtual void draw_faces(const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, int nr_faces, int face_degree, bool flip_normals=false) const =0
pass geometry of given faces to current shader program and generate draw calls to render triangles
virtual void pop_window_transformation_array()
restore previous viewport and depth range arrays defining the window transformations
void pop_projection_matrix()
see push_P for an explanation
void push_projection_matrix()
same as push_V but for the projection matrix - a different matrix stack is used.
virtual void set_projection_matrix(const dmat4 &P)
set the current projection matrix, which transforms from eye to clip space
void pop_modelview_matrix()
see push_V for an explanation
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
virtual void set_viewport(const ivec4 &viewport, int array_index=-1)
set the current viewport or one of the viewports in the window transformation array
virtual void set_modelview_matrix(const dmat4 &MV)
set the current modelview matrix, which transforms from world to eye space
this class encapsulate frame buffers that live on the GPU and can be used as destination for the rend...
bool create(const context &ctx, int _width=-1, int _height=-1)
create framebuffer if extension is supported, otherwise return false.
bool is_complete(const context &ctx) const
check for completeness, if not complete, get the reason in last_error
bool attach(const context &ctx, const render_buffer &rb, int i=0)
attach render buffer to depth buffer if it is a depth buffer, to stencil if it is a stencil buffer or...
bool enable(context &ctx, int i0=-1, int i1=-1, int i2=-1, int i3=-1, int i4=-1, int i5=-1, int i6=-1, int i7=-1, int i8=-1, int i9=-1, int i10=-1, int i11=-1, int i12=-1, int i13=-1, int i14=-1, int i15=-1)
enable the framebuffer either with all color attachments if no arguments are given or if arguments ar...
bool disable(context &ctx)
disable the framebuffer object
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool enable(context &ctx)
enable the shader program
bool disable(context &ctx)
disable shader program and restore fixed functionality
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
bool attach_code(const context &ctx, const shader_code &code)
attach a compiled shader code instance that is managed outside of program
bool link(const context &ctx, bool show_error=false)
link shaders to an executable program
bool is_enabled() const
check whether program is currently enabled
int get_uniform_location(const context &ctx, const std::string &name) const
query location index of an uniform
the texture class encapsulates all functionality independent of the rendering api.
namespace for data management components
ComponentFormat
define standard formats, which should be used to avoid wrong assignment of component names
ComponentIntegerInterpretation
define different interpretations of integer components
unsigned find_best_match(const component_format &fmt, const char **format_descriptions, const component_format *fmt0, bool(*fmt1_better_match)(const component_format &fmt, const component_format &fmt1, const component_format &fmt2), bool show_debug_info)
find the best matching format in a list of formats described by strings and return index of best matc...
bool fmt1_compares_better(const component_format &fmt, const component_format &fmt1, const component_format &fmt2)
default function to check whether fmt1 is a better match to fmt than fmt2
void gl_texture_to_screen(float xmin, float ymin, float xmax, float ymax, float umin, float vmin, float umax, float vmax)
cover the current viewport with a textured quad
bool load_texture(const cgv::data::const_data_view &data, unsigned gl_tex_format, unsigned level, unsigned cube_side, int num_array_layers, const std::vector< data_view > *palettes)
load data to a texture with the glTexImage commands and generate mipmaps if the level parameter is -1...
unsigned int create_texture(const cgv::data::const_data_view &dv, bool mipmap, const std::vector< data_view > *palettes, unsigned tex_id)
create a texture from the given data view creating a mipmap pyramid
void read_texture(cgv::data::data_view &data, unsigned level)
read back a texture into a data view with the glGetTexImage command
bool complete_program_form_render_to_texture3D(cgv::render::context &ctx, cgv::render::shader_program &prog, std::string *error_message)
complete the given shader program that is assumed to have a working fragment shader.
bool replace_texture(const cgv::data::const_data_view &data, int level, int x, int y, int z, const std::vector< cgv::data::data_view > *palettes)
replace part or complete data of currently bound texture with the data in the given data view
bool ensure_glew_initialized()
initialize glew in the first call to this function and always return whether this was successful
bool cover_screen(context &ctx, shader_program *prog_ptr, bool flip_tex_v_coord, float xmin, float ymin, float xmax, float ymax, float umin, float vmin, float umax, float vmax)
cover the current viewport with a textured quad using the textured default shader program or the one ...
void gl_1D_texture_to_screen(bool vary_along_x, float xmin, float ymin, float xmax, float ymax)
cover the current viewport or a rectangle with it with a quad textured by a 1D texture
unsigned find_best_texture_format(const cgv::data::component_format &_cf, cgv::data::component_format *best_cf, const std::vector< data_view > *palettes, bool show_debug_info)
map the given component format to the best matching available gl component format
bool generate_mipmaps(unsigned int dim, bool is_cubemap, bool is_array, std::string *last_error)
generate mipmaps for the currently bound texture, which has the given texture dimension; optionally p...
unsigned get_gl_cube_map_target(unsigned side)
return one of the six cube map sides gl enums
bool render_to_texture3D(context &ctx, shader_program &prog, TextureSampling texture_sampling, texture &target_tex, texture *target_tex2, texture *target_tex3, texture *target_tex4)
Render to the given target 3D texture with the given shader program that must be completed with the f...
TextureSampling
different sampling strategies for rendering to textures that steer the computation of the tex_coord i...
@ TS_VERTEX
tex_coord ranges from [0,0,0] to [1,1,1]
@ TS_CELL
for texture resulution N x M x L the tex_coord ranges from [1/2N, 1/2M, 1/2L] to [1-1/2N,...
TypeId
ids for the different types and type constructs
cgv::math::fvec< float, 4 > vec4
declare type of 4d single precision floating point vectors (used for homogeneous coordinates)
cgv::media::color< float, cgv::media::RGB, cgv::media::OPACITY > rgba
declare rgba color type with 32 bit components
cgv::math::fvec< int32_t, 4 > ivec4
declare type of 4d 32 bit integer vectors
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors