gKit2 light

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

uniforms.cpp
1
2 #include <cstdio>
3
4 #include <set>
5
6 #include "program.h"
7 #include "uniforms.h"
8
9
10 static
11 int location( const GLuint program, const char *uniform )
12 {
13 if(program == 0)
14 return -1;
15
16 // recuperer l'identifiant de l'uniform dans le program
17 GLint location= glGetUniformLocation(program, uniform);
18 if(location < 0)
19 {
20 char error[1024]= { 0 };
21 #ifdef GL_VERSION_4_3
22 {
23 char label[1024];
24 glGetObjectLabel(GL_PROGRAM, program, sizeof(label), NULL, label);
25
26 sprintf(error, "uniform( %s %u, '%s' ): not found.", label, program, uniform);
27 }
28 #else
29 sprintf(error, "uniform( program %u, '%s'): not found.", program, uniform);
30 #endif
31
32 static std::set<std::string> log;
33 if(log.insert(error).second == true)
34 // pas la peine d'afficher le message 60 fois par seconde...
35 printf("%s\n", error);
36
37 return -1;
38 }
39
40 #ifndef GK_RELEASE
41 // verifier que le program est bien en cours d'utilisation, ou utiliser glProgramUniform, mais c'est gl 4
42 GLuint current;
43 glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *) &current);
44 if(current != program)
45 {
46 char error[1024]= { 0 };
47 #ifdef GL_VERSION_4_3
48 {
49 char label[1024];
50 glGetObjectLabel(GL_PROGRAM, program, sizeof(label), NULL, label);
51 char labelc[1024];
52 glGetObjectLabel(GL_PROGRAM, current, sizeof(labelc), NULL, labelc);
53
54 sprintf(error, "uniform( %s %u, '%s' ): invalid shader program %s %u", label, program, uniform, labelc, current);
55 }
56 #else
57 sprintf(error, "uniform( program %u, '%s'): invalid shader program %u...", program, uniform, current);
58 #endif
59
60 printf("%s\n", error);
61 glUseProgram(program);
62 }
63 #endif
64
65 return location;
66 }
67
68 void program_uniform( const GLuint program, const char *uniform, const unsigned int v )
69 {
70 glUniform1ui( location(program, uniform), v );
71 }
72
73 void program_uniform( const GLuint program, const char *uniform, const int v )
74 {
75 glUniform1i( location(program, uniform), v );
76 }
77
78 void program_uniform( const GLuint program, const char *uniform, const float v )
79 {
80 glUniform1f( location(program, uniform), v );
81 }
82
83 void program_uniform( const GLuint program, const char *uniform, const vec2& v )
84 {
85 glUniform2fv( location(program, uniform), 1, &v.x );
86 }
87
88 void program_uniform( const GLuint program, const char *uniform, const vec3& v )
89 {
90 glUniform3fv( location(program, uniform), 1, &v.x );
91 }
92
93 void program_uniform( const GLuint program, const char *uniform, const Point& a )
94 {
95 glUniform3fv( location(program, uniform), 1, &a.x );
96 }
97
98 void program_uniform( const GLuint program, const char *uniform, const Vector& v )
99 {
100 glUniform3fv( location(program, uniform), 1, &v.x );
101 }
102
103 void program_uniform( const GLuint program, const char *uniform, const vec4& v )
104 {
105 glUniform4fv( location(program, uniform), 1, &v.x );
106 }
107
108 void program_uniform( const GLuint program, const char *uniform, const Color& c )
109 {
110 glUniform4fv( location(program, uniform), 1, &c.r );
111 }
112
113 void program_uniform( const GLuint program, const char *uniform, const Transform& v )
114 {
115 glUniformMatrix4fv( location(program, uniform), 1, GL_TRUE, v.buffer() );
116 }
117
118 void program_use_texture( const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler )
119 {
120 // verifie que l'uniform existe
121 int id= location(program, uniform);
122 if(id < 0)
123 return;
124
125 // selectionne l'unite de texture
126 glActiveTexture(GL_TEXTURE0 + unit);
127 // configure la texture
128 glBindTexture(GL_TEXTURE_2D, texture);
129
130 if(sampler > 0)
131 // et ses parametres de filtrage, si necessaire
132 glBindSampler(unit, sampler);
133
134 // transmet l'indice de l'unite de texture au shader
135 glUniform1i(id, unit);
136 }
uniforms.h
vec3
vecteur generique, utilitaire.
Definition: vec.h:104
vec2
vecteur generique, utilitaire.
Definition: vec.h:94
Color
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:13
vec4
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition: vec.h:121
program.h
program_uniform
void program_uniform(const GLuint program, const char *uniform, const unsigned int v)
affecte une valeur a un uniform du shader program. uint.
Definition: uniforms.cpp:68
program_use_texture
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrages...
Definition: uniforms.cpp:118
Vector
representation d'un vecteur 3d.
Definition: vec.h:42
Transform::buffer
const float * buffer() const
renvoie l'adresse de la premiere valeur de la matrice.
Definition: mat.h:44
printf
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
Transform
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:20
Point
representation d'un point 3d.
Definition: vec.h:19
label
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
Definition: widgets.cpp:142