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 :

afficher une texture dans une texture


Sujet :

OpenGL

  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 560
    Par défaut afficher une texture dans une texture
    Bonjour,
    Je suis pas un grand spécialiste d opengl mais j ai un petit projet qui nécessite opengl.

    En fait j affiche un flux de ma webcam et je voudrais insérer le flux d une vidéo. En fait j ai un programme qui va détecter un marker 2d grâce à aruco et je voudrais insérer le flux vidéo sur ce marker.

    Je connais la position de ma webcam par rapport à ce marker. Je ne sais pas comment je dois appréhender le problème.

    J ai donc une texture qui affiche le flux de ma webcam. Comment dois afficher le flux de ma vidéo ? Dans une texture? Faire de l affichage pixel par pixel dans la texture initiale?

    Si quelqu un pouvait m éclairer?

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 560
    Par défaut
    J'ai travaillé un peu sur mon projet et j'ai le résultat suivant. J'ai donc mon flux video qui passe par un traitement sur cuda et qui doit etre affiché.
    L'affichage semble bien se passer mais je veux afficher un element au dessus de ma texture d'arriere plan (dans l'exemple un teapot) mais ca ne fonctionne pas.
    Voici deja mon code:
    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
    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
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    #include "Displayer.h"
     
     
    static const char *glsl_drawtex_vertshader_src =
            "void main(void)\n"
            "{\n"
            "    gl_Position = gl_Vertex;\n"
            "    gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;\n"
            "}\n";
     
    static const char *glsl_drawtex_fragshader_src =
            "#version 130\n"
            "uniform usampler2D texImage;\n"
            "void main()\n"
            "{\n"
            "   vec4 c = texture(texImage, gl_TexCoord[0].xy);\n"
            "    gl_FragColor = c / 255.0;\n"
            "}\n";
     
     
     
    static const char *glsl_draw_fragshader_src =
            "#version 130\n"
            "out uvec4 FragColor;\n"
            "void main()\n"
            "{"
            "  FragColor = uvec4(gl_Color.xyz * 255.0, 255.0);\n"
            "}\n";
     
     
    static const char *glsl_draw_vert =
            "void main()\n"
            "{\n"
            "    gl_Position = ftransform();\n"
            "}\n";
     
     
     
    static const char *glsl_draw_frag =
            "void main()\n"
            "{\n"
            "    gl_FragColor = vec4(0.4,0.4,0.8,1.0);\n"
            "}\n";
     
     
    __global__ void DisplayerKernel(u_int8_t *u8x3_Data, unsigned int *g_odata, int w,int h)
    {
     
        int x = blockIdx.x*blockDim.x;
        int y = blockIdx.y*blockDim.y;
        int xglobal = w - x+threadIdx.x;
        int yglobal = h - y+threadIdx.y;
     
     
        if(xglobal>=w || yglobal >=h)
            return;
     
        int iptr        =  xglobal+ yglobal*w;
        int iptr_dst    =  (xglobal-1)+ (h-yglobal-1)*w;
     
        int iptr_rgb    =  xglobal*3+ yglobal*w*3;
        u_int8_t u8_R   = u8x3_Data[iptr_rgb];
        u_int8_t u8_G   = u8x3_Data[iptr_rgb+1];
        u_int8_t u8_B   = u8x3_Data[iptr_rgb+2];
        u_int8_t u8_A   = 0x00;
     
        g_odata[iptr_dst]   = (int(u8_A)<<24) | (int(u8_R)<<16) | (int(u8_G)<<8) | int(u8_B);
     
    }
     
     
    Displayer::Displayer(int i_windowheight, int i_windowwidth, int i_imageheight, int i_imagewidth):
        // Blending_GPU(iwidth,iheight)
        i_windowwidth_(i_windowwidth),
        i_windowheight_(i_windowheight),
        i_imagewidth_(i_imagewidth),
        i_imageheight_(i_imageheight)
    {
        //Init the GL context
        init();
    }
     
     
    Displayer::~Displayer()
    {
        // free device memory
        std::cout << "Destroy Displayer " << std::endl;
    }
     
     
     
    void Displayer::display(u_int8_t* u8x3_rgbImage_Device)
    {
     
     
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
     
        // run the Cuda kernel
        unsigned int *out_data;
        out_data = cuda_dest_resource;
     
        dim3 blocks(ceil((float)i_imagewidth_ / ( BLOCK_SIZE_X)), ceil((float)i_imageheight_ / BLOCK_SIZE_Y));
        dim3 threads(BLOCK_SIZE_X, BLOCK_SIZE_Y);
     
        DisplayerKernel<<<blocks,threads>>>(u8x3_rgbImage_Device,out_data,i_imagewidth_,i_imageheight_);
        checkCudaErrors(cudaGetLastError());
     
        // We want to copy cuda_dest_resource data to the texture
        // map buffer objects to get CUDA device pointers
        cudaArray *texture_ptr;
        checkCudaErrors(cudaGraphicsMapResources(1, &cuda_tex_result_resource, 0));
        checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(&texture_ptr, cuda_tex_result_resource, 0, 0));
     
        int inum_texels = i_imagewidth_ * i_imageheight_;
        int inum_values = inum_texels * 4;
        int isize_tex_data = sizeof(GLubyte) * inum_values;
        checkCudaErrors(cudaMemcpyToArray(texture_ptr, 0, 0, cuda_dest_resource, isize_tex_data, cudaMemcpyDeviceToDevice));
     
        checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_tex_result_resource, 0));
     
     
        ////////////////////////////////////
        ///////////////////////////////////////////////////
        /// Display texture
        glBindTexture(GL_TEXTURE_2D, tex_cudaResult);
        glEnable(GL_TEXTURE_2D);
    //    glDisable(GL_DEPTH_TEST);
        //glDisable(GL_LIGHTING);
    //    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     
        /*glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        //glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 10.0);
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
     
        glViewport(0, 0, i_windowwidth_, i_windowheight_);
    */
        glUseProgram(shDrawTex);
        GLint id = glGetUniformLocation(shDrawTex, "texImage");
        glUniform1i(id, 0); // texture unit 0 to "texImage"
     
     
        glBegin(GL_QUADS);
        glTexCoord2f(0.0, 0.0);
        glVertex3f(-1.0, -1.0, 0.0);
        glTexCoord2f(1.0, 0.0);
        glVertex3f(1.0, -1.0, 0.0);
        glTexCoord2f(1.0, 1.0);
        glVertex3f(1.0, 1.0, 0.0);
        glTexCoord2f(0.0, 1.0);
        glVertex3f(-1.0, 1.0, 0.0);
        glEnd();
     
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
     
        glDisable(GL_TEXTURE_2D);
     
        cudaDeviceSynchronize();
     
        // flip backbuffer
        //glutSwapBuffers();
     
     
     
        glUseProgram(shDraw);
    //    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        glLoadIdentity();
        gluLookAt(0.0,0.0,5.0,
                  0.0,0.0,-1.0,
                  0.0f,1.0f,0.0f);
     
        static float a= 0;
        //glLightfv(GL_LIGHT0, GL_POSITION, lpos);
        glRotatef(a,0,1,1);
        glutSolidTeapot(1);
        a+=0.1;
     
        glutSwapBuffers();
        glUseProgram(0);
     
    }
     
    void printShaderInfoLog(GLuint obj)
    {
        int infologLength = 0;
        int charsWritten  = 0;
        char *infoLog;
     
        glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infologLength);
     
        if (infologLength > 0)
        {
            infoLog = (char *)malloc(infologLength);
            glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
            printf("%s\n",infoLog);
            free(infoLog);
        }
    }
     
    void Displayer::init()
    {
     
     
     
     
     
     
        setenv ("DISPLAY", ":0", 0);
     
        /////////////////////////
        // Create GL context
        char *myargv[1];
        int myargc=1;
        myargv [0]= "BGEDisplayer";
        glutInit(&myargc, myargv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH);
        glutInitWindowSize(i_windowwidth_, i_windowheight_);
        iGLUTWindowHandle = glutCreateWindow("CUDA OpenGL post-processing");
     
     
            glEnable(GL_DEPTH_TEST);
            glClearColor(1.0,1.0,1.0,1.0);
            glEnable(GL_CULL_FACE);
     
     
        // initialize necessary OpenGL extensions
        glewInit();
     
        /*glClearColorIuiEXT(128,128,128,255);
     
        glDisable(GL_DEPTH_TEST);*/
     
        // viewport
        glViewport(0, 0, i_windowwidth_, i_windowheight_);
     
        // projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, (GLfloat)i_windowwidth_ / (GLfloat) i_windowheight_, 0.1f, 10.0f);
        glMatrixMode(GL_MODELVIEW);
     
     
     
        /////////////////////////////
        //// createTextureDst
        // create a texture
        glGenTextures(1, &tex_cudaResult);
        glBindTexture(GL_TEXTURE_2D, tex_cudaResult);
     
        // set basic parameters
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI_EXT, i_imagewidth_, i_imageheight_, 0, GL_RGBA_INTEGER_EXT, GL_UNSIGNED_BYTE, NULL);
        // register this texture with CUDA
        checkCudaErrors(cudaGraphicsGLRegisterImage(&cuda_tex_result_resource, tex_cudaResult,
                                                    GL_TEXTURE_2D, cudaGraphicsMapFlagsWriteDiscard));
     
        // load shader programs
        shDraw      = compileGLSLprogram(glsl_draw_vert, glsl_draw_frag);
        shDrawTex   = compileGLSLprogram(glsl_drawtex_vertshader_src, glsl_drawtex_fragshader_src);
     
     
        //     initCUDABuffers()
        // set up vertex data parameter
        int inum_texels = i_imagewidth_ * i_imageheight_;
        int inum_values = inum_texels * 4;
        int isize_tex_data = sizeof(GLubyte) * inum_values;
        checkCudaErrors(cudaMalloc((void **)&cuda_dest_resource, isize_tex_data));
     
     
     
     
    }
     
     
     
    GLuint Displayer::compileGLSLprogram(const char *vertex_shader_src, const char *fragment_shader_src)
    {
        GLuint v, f, p = 0;
     
        p = glCreateProgram();
     
        if (vertex_shader_src)
        {
            v = glCreateShader(GL_VERTEX_SHADER);
            glShaderSource(v, 1, &vertex_shader_src, NULL);
            glCompileShader(v);
     
            // check if shader compiled
            GLint compiled = 0;
            glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
     
            if (!compiled)
            {
                //#ifdef NV_REPORT_COMPILE_ERRORS
                char temp[256] = "";
                glGetShaderInfoLog(v, 256, NULL, temp);
                printf("Vtx Compile failed:\n%s\n", temp);
                //#endif
                glDeleteShader(v);
                return 0;
            }
            else
            {
                glAttachShader(p,v);
            }
        }
     
        if (fragment_shader_src)
        {
            f = glCreateShader(GL_FRAGMENT_SHADER);
            glShaderSource(f, 1, &fragment_shader_src, NULL);
            glCompileShader(f);
     
            // check if shader compiled
            GLint compiled = 0;
            glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
     
            if (!compiled)
            {
                //#ifdef NV_REPORT_COMPILE_ERRORS
                char temp[256] = "";
                glGetShaderInfoLog(f, 256, NULL, temp);
                printf("frag Compile failed:\n%s\n", temp);
                //#endif
                glDeleteShader(f);
                return 0;
            }
            else
            {
                glAttachShader(p,f);
            }
        }
     
        glLinkProgram(p);
     
        int infologLength = 0;
        int charsWritten  = 0;
     
        glGetProgramiv(p, GL_INFO_LOG_LENGTH, (GLint *)&infologLength);
     
        if (infologLength > 0)
        {
            char *infoLog = (char *)malloc(infologLength);
            glGetProgramInfoLog(p, infologLength, (GLsizei *)&charsWritten, infoLog);
            printf("Shader compilation error: %s\n", infoLog);
            free(infoLog);
        }
     
        return p;
    }
    Je ne comprend pas pourquoi mon teapot ne s'affiche pas au dessus de ma texture.
    Quelqu'un aurait une piste?

  3. #3
    Expert confirmé

    Avatar de dragonjoker59
    Homme Profil pro
    Software Developer
    Inscrit en
    Juin 2005
    Messages
    2 032
    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 032
    Billets dans le blog
    12
    Par défaut
    Salut!

    Pour que ton modèle s'affiche au dessus de la texture, il faut dessiner celle-ci en arrière plan (ce que tu fais) en désactivant le depth write pendant que tu la dessines: Il faut pour cela combiner glDisable( GL_DEPTH_TEST ); et glDepthMask( GL_FALSE );

    Note à part : il serait temps de te débarrasser des fonctions OpenGL 1.x (glBegin, glEnd, glVertex, ...) et d'utiliser les VBO, VAO et tout le tintouin.
    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).

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 560
    Par défaut
    Merci pour la réponse.

    Je ne trouve pas d'exemple pour l'utilisation de VAO dans le cas d'une texture d'arrière plan.
    Est-ce la même chose qu'une texture normal?

  5. #5
    Expert confirmé

    Avatar de dragonjoker59
    Homme Profil pro
    Software Developer
    Inscrit en
    Juin 2005
    Messages
    2 032
    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 032
    Billets dans le blog
    12
    Par défaut
    Les VBO et VAO servent à l'affichage des modèles (en l'occurence ton quad), à part pour les coordonnées de texture, aucun rapport avec la texture.
    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).

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    560
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2007
    Messages : 560
    Par défaut
    Merci pour la réponse.

    Comment on va différencier une texture que l on va pouvoir bouger d une texture d'arrière plan dans le code?

  7. #7
    Expert confirmé

    Avatar de dragonjoker59
    Homme Profil pro
    Software Developer
    Inscrit en
    Juin 2005
    Messages
    2 032
    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 032
    Billets dans le blog
    12
    Par défaut
    Ta texture d'arrière plan est dessinée sur un quad faisant les dimensions du viewport courant, et l'arrière plan est (dans un premier temps en tout cas) est affiché avant tout le reste.
    Les modèles que tu affiches en premier plan possèdent leurs propres coordonnées de texture.
    Donc tu remarques que ce n'est pas la texture que l'on bouge, mais l'objet qui la porte.
    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).

Discussions similaires

  1. [Débutant] Afficher des messages dans une RichTextBox dans une FORM
    Par Poulki dans le forum C#
    Réponses: 1
    Dernier message: 12/05/2015, 10h19
  2. Réponses: 5
    Dernier message: 15/09/2011, 22h53
  3. [XL-2007] Afficher une checkbox dans une feuille si une checkbox d'une autre feuille est cochée
    Par JessieCoutas dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 18/08/2009, 13h35
  4. Envoyer une formulaire dans une page dans une Frame
    Par zooffy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 29/06/2007, 10h13
  5. Recherche une valeur d'une cellule dans une colonne d'une autre feuille
    Par kourria dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 21/06/2007, 13h48

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