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 :

Problemes sur skybox [OpenGL 3.x]


Sujet :

OpenGL

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    872
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

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

    Informations forums :
    Inscription : Mai 2010
    Messages : 872
    Par défaut Problemes sur skybox
    Bonjour,

    Je suis en train de passer tout mon code en opengl "moderne" et je rencontre actuellement plusieurs problemes avec ma skybox :

    - comme il n'y a plus vraiment possibilite de desactiver le depth mask (je n'ai pas trouve comment faire tout du moins), il y a des fois des angles de ma skybox qui sortent de la taille de la profondeur et nous avons donc des trous qui apparaissent.
    - on voit les "lignes" entre les points qui buggent (allant meme jusqu'a etre dessiner devant d'autres objets).
    - on voit les jointures du cube (ca je pense que ce n'est qu'une question de parametrage des textures).

    Comme il n'y a de rien qu'une image pour illustrer les problemes cites precedemment, la voici :



    Voici donc le code de ma classe skybox :

    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
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    #include "SkyBox.hpp"
    #include "../MyWindow.hpp"
    #include "../HandleError.hpp"
    #include "../String_utils.hpp"
    #include "../Camera.hpp"
    #include <GL/glew.h>
     
    #include <map>
    #include <string>
     
    using namespace std;
    using namespace Object;
     
    void  HandleSDL::createSkyBoxTextures(string textures[])
    {
      GLenum cube_map_target[6] = {
        GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
        GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
        GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
        GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
        GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
      };
     
      for (int i = 0; i < 6; i++)
        {
          SDL_Surface * picture_surface = NULL;
          SDL_Surface *gl_surface = NULL;
          SDL_Surface * gl_fliped_surface = NULL;
          Uint32 rmask, gmask, bmask, amask;
     
          picture_surface = IMG_Load(textures[i].c_str());
          if (picture_surface == NULL)
            {
              HandleError::showError(textures[i] + std::string(": No such file or directory"));
              continue;
            }
     
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
          rmask = 0xff000000;
          gmask = 0x00ff0000;
          bmask = 0x0000ff00;
          amask = 0x000000ff;
    #else
          rmask = 0x000000ff;
          gmask = 0x0000ff00;
          bmask = 0x00ff0000;
          amask = 0xff000000;
    #endif
     
          SDL_PixelFormat format = *(picture_surface->format);
          format.BitsPerPixel = 32;
          format.BytesPerPixel = 4;
          format.Rmask = rmask;
          format.Gmask = gmask;
          format.Bmask = bmask;
          format.Amask = amask;
     
          gl_surface = SDL_ConvertSurface(picture_surface, &format, SDL_SWSURFACE);
     
          gl_fliped_surface = flipSurface(gl_surface);
          gluBuild2DMipmaps(cube_map_target[i], 4, gl_fliped_surface->w,
                            gl_fliped_surface->h, GL_RGBA, GL_UNSIGNED_BYTE,
                            gl_fliped_surface->pixels);
     
          SDL_FreeSurface(gl_fliped_surface);
          SDL_FreeSurface(gl_surface);
          SDL_FreeSurface(picture_surface);
        }
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //GL_NEAREST_MIPMAP_LINEAR
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_NEAREST_MIPMAP_LINEAR
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //GL_CLAMP
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //GL_CLAMP
      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    }
     
    SkyBox::SkyBox(float size) : Cube(Vector3D(0.f, 0.f, 0.f), Rotation(), Color(), size)
    {
      m_hasTexture = true;
      m_shader = new Shader;
      m_pointsNumber = 0;
     
      glGenTextures(1, &cube_map_texture_ID);
     
      glEnable(GL_TEXTURE_CUBE_MAP_ARB);
      glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cube_map_texture_ID);
     
    #ifdef WIN32
      std::string tex[] = {"./textures/skybox/west.jpg", "./textures/skybox/east.jpg", "./textures/skybox/ground.jpg",
                           "./textures/skybox/sky.jpg", "./textures/skybox/north.jpg", "./textures/skybox/south.jpg"};
    #else
      std::string tex[] = {"/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/west.jpg",
                           "/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/east.jpg",
                           "/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/ground.jpg",
                           "/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/sky.jpg",
                           "/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/north.jpg",
                           "/windows/Users/imperio/Projects/My_World_SDL/My_World_SDL/My_World_SDL/textures/skybox/south.jpg"};
    #endif
      MyWindow::createSkyBoxTextures(tex);
      glDisable(GL_TEXTURE_CUBE_MAP_ARB);
    }
     
    SkyBox::~SkyBox()
    {
      /*for (int i = 0; i < 6; ++i)
        glDeleteTextures(1, &textures[i]);*/
      glDeleteTextures(1, &cube_map_texture_ID);
    }
     
    void  SkyBox::initializeGL()
    {
      std::string vert;
      std::string frag;
     
      vert =
          "#version 330\n"
     
          "in vec3 vertor;\n"
          "out vec3 texCoord;\n"
          "uniform mat4 PVM;\n"
     
          "void main(){\n"
          "gl_Position = PVM * vec4(vertor, 1.0);\n"
          "texCoord = vertor;\n"
          "}";
     
      frag =
          "#version 330\n"
     
          "in vec3 texCoord;\n"
          "out vec4 fragColor;\n"
          "uniform samplerCube cubemap;\n"
     
          "void main (void){\n"
          "fragColor = texture(cubemap, texCoord);\n"
          "};";
      m_shader->setVertexSource(vert);
      m_shader->setFragmentSource(frag);
      if (!m_shader->load()){
          HandleError::showError("Shader didn't load in Skybox");
          exit(-1);
        }
     
      GLfloat tmpVertices[] = {
        -1.f,-1.f,-1.f, -1.f,1.f,-1.f, -1.f,-1.f,1.f, -1.f,1.f,1.f,
        1.f,-1.f,-1.f, 1.f,-1.f,1.f, 1.f,1.f,-1.f, 1.f,1.f,1.f,
        -1.f,-1.f,-1.f, -1.f,-1.f,1.f, 1.f,-1.f,-1.f, 1.f,-1.f,1.f,
        -1.f,1.f,-1.f, 1.f,1.f,-1.f, -1.f,1.f,1.f, 1.f,1.f,1.f,
        -1.f,-1.f,-1.f, 1.f,-1.f,-1.f, -1.f,1.f,-1.f, 1.f,1.f,-1.f,
        -1.f,-1.f,1.f, -1.f,1.f,1.f, 1.f,-1.f,1.f, 1.f,1.f,1.f};
     
      int max = sizeof(tmpVertices) / sizeof(tmpVertices[0]);
     
      for (int i(0); i < max; ++i){
          m_vertices.push_back(tmpVertices[i]);
          m_textures.push_back(tmpVertices[i]);
        }
      m_pointsNumber = m_vertices.size() / 3;
     
      PVM = glGetUniformLocation(m_shader->getProgramID(), "PVM");
      vertex = glGetAttribLocation(m_shader->getProgramID(), "vertor");
     
      this->initVertexBufferObject();
      this->initVertexArrayObject();
    }
     
    void  SkyBox::initVertexBufferObject()
    {
      m_verticesSize = m_vertices.size() * sizeof(GLfloat);
      m_texturesSize = m_textures.size() * sizeof(GLfloat);
     
      glGenBuffers(1, &m_vboID);
      //verrouillage de l'objet
      glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
     
      // Allocation de la memoire video
      glBufferData(GL_ARRAY_BUFFER, m_verticesSize + m_texturesSize, 0, GL_STATIC_DRAW);
     
      // Transfert des donnees
      glBufferSubData(GL_ARRAY_BUFFER, 0, m_verticesSize, &m_vertices[0]);
      glBufferSubData(GL_ARRAY_BUFFER, m_verticesSize, m_texturesSize, &m_textures[0]);
     
      glEnableVertexAttribArray(vertex);
      glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
     
      // Deverrouillage de l'objet
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
     
    void  SkyBox::bindVertexBufferObject()
    {
      // Verrouillage du VBO
      glBindBuffer(GL_ARRAY_BUFFER, m_vboID);
     
      // Acces aux vertices dans la memoire video
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
      glEnableVertexAttribArray(0);
     
      //envoi des coordonnees des textures
      glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(m_verticesSize));
      glEnableVertexAttribArray(2);
      // Deverrouillage du VBO
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
     
    void  SkyBox::initializeGLNoList()
    {
    }
     
    void  SkyBox::paintGL(float &x, float &y)
    {
      glUseProgram(m_shader->getProgramID());
     
      glBindVertexArray(m_vaoID);
     
      float dist = Camera::getDistanceView() - 400.f;
      glm::mat4 RotateX = glm::rotate(glm::mat4(1.0f), x, glm::vec3(-1.0f, 0.0f, 0.0f));
      glm::mat4 RotateY = glm::rotate(RotateX, y, glm::vec3(0.0f, 1.0f, 0.0f));
      glm::mat4 View = glm::mat4(1.0f);
      glm::mat4 Model = glm::scale(glm::mat4(1.0f), glm::vec3(dist, dist, dist));
      glm::mat4 M = Camera::getProjectionMatrix() * View * Model * RotateY;
     
      glUniformMatrix4fv(PVM, 1, GL_FALSE, glm::value_ptr(M));
     
      glEnable(GL_TEXTURE_CUBE_MAP_ARB);
      m_texture.bind();
      glDrawArrays(GL_TRIANGLE_STRIP, 0, m_pointsNumber);
      m_texture.unbind();
      glDisable(GL_TEXTURE_CUBE_MAP_ARB);
     
      glBindVertexArray(0);
      glUseProgram(0);
    }
    Je crois que tout le code qui nous interesse est la.

    Merci d'avance pour votre aide !

    PS: j'en profite pour poser une autre question : vaut-il mieux utiliser GL_TRIANGLES ou GL_TRIANGLE_STRIP ?

  2. #2
    Membre chevronné

    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    311
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 311
    Par défaut
    Salut

    Pour désactiver le Z-Buffer, en lecture on utilise glDisable(GL_DEPTH_TEST) et en écriture on utilise glDepthMask(GL_FALSE). Cela t’évitera de dimensionner et placer ta skybox dans le repère monde et donc te passer de la matrice model pour ton pipeline de rendu afin de corriger ton near/far clipping.
    Cela permettra même de régler ton problème de jointure entre tes arrêtes, si celui-ci résulte d’un problème de précision.

    Citation Envoyé par imperio Voir le message
    PS: j'en profite pour poser une autre question : vaut-il mieux utiliser GL_TRIANGLES ou GL_TRIANGLE_STRIP ?
    Les séquences Strip ou Fan permettent de réduire considérablement la taille de tes VBO et IBO

  3. #3
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    872
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

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

    Informations forums :
    Inscription : Mai 2010
    Messages : 872
    Par défaut
    Eh bien, merci... Je ne m'attendais pas a une solution aussi simple ! C'est donc resolu ! Voici le code modifie :

    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
    void  SkyBox::paintGL(float &x, float &y)
    {
        glUseProgram(m_shader->getProgramID());
        glDepthMask(GL_FALSE);
        glBindVertexArray(m_vaoID);
     
        glm::mat4 RotateX = glm::rotate(glm::mat4(1.0f), x, glm::vec3(-1.0f, 0.0f, 0.0f));
        glm::mat4 RotateY = glm::rotate(RotateX, y, glm::vec3(0.0f, 1.0f, 0.0f));
        glm::mat4 M = Camera::getProjectionMatrix() * RotateY;
     
        glUniformMatrix4fv(PVM, 1, GL_FALSE, glm::value_ptr(M));
     
        glEnable(GL_TEXTURE_CUBE_MAP_ARB);
        m_texture.bind();
        glDrawArrays(GL_TRIANGLE_STRIP, 0, m_pointsNumber);
        m_texture.unbind();
        glDisable(GL_TEXTURE_CUBE_MAP_ARB);
     
        glDepthMask(GL_TRUE);
     
        glBindVertexArray(0);
        glUseProgram(0);
    }

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

Discussions similaires

  1. [LG]Probleme sur l'identification de typage
    Par axelmeunier dans le forum Langage
    Réponses: 10
    Dernier message: 19/01/2004, 13h41
  2. Réponses: 3
    Dernier message: 07/04/2003, 20h06
  3. Probleme sur le Fields des fichiers Xmlgram
    Par Sandrine75 dans le forum XMLRAD
    Réponses: 4
    Dernier message: 20/03/2003, 17h09
  4. Probleme sur un AppendChild
    Par Toxine77 dans le forum XMLRAD
    Réponses: 3
    Dernier message: 14/03/2003, 18h25
  5. Probleme sur les chaines de caractere
    Par scorpiwolf dans le forum C
    Réponses: 8
    Dernier message: 06/05/2002, 19h01

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