29GLuint texcoord_buffer;
30unsigned int vertex_count;
43 if(mesh.vertex_count() == 0)
46 vertex_count= mesh.vertex_count();
48 camera.lookat(
Point(0, 0, 0), 5);
52 glGenVertexArrays(1, &vao);
53 glBindVertexArray(vao);
56 glGenBuffers(1, &vertex_buffer);
57 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
58 glBufferData(GL_ARRAY_BUFFER, mesh.vertex_buffer_size(), mesh.vertex_buffer(), GL_STATIC_DRAW);
61 GLint position= glGetAttribLocation(program,
"position");
64 glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, 0);
65 glEnableVertexAttribArray(position);
68 glGenBuffers(1, &texcoord_buffer);
69 glBindBuffer(GL_ARRAY_BUFFER, texcoord_buffer);
70 glBufferData(GL_ARRAY_BUFFER, mesh.texcoord_buffer_size(), mesh.texcoord_buffer(), GL_STATIC_DRAW);
73 GLint texcoord= glGetAttribLocation(program,
"texcoord");
76 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
77 glEnableVertexAttribArray(texcoord);
82 glBindBuffer(GL_ARRAY_BUFFER, 0);
85 glGenSamplers(1, &sampler);
87 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
88 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
90 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
93 glGenSamplers(1, &sampler_linear);
95 glSamplerParameteri(sampler_linear, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
96 glSamplerParameteri(sampler_linear, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
97 glSamplerParameteri(sampler_linear, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
98 glSamplerParameteri(sampler_linear, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
100 glGenSamplers(1, &sampler_aniso);
102 glSamplerParameteri(sampler_aniso, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
103 glSamplerParameteri(sampler_aniso, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
104 glSamplerParameteri(sampler_aniso, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
105 glSamplerParameteri(sampler_aniso, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
106 glSamplerParameterf(sampler_aniso, GL_TEXTURE_MAX_ANISOTROPY, 8);
112 GLenum data_format= GL_RGBA;
113 GLenum data_type= GL_UNSIGNED_BYTE;
114 if(image.channels == 3)
117 glGenTextures(1, &texture);
118 glBindTexture(GL_TEXTURE_2D, texture);
120 glTexImage2D(GL_TEXTURE_2D, 0,
121 GL_RGBA, image.width, image.height, 0,
122 data_format, data_type, image.data() );
124 glGenerateMipmap(GL_TEXTURE_2D);
127 glBindTexture(GL_TEXTURE_2D, 0);
131 glClearColor(0.2f, 0.2f, 0.2f, 1);
134 glDepthFunc(GL_LESS);
135 glEnable(GL_DEPTH_TEST);
139 glEnable(GL_CULL_FACE);
146 glDeleteVertexArrays(1, &vao);
147 glDeleteBuffers(1, &vertex_buffer);
148 glDeleteBuffers(1, &texcoord_buffer);
149 glDeleteSamplers(1, &sampler);
150 glDeleteTextures(1, &texture);
156 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
163 if(mb & SDL_BUTTON(1))
165 camera.rotation(mx, my);
167 else if(mb & SDL_BUTTON(3))
171 else if(mb & SDL_BUTTON(2))
184 glBindVertexArray(vao);
185 glUseProgram(program);
193 Transform mvp= projection * view * model;
194 program_uniform(program,
"mvpMatrix", mvp);
197 glBindSampler(0, sampler);
198 glBindTexture(GL_TEXTURE_2D, texture);
201 GLint location= glGetUniformLocation(program,
"texture0");
202 glUniform1i(location, 0);
205 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
208 for(
int i= 0; i <4; i++)
215 Transform mvp= projection * view * model;
216 program_uniform(program,
"mvpMatrix", mvp);
218 glBindSampler(0, sampler_linear);
219 glBindTexture(GL_TEXTURE_2D, texture);
221 GLint location= glGetUniformLocation(program,
"texture0");
222 glUniform1i(location, 0);
224 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
227 for(
int i= 0; i <4; i++)
234 Transform mvp= projection * view * model;
235 program_uniform(program,
"mvpMatrix", mvp);
237 glBindSampler(0, sampler_aniso);
238 glBindTexture(GL_TEXTURE_2D, texture);
240 GLint location= glGetUniformLocation(program,
"texture0");
241 glUniform1i(location, 0);
243 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
248 glBindTexture(GL_TEXTURE_2D, 0);
251 glBindVertexArray(0);
257int main(
int argc,
char **argv )
272 printf(
"[error] init failed.\n");
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Context create_context(Window window)
cree et configure un contexte opengl
int window_height()
renvoie la hauteur de la fenetre de l'application.
void release_window(Window window)
destruction de la fenetre.
int run(Window window, int(*draw)())
boucle de gestion des evenements de l'application.
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().
Window create_window(const int w, const int h, const int major, const int minor)
creation d'une fenetre pour l'application.
void release_context(Context context)
detruit le contexte openGL.
int window_width()
renvoie la largeur de la fenetre de l'application.
ImageData read_image_data(const void *buffer, const unsigned size, const bool flipY)
charge les donnees d'un fichier png stocke en memoire. renvoie une image initialisee par defaut en ca...
Transform Identity()
construit la transformation identite.
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
int init(std::vector< const char * > &options)
stockage temporaire des donnees d'une image.
representation d'un point 3d.