Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glimac/SDLWindowManager.hpp>
- #include <GL/glew.h>
- #include <iostream>
- #include <glimac/Program.hpp>
- #include <glimac/FilePath.hpp>
- #include <glimac/glm.hpp>
- #include <vector>
- using namespace glimac;
- struct Vertex2DColor {
- glm::vec2 position;
- glm::vec3 color;
- Vertex2DColor() {
- }
- Vertex2DColor(glm::vec2 position, glm::vec3 color) {
- this->position = position;
- this->color = color;
- }
- };
- int main(int argc, char **argv) {
- // Initialize SDL and open a window
- SDLWindowManager windowManager(800, 800
- , "GLImac");
- // Initialize glew for OpenGL3+ support
- GLenum glewInitError = glewInit();
- if (GLEW_OK != glewInitError) {
- std::cerr << glewGetErrorString(glewInitError) << std::endl;
- return EXIT_FAILURE;
- }
- std::cout << "OpenGL Version : " << glGetString(GL_VERSION) << std::endl;
- std::cout << "GLEW Version : " << glewGetString(GLEW_VERSION) << std::endl;
- FilePath applicationPath(argv[0]);
- Program program =
- loadProgram(applicationPath.dirPath() + "shaders/triangle.vs.glsl",
- applicationPath.dirPath() + "shaders/triangle.fs.glsl");
- program.use();
- /*********************************
- * HERE SHOULD COME THE INITIALIZATION CODE
- *********************************/
- const GLuint VERTEX_ATTR_POSITION = 3;
- const GLuint VERTEX_ATTR_COLOR = 8;
- const GLuint NBR_TRIANGLE = 80;
- const float RADIUS = 0.5;
- GLuint vbo;
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- std::vector<Vertex2DColor> vertices = {
- Vertex2DColor(glm::vec2(0, 0), glm::vec3(0.5, 0.5, 0.5)), Vertex2DColor(glm::vec2(0.5, 0), glm::vec3(0.5, 0.5, 0.5))
- };
- // => Tableau de sommets : un seul exemplaire de chaque sommet
- vertices[0] = Vertex2DColor(glm::vec2(0, 0), glm::vec3(1, 0, 0));
- vertices[1] = Vertex2DColor(glm::vec2(0.5, 0), glm::vec3(1, 0, 0));
- float angle = (2*glm::pi<float>())/NBR_TRIANGLE;
- for (int i = 2; i < NBR_TRIANGLE; i++) {
- float p = angle * (float)i;
- vertices[i+1] = Vertex2DColor(glm::vec2(RADIUS*glm::cos(p), RADIUS * glm::sin(p)), glm::vec3(1, 0, 0));
- }
- glBufferData(GL_ARRAY_BUFFER, (NBR_TRIANGLE + 1) * sizeof(Vertex2DColor), vertices.data(), GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- // => Creation du IBO
- GLuint ibo;
- glGenBuffers(1, &ibo);
- // => On bind sur GL_ELEMENT_ARRAY_BUFFER, cible reservée pour les IBOs
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
- uint32_t indices[NBR_TRIANGLE*3];
- for (int i = 1; i < NBR_TRIANGLE; i += 3) {
- printf("%d\n", i);
- indices[i] = 0;
- indices[i + 1] = i;
- indices[i + 2] = i + 1;
- }
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glEnableVertexAttribArray(VERTEX_ATTR_POSITION);
- glEnableVertexAttribArray(VERTEX_ATTR_COLOR);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glVertexAttribPointer(VERTEX_ATTR_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2DColor), (const GLvoid *) offsetof(Vertex2DColor, position));
- glVertexAttribPointer(VERTEX_ATTR_COLOR, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex2DColor),
- (const GLvoid *) (offsetof(Vertex2DColor, color)));
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- // Application loop:
- bool done = false;
- while (!done) {
- // Event loop:
- SDL_Event e;
- while (windowManager.pollEvent(e)) {
- if (e.type == SDL_QUIT) {
- done = true; // Leave the loop after this iteration
- }
- }
- /*********************************
- * HERE SHOULD COME THE RENDERING CODE
- *********************************/
- glClear(GL_COLOR_BUFFER_BIT);
- glBindVertexArray(vao);
- glDrawArrays(GL_TRIANGLES, 0, NBR_TRIANGLE*3);
- glBindVertexArray(0);
- // Update the display
- windowManager.swapBuffers();
- }
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement