Salut les OpenGL.
Comme dit dans le titre j'ai créer une classe Cube dont je cherche a afficher 5 instances, toutes a des positions différentes tournant sur leurs axes.
Et le résultat est que les 5 cubes sont affichés mais pas comment j'aimerai:
Les cubes s'affichent tout d'abord a la même position le premier tourne sur lui-même et les autres accomplissent une rotation complète autour du premier cube pour revenir a la même position de départ: à la même position que le premier cube.
Je suppose que le problème vient de mes shaders qui donc je suppose entremêle l'affichage...
Voici le fichier vertex shader: mv_cube_vertice_shader2.glsl.
Voici le fichier fragment shader: mv_cube_fragment_shader2.glsl.
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 #version 420 core in vec4 position; out VS_OUT { vec4 color; } vs_out; uniform vec4 mv_color ; uniform mat4 mv_matrix ; void main(void) { gl_Position = mv_matrix * position ; vs_out.color = mv_color ; }
Le fichier: main.cpp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 #version 420 core out vec4 color; in VS_OUT { vec4 color; } fs_in; void main(void) { color = fs_in.color ; }
Et les fichiers inclus: Cube.h.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 #include <SDL2/SDL.h> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> #include <random> #include <GL/glew.h> #define WIDTH (int16_t) 800 #define HEIGHT (int16_t) 600 bool loop ; #define set_loop(x) (loop=(x)) #include "./Files/SDL2_utils.cpp" #include "./glutils/Vector.h" #include "./glutils/Vertex.h" #include "./glutils/Matrix.h" #include "./glutils/Color.h" #include "./glutils/Cube.h" #include "./glutils/gl_data.h" /** To compil with: * * $ g++ -std=c++11 main.cpp -lSDL2 -lGL -lGLEW * ***********************************************/ using namespace std ; int main(void) { if (sdl_init(SDL_INIT_EVERYTHING) != 0) { fprintf(stderr,"Failed to init SDL2 \n") ; exit(EXIT_FAILURE) ; } /** Setting GL attribues if not supported the GL context creation will fail. **/ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) ; // SDL_GL_CONTEXT_PROFILE_CORE SDL_GL_CONTEXT_PROFILE_COMPATIBILITY SDL_Window* pWindow = NULL; pWindow = sdl_create_window("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL) ; if( pWindow == NULL ) { fprintf(stderr,"Fail to create SDL window: %s\n", SDL_GetError()); exit(EXIT_FAILURE) ; } /** GL context creation. **/ SDL_GLContext glcontext ; if ((glcontext = SDL_GL_CreateContext(pWindow)) == NULL ) { fprintf(stderr,"Fail to create SDL GL context: %s\n", SDL_GetError()); exit(EXIT_FAILURE) ; } if (SDL_GL_MakeCurrent(pWindow, glcontext) != 0) { fprintf(stderr,"Fail to make current window GL context: %s\n", SDL_GetError()); exit(EXIT_FAILURE) ; } if (SDL_GL_SetSwapInterval(-1) == -1) { fprintf(stderr,"GL swap interval not suppported: %s\n", SDL_GetError()); exit(EXIT_FAILURE) ; } // Initialize GLEW //glewExperimental=true; if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); return -1; } int maj, min ; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &maj) ; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &min) ; fprintf(stdout,"OpenGL Version %d.%d\n", maj, min) ; /** Load shaders program: don't know event to load a program id pro Cube instance or not -> the result is the same displaying. **/ //GLuint program = LoadShaders("./shaders/mv_cube_vertice_shader2.glsl","./shaders/mv_cube_fragment_shader2.glsl") ; /** Define clear color. **/ glClearColor(0.0, 0.0, 0.0, 1.0) ; /** Depth settings (needed because it default to 0.0 to 1.0). **/ glEnable(GL_DEPTH_TEST) ; glDepthRangef(1.0, -1.0) ; /** Initialize a random device: **/ random_device rd; mt19937 gen(rd()); /** Define some colors. **/ Color<float> red(1.0, 0.0, 0.0, 1.0) ; Color<float> green(0.0, 1.0, 0.0, 1.0) ; Color<float> blue(0.0, 0.2, 1.0, 1.0) ; Color<float> yellow(1.0, 1.0, 0.0, 1.0) ; Color<float> turkish(0.0, 1.0, 1.0, 1.0) ; Color<float> pink(1.0, 0.0, 1.0, 1.0) ; vector<Color<float>> cube_colors ; cube_colors.push_back(red) ; cube_colors.push_back(green) ; cube_colors.push_back(blue) ; cube_colors.push_back(yellow) ; cube_colors.push_back(turkish) ; cube_colors.push_back(pink) ; vector<Cube> cubes ; vector<Matrix<float>> matrixes ; uniform_real_distribution<float> rand_vertex_pos(-0.5, 0.5) ; uniform_real_distribution<float> rand_cube_size(0.25, 0.45) ; for (int c=0 ; c < 5 ; c++) { /** Genetrate Cube instance: **/ Cube cube(Vertex<float>(rand_vertex_pos(gen), rand_vertex_pos(gen), 0.0f), rand_cube_size(gen)) ; /** Load a program to every instance: it has the same effect as loading one main program. **/ GLuint program = LoadShaders("./shaders/mv_cube_vertice_shader2.glsl","./shaders/mv_cube_fragment_shader2.glsl") ; /** Parametrize the cube instance: **/ cube.set_program(program) ; cube.set_colors(cube_colors) ; /** Store cube instance. **/ cubes.push_back(cube) ; /** Store a matrix for every Cube instance. **/ Matrix<float> matrix ; matrixes.push_back(matrix) ; } /** Executionn speed. **/ static const int FPS=30 ; static const int DELAY=1000/FPS ; static const unsigned int speed = 75 ; set_loop(true) ; while (loop) { long int start_frame = time(0)*1000 ; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; int cntr=0 ; for (auto &cube : cubes) { /** Translate cube center to origin. **/ matrixes.at(cntr).translate(-cube.get_center()) ; /** Rotate on his own axes because the cube is centered on the origin. **/ matrixes.at(cntr).rotate_x(1) ; matrixes.at(cntr).rotate_y(1) ; matrixes.at(cntr).rotate_z(1) ; /** TYranslate the cube back to his start position. **/ matrixes.at(cntr).translate(cube.get_center()) ; /** Apply matrix. **/ cube.set_matrix(matrixes.at(cntr)) ; /** Dislaying cube instance . **/ cube.display() ; cntr++ ; } SDL_GL_SwapWindow(pWindow) ; get_events(); long int end_frame=time(0)*1000 ; if (DELAY > (end_frame-start_frame)) { SDL_Delay( DELAY - (end_frame-start_frame) + speed ) ; } } SDL_GL_DeleteContext(glcontext); SDL_Quit() ; return 0 ; }
Et le fichier: Matrix.h.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 #ifndef GL_UTILS_CUBE_H #define GL_UTILS_CUBE_H #include <array> #include <vector> #include <algorithm> #include <GL/glew.h> #include "Vertex.h" #include "Color.h" #include "Matrix.h" using namespace std ; class Quad { array<float, 4*4> ret ; vector<Vertex<float>> faces ; public : Quad(const Vertex<float> v1, const Vertex<float> v2, const Vertex<float> v3, const Vertex<float> v4) { faces.push_back(v1) ; faces.push_back(v2) ; faces.push_back(v3) ; faces.push_back(v4) ; update_data() ; } ; float operator [] (const int idx) { return ret.at(idx) ; } void update_data() { int idx=0 ; for ( uint8_t cc=0 ; cc < faces.size() ; cc++) { for ( int8_t c=0 ; c < 4 ; c++) { ret.at(idx)=faces.at(cc)[c] ; idx++ ; } } } ; float *get_quad() { return ret.data() ; } Vertex<float> get_vertex(const int idx) { return faces.at(idx) ; } ; } ; class Cube { vector<Quad> faces ; array<float, 4*4*6> ret ; unsigned int buffer ; vector<Color<float>> colors ; int mv_color ; int mv_matrix ; unsigned int program ; vector<unsigned int> texture_ids ; Vertex<float> cube_center ; Matrix<float> matrix ; public : static int buffer_counter ; Cube(Vertex<float> center, float side_length) ; void display() ; float *get_cube() { return ret.data() ; } ; void set_colors(vector<Color<float>> col) { copy(col.begin(), col.end(), colors.begin() ) ; } void set_program(unsigned int prg) { program=prg ; mv_color = glGetUniformLocation(program, "mv_color"); mv_matrix = glGetUniformLocation(program, "mv_matrix") ; } void set_matrix(Matrix<float> mat) { matrix = mat ; } ; Vertex<float> get_center() { return cube_center ; } ; } ; int Cube::buffer_counter=0 ; Cube::Cube(Vertex<float> center, float side_length) { cube_center=center ; const Vertex<float> face_front_up_left(center[0]-side_length/2, center[1]+side_length/2, center[2]-side_length/2) ; const Vertex<float> face_front_up_right(center[0]+side_length/2, center[1]+side_length/2, center[2]-side_length/2) ; const Vertex<float> face_front_down_right(center[0]+side_length/2, center[1]-side_length/2, center[2]-side_length/2) ; const Vertex<float> face_front_down_left(center[0]-side_length/2, center[1]-side_length/2, center[2]-side_length/2) ; const Vertex<float> face_back_up_left(center[0]-side_length/2, center[1]+side_length/2, center[2]+side_length/2) ; const Vertex<float> face_back_up_right(center[0]+side_length/2, center[1]+side_length/2, center[2]+side_length/2) ; const Vertex<float> face_back_down_right(center[0]+side_length/2, center[1]-side_length/2, center[2]+side_length/2) ; const Vertex<float> face_back_down_left(center[0]-side_length/2, center[1]-side_length/2, center[2]+side_length/2) ; Quad face_front(face_front_down_left, face_front_up_left, face_front_up_right, face_front_down_right) ; Quad face_back(face_back_down_left, face_back_up_left, face_back_up_right, face_back_down_right) ; Quad face_left(face_front_up_left, face_back_up_left, face_back_down_left, face_front_down_left) ; Quad face_right(face_front_up_right, face_back_up_right, face_back_down_right, face_front_down_right) ; Quad face_up(face_front_up_left, face_back_up_left, face_back_up_right, face_front_up_right) ; Quad face_down(face_front_down_left, face_front_down_right, face_back_down_right, face_back_down_left) ; faces.push_back(face_front) ; faces.push_back(face_back) ; faces.push_back(face_left) ; faces.push_back(face_right) ; faces.push_back(face_up) ; faces.push_back(face_down) ; int c=0 ; for (auto &val : faces) { for (int cc=0 ; cc < 4*4 ; cc++) { ret.at(c)=val[cc] ; c++ ; } } glGenBuffers(1, &buffer) ; glBindBuffer(GL_ARRAY_BUFFER, buffer) ; glBufferData(GL_ARRAY_BUFFER, 4*4*6*sizeof(float), get_cube(), GL_STATIC_DRAW) ; glVertexAttribPointer(buffer_counter, 4, GL_FLOAT, GL_FALSE, 0, NULL) ; glEnableVertexAttribArray(buffer_counter) ; buffer_counter++ ; Color<float> dummy(0.0,0.0,0.0) ; colors.resize(6,dummy) ; } void Cube::display() { glUseProgram(program) ; glUniformMatrix4fv(mv_matrix, 1, GL_FALSE, matrix.get_matrix()); /** Start draw cube. **/ glUniform4fv(mv_color, 1, colors.at(0).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 0, 4) ; glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; glUniform4fv(mv_color, 1, colors.at(1).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 4, 4); glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; glUniform4fv(mv_color, 1, colors.at(2).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 8, 4); glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; glUniform4fv(mv_color, 1, colors.at(3).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 12, 4); glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; glUniform4fv(mv_color, 1, colors.at(4).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 16, 4); glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; glUniform4fv(mv_color, 1, colors.at(5).get_vector()) ; glDrawArrays(GL_TRIANGLE_FAN, 20, 4); glMemoryBarrier(GL_UNIFORM_BARRIER_BIT) ; /** End draw cube. **/ } #endif
Et le fichier: Vertex.h.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 #ifndef GL_UTILS_MATRIX #define GL_UTILS_MATRIX #include <array> #include <cmath> #include "Vector.h" #include "Vertex.h" template <typename T> class Matrix { private : array<T, 4*4> main_matrix ; array<T, 4*4> processing_matrix ; void multiply() ; public : Matrix() ; Matrix(const Matrix<T> &matrix) ; void translate(Vertex<T> vector) ; void translate(Vector<T> vector) ; void scale(T factor) ; void scale(Vector<T> vector) ; void rotate_x(T degrees) ; void rotate_y(T degrees) ; void rotate_z(T degrees) ; void apply_vertex(Vertex<T> &vertex) ; const T *get_matrix() const { return main_matrix.data() ; } } ; template <typename T> Matrix<T>::Matrix() { main_matrix.at(0) = 1.0 ; main_matrix.at(4) = 0.0 ; main_matrix.at(8) = 0.0 ; main_matrix.at(12) = 0.0 ; main_matrix.at(1) = 0.0 ; main_matrix.at(5) = 1.0 ; main_matrix.at(9) = 0.0 ; main_matrix.at(13) = 0.0 ; main_matrix.at(2) = 0.0 ; main_matrix.at(6) = 0.0 ; main_matrix.at(10) = 1.0 ; main_matrix.at(14) = 0.0 ; main_matrix.at(3) = 0.0 ; main_matrix.at(7) = 0.0 ; main_matrix.at(11) = 0.0 ; main_matrix.at(15) = 1.0 ; } template <typename T> Matrix<T>::Matrix(const Matrix<T> &matrix) { copy(matrix.main_matrix.begin(), matrix.main_matrix.end(), main_matrix.begin()) ; copy(matrix.processing_matrix.begin(), matrix.processing_matrix.end(), processing_matrix.begin()) ; } template <typename T> void Matrix<T>::translate(Vertex<T> vector) { /** Translating trough a given vector. **/ processing_matrix.at(0) = 1.0 ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = vector[0] ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = 1.0 ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = vector[1] ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = 1.0 ; processing_matrix.at(14) = vector[2] ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::translate(Vector<T> vector) { /** Translating trough a given vector. **/ processing_matrix.at(0) = 1.0 ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = vector[0] ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = 1.0 ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = vector[1] ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = 1.0 ; processing_matrix.at(14) = vector[2] ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::scale(T factor) { /** Scaling . **/ processing_matrix.at(0) = factor ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = 0.0 ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = factor ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = 0.0 ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = factor ; processing_matrix.at(14) = 0.0 ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::scale(Vector<T> vector) { /** Scaling . **/ processing_matrix.at(0) = vector[0] ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = 0.0 ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = vector[1] ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = 0.0 ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = vector[2] ; processing_matrix.at(14) = 0.0 ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::rotate_x(T degrees) { T radians = degrees / 180.0 * M_PI ; T c= (T) cos(radians) ; // Compute the cosine from the given angle. T s= (T) sin(radians) ; // Compute the sine from the given angle. processing_matrix.at(0) = 1.0 ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = 0.0 ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = c ; processing_matrix.at(9) = -s ; processing_matrix.at(13) = 0.0 ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = s ; processing_matrix.at(10) = c ; processing_matrix.at(14) = 0.0 ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::rotate_y(T degrees) { T radians = degrees / 180.0 * M_PI ; T c= (T) cos(radians) ; // Compute the cosine from the given angle. T s= (T) sin(radians) ; // Compute the sine from the given angle. processing_matrix.at(0) = c ; processing_matrix.at(4) = 0.0 ; processing_matrix.at(8) = s ; processing_matrix.at(12) = 0.0 ; processing_matrix.at(1) = 0.0 ; processing_matrix.at(5) = 1.0 ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = 0.0 ; processing_matrix.at(2) = -s ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = c ; processing_matrix.at(14) = 0.0 ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::rotate_z(T degrees) { T radians = degrees / 180.0 * M_PI ; T c= (T) cos(radians) ; // Compute the cosine from the given angle. T s= (T) sin(radians) ; // Compute the sine from the given angle. processing_matrix.at(0) = c ; processing_matrix.at(4) = -s ; processing_matrix.at(8) = 0.0 ; processing_matrix.at(12) = 0.0 ; processing_matrix.at(1) = s ; processing_matrix.at(5) = c ; processing_matrix.at(9) = 0.0 ; processing_matrix.at(13) = 0.0 ; processing_matrix.at(2) = 0.0 ; processing_matrix.at(6) = 0.0 ; processing_matrix.at(10) = 1.0 ; processing_matrix.at(14) = 0.0 ; processing_matrix.at(3) = 0.0 ; processing_matrix.at(7) = 0.0 ; processing_matrix.at(11) = 0.0 ; processing_matrix.at(15) = 1.0 ; multiply() ; } template <typename T> void Matrix<T>::multiply() { array<T, 4*4> tmp_matrix ; int x=0 ; while (x < 16) { // Multiply the processing matrix with the main matrix values. int value1 = x % 4 ; // Column value. int value2 = (x/4)*4 ; // Row value. tmp_matrix.at(x) = processing_matrix.at(value1) * main_matrix.at(value2+0) + processing_matrix.at(value1+4) * main_matrix.at(value2+1) + processing_matrix.at(value1+8) * main_matrix.at(value2+2) + processing_matrix.at(value1+12) * main_matrix.at(value2+3) ; x++ ; } for (int c=0 ; c < 16 ; c++) { // Copy the result in the main matrix. main_matrix[c]=tmp_matrix[c] ; } } template <typename T> void Matrix<T>::apply_vertex(Vertex<T> &vertex) { /** Return an vertex issue from the main matrix multiplying. **/ T x = vertex[0] * main_matrix.at(0) + vertex[1] * main_matrix.at(4) + vertex[2] * main_matrix.at(8) + main_matrix.at(12) ; T y = vertex[0] * main_matrix.at(1) + vertex[1] * main_matrix.at(5) + vertex[2] * main_matrix.at(9) + main_matrix.at(13) ; T z = vertex[0] * main_matrix.at(2) + vertex[1] * main_matrix.at(6) + vertex[2] * main_matrix.at(10) + main_matrix.at(14) ; vertex[0]=x ; vertex[0]=y ; vertex[2]=z ; } #endif
Et finalement le fichier: gl_data.h.
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 #ifndef GL_UTILS_VERTEX_H #define GL_UTILS_VERTEX_H #include <array> template <typename T> class Vertex { std::array<T, 4> vertex ; public : Vertex() {} ; Vertex(const T x, const T y, const T z, const T w=1) { vertex={x,y,z,w} ;} ; T& operator [] ( const int idx) { return vertex.at(idx) ; } ; Vertex& operator -() { vertex[0] = -vertex[0] ; vertex[1] = -vertex[1] ; vertex[2] = -vertex[2] ; return *this ; } const T *get_vector() const { return vertex.data() ; } ; } ; #endif
Je ne sais précisement d'ou vient le problème et toute aide est la bienvenue.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 #ifndef GL_UTILS_DATA_H #define GL_UTILS_DATA_H #include <iostream> #include <fstream> #include <string> GLuint load_texture(string path) { string cmd ; cmd += "python " ; cmd += "./scripts/generate_texture_datas.py " ; cmd += path ; if (system(cmd.c_str()) != 0) { cout << "Error launching script generate_texture_datas.py on file:\n" << path << endl ; exit(EXIT_FAILURE) ; } ifstream r_file ; r_file.open("/tmp/texture.px") ; if (! r_file) { cout << "Fail to open file: " << "/tmp/texture.px" << endl ; exit(EXIT_FAILURE) ; } /** Setting file position to end **/ r_file.seekg(0, r_file.end) ; /** Getting the file size. **/ streampos file_size = r_file.tellg() ; /** Reset file position to begin to read it entirely thoughhis size know. **/ r_file.seekg(0, r_file.beg) ; /** Setting buffer sized as great as the file content. **/ string data_buffer(file_size,'\0') ; /** rw operations. (&*buffer.begin()) **/ r_file.read((&*data_buffer.begin()), file_size) ; r_file.close() ; r_file.open("/tmp/texture.info") ; if (! r_file) { cout << "Fail to open file: " << "/tmp/texture.info" << endl ; exit(EXIT_FAILURE) ; } string input ; int mode, byte_count ; int width, height ; getline(r_file, input) ; if (input == "RGB") { mode = GL_RGB ; byte_count=3 ; cout << path << " " << "RGB\n" ; } else if (input == "RGBA") { mode = GL_RGBA ; byte_count=4 ; cout << path << " " << "RGBA\n" ; } else { cout << "Cannot identify mode\n" ; exit(EXIT_FAILURE) ; } input.clear(); getline(r_file, input) ; width = stoi(input) ; input.clear(); getline(r_file, input) ; height = stoi(input) ; r_file.close() ; cout << path << " " << "width: " << width << " height: " << height << endl ; GLuint text_id ; glGenTextures(1, &text_id) ; glBindTexture(GL_TEXTURE_2D, text_id); glTexImage2D(GL_TEXTURE_2D, //target 0, //level=First mip-level 4, //internal_format=bytes per pixel width, //image width height, //image height 0, //border=texture border set or not 1|0 GL_RGBA, //format=GL_RGB|GL_RGBA GL_UNSIGNED_BYTE, //type=storing format (unsigned char *) data_buffer.data()) ; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); cout << "\nSuccess to load texture from file:\n" << path << endl ; cout.flush() ; return text_id ; } GLuint LoadShaders(string vertex_file_path, string fragment_file_path) { // Crée les shaders GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER) ; GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER) ; // Lit le code du vertex shader à partir du fichier string VertexShaderCode ; ifstream VertexShaderStream(vertex_file_path.c_str(), ios::in) ; if (VertexShaderStream.is_open()) { string Line = ""; while (getline(VertexShaderStream, Line)) { VertexShaderCode += "\n" + Line; } VertexShaderStream.close(); } // Lit le code du fragment shader à partir du fichier string FragmentShaderCode; ifstream FragmentShaderStream(fragment_file_path.c_str(), ios::in); if (FragmentShaderStream.is_open()) { string Line = ""; while (getline(FragmentShaderStream, Line)) { FragmentShaderCode += "\n" + Line; } FragmentShaderStream.close(); } GLint Result = GL_FALSE; int InfoLogLength; // Compile le vertex shader printf("Compiling shader : %s\n", vertex_file_path.c_str()); char const * VertexSourcePointer = VertexShaderCode.c_str(); glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); glCompileShader(VertexShaderID); // Vérifie le vertex shader glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector<char> VertexShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); // Compile le fragment shader printf("Compiling shader : %s\n", fragment_file_path.c_str()); char const * FragmentSourcePointer = FragmentShaderCode.c_str(); glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); glCompileShader(FragmentShaderID); // Vérifie le fragment shader glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector<char> FragmentShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); // Lit le programme fprintf(stdout, "Linking program\n"); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); // Vérifie le programme glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); vector<char> ProgramErrorMessage(InfoLogLength,'\0') ; glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "Error message: %s\n", &ProgramErrorMessage[0]); glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); return ProgramID; } #endif
Si vous avez des commentaires, conseils, critiques sur mon code ou ma façon de coder ils sont la bienvenue.
Merci pour vos réponses et votre précieuse aide.
Luke spywoker.
Partager