24 GLuint sampler_linear;
29 GLuint texcoord_buffer;
30 unsigned int vertex_count;
54 glGenVertexArrays(1, &vao);
55 glBindVertexArray(vao);
58 glGenBuffers(1, &vertex_buffer);
59 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
63 GLint position= glGetAttribLocation(program,
"position");
66 glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, 0);
67 glEnableVertexAttribArray(position);
70 glGenBuffers(1, &texcoord_buffer);
71 glBindBuffer(GL_ARRAY_BUFFER, texcoord_buffer);
75 GLint texcoord= glGetAttribLocation(program,
"texcoord");
78 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
79 glEnableVertexAttribArray(texcoord);
84 glBindBuffer(GL_ARRAY_BUFFER, 0);
87 glGenSamplers(1, &sampler);
89 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
90 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
91 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
92 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
95 glGenSamplers(1, &sampler_linear);
97 glSamplerParameteri(sampler_linear, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
98 glSamplerParameteri(sampler_linear, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
99 glSamplerParameteri(sampler_linear, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
100 glSamplerParameteri(sampler_linear, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
102 glGenSamplers(1, &sampler_aniso);
104 glSamplerParameteri(sampler_aniso, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
105 glSamplerParameteri(sampler_aniso, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
106 glSamplerParameteri(sampler_aniso, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
107 glSamplerParameteri(sampler_aniso, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
108 glSamplerParameterf(sampler_aniso, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8);
114 GLenum data_format= GL_RGBA;
115 GLenum data_type= GL_UNSIGNED_BYTE;
116 if(image.channels == 3)
119 glGenTextures(1, &texture);
120 glBindTexture(GL_TEXTURE_2D, texture);
122 glTexImage2D(GL_TEXTURE_2D, 0,
123 GL_RGBA, image.width, image.height, 0,
124 data_format, data_type, image.buffer() );
126 glGenerateMipmap(GL_TEXTURE_2D);
129 glBindTexture(GL_TEXTURE_2D, 0);
133 glClearColor(0.2f, 0.2f, 0.2f, 1);
136 glDepthFunc(GL_LESS);
137 glEnable(GL_DEPTH_TEST);
141 glEnable(GL_CULL_FACE);
148 glDeleteVertexArrays(1, &vao);
149 glDeleteBuffers(1, &vertex_buffer);
150 glDeleteBuffers(1, &texcoord_buffer);
151 glDeleteSamplers(1, &sampler);
152 glDeleteTextures(1, &texture);
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
165 if(mb & SDL_BUTTON(1))
169 else if(mb & SDL_BUTTON(3))
173 else if(mb & SDL_BUTTON(2))
186 glBindVertexArray(vao);
187 glUseProgram(program);
195 Transform mvp= projection * view * model;
199 glBindSampler(0, sampler);
200 glBindTexture(GL_TEXTURE_2D, texture);
203 GLint location= glGetUniformLocation(program,
"texture0");
204 glUniform1i(location, 0);
207 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
210 for(
int i= 0; i <4; i++)
217 Transform mvp= projection * view * model;
220 glBindSampler(0, sampler_linear);
221 glBindTexture(GL_TEXTURE_2D, texture);
223 GLint location= glGetUniformLocation(program,
"texture0");
224 glUniform1i(location, 0);
226 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
229 for(
int i= 0; i <4; i++)
236 Transform mvp= projection * view * model;
239 glBindSampler(0, sampler_aniso);
240 glBindTexture(GL_TEXTURE_2D, texture);
242 GLint location= glGetUniformLocation(program,
"texture0");
243 glUniform1i(location, 0);
245 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
250 glBindTexture(GL_TEXTURE_2D, 0);
253 glBindVertexArray(0);
259 int main(
int argc,
char **argv )
274 printf(
"[error] init failed.\n");
Context create_context(Window window, const int major, const int minor)
cree et configure un contexte opengl
ImageData read_image_data(const char *filename)
charge les donnees d'un fichier png. renvoie une image initialisee par defaut en cas d'echec...
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet...
const float * vertex_buffer() const
renvoie l'adresse de la position du premier sommet. permet de construire les vertex buffers openGL...
void bounds(Point &pmin, Point &pmax)
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
stockage temporaire des donnees d'une image.
representation d'un objet / maillage.
void move(const float z)
rapproche / eloigne la camera du centre.
Transform Identity()
construit la transformation identite.
std::size_t vertex_buffer_size() const
renvoie la longueur (en octets) du vertex buffer.
const float * texcoord_buffer() const
renvoie l'adresse des coordonnees de textures du premier sommet. par convention, c'est un vec2...
void program_uniform(const GLuint program, const char *uniform, const unsigned int v)
affecte une valeur a un uniform du shader program. uint.
void draw(Mesh &m, const Transform &model, const Transform &view, const Transform &projection, const GLuint texture)
applique une texture a la surface de l'objet. ne fonctionne que si les coordonnees de textures sont f...
Transform view() const
renvoie la transformation vue.
int window_width()
renvoie la largeur de la fenetre de l'application.
void release()
detruit les objets openGL.
Transform projection(const float width, const float height, const float fov) const
renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres...
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
Window create_window(const int w, const int h)
creation d'une fenetre pour l'application.
GLuint read_program(const char *filename, const char *definitions)
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
std::size_t texcoord_buffer_size() const
renvoie la taille (en octets) du texcoord buffer.
void translation(const float x, const float y)
deplace le centre / le point observe.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
int window_height()
renvoie la hauteur de la fenetre de l'application.
int release_program(const GLuint program)
detruit les shaders et le program.
void release_context(Context context)
detruit le contexte openGL.
representation d'un point 3d.
int vertex_count() const
renvoie le nombre de sommets.
void release_window(Window window)
destruction de la fenetre.
int run(Window window, int(*draw)(void))
fonction principale. gestion des evenements et appel de la fonction draw de l'application.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes. utiliser glDrawArrays pour l'afficher. a detruire avec Mesh::release( ).