27GLuint texcoord_buffer;
28unsigned int vertex_count;
41 if(mesh.vertex_count() == 0)
44 vertex_count= mesh.vertex_count();
47 mesh.bounds(pmin, pmax);
48 camera.lookat(pmin, pmax);
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_LINEAR_MIPMAP_LINEAR);
88 glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
89 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
90 glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
95 GLenum data_format= GL_RGBA;
96 GLenum data_type= GL_UNSIGNED_BYTE;
97 if(image.channels == 3)
100 glGenTextures(1, &texture);
101 glBindTexture(GL_TEXTURE_2D, texture);
103 glTexImage2D(GL_TEXTURE_2D, 0,
104 GL_RGBA, image.width, image.height, 0,
105 data_format, data_type, image.data() );
107 glGenerateMipmap(GL_TEXTURE_2D);
110 glBindTexture(GL_TEXTURE_2D, 0);
114 glClearColor(0.2f, 0.2f, 0.2f, 1);
117 glDepthFunc(GL_LESS);
118 glEnable(GL_DEPTH_TEST);
122 glEnable(GL_CULL_FACE);
129 glDeleteVertexArrays(1, &vao);
130 glDeleteBuffers(1, &vertex_buffer);
131 glDeleteBuffers(1, &texcoord_buffer);
132 glDeleteSamplers(1, &sampler);
133 glDeleteTextures(1, &texture);
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
146 if(mb & SDL_BUTTON(1))
148 camera.rotation(mx, my);
150 else if(mb & SDL_BUTTON(3))
154 else if(mb & SDL_BUTTON(2))
167 glBindVertexArray(vao);
168 glUseProgram(program);
176 Transform mvp= projection * view * model;
177 program_uniform(program,
"mvpMatrix", mvp);
180 glBindSampler(0, sampler);
181 glBindTexture(GL_TEXTURE_2D, texture);
184 GLint location= glGetUniformLocation(program,
"texture0");
185 glUniform1i(location, 0);
188 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
191 glBindTexture(GL_TEXTURE_2D, 0);
194 glBindVertexArray(0);
200int main(
int argc,
char **argv )
215 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, const int samples)
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.
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)
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.