cgv
Loading...
Searching...
No Matches
gl_tools.cxx
1#include "gl_tools.h"
2
3#include <cgv/media/image/image_reader.h>
4
5#include <cgv/render/context.h>
6#include <cgv/render/shader_program.h>
7#include <cgv_gl/gl/gl.h>
8
9#ifndef GL_CLAMP_TO_EDGE
10#define GL_CLAMP_TO_EDGE 0x812F
11#endif
12
13using namespace cgv::data;
14using namespace cgv::media::image;
15
16namespace cgv {
17 namespace render {
18 namespace gl {
19
20unsigned map_material_side(MaterialSide ms)
21{
22 static unsigned material_side_mapping[] = { GL_FALSE, GL_FRONT, GL_BACK, GL_FRONT_AND_BACK };
23 return material_side_mapping[ms];
24}
25
26
27unsigned int read_image_to_texture(const std::string& file_name, bool mipmaps, double* aspect_ptr, bool* has_alpha_ptr)
28{
29 std::vector<data_format> palette_formats;
30 std::vector<data_view> palettes;
31 data_format df;
32 data_view dv;
33 image_reader ir(df, &palette_formats);
34 if (!ir.read_image(file_name,dv,&palettes)) {
35 std::cerr << ir.get_last_error().c_str() << std::endl;
36 return -1;
37 }
38 if (aspect_ptr)
39 *aspect_ptr = (double)df.get_width()/df.get_height();
40 if (has_alpha_ptr) {
41 if (palette_formats.size() > 0 && df.get_nr_components() == 1 && df.get_component_name(0) == "0")
42 *has_alpha_ptr = palette_formats[0].get_component_index("A") != (unsigned int)-1;
43 else
44 *has_alpha_ptr = df.get_component_index("A") != (unsigned int)-1;
45 }
46 return create_texture(dv, mipmaps, &palettes);
47}
48
49bool read_image_to_textures(const std::string& file_name, std::vector<unsigned>& tex_ids, std::vector<float>& durations, bool mipmaps, double* aspect_ptr, bool* has_alpha_ptr)
50{
51 std::vector<data_format> palette_formats;
52 std::vector<data_view> palettes;
53 data_view dv;
54 data_format df;
55 image_reader ir(df, &palette_formats);
56 // open image
57 if (!ir.open(file_name)) {
58 std::cerr << ir.get_last_error().c_str() << std::endl;
59 return false;
60 }
61 palettes.resize(palette_formats.size());
62 // query number of images
63 unsigned nr = ir.get_nr_images();
64 tex_ids.resize(nr);
65 // create textures
66 glGenTextures(nr,&tex_ids.front());
67 // read images and create textures
68 durations.clear();
69 for (unsigned i=0; i<nr; ++i) {
70 // query palettes
71 for (unsigned j=0; j<palette_formats.size(); ++j) {
72 if (!ir.read_palette(j, palettes[j]))
73 return false;
74 }
75 durations.push_back(ir.get_image_duration());
76 if (durations.back() == 0)
77 durations.back() = 0.04f;
78 if (!ir.read_image(dv)) {
79 std::cerr << ir.get_last_error().c_str() << std::endl;
80 return false;
81 }
82 create_texture(dv,mipmaps,&palettes,tex_ids[i]);
83 }
84 if (aspect_ptr)
85 *aspect_ptr = (double)df.get_width()/df.get_height();
86 if (has_alpha_ptr) {
87 if (palette_formats.size() > 0 && df.get_nr_components() == 1 && df.get_component_name(0) == "0")
88 *has_alpha_ptr = palette_formats[0].get_component_index("A") != (unsigned int)-1;
89 else
90 *has_alpha_ptr = df.get_component_index("A") != (unsigned int)-1;
91 }
92 return true;
93}
94
97{
98 GLint lv;
99 glGetIntegerv(GL_LIGHT_MODEL_LOCAL_VIEWER, &lv);
100 prog.set_uniform(ctx, "local_viewer", (bool)(lv != 0));
101 unsigned n = ctx.get_max_nr_enabled_light_sources();
102 cgv::math::vec<int> enabled(n);
103 for (unsigned i=0; i<n; ++i)
104 glGetIntegerv(GL_LIGHT0+i, &enabled(i));
105 prog.set_uniform_array(ctx, "lights_enabled", enabled);
106}
107
108
109std::vector<shader_program*>& ref_shader_prog_stack()
110{
111 static std::vector<shader_program*> sps;
112 return sps;
113}
114
117{
118 ref_shader_prog_stack().push_back(&prog);
119}
120
123{
124 ref_shader_prog_stack().pop_back();
125}
126
128{
129 static shader_program prog;
130 if (!ref_shader_prog_stack().empty())
131 return *ref_shader_prog_stack().back();
132 std::cerr << "call to deprecated function ref_textured_material_prog()" << std::endl;
133 return prog;
134}
135
136static GLenum interface_ids[] = {
137 GL_UNIFORM,
138 GL_UNIFORM_BLOCK,
139 GL_PROGRAM_INPUT,
140 GL_PROGRAM_OUTPUT,
141 GL_VERTEX_SUBROUTINE, GL_TESS_CONTROL_SUBROUTINE, GL_TESS_EVALUATION_SUBROUTINE, GL_GEOMETRY_SUBROUTINE, GL_FRAGMENT_SUBROUTINE, GL_COMPUTE_SUBROUTINE,
142 GL_VERTEX_SUBROUTINE_UNIFORM, GL_TESS_CONTROL_SUBROUTINE_UNIFORM, GL_TESS_EVALUATION_SUBROUTINE_UNIFORM, GL_GEOMETRY_SUBROUTINE_UNIFORM, GL_FRAGMENT_SUBROUTINE_UNIFORM, GL_COMPUTE_SUBROUTINE_UNIFORM,
143 GL_TRANSFORM_FEEDBACK_VARYING,
144 GL_BUFFER_VARIABLE,
145 GL_SHADER_STORAGE_BLOCK
146};
147
148void print_program_ressources(shader_program& prog, const std::string& interface_name, ProgramInterface prog_intf)
149{
150 GLuint prog_id;
151 prog.put_id(prog_id);
152 GLint nr;
153 GLenum interface_id = interface_ids[prog_intf];
154 glGetProgramInterfaceiv(prog_id, interface_id, GL_ACTIVE_RESOURCES, &nr);
155 if (nr == -1)
156 return;
157 for (GLuint i = 0; i < GLuint(nr); ++i) {
158 char name[512];
159 GLsizei length;
160 glGetProgramResourceName(prog_id, interface_id, i, 512, &length, name);
161 std::cout << interface_name << "(" << i << ")=" << name << std::endl;
162 }
163}
164
165
166 }
167 }
168}
std::string get_component_name(unsigned int i) const
return the name of the component with index i
unsigned int get_component_index(const std::string &name) const
return the index of a component given by name or -1 if not found
unsigned int get_nr_components() const
return the number of components
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
size_t get_width() const
return the resolution in the first dimension, or 1 if not defined
size_t get_height() const
return the resolution in the second dimension, or 1 if not defined
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
A column vector class.
Definition vec.h:28
the image reader chooses a specific reader automatically based on the extension of the given file nam...
unsigned get_nr_images() const
return the number of images in the file, what can cause the whole file to be scanned
const std::string & get_last_error() const
return a reference to the last error message
bool read_image(const std::string &file_name, cgv::data::data_view &dv, std::vector< cgv::data::data_view > *palettes=0)
read the whole image into the given data view.
float get_image_duration() const
return the duration of the current image in seconds, if returned value is 0, no duration is available
bool read_palette(unsigned int i, cgv::data::data_view &dv)
read the i-th palette in case of a paletted file format, and handle the data view as in the read_imag...
bool open(const std::string &file_name)
open the file and read the image header in order to determine the data format of the file,...
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual unsigned get_max_nr_enabled_light_sources() const
return maximum number of light sources, that can be enabled in parallel
Definition context.cxx:659
void put_id(T &id) const
cast the refence to rendering api specific representation of component id to the specified type
Definition context.h:319
a shader program combines several shader code fragments to a complete definition of the shading pipel...
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 set_uniform_array(const context &ctx, const std::string &name, const T &array)
set uniform array from array array where number elements can be derived from array through array_desc...
namespace for data management components
namespace for image processing
void push_textured_material_prog(shader_program &prog)
push a shader program onto the textured material stack
Definition gl_tools.cxx:116
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 set_lighting_parameters(context &ctx, shader_program &prog)
set the program variables needed by the lighting.glsl shader
Definition gl_tools.cxx:96
bool read_image_to_textures(const std::string &file_name, std::vector< unsigned > &tex_ids, std::vector< float > &durations, bool mipmaps, double *aspect_ptr, bool *has_alpha_ptr)
read several images from one image file that can contain an animation
Definition gl_tools.cxx:49
void print_program_ressources(shader_program &prog, const std::string &interface_name, ProgramInterface prog_intf)
print program resources for given interface
Definition gl_tools.cxx:148
unsigned int read_image_to_texture(const std::string &file_name, bool mipmaps, double *aspect_ptr, bool *has_alpha_ptr)
read the given image file into a texture and return the texture id or -1 in case of failure.
Definition gl_tools.cxx:27
shader_program & ref_textured_material_prog(context &ctx)
return a reference to the singleton textured material shader program, which is constructed on demand ...
Definition gl_tools.cxx:127
ProgramInterface
different program interfaces
Definition gl_tools.h:111
void pop_textured_material_prog()
pop a shader program from the textured material stack
Definition gl_tools.cxx:122
MaterialSide
different sides of a material
Definition context.h:128
the cgv namespace
Definition print.h:11