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 gl_FragCoord


Sujet :

OpenGL

  1. #1
    Invité
    Invité(e)
    Par défaut Problème avec gl_FragCoord
    Salut, j'essaye d'effectuer un rendu par pixel de la lumière à l'aide d'une normal map.

    Les composantes rgb de la normale map contiennent les composantes x, y et z des normales (vecteurs perpendiculaires à mes tiles) et la composante w contient la hauteur du fragment. (que je défini moi même)

    J'envoie la position de la lumière : x et y contiennent la position de la lumière, z la hauteur de la lumière et j'envoie dans la composante w le rayon de la lumière, j'envoie donc la lumière comme ceci à mon fragment shader :

    perPixLightingShader->setParameter("uLightPos", el->getCenter().x, el->getCenter().y, el->getCenter().z, el->getSize().x);
    perPixLightingShader->setParameter("lightColor", el->getColor().r, el->getColor().g,el->getColor().b,el->getColor().a);

    Je transforme ensuite la position de la lumière dans le vertex shader pour l'avoir en coordonnées écran dans le fragment
    shader :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    "#version 130 \n"
            "uniform vec4 uLightPos;"
            "out vec4 lightPos;"
               "void main () {"
                    "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
                    "gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
                    "gl_FrontColor = gl_Color;"
                    "lightPos = gl_ModelViewMatrix * uLightPos;"
                    "lightPos.w = uLightPos.w;"
            "}";

    Le problème survient dans le fragment shader, la distance entre la position de la lumière et le pixel courant est toujours > au rayon et je ne comprends vraiment pas pourquoi :
    Code cpp : 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
     
    const std::string perPixLightingFragmentShader =
            "#version 130 \n"
            "uniform sampler2D normalMap;"
            "uniform vec3 resolution;"
            "uniform vec4 lightColor;"
            "uniform sampler2D lightMap;"
            "in vec4 lightPos;"
            "void main () { "
                "vec2 position = ( gl_FragCoord.xy / resolution.xy );"
                "vec4 bump = texture2D(normalMap, position);"
                "vec3 pixPos = vec3 (gl_FragCoord.x, gl_FragCoord.y, bump.a);"
                "vec4 lightMapColor = texture2D(lightMap, position);"
                "float ray = lightPos.a;"
                "if (gl_FragCoord.z >= bump.a && distance(lightPos.xyz, pixPos) <= ray) {"
                    "vec3 vertexToLight = lightPos.xyz - pixPos;"
                    "float attenuation = 1.0f - (length(vertexToLight) / ray);"
                    "if (bump.x != 0 || bump.y != 0 || bump.z != 0) {"
                        "vec3 dirToLight = normalize(vertexToLight.xyz);"
                        "float nDotl = dot (dirToLight, bump.xzy);"
                        "attenuation *= nDotl;"
                    "}"
                    "gl_FragColor = lightColor * max(0.0f, attenuation);"
                "} else {"
                    "gl_FragColor = lightMapColor;"
                "}"
            "}";

    Je pense que gl_FragCoord et lightPos ne sont pas dans le même repère, pourtant gl_FragCoord devrait donner la position du fragment par rapport au coin supérieur gauche de la fenêtre non ?
    Dernière modification par LittleWhite ; 26/07/2014 à 11h06. Motif: Tag

  2. #2
    Expert éminent sénior

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

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

    Informations forums :
    Inscription : Juin 2005
    Messages : 2 031
    Points : 11 379
    Points
    11 379
    Billets dans le blog
    10
    Par défaut
    Salut !

    En lisant la doc, je suis tombé sur ceci :
    By default, gl_FragCoord assumes a lower-left origin for window coordinates and assumes pixel centers are located at half-pixel centers.
    Donc position relative au coin inférieur gauche de la fenêtre.
    Cela ne pourrait-il pas venir de là ?

    EDIT:

    Dans la même page de doc (ici), il est dit que tu peux redéclarer gl_FragCoord avec les attributs qui t'intéressent :
    he origin of gl_FragCoord may be changed by redeclaring gl_FragCoord with the origin_upper_left identifier.
    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
    Invité
    Invité(e)
    Par défaut
    J'ai fais plusieurs choses, la première c'est de faire une fonction qui se charge de passer des coordonnées mondes en coordonnées pixel :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     states.shader = perPixLightingShader;
            states.blendMode = sf::BlendAdd;
            for (unsigned int i = 0; i < lights.size(); i++) {
                EntityLight* el = static_cast<EntityLight*> (lights[i]);
                Vec3f center = frcm->getWindow().mapCoordsToPixel(el->getCenter());
                perPixLightingShader->setParameter("lightPos", center.x, center.y, center.z, el->getSize().x * 0.5f);
                perPixLightingShader->setParameter("lightColor", el->getColor().r, el->getColor().g,el->getColor().b,el->getColor().a);
                lightMap->draw(*el, states);
            }

    Ainsi je passe directement la position de la lumière à mon fragment shader, sans devoir faire des transformations dans le vertex shader sans trop savoir.

    J'ai le x et le z de la lumière qui varient dans le fragment shader, le y reste constant car c'est la hauteur de la lumière (qui correspond au centre en y de la lumière dans mon cas)
    Donc plus on s'éloigne du centre de la lumière, plus la distance entre le centre de la lumière et le pixel sera grand, et donc, plus il y aura atténuation.
    En gros c'est ce que je veux faire.

    Les normales pointent toutes vers le haut (en 0, 1, 0) sauf sur le bord des tiles évidement.

    Ainsi ça me donnerai un éclairage plus atténué sur les bords.

    J'ai essayé de changer et de prendre le coin supérieur gauche tout comme moi je le fais mais ça ne compile pas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    layout(origin_upper_left) in vec4 gl_Fragcoord;
    J'ai alors eu l'idée d'inverser la coordonnées en y de la lumière pour qu'elle soient dans l'autre sens en y :

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    "vec3 nLightPos = vec3 (lightPos.x, lightPos.z, resolution.y - lightPos.y);"

    Mais cela ne fonctionne pas, voici le shader complet :

    Code cpp : 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
     
    "#version 130 \n"
            "uniform sampler2D normalMap;"
            "uniform vec3 resolution;"
            "uniform vec4 lightColor;"
            "uniform sampler2D lightMap;"
            "uniform vec4 lightPos;"
            "void main () { "
                "vec2 position = ( gl_FragCoord.xy / resolution.xy );"
                "vec4 bump = texture2D(normalMap, position);"
                "vec3 pixPos = vec3 (gl_FragCoord.x, bump.a * resolution.z, gl_FragCoord.y);"
                "vec4 lightMapColor = texture2D(lightMap, position);"
                "vec3 nLightPos = vec3 (lightPos.x, lightPos.z, resolution.y - lightPos.y);"
                "float ray = lightPos.w;"
                "if (gl_FragCoord.z >= bump.a && distance(nLightPos, pixPos) <= ray) {"
                    "vec3 vertexToLight = nLightPos - pixPos;"
                    "float attenuation = 1.0f - (length(vertexToLight) / ray);"
                    "if (bump.x != 0 || bump.y != 0 || bump.z != 0) {"
                        "vec3 dirToLight = normalize(vertexToLight.xyz);"
                        "float nDotl = dot (dirToLight, bump.xzy);"
                        "attenuation *= nDotl;"
                    "}"
                    "gl_FragColor = lightColor * max(0.0f, attenuation);"
                "} else {"
                    "gl_FragColor = lightMapColor;"
                "}"
            "}";

    Le fait est que je n'ai pas d'éclairage. :/

  4. #4
    Invité
    Invité(e)
    Par défaut
    En retirant le if ça fonctionne mais je n'ai pas d'atténuation, à croire que tout les fragments sont à la même position. (par rapport au centre de la lumière)
    .....

    Donc c'est juste la distance entre le fragment courant et le centre de la lumière qui n'est pas bonne et je vois pas pourquoi.

  5. #5
    Invité
    Invité(e)
    Par défaut
    Bon j'ai essayé cela (en prenant l'inverse de la matrice de projection pour dénormaliser le z) :

    Code cpp : 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
     
    const std::string perPixLightingVertexShader =
            "#version 130 \n"
            "out mat4 invProjMat;"
            "void main () {"
                    "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
                    "gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
                    "gl_FrontColor = gl_Color;"
                    "invProjMat = inverse(gl_ProjectionMatrix);"
            "}";
            const std::string perPixLightingFragmentShader =
            "#version 130 \n"
            "uniform sampler2D normalMap;"
            "uniform vec3 resolution;"
            "uniform vec4 lightColor;"
            "uniform sampler2D lightMap;"
            "uniform vec4 lightPos;"
            "in mat4 invProjMat;"
            "void main () { "
                "vec2 position = ( gl_FragCoord.xy / resolution.xy );"
                "vec4 bump = texture2D(normalMap, position);"
                "vec4 cBump = invProjMat * vec4(0, 0, bump.w, 0);"
                "vec3 pixPos = vec3 (gl_FragCoord.x, cBump.z, resolution.y - gl_FragCoord.y);"
                "vec4 lightMapColor = texture2D(lightMap, position);"
                "vec3 nLightPos = vec3 (lightPos.x, lightPos.z, lightPos.y);"
                "float radius = lightPos.w;"
                "if (gl_FragCoord.z >= bump.w && distance(pixPos, nLightPos) <= radius) {"
                    "vec3 vertexToLight = nLightPos - pixPos;"
                    "float attenuation = 1.0f - (length(vertexToLight) / radius);"
                    "if (bump.x != 0 || bump.y != 0 || bump.z != 0) {"
                        "vec3 dirToLight = normalize(vertexToLight.xyz);"
                        "float nDotl = dot (dirToLight, bump.xzy);"
                        "attenuation *= nDotl;"
                    "}"
                    "gl_FragColor = lightColor * max(0.0f, attenuation);"
                "} else {"
                    "gl_FragColor = lightMapColor;"
                "}"
            "}";

    Ca marche déjà mieux à part que la lumière disparaît trop vite de l'écran, comme si elle était trop vite près du "near plane" et du "far plane".

  6. #6
    Invité
    Invité(e)
    Par défaut
    J'ai essayé ça pour avoir le bon pixel sur la normal map et la lightmap:

    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    "vec2 position = (gl_FragCoord.xy / resolution.xy);"
    "position.y = resolution.y - position.y;"
    "vec4 bump = texture2D(normalMap, position);"
    "vec4 cBump = inverse(projMat) * vec4(0, 0, bump.w, 0);"
    "vec3 pixPos = vec3 (gl_FragCoord.x, cBump.z, resolution.y - gl_FragCoord.y);"
    "vec4 lightMapColor = texture2D(lightMap, position);"
    "vec3 nLightPos = vec3 (lightPos.x, lightPos.z, lightPos.y);"
    "float radius = lightPos.w;"

    Mais ça ne marche pas, le problème vient vraiment du fait que j'utilise le coin en haut à gauche pour dessiner la lumière comme référence, hors que OpenGL utilise le coin en bas à gauche comme référence pour la position des fragments.

    Etant donné que les layouts ne fonctionnent pas avec la version 130 du langage GLSL, j'ai installé mesa 10.3 qui supporte le GLSL 330 sur ubuntu mais il ne veut pas activer un context GLSL 3. :/
    Dernière modification par LittleWhite ; 12/07/2014 à 14h16.

  7. #7
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 859
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 859
    Points : 218 580
    Points
    218 580
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Etant donné que les layouts ne fonctionnent pas avec la version 130 du langage GLSL, j'ai installé mesa 10.3 qui supporte le GLSL 330 sur ubuntu mais il ne veut pas activer un context GLSL 3. :/
    Euh, la version 10.3 ? ( http://jeux.developpez.com/actu/7200...es-extensions/ )
    Sinon, testez avec l'application glxinfo pour savoir quelle est le contexte/la version maximale disponible.
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  8. #8
    Invité
    Invité(e)
    Par défaut
    Salut voici ce que me donne glxinfo :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     glxinfo | grep OpenGLOpenGL vendor string: X.Org
    OpenGL renderer string: Gallium 0.4 on AMD CEDAR
    OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.3.0-devel (git-ee2a818 trusty-oibaf-ppa)
    OpenGL core profile shading language version string: 3.30
    OpenGL core profile context flags: (none)
    OpenGL core profile profile mask: core profile
    OpenGL core profile extensions:
    OpenGL version string: 3.0 Mesa 10.3.0-devel (git-ee2a818 trusty-oibaf-ppa)
    OpenGL shading language version string: 1.30
    OpenGL context flags: (none)
    OpenGL extensions:
    Il me dit qu'il supporte la version 3.30 du shading language, et pourtant lorsque j'essayer de créer un context opengl avec le profile mask (core profile) il me dit que la version la plus récente d glsl supportée et la 1.30 donc celle de mon ancienne version :

    Code cpp : 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
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
     
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <GL/gl.h>
    #include <GL/glx.h>
     
    #define GLX_CONTEXT_MAJOR_VERSION_ARB       0x2091
    #define GLX_CONTEXT_MINOR_VERSION_ARB       0x2092
    typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
     
    // Helper to check for extension string presence.  Adapted from:
    //   <a href="http://www.opengl.org/resources/features/OGLextensions/" target="_blank">http://www.opengl.org/resources/features/OGLextensions/</a>
    static bool isExtensionSupported(const char *extList, const char *extension)
    {
      const char *start;
      const char *where, *terminator;
     
      /* Extension names should not have spaces. */
      where = strchr(extension, ' ');
      if (where || *extension == '\0')
        return false;
     
      /* It takes a bit of care to be fool-proof about parsing the
         OpenGL extensions string. Don't be fooled by sub-strings,
         etc. */
      for (start=extList;;) {
        where = strstr(start, extension);
     
        if (!where)
          break;
     
        terminator = where + strlen(extension);
     
        if ( where == start || *(where - 1) == ' ' )
          if ( *terminator == ' ' || *terminator == '\0' )
            return true;
     
        start = terminator;
      }
     
      return false;
    }
     
    static bool ctxErrorOccurred = false;
    static int ctxErrorHandler( Display *dpy, XErrorEvent *ev )
    {
        ctxErrorOccurred = true;
        return 0;
    }
     
    int main(int argc, char* argv[])
    {
      Display *display = XOpenDisplay(NULL);
     
      if (!display)
      {
        printf("Failed to open X display\n");
        exit(1);
      }
     
      // Get a matching FB config
      static int visual_attribs[] =
        {
          GLX_X_RENDERABLE    , True,
          GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
          GLX_RENDER_TYPE     , GLX_RGBA_BIT,
          GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
          GLX_RED_SIZE        , 8,
          GLX_GREEN_SIZE      , 8,
          GLX_BLUE_SIZE       , 8,
          GLX_ALPHA_SIZE      , 8,
          GLX_DEPTH_SIZE      , 24,
          GLX_STENCIL_SIZE    , 8,
          GLX_DOUBLEBUFFER    , True,
          //GLX_SAMPLE_BUFFERS  , 1,
          //GLX_SAMPLES         , 4,
          None
        };
     
      int glx_major, glx_minor;
     
      // FBConfigs were added in GLX version 1.3.
      if ( !glXQueryVersion( display, &glx_major, &glx_minor ) ||
           ( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) )
      {
        printf("Invalid GLX version");
        exit(1);
      }
     
      printf( "Getting matching framebuffer configs\n" );
      int fbcount;
      GLXFBConfig* fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount);
      if (!fbc)
      {
        printf( "Failed to retrieve a framebuffer config\n" );
        exit(1);
      }
      printf( "Found %d matching FB configs.\n", fbcount );
     
      // Pick the FB config/visual with the most samples per pixel
      printf( "Getting XVisualInfos\n" );
      int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;
     
      int i;
      for (i=0; i<fbcount; ++i)
      {
        XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
        if ( vi )
        {
          int samp_buf, samples;
          glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
          glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES       , &samples  );
     
          printf( "  Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
                  " SAMPLES = %d\n",
                  i, vi -> visualid, samp_buf, samples );
     
          if ( best_fbc < 0 || samp_buf && samples > best_num_samp )
            best_fbc = i, best_num_samp = samples;
          if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp )
            worst_fbc = i, worst_num_samp = samples;
        }
        XFree( vi );
      }
     
      GLXFBConfig bestFbc = fbc[ best_fbc ];
     
      // Be sure to free the FBConfig list allocated by glXChooseFBConfig()
      XFree( fbc );
     
      // Get a visual
      XVisualInfo *vi = glXGetVisualFromFBConfig( display, bestFbc );
      printf( "Chosen visual ID = 0x%x\n", vi->visualid );
     
      printf( "Creating colormap\n" );
      XSetWindowAttributes swa;
      Colormap cmap;
      swa.colormap = cmap = XCreateColormap( display,
                                             RootWindow( display, vi->screen ),
                                             vi->visual, AllocNone );
      swa.background_pixmap = None ;
      swa.border_pixel      = 0;
      swa.event_mask        = StructureNotifyMask;
     
      printf( "Creating window\n" );
      Window win = XCreateWindow( display, RootWindow( display, vi->screen ),
                                  0, 0, 100, 100, 0, vi->depth, InputOutput,
                                  vi->visual,
                                  CWBorderPixel|CWColormap|CWEventMask, &swa );
      if ( !win )
      {
        printf( "Failed to create window.\n" );
        exit(1);
      }
     
      // Done with the visual info data
      XFree( vi );
     
      XStoreName( display, win, "GL 3.0 Window" );
     
      printf( "Mapping window\n" );
      XMapWindow( display, win );
     
      // Get the default screen's GLX extension list
      const char *glxExts = glXQueryExtensionsString( display,
                                                      DefaultScreen( display ) );
     
      // NOTE: It is not necessary to create or make current to a context before
      // calling glXGetProcAddressARB
      glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
      glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
               glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
     
      GLXContext ctx = 0;
     
      // Install an X error handler so the application won't exit if GL 3.0
      // context allocation fails.
      //
      // Note this error handler is global.  All display connections in all threads
      // of a process use the same error handler, so be sure to guard against other
      // threads issuing X commands while this code is running.
      ctxErrorOccurred = false;
      int (*oldHandler)(Display*, XErrorEvent*) =
          XSetErrorHandler(&ctxErrorHandler);
     
      // Check for the GLX_ARB_create_context extension string and the function.
      // If either is not present, use GLX 1.3 context creation method.
      if ( !isExtensionSupported( glxExts, "GLX_ARB_create_context" ) ||
           !glXCreateContextAttribsARB )
      {
        printf( "glXCreateContextAttribsARB() not found"
                " ... using old-style GLX context\n" );
        ctx = glXCreateNewContext( display, bestFbc, GLX_RGBA_TYPE, 0, True );
      }
     
      // If it does, try to get a GL 3.0 context!
      else
      {
        int context_attribs[] =
          {
            GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
            GLX_CONTEXT_MINOR_VERSION_ARB, 0,
            GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
            //GLX_CONTEXT_FLAGS_ARB        , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
            None
          };
     
        printf( "Creating context\n" );
        ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
                                          True, context_attribs );
     
        // Sync to ensure any errors generated are processed.
        XSync( display, False );
        if ( !ctxErrorOccurred && ctx )
          printf( "Created GL 3.0 context\n" );
        else
        {
          // Couldn't create GL 3.0 context.  Fall back to old-style 2.x context.
          // When a context version below 3.0 is requested, implementations will
          // return the newest context version compatible with OpenGL versions less
          // than version 3.0.
          // GLX_CONTEXT_MAJOR_VERSION_ARB = 1
          context_attribs[1] = 1;
          // GLX_CONTEXT_MINOR_VERSION_ARB = 0
          context_attribs[3] = 0;
     
          ctxErrorOccurred = false;
     
          printf( "Failed to create GL 3.0 context"
                  " ... using old-style GLX context\n" );
          ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
                                            True, context_attribs );
        }
      }
     
      // Sync to ensure any errors generated are processed.
      XSync( display, False );
     
      // Restore the original error handler
      XSetErrorHandler( oldHandler );
     
      if ( ctxErrorOccurred || !ctx )
      {
        printf( "Failed to create an OpenGL context\n" );
        exit(1);
      }
     
      // Verifying that context is a direct context
      if ( ! glXIsDirect ( display, ctx ) )
      {
        printf( "Indirect GLX rendering context obtained\n" );
      }
      else
      {
        printf( "Direct GLX rendering context obtained\n" );
      }
     
      printf( "Making context current\n" );
      glXMakeCurrent( display, win, ctx );
      const unsigned char* glslversion = glGetString(GL_SHADING_LANGUAGE_VERSION);
      printf("%s\n", glslversion);
      const unsigned char* openglversion = glGetString(GL_VERSION);
      printf("%s\n", openglversion);
      glClearColor( 0, 0.5, 1, 1 );
      glClear( GL_COLOR_BUFFER_BIT );
      glXSwapBuffers ( display, win );
     
      sleep( 1 );
     
      glClearColor ( 1, 0.5, 0, 1 );
      glClear ( GL_COLOR_BUFFER_BIT );
      glXSwapBuffers ( display, win );
     
      sleep( 1 );
     
      glXDestroyContext( display, ctx );
      glXMakeCurrent( display, 0, 0 );
     
     
      XDestroyWindow( display, win );
      XFreeColormap( display, cmap );
      XCloseDisplay( display );
      return 0;
    }

    Hum..., comment faire pour changer a version par défaut du driver ? (Afin qu'il ne prenne plus l'ancienne)

  9. #9
    Invité
    Invité(e)
    Par défaut
    Ho purée, il fallait simplement que je mette minor version à 3 aussi, je ne comprenais absolument pas à quoi ça correspondait minor version :

    Mais maintenant je sais que ça correspond au deuxième chiffre de la version. (Arg!)

  10. #10
    Invité
    Invité(e)
    Par défaut Code qui ne compile pas.
    Bon, j'ai essayé ce code ci :

    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
     
    "#version 330 core \n"
                "layout(origin_upper_left) in vec4 gl_FragCoord;"
                "layout(pixel_center_integer);"
                "layout(location = 0) in vec3 vertex_position;"
                "layout(location = 1) in vec4 vertex_color;"
                "layout(location = 2) in vec2 vertex_texCoords0;"
                "layout(location = 3) in vec3 vertex_normal;"
                "uniform mat4 mvp;"
                "out vec2 texCoords;"
                "out vec4 color;"
                "void main () {"
                    "gl_Position = mvp * vec4(vertex_position.xyz, 1);"
                    "texCoords = vertex_texCoords0;"
                    "color = vertex_color;"
                "}";
    Mais ça ne compile pas.

  11. #11
    Invité
    Invité(e)
    Par défaut
    Bon bah j'ai trouvé :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     layout(origin_upper_left, pixel_center_integer) in vec4 gl_FragCoord;
    Reste plus qu'à voir si les coordonnées des fragments seront bien celle que j'attend.

Discussions similaires

  1. VC++ Direct3D8, problème avec LPD3DXFONT et LPD3DTEXTURE8
    Par Magus (Dave) dans le forum DirectX
    Réponses: 3
    Dernier message: 03/08/2002, 11h10
  2. Problème avec [b]struct[/b]
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 2
    Dernier message: 17/07/2002, 10h25
  3. Problème avec le type 'Corba::Any_out'
    Par Steven dans le forum CORBA
    Réponses: 2
    Dernier message: 14/07/2002, 18h48
  4. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10

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