bonjour,

j'essaie de faire un programme en utilisant opengl et les shader, mais j'obtiens une fenêtre vide lorsque j'utilise mon programme et je ne vois pas ce qui cloche.

Voici mon code:
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
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
#include <fstream>
 
unsigned int vertexID, fragmentID, programID;
int positionIndex, colorIndex;
GLuint bufferVBO[3];
 
GLfloat cubeArray[24] = {
1.0f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f,
1.0f, 0.0f, 1.0f, -1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f
};
 
GLfloat colorArray[24] = {
0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f
};
 
GLuint indiceArray[36] = {
0,1,2,2,1,3,
4,5,6,6,5,7,
3,1,5,5,1,7,
0,2,6,6,2,4,
6,7,0,0,7,1,
2,3,4,4,3,5
};
 
//chargemetn des source d'un shader
char* loadSource(const char *filename)
{
	char *src = NULL;   /* code source de notre shader */
	FILE *fp = NULL;    /* fichier */
	long size;          /* taille du fichier */
	long i;             /* compteur */
 
 
	fp = fopen(filename, "r");
	if(fp == NULL)
	{
		fprintf(stderr, "impossible d'ouvrir le fichier '%s'\n", filename);
		return NULL;
	}
 
 
	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
 
	rewind(fp);
 
	src = new char(size+1);
	if(src == NULL)
	{
		fclose(fp);
		fprintf(stderr, "erreur d'allocation de memoire!\n");
		return NULL;
	}
 
	for(i=0; i<size; i++)
		src[i] = fgetc(fp);
	src[size] = '\0';
	fclose(fp);
 
	return src;
}
 
 
void reshapeGL(int h, int w)
{
 
}
 
//affichage opengl
void displayGL(void)
{
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_INDEX_ARRAY);
	glBindBuffer(GL_ARRAY_BUFFER, bufferVBO[0]);
	glEnableVertexAttribArray(positionIndex);
	glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, 0, &cubeArray[0]);
 
	glBindBuffer(GL_ARRAY_BUFFER, bufferVBO[1]);
	glEnableVertexAttribArray(colorIndex);
	glVertexAttribPointer(colorIndex, 3, GL_FLOAT, GL_FALSE, 0, &colorArray[0]);
 
 
	glBindBuffer(GL_ARRAY_BUFFER, bufferVBO[2]);
	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, &indiceArray[0]);
 
	glDisableVertexAttribArray(colorIndex);
	glDisableVertexAttribArray(positionIndex);
 
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_INDEX_ARRAY);
	glFlush();
}
 
//initialisation opengl
void initGL(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitContextVersion(3,3);
	glutInitContextFlags (GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(640,480);
	glutInitWindowPosition(50,50);
	glutCreateWindow("test shader");
	glutDisplayFunc(displayGL);
	glutReshapeFunc(reshapeGL);
}
 
int main(int argc, char *argv[])
{
	int OpenGLVersion[2];
	GLint erreurCompilation = 0, tailleErreur = 0;
	char *erreur = NULL;
 
	//intialisation
	initGL(argc,argv);
	GLenum err=glewInit();
	if(err!=GLEW_OK)
	{
		std::cout << "glewInit failed, aborting." << std::endl;
		exit(1);
	}
	glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]);
	glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]);
	std::cout << "OpenGL version = " << OpenGLVersion[0] << ".";
	std::cout << OpenGLVersion[1] << std::endl << std::endl;
 
	GLenum errorState = GL_NO_ERROR;
 
	glClearColor(0.0,0.5,0.0,1.0);
 
	glClearDepth(1.0);
	glDepthFunc(GL_LESS);
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
 
	gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);
	glMatrixMode(GL_MODELVIEW);
 
	//creation des shaders
	vertexID = glCreateShader(GL_VERTEX_SHADER);
	const char * vertexSource = loadSource("shader/test.vert");
	glShaderSource(vertexID, 1, &vertexSource, NULL);
 
 
	fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
	const char * fragmentSource = loadSource("shader/test.frag");
	glShaderSource(fragmentID, 1, &fragmentSource, NULL);
 
	//compilation des shaders
	glCompileShader(vertexID);
	glCompileShader(fragmentID);
	glGetShaderiv(vertexID, GL_COMPILE_STATUS, &erreurCompilation);
	if(erreurCompilation != GL_TRUE)
	{
 
		glGetShaderiv(vertexID, GL_INFO_LOG_LENGTH, &tailleErreur);
 
		erreur = new char(tailleErreur + 1);
 
		glGetInfoLogARB(vertexID, tailleErreur, &tailleErreur, erreur);
		printf("%s\n\n", erreur);
 
		return -1;
	}
 
	//creation du programme
	programID = glCreateProgram();
 
	//liaison des shader au programme
	glAttachShader(programID, vertexID);
	glAttachShader(programID, fragmentID);
	glLinkProgram(programID);
 
	//activation des tableau de vertex et index
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_INDEX_ARRAY);
 
	//creation de vbo
	glGenBuffers(3, bufferVBO);
 
	//mise en tampon des buffers
	glBindBuffer(GL_ARRAY_BUFFER, bufferVBO[0]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(cubeArray), cubeArray, GL_STATIC_DRAW);
	positionIndex = glGetAttribLocation(programID, "invertex");
	glBindAttribLocation(programID, positionIndex, "invertex");
 
	glBindBuffer(GL_ARRAY_BUFFER, bufferVBO[1]);
	glBufferData(GL_ARRAY_BUFFER, sizeof(colorArray), colorArray, GL_STATIC_DRAW);
	colorIndex = glGetAttribLocation(programID, "incolor");
	glBindAttribLocation(programID, colorIndex, "incolor");
 
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferVBO[2]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indiceArray), indiceArray, GL_STATIC_DRAW);
 
	//verification des index
	std::cout << "valeur des index : " << positionIndex << "  " << colorIndex << std::endl;
 
 
	//utilisation du programme
	glUseProgram(programID);
 
	//boucle opengl
	glutMainLoop();
 
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_INDEX_ARRAY);
}


Mercid'avance pour votre aide.