Bonjours les GL,
j'ai le problème suivant: je n'arrive pas a faire fonctionner le concept de shading.
Ou plutôt a implémenter le fonctionnement d'une source lumineuse qui projetterai de la lumière sur un polyèdre
ayant une couleur uniforme pour toutes ses faces.
Le but étant bien sur de calculer le taux de lumière réfracter donc a illuminer mon polyèdre de multiples intensités d'une couleurs,
selon la position de la source de lumière.
J'utilise pour l'instant l'algorithme de flat-shading: qui se base sur un calcule de deux vecteurs.
1. Le vecteur de position du vertex a colorer.
2. La source de lumière.
Et qui renvoie un cosinus dont ont peut se servir de la valeur (a condition que valeur == -1.0-0 ; // face a la lumière)
pour multiplier la valeur de départ de la couleur.
:note: En fait le flat-shading n'est pas exact mais acceptable comme algorithme d'illumination et il demande peu de calcule.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 if (cos(alpha) < 0) { // The vertex and the light vectors make an angle of 90°-270°. // So the light is refracted in relationship to his position. color = vertex * -cos(alpha) ; } else { // The vertex is not lighted. color = color ; }
---
J'ai essayer de multiples manières et selon le procéder suivant:
en implémentant la fonction de calcule du multiplicateur dans le shader.
---
Bon un du code est bien mieux que du blabla.
Voici ma méthode d'affichage:
Vous voyez bien que j'implémente les vertices dans un buffer mais le(s) couleur(s) sont passer par une variable uniform (Une couleur par face).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 void Ellipsoid::display() { GLint color_id ; GLint mv_matrix ; GLint cam_matrix ; GLint proj_matrix ; GLint light_src_id ; light_src_id = glGetUniformLocation(program, "lighting_pos") ; color_id = glGetUniformLocation(program, "color_vec") ; mv_matrix = glGetUniformLocation(program, "mv_matrix") ; cam_matrix = glGetUniformLocation(program, "cam_matrix") ; proj_matrix = glGetUniformLocation(program, "proj_matrix") ; // So that we can get the value for debugging purpose. GLint testing_id = glGetUniformLocation(program, "testing") ; glUseProgram(program) ; glUniformMatrix4fv(proj_matrix, 1, GL_FALSE, glm::value_ptr(proj)) ; glUniformMatrix4fv(cam_matrix, 1, GL_FALSE, glm::value_ptr(camera)) ; glUniformMatrix4fv(mv_matrix, 1, GL_FALSE, matrix.data() ) ; GLint position_id = glGetAttribLocation(program, "position"); GLint text_sampler2D = glGetUniformLocation(program, "myTextureSampler"); GLint texture_id = glGetAttribLocation(program, "vertexUV"); GLint intensity_id = glGetUniformLocation(program, "color_intensity"); glUniform1i(text_sampler2D, 0); glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, NULL) ; glBindBuffer(GL_ARRAY_BUFFER, buffers[1]); glVertexAttribPointer(texture_id, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(position_id); glEnableVertexAttribArray(texture_id); /** Start draw ellipsoid_sphere. **/ int offset = 0 ; for (uint32_t c=0 ; c < number_of_quads ; c++) { if (use_colors) { if (uni) { // Boolean member value. if (not colors.empty()) { glUniform4fv(color_id, 1, colors.at(0).get_vector()) ; } } else { if (not colors.empty()) { glUniform4fv(light_src_id, 1, glm::value_ptr(lighting_vector)) ; glUniform4fv(color_id, 1, colors.at(c % colors.size()).get_vector()) ; #if 1 // We grab the value of the output of the shadering function for displaying it's value: It always equal zero.zero. float testing_val = 0 ; glGetUniformfv(program, testing_id, &testing_val) ; fprintf(stderr,"-> %.12f\n", testing_val) ; #endif } } } else if (use_textures) { // Boolean member value. if (uni) { glBindTexture(GL_TEXTURE_2D, texture_ids.at(0)); } else { glBindTexture(GL_TEXTURE_2D, texture_ids.at(c)); } } if (show_faces) { // Boolean member value. glDrawArrays(GL_TRIANGLE_FAN, offset, 4); } if (show_lines) { // Boolean member value. glUniform4fv(color_id, 1, lines_colors.get_vector()) ; glDrawArrays(GL_LINE_LOOP, offset, 4); } offset += 4 ; } /** End draw ellipsoid_sphere. **/ glDisableVertexAttribArray(position_id); glDisableVertexAttribArray(texture_id); }
Du coup Il m'est pas possible de multiplier un vertice par une fonction qui renvoie le cosinus des deux vecteurs.
Autrement dit d'implémenter la fonction de calcule autre part que dans le shader.
Et en plus comme je ne peut accéder au vertice qui est dans le buffer mais je ne peut utiliser ma méthode de flat-shading
qui consiste a multiplier la valeur de la couleur d'origine du polyèdre par le cosinus si celui ci < 0.
---
Le shader de vertice est le suivant:
Le problème est aussi que la même fonction nommer test(...) implémenté dans le code source fonctionne: elle ne renvoie pas toujours zéro.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 #version 150 core //#version 430 core in vec4 position ; out VS_OUT { vec4 color; } vs_out ; #if 1 float test(vec3 vertex, vec3 lighting_pos) { float vertex_vec_length = length(vertex) ; float lighting_pos_length = length(lighting_pos) ; vertex = normalize(vertex) ; lighting_pos = normalize(lighting_pos) ; float cos_of_dot = (vertex[0] * lighting_pos[0] + vertex[1] * lighting_pos[1] + vertex[2] * lighting_pos[2]) / (vertex_vec_length * lighting_pos_length) ; return cos_of_dot ; } #endif uniform vec4 color_vec ; uniform vec4 lighting_pos ; uniform mat4 mv_matrix ; uniform mat4 cam_matrix ; uniform mat4 proj_matrix ; uniform float color_intensity ; #if 1 uniform float testing = test(position, lighting_pos) ; #endif void main(void) { gl_Position = proj_matrix * cam_matrix * mv_matrix * position ; vs_out.color = color_vec ; // The goal is to multiplicate: color_vec * test(position, lighting_pos) but test(...) return always 0.0 ; //vs_out.color = color_vec * test(position, lighting_pos) ; // In this case it display nothing.... }
par contre dans le shader elle ne fonctionne pas (elle renvoie toujours zero.zero) ce qui me parait bizarroid...
---
Toute aide, conseils, réponses est la bienvenue.
Merci de bien vouloir éclairé ma lanterne.
P.S: J'ai essayer de maintes fois et j'avais même réussis a implémenter le concept en thrash python.
Partager