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
| #include <SDL/SDL.h>
#include <SDL_Image.h>
#ifdef WIN32
#include <GL/glew.h>
#else
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#endif
#include "Shader.h"
#include "Matrice.h"
Uint32 obtenirPixel(SDL_Surface *surface, int x, int y);
int main(int argc, char **argv)
{
// Notre fenêtre et le clavier
SDL_WindowID fenetre;
SDL_GLContext contexteOpenGL;
SDL_Event evenements;
char terminer = 0;
int x = 0, y = 0;
int nbreVertex = 0;
int *vertices = NULL;
int nbreIndices = 0;
unsigned int *indices = NULL;
Uint32 pixel;
Uint8 r, g, b, a;
Uint8 moyenne;
FILE *fichier = NULL;
fichier = fopen("test.txt", "w+");
SDL_Surface *image = NULL;
image = IMG_Load("image.png");
// Initialisation de la SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Erreur lors de l'initialisation de la SDL : %s", SDL_GetError());
SDL_Quit();
return -1;
}
// Version d'OpenGL
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
// Double Buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
// Création de la fenêtre
fenetre = SDL_CreateWindow("Cube - OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
contexteOpenGL = SDL_GL_CreateContext(fenetre);
#ifdef WIN32
// On initialise GLEW
GLenum initialisationGLEW = glewInit();
// Si l'initialisation a échouée :
if(initialisationGLEW != GLEW_OK)
{
// On affiche l'erreur grâce à la fonction : glewGetErrorString(GLenum code)
printf("Erreur d'initialisation de GLEW : %s", glewGetErrorString(initialisationGLEW));
// On quitte la SDL
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#endif
// Activation du Depth Buffer
glEnable(GL_DEPTH_TEST);
// Création des matrices + initialisations
Matrice projection, modelview;
loadIdentity(&projection);
loadIdentity(&modelview);
// Création de la projection
perspective(&projection, 70, (float) 800/600, 1, 100);
// Vertices et indices
nbreVertex = (image->h*image->w)*3;
vertices = malloc(nbreVertex*sizeof(int));
//creation
SDL_LockSurface(image);
for (x = 0; x<image->w; x++)
{
for (y = 0; y<image->h; y++)
{
pixel = obtenirPixel(image, x, y);
SDL_GetRGBA(pixel, image->format, &r, &g, &b, &a);
moyenne = (r+g+b)/3;
/*fprintf(fichier, "Pixel Color -> R: %d, G: %d, B: %d, A: %d, X:%d, Y:%d, moyenne:%d\n", r, g, b, a, x, y, moyenne);*/
vertices[(image->w*y*3)+3*x] = x;//a revoir
vertices[((image->w*y*3)+3*x)+1] = y;
vertices[((image->w*y*3)+3*x)+2] = moyenne; //ça ça à l'air d'être bon
}
}
SDL_UnlockSurface(image);
nbreIndices = nbreVertex/3;
indices = malloc(nbreIndices*sizeof(unsigned int));
// Indices
y=0;
for (x = 0; x<nbreIndices; x=x+3)
{
if (x>(image->h) && x<((image->h*image->w)-(image-> h)))
{
//créer des triangles
indices[y] = x;
indices[y+1] = x+1;
indices[y+2] = x+image->w;
y = y+3;
}
} //les triangles ont l'air de marcher
fclose(fichier);
// Couleurs
float blanc[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
Shader shaderCouleur;
creerShader(&shaderCouleur, "Shaders/couleurs.vert", "Shaders/couleurs.frag");
// Autres variables
Uint32 debutBoucle = 0, finBoucle = 0, tempsEcoule = 0;
int frameRate = 1000 / 50;
// Boucle principale
// Gestion des évènements
while (!terminer)
{
SDL_PollEvent(&evenements);
if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
terminer = 1;
if (evenements.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
terminer = 1;
// Nettoyage de la fenêtre et du Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
loadIdentity(&modelview);
lookAt(&modelview, 3, 3, 3, 0, 0, 0, 0, 1,0);
glUseProgram(shaderCouleur.programID);
glEnableVertexAttribArray(0);//vertex
glEnableVertexAttribArray(1);//couleur
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glUniformMatrix4fv(glGetUniformLocation(shaderCouleur.programID, "projection"), 1, GL_TRUE, projection.valeurs);
glUniformMatrix4fv(glGetUniformLocation(shaderCouleur.programID, "modelview"), 1, GL_TRUE, modelview.valeurs);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, blanc);
glDrawElements(GL_TRIANGLES, nbreIndices, GL_UNSIGNED_INT, indices);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
glUseProgram(0);
SDL_GL_SwapWindow(fenetre);
finBoucle = SDL_GetTicks();
tempsEcoule = finBoucle - debutBoucle;
// Si necessaire, on met en pause le programme
if(tempsEcoule < frameRate)
SDL_Delay(frameRate - tempsEcoule);
}
detruireShader(&shaderCouleur);
SDL_FreeSurface(image);
SDL_Quit();
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return 0;
} |
Partager