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
| float coords [] = {
// coordonnées d'un cube
...
};
float normals [] = {
// normales d'un cube
...
};
float texCoords [] = {
// coordonnées de texture d'un cube
...
};
GLushort indices [] = {
// indices d'un cube
...
};
float ambiant[] = {
0.3, 0.3, 0.3, 1
};
float diffuse [] = {
0.7, 0.7, 0.7, 1
};
float position [] = {
3, 0, 8, 1
};
struct vertex_t {
float x, y, z;
float nx, ny, nz;
float tx, ty;
float pad;
};
struct texel_t {
GLubyte r, g, b, a;
};
GLuint vboId;
GLuint ivboId;
GLuint texId;
void initGL() {
glLightfv(GL_LIGHT0, GL_AMBIENT, ambiant);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0.5f);
glClearDepthf(1.f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glGenBuffers(1, &vboId);
glGenBuffers(1, &ivboId);
glGenTextures(1, &texId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
const GLuint numV = 24;
vertex_t vertices [numV];
for (GLuint i=0; i<numV; i++) {
vertices[i].x = coords[3*i];
vertices[i].y = coords[3*i+1];
vertices[i].z = coords[3*i+2];
vertices[i].nx = normals[3*i];
vertices[i].ny = normals[3*i+1];
vertices[i].nz = normals[3*i+2];
vertices[i].tx = texCoords[2*i];
vertices[i].ty = texCoords[2*i+1];
}
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindTexture(GL_TEXTURE_2D, texId);
const GLuint tsize = 256;
texel_t texels [tsize*tsize];
for(GLuint i=0; i<tsize*tsize; i++) {
texels[i].r = 255; // fake red texture
texels[i].g = 0;
texels[i].b = 0;
texels[i].a = 255;
}
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, tsize, tsize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texels);
}
const float rspeed = 2;
float rot = 0;
void renderGL() {
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glFrontFace(GL_CCW);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivboId);
glBindTexture(GL_TEXTURE_2D, texId);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(vertex_t), 0);
glNormalPointer(GL_FLOAT, sizeof(vertex_t),
reinterpret_cast<GLvoid*>(3*sizeof(float)));
glTexCoordPointer(2, GL_FLOAT, sizeof(vertex_t),
reinterpret_cast<GLvoid*>(6*sizeof(float)));
glLoadIdentity();
glTranslatef(0, 0, -8);
glRotatef(rot, 0, 1, 0);
rot += rspeed;
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void destroyGL() {
glDeleteTextures(1, &texId);
glDeleteBuffers(1, &ivboId);
glDeleteBuffers(1, &vboId);
} |
Partager