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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
/*
COMPILATION :
gcc ./Main.c -o ./Main.out -lGL -lglut -lGLU `sdl-config --cflags --libs` -lSDL_image
*/
#define BUFFER_OFFSET(a) ((char*)NULL + (a))
static void affichage(GLfloat *tabCouleur);
static unsigned char* chargementImage(int *width, int *height);
static void clavier();
static Uint32 getpixel(SDL_Surface *surface, int x, int y);
void makeVBO(unsigned char *matrice, int width, int height);
GLfloat * initCouleur(int width, int height, GLfloat r, GLfloat v, GLfloat b);
static GLuint id;
static GLfloat *tabVertex = NULL;
static GLfloat posCamZ = 0.0;
static GLfloat angleZ = 0.0;
static GLfloat angleX = 0.0;
static GLfloat Z = 0.0;
static GLfloat posCamX = 0.0;
static GLfloat posCamY = 0.0;
//Génération du tableau de couleur
GLfloat * initCouleur(int width, int height, GLfloat r, GLfloat v, GLfloat b)
{
GLfloat *tabCouleur = NULL;
tabCouleur = calloc((width * height)*3, sizeof(tabCouleur));
int i = 0, rank = 0;
for(i=0; i < (((width * height)*3)-1); i++)
{
if(rank == 0)
{
tabCouleur[i] = r;
rank++;
}
else if(rank == 1)
{
tabCouleur[i] = v;
rank++;
}
else
{
rank = 0;
tabCouleur[i] = b;
}
}
return tabCouleur;
free(tabCouleur);
}
//Génération du VBO qu'on va afficher
void makeVBO(unsigned char *matrice, int width, int height)
{
GLfloat *matriceVertex = NULL;
int i = 0, rank = 0, loopLargeur = width - 1, j=0;
GLfloat x=0.0, y=0.0, z=0.0;
matriceVertex = calloc((width * height)*3, sizeof *matriceVertex);
for(i=0, j=0; i < ((width*height)*3); i+=3, j++)
{
matriceVertex[i] = x;
matriceVertex[i+1] = (GLfloat)matrice[j];
matriceVertex[i+2] = z;
x+=3;
if(!(x < width*3))
{
z += 3.0;
x = 0.0;
}
}
for(j=0; j < ((width*height)*3); j++)
printf("%d: %lf\n", j, matriceVertex[j]);
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, sizeof(matriceVertex), matriceVertex, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, ((width * height)*3) * sizeof(matriceVertex), matriceVertex);
glBindBuffer(GL_ARRAY_BUFFER, 0);
fprintf(stdout, "Mise en place du VBO : %d vertex\n", (width * height));
free(matrice);
free(matriceVertex);
}
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
//Chargement de mon image dans un tableau
unsigned char* chargementImage(int *width, int *height)
{
SDL_Surface * s = NULL;
s = IMG_Load("in.bmp");
unsigned char *surfaceMonochrome;
int i=0, j=0;
surfaceMonochrome = calloc(s->w * s->h, sizeof *surfaceMonochrome);
Uint8 r, g ,b;
if(s == NULL)
{
fprintf(stderr, "Erreur lors du chargement de l'image : in.bmp\n");
return NULL;
}
else
{
fprintf(stdout, "Chargement de l'image : in.bmp\n");
fprintf(stdout, "Largeur : %dpx\nHauteur : %dpx\n", s->w, s->h);
*width = s->w;
*height = s->h;
SDL_LockSurface(s);
for(i=0; i < s->w; i++)
{
for(j=0; j < s->h; j++)
{
SDL_GetRGB(getpixel(s, i, j), s->format, &r, &g, &b);
surfaceMonochrome[j * s->w + i] = (char)((r + g + b) / 3);
}
}
SDL_UnlockSurface(s);
return surfaceMonochrome;
}
}
// Une demande d'affichage vient d'être générée
void affichage(GLfloat *tabCouleur)
{
/* On efface l'écran avec la couleur du fond et le Z-Buffer */
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
int i=0;
unsigned int index[33*33];
for(i=0; i < 33*33; i++)
index[i] = i;
//Gestion de la camera
glLoadIdentity();
glRotatef(angleZ, 0, 1, 0);
glRotatef(angleX, 1, 0, 0);
glTranslatef(0, 0, Z);
gluLookAt (10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0);
//Dessin du VBO
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, id);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glColorPointer(3, GL_FLOAT, 0, tabCouleur);
glDrawElements(GL_TRIANGLE_STRIP, 33*33, GL_UNSIGNED_INT, index);
//glDrawArrays(GL_LINE_STRIP, 0, 33*33);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
//Gestion des événements clavier
void clavier()
{
SDL_Event event;
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
exit(1);
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
Z += 10;
break;
case SDLK_DOWN:
Z -= 10;
break;
case SDLK_LEFT:
angleZ -= 10;
break;
case SDLK_RIGHT:
angleZ += 10;
break;
case 'z':
angleX -= 10;
break;
case 's':
angleX += 10;
break;
case 'q':
exit(1);
break;
}
}
}
int main(int argc, char **argv)
{
int done=1;
SDL_Event event;
SDL_Surface* screen;
int widthImage, heightImage;
int newx, newy, tmpx,tmpy, curmx,curmy;
GLfloat rapport = ((float) (800))/ ((float) (600));
GLfloat *tabCouleur;
unsigned char *matrice;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_WM_SetCaption("Affichage du terrain",NULL);
screen = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
if(screen==NULL)
return EXIT_FAILURE;
//Chargement de l'image
matrice = chargementImage(&widthImage, &heightImage);
//Création du tableau de couleur
tabCouleur = initCouleur(widthImage, heightImage, 0.0, 255.0, 0.0);
//Création du VBO
makeVBO(matrice, widthImage, heightImage);
glEnable(GL_DEPTH_TEST);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16);
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40,rapport,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0,0.0,0.0,0.0);
while(done)
{
//Affichage
affichage(tabCouleur);
clavier();
SDL_GL_SwapBuffers();
}
/*Quitter*/
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager