IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

Problème avec glDrawElements [OpenGL 4.x]


Sujet :

OpenGL

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut Problème avec glDrawElements
    Bonjour,

    Je cherche actuellement à faire une class me permettant de stocker et d'afficher plusieurs hexagones en 2D à l'aide de triangle fan.

    Un hexagone contient 7 vertex (centre + 6 angles) avec seulement la position en deux dimension, le reste du travail est fait par un element buffer array qui utilise une primitive restart.

    La class prend un paramètre le nombre d'hexagone à réserver ainsi que la longueur de leur rayon, elle me permet ensuite de rajouter un hexagone en (x, y) (centre de l'hexagone) avec la methode add.

    Le problème survient quand j'essaye d'afficher avec la méthode render, cela n'affiche tout simplement rien.

    Je tiens à préciser que j'affiche dans une vue orthographic et que je ne pense pas que le problème vienne du shader, car les tests que j'ai fait dans le main ont fonctionnés mais que des que je passe dans la class glDrawElements n'affiche plus rien. J'ai aussi changé glDrawElements par glDrawArrays(GL_TRIANGLES, 0, 3) et cela m'a bien affiché un triangle au bon emplacement. J'ai quand même vérifié le contenu des buffers avec glMapBuffer en lecture et elle m'ont paru conforme.

    La seule chose que j'ai pu voir c'est que si je dessine en glPolygonMode(GL_FRONT_AND_BACK, GL_LINE), je peux voir un trait qui pars du centre de mon hexagone et qui va en (0, 0).

    Tray.hpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
     
    #ifndef TRAY_H
    #define TRAY_H
     
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    #include <iostream>
     
    class Tray
    {
        public:
     
            Tray(int n, int r);
            ~Tray();
            void flip();
            void add(int x, int y);
            void render();
     
        private:
     
            int m_size;
            int m_idx;
            int m_ray;
     
            std::vector<GLfloat> m_vertices;
            std::vector<GLuint> m_indices;
     
            GLuint m_vao;
            GLuint m_vbo;
            GLuint m_ibo;
     
            const int VERTEX_SIZE = 2;
            const int INDICE_SIZE = 9;
            const int HEXAGONE_SIZE = 7;
            const GLuint PRIMITIVE_RESTART = 42;
    };
     
    #endif
    Tray.cpp :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
     
    #include "Tray.hpp"
     
    Tray::Tray(int n, int r) : m_size(n), m_idx(0), m_ray(r), m_vertices(), m_indices()
    {
        m_vertices.resize(n * VERTEX_SIZE * HEXAGONE_SIZE, 0.f);
        m_indices.resize(n * INDICE_SIZE, 0);
     
        glPrimitiveRestartIndex(PRIMITIVE_RESTART);
     
        glGenVertexArrays (1, &m_vao);
        glGenBuffers(1, &m_vbo);
        glGenBuffers(1, &m_ibo);
     
        glBindVertexArray(m_vao);
     
        //VBO
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(GLfloat), &m_vertices[0], GL_STATIC_DRAW);
     
        //IBO
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLuint), &m_indices[0], GL_STATIC_DRAW);
     
        //VAO
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
        glEnableVertexAttribArray(0);
     
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }
     
    Tray::~Tray()
    {
        glDeleteVertexArrays(1, &m_vao);
        glDeleteBuffers(1, &m_ibo);
        glDeleteBuffers(1, &m_vbo);
    }
     
    void Tray::add(int x, int y)
    {
        m_vertices[m_idx * VERTEX_SIZE * HEXAGONE_SIZE + 0] = x;
        m_vertices[m_idx * VERTEX_SIZE * HEXAGONE_SIZE + 1] = y;
     
        float rad;
     
        for(int i=0; i < HEXAGONE_SIZE - 1; i++)
        {
            rad = M_PI * ((i*60.f)/180.f);
            m_vertices[(i+1) * VERTEX_SIZE + m_idx * VERTEX_SIZE * HEXAGONE_SIZE + 0] = x + m_ray * std::cos(rad);
            m_vertices[(i+1) * VERTEX_SIZE + m_idx * VERTEX_SIZE * HEXAGONE_SIZE + 1] = y + m_ray * std::sin(rad);
        }
     
        m_indices[m_idx * INDICE_SIZE + 0] = m_idx * HEXAGONE_SIZE + 0;
        m_indices[m_idx * INDICE_SIZE + 1] = m_idx * HEXAGONE_SIZE + 1;
        m_indices[m_idx * INDICE_SIZE + 2] = m_idx * HEXAGONE_SIZE + 2;
        m_indices[m_idx * INDICE_SIZE + 3] = m_idx * HEXAGONE_SIZE + 3;
        m_indices[m_idx * INDICE_SIZE + 4] = m_idx * HEXAGONE_SIZE + 4;
        m_indices[m_idx * INDICE_SIZE + 5] = m_idx * HEXAGONE_SIZE + 5;
        m_indices[m_idx * INDICE_SIZE + 6] = m_idx * HEXAGONE_SIZE + 6;
        m_indices[m_idx * INDICE_SIZE + 7] = m_idx * HEXAGONE_SIZE + 1;
        m_indices[m_idx * INDICE_SIZE + 8] = PRIMITIVE_RESTART;
     
        m_idx++;
    }
     
    void Tray::flip()
    {
        GLfloat* data;
     
        glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
        data = static_cast<GLfloat*>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
        if(data != nullptr)
        {
            std::copy(m_vertices.begin(), m_vertices.end(), data);
            glUnmapBuffer(GL_ARRAY_BUFFER);
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
     
        glBindBuffer(GL_ARRAY_BUFFER, m_ibo);
        data = static_cast<GLfloat*>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
        if(data != nullptr)
        {
            std::copy(m_indices.begin(), m_indices.end(), data);
            glUnmapBuffer(GL_ARRAY_BUFFER);
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
     
    void Tray::render()
    {
        glBindVertexArray(m_vao);
        //glDrawArrays(GL_TRIANGLES, 0, 3);
        glDrawElements(GL_TRIANGLE_FAN, m_idx * INDICE_SIZE, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
    }
    Merci d'avance pour votre aide.

  2. #2
    Expert confirmé

    Avatar de dragonjoker59
    Homme Profil pro
    Software Developer
    Inscrit en
    Juin 2005
    Messages
    2 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Software Developer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2005
    Messages : 2 033
    Billets dans le blog
    12
    Par défaut
    Salut!

    As-tu essayé sans le glPrimitiveRestartIndex, en ne dessinant qu'un héxagone?


    PS:

    Il y a une section OpenGL sur Developpez.com: http://www.developpez.net/forums/f53...hiques/opengl/
    Si vous ne trouvez plus rien, cherchez autre chose...

    Vous trouverez ici des tutoriels OpenGL moderne.
    Mon moteur 3D: Castor 3D, presque utilisable (venez participer, il y a de la place)!
    Un projet qui ne sert à rien, mais qu'il est joli (des fois) : ProceduralGenerator (Génération procédurale d'images, et post-processing).

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut
    J'ai viré glPrimitiveRestartIndex(PRIMITIVE_RESTART) mais cela ne semble rien changer.

    Je rajoute le main et le shaders.

    main.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
     
    #include <cstdlib>
    #include <iostream>
     
    #define GLEW_STATIC
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
     
    #include "Shader.hpp"
    #include "Tray.hpp"
     
    using namespace std;
     
    int main()
    {
        // Initialize GLFW 3
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     
        // SET the window no re-sizable
        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
     
        // SET anti-aliasing
        glfwWindowHint(GLFW_SAMPLES, 8);
     
        // Create a windowed window and it is OpenGL context
        GLFWwindow* window = glfwCreateWindow(800, 600, "Hex", nullptr, nullptr);
        if(window == nullptr)
        {
    	std::cout << "Failed to create GLFW window" << std::endl;
    	glfwTerminate();
    	return EXIT_FAILURE;
        }
     
        // Make the window context current
        glfwMakeContextCurrent(window);
     
        // Initialize GLEW
        glewExperimental = GL_TRUE;
        if (glewInit() != GLEW_OK)
        {
    	std::cout << "Failed to initialize GLEW" << std::endl;
        }
     
        // Get version info
        const GLubyte* renderer = glGetString(GL_RENDERER);
        const GLubyte* version = glGetString(GL_VERSION);
        cout << "Renderer : " << renderer << endl;
        cout << "OpenGL version supported " << version << endl;
     
        Shader shader("shaders/basic_vs.glsl", "shaders/basic_fs.glsl");
     
        glm::mat4 projMat = glm::ortho(0.f, 800.f, 600.f, 0.f);
        shader.setUniformMatrix("u_projView", projMat);
        shader.use();
     
        Tray t(1, 200);
        t.add(400, 400);
        t.flip();
     
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
     
        while(!glfwWindowShouldClose(window))
        {
        	glfwPollEvents();
     
        	glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        	glClear(GL_COLOR_BUFFER_BIT);
     
    	t.render();
     
        	glfwSwapBuffers(window);
        }
     
        glfwTerminate();
        return EXIT_SUCCESS;
    }
    shader vertex :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    #version 400 core
     
    layout(location = 0) in vec2 Position;
     
    uniform mat4 u_projView;
     
    void main()
    {  
        gl_Position = u_projView * vec4(Position, 0.0, 1.0);
    }
    shader fragment :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    #version 400 core
     
    out vec4 vColor;
     
    void main()
    {
        vColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
    C'était mon premier post sur le forum, donc je n'étais pas sur de l'endroit ou poster, en plus j'ai vus des topic OpenGL sur la même page. Mais merci pour le lien, la prochaîne fois je posterai dans la bonne section.

  4. #4
    Expert confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Février 2005
    Messages
    5 502
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2005
    Messages : 5 502
    Par défaut
    Quid du culling ?
    Quid des normales (générées ou pas) ?

  5. #5
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente Maritime (Poitou Charente)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut
    Il n'y a pas de normales étant donné que je suis en 2D. Le culling non plus ne me semble pas utile, vus que le peu que je vais afficher sera entièrement visible. Après je peux me tromper, que me conseillez vous ?

  6. #6
    Expert confirmé

    Avatar de dragonjoker59
    Homme Profil pro
    Software Developer
    Inscrit en
    Juin 2005
    Messages
    2 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Software Developer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2005
    Messages : 2 033
    Billets dans le blog
    12
    Par défaut
    Effectivement, pour les normales, je ne vois pas non plus ce que bacelar voulait dire.
    Par contre pour le cullling, je pense qu'il parlait du back face culling, qui peut être une piste à explorer, sachant que par défaut les faces arrières sont cachées (glCullFace(GL_BACK)), il faudrait voir si glDisable(GL_CULL_FACE) change quelque chose.
    Si vous ne trouvez plus rien, cherchez autre chose...

    Vous trouverez ici des tutoriels OpenGL moderne.
    Mon moteur 3D: Castor 3D, presque utilisable (venez participer, il y a de la place)!
    Un projet qui ne sert à rien, mais qu'il est joli (des fois) : ProceduralGenerator (Génération procédurale d'images, et post-processing).

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec Quake3 OpenGL
    Par Meister EngeL dans le forum Composants
    Réponses: 3
    Dernier message: 17/12/2007, 19h26
  2. [OpenGl] problème avec SDL_AddTimer
    Par Angelsafrania dans le forum SDL
    Réponses: 2
    Dernier message: 20/08/2007, 20h44
  3. Probléme avec OpenGL
    Par amine_star dans le forum Bibliothèques
    Réponses: 1
    Dernier message: 07/04/2007, 13h40
  4. [OpenGL] Problème avec glGetString
    Par djo.mos dans le forum API, COM et SDKs
    Réponses: 1
    Dernier message: 17/06/2005, 11h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo