28GLuint texcoord_buffer;
42 if(mesh.vertex_count() == 0)
45 vertex_count= mesh.vertex_count();
48 mesh.bounds(pmin, pmax);
49 camera.lookat(pmin, pmax);
53 glGenVertexArrays(1, &vao);
54 glBindVertexArray(vao);
57 glGenBuffers(1, &vertex_buffer);
58 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
59 glBufferData(GL_ARRAY_BUFFER, mesh.vertex_buffer_size(), mesh.vertex_buffer(), GL_STATIC_DRAW);
62 GLint position= glGetAttribLocation(program,
"position");
65 glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, 0);
66 glEnableVertexAttribArray(position);
69 glGenBuffers(1, &texcoord_buffer);
70 glBindBuffer(GL_ARRAY_BUFFER, texcoord_buffer);
71 glBufferData(GL_ARRAY_BUFFER, mesh.texcoord_buffer_size(), mesh.texcoord_buffer(), GL_STATIC_DRAW);
74 GLint texcoord= glGetAttribLocation(program,
"texcoord");
77 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
78 glEnableVertexAttribArray(texcoord);
83 glBindBuffer(GL_ARRAY_BUFFER, 0);
86 glGenSamplers(1, &sampler);
88 glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
89 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
91 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
100 glBindTexture(GL_TEXTURE_2D, 0);
104 glClearColor(0.2f, 0.2f, 0.2f, 1);
107 glDepthFunc(GL_LESS);
108 glEnable(GL_DEPTH_TEST);
112 glEnable(GL_CULL_FACE);
119 glDeleteVertexArrays(1, &vao);
120 glDeleteBuffers(1, &vertex_buffer);
121 glDeleteBuffers(1, &texcoord_buffer);
122 glDeleteSamplers(1, &sampler);
123 glDeleteTextures(1, &base_texture);
124 glDeleteTextures(1, &detail_texture);
130 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
137 if(mb & SDL_BUTTON(1))
139 camera.rotation(mx, my);
141 else if(mb & SDL_BUTTON(3))
145 else if(mb & SDL_BUTTON(2))
158 glBindVertexArray(vao);
159 glUseProgram(program);
167 Transform mvp= projection * view * model;
168 program_uniform(program,
"mvpMatrix", mvp);
171 glActiveTexture(GL_TEXTURE0);
172 glBindTexture(GL_TEXTURE_2D, base_texture);
173 glBindSampler(0, sampler);
175 glActiveTexture(GL_TEXTURE0 +1);
176 glBindTexture(GL_TEXTURE_2D, detail_texture);
177 glBindSampler(1, sampler);
181 location= glGetUniformLocation(program,
"base_texture");
182 glUniform1i(location, 0);
184 location= glGetUniformLocation(program,
"detail_texture");
185 glUniform1i(location, 1);
196 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
199 glBindTexture(GL_TEXTURE_2D, 0);
202 glBindVertexArray(0);
208int main(
int argc,
char **argv )
223 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.
Transform Identity()
construit la transformation identite.
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.
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
int init(std::vector< const char * > &options)
representation d'un point 3d.