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

WebGL Discussion :

WebGL affichage de texture


Sujet :

WebGL

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 9
    Points
    9
    Par défaut WebGL affichage de texture
    Bonjours, mon problème est le suivant je suis en train de suivre le cours sur WebGL dispo sur le site, mais à la leçon 5, je n'arrive pas à afficher ma texture, je pense qu'il s'agît d'un problème de chargement de l'image. Car lorsque je lance l'exemple (lien en haut du cours tout fonctionne), cependant même en copiant les source de cette page et en changeant le chemin vers l'image, je n'obtient qu'un cube noir, or j'ai vu en parcourant le web que ce problème était récurant mais je n'ai pas trouver de solution.

    Je vous met en ligne mon code qui est grossièrement le même que celui du cour, mais bon sait-on jamais, j'ai peut-être fais une faute de frappe.
    Merci d'avance.

    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
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
     
     <!--Header-->
        <head>
            <title>Ma page WebGL</title>
    		<meta http-equiv="content-type" content="text/html; charset=windows-1252">
            <script type="text/javascript" src="scripts/glMatrix-0.js"></script>
            <script type="text/jscript" src="scripts/webgl-utils.js"></script>
     
            <!--Style-->
            <style type="text/css">
                body
                {
                    background-color: rgb(36,147,174);
                }
            </style>
     
            <!--Shaders-->
            <script id="shader-vs" type="x-shader/x-vertex">
                attribute vec3 aVertexPosition;
                attribute vec2 aTextureCoord;
     
                uniform mat4 uMVMatrix;
                uniform mat4 uPMatrix;
     
                varying vec2 vTextureCoord;
     
                void main (void) {
                    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
                    vTextureCoord = aTextureCoord;
                }
            </script>
     
            <script id="shader-fs" type="x-shader/x-fragment">
                precision mediump float;
     
                uniform sampler2D uSampler;
     
                varying highp vec2 vTextureCoord;           
     
                void main(void) {
                    gl_FragColor = texture2D(uSampler, vTextureCoord);
                }
            </script>
     
            <!--Scripts-->
            <script type="text/javascript">
                /*Variables*/
                var gl;
                var shaderProgram;
                var mvMatrix = mat4.create();
                var pMatrix = mat4.create();
                var mvMatrixStack = [];
     
                ////////////////////////////////////
                //Stack matrix function
                function mvPushMatrix() {
                    var copy = mat4.create();
                    mat4.set(mvMatrix, copy);
                    mvMatrixStack.push(copy);
                }
     
                ////////////////////////////////////
                //Stack matrix function
                function mvPopMatrix() {
                    if (mvMatrixStack.length == 0) {
                        throw "Invalid popMatrix!";
                    }
                    mvMatrix = mvMatrixStack.pop();
                }
     
     
     
                ////////////////////////////////////
                //WebGL initialisation
                function initGL(canvas) {
                    gl = canvas.getContext("experimental-webgl");
                    gl.viewportWidth = canvas.width;
                    gl.viewportHeight = canvas.height;
     
                    if (!gl) {
                        alert("Couldn't initialise WebGL properly :(");
                    }
                }
     
                ////////////////////////////////////
                //Get shader function
                function getShader(gl, id) {
     
                    var shaderScript = document.getElementById(id);
                    if (!shaderScript) {
                        return null;
                    }
     
     
                    var str = "";
                    var child = shaderScript.firstChild;
                    while (child) {
                        if (child.nodeType == 3) {
                            str += child.textContent;
                        }
                        child = child.nextSibling;
                    }
     
     
                    var shader;
                    if (shaderScript.type == "x-shader/x-vertex") {
                        shader = gl.createShader(gl.VERTEX_SHADER);
                    }
                    else if (shaderScript.type == "x-shader/x-fragment") {
                        shader = gl.createShader(gl.FRAGMENT_SHADER);
                    }
                    else {
                        return null;
                    }
     
                    gl.shaderSource(shader, str);
                    gl.compileShader(shader);
     
                    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
                        alert(gl.getShaderInfoLog(shader));
                        return null;
                    }
     
                    return shader;
                }
     
                ////////////////////////////////////
                //Shaders initialisation
                function initShaders() {
                    var fragmentShader = getShader(gl, "shader-fs");
                    var vertexShader = getShader(gl, "shader-vs");
     
                    shaderProgram = gl.createProgram();
                    gl.attachShader(shaderProgram, vertexShader);
                    gl.attachShader(shaderProgram, fragmentShader);
                    gl.linkProgram(shaderProgram);
     
                    if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
                        alert("Could not initialise shaders");
                    }
     
                    gl.useProgram(shaderProgram);
     
                    shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
                    gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
     
                    shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
                    gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
     
                    shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
                    shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
                    shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
                }
     
                ////////////////////////////////////
                //Set matrix uniforms
                function setMatrixUniforms() {
                    gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
                    gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
                }
     
                ////////////////////////////////////
                //Draw the scene
                var cubeVertexPositionBuffer;
                var cubeVertexTexCoordbuffer;
                var cubeVertexIndexBuffer;
     
     
                function initBuffers() {
                    ////////////////////////////
                    //VERTEX
                    cubeVertexPositionBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
                    var vertices = [
                    // Face avant
                                      -1.0, -1.0, 1.0,
                                       1.0, -1.0, 1.0,
                                       1.0, 1.0, 1.0,
                                      -1.0, 1.0, 1.0,
     
                    // Face arrière
                                      -1.0, -1.0, -1.0,
                                      -1.0, 1.0, -1.0,
                                       1.0, 1.0, -1.0,
                                       1.0, -1.0, -1.0,
     
                    // Face du dessus
                                      -1.0, 1.0, -1.0,
                                      -1.0, 1.0, 1.0,
                                       1.0, 1.0, 1.0,
                                       1.0, 1.0, -1.0,
     
                    // Face du dessous
                                      -1.0, -1.0, -1.0,
                                       1.0, -1.0, -1.0,
                                       1.0, -1.0, 1.0,
                                      -1.0, -1.0, 1.0,
     
                    // Face arrière
                                       1.0, -1.0, -1.0,
                                       1.0, 1.0, -1.0,
                                       1.0, 1.0, 1.0,
                                       1.0, -1.0, 1.0,
     
                    // Face gauche
                                      -1.0, -1.0, -1.0,
                                      -1.0, -1.0, 1.0,
                                      -1.0, 1.0, 1.0,
                                      -1.0, 1.0, -1.0,
                                    ];
                    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
                    cubeVertexPositionBuffer.itemSize = 3;
                    cubeVertexPositionBuffer.numItems = 24;
     
     
                    ////////////////////////////
                    //COLOR
                    cubeVertexTexCoordbuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTexCoordbuffer);
                    var texCoord = [
                    // Front face
                                      0.0, 0.0,
                                      1.0, 0.0,
                                      1.0, 1.0,
                                      0.0, 1.0,
     
                    // Back face
                                      1.0, 0.0,
                                      1.0, 1.0,
                                      0.0, 1.0,
                                      0.0, 0.0,
     
                    // Top face
                                      0.0, 1.0,
                                      0.0, 0.0,
                                      1.0, 0.0,
                                      1.0, 1.0,
     
                    // Bottom face
                                      1.0, 1.0,
                                      0.0, 1.0,
                                      0.0, 0.0,
                                      1.0, 0.0,
     
                    // Right face
                                      1.0, 0.0,
                                      1.0, 1.0,
                                      0.0, 1.0,
                                      0.0, 0.0,
     
                    // Left face
                                      0.0, 0.0,
                                      1.0, 0.0,
                                      1.0, 1.0,
                                      0.0, 1.0,
                                   ];
                    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texCoord), gl.STATIC_DRAW);
                    cubeVertexTexCoordbuffer.itemSize = 2;
                    cubeVertexTexCoordbuffer.numImtems = 24;
     
                    ////////////////////////////
                    //INDEX
                    cubeVertexIndexBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
                    var cubeVertexIndices = [
                                              0, 1, 2, 0, 2, 3,    // Face avant
                                              4, 5, 6, 4, 6, 7,    // Face arrière
                                              8, 9, 10, 8, 10, 11,  // Face du dessus
                                              12, 13, 14, 12, 14, 15, // Face du dessous
                                              16, 17, 18, 16, 18, 19, // Face droite
                                              20, 21, 22, 20, 22, 23  // Face gauche
                                            ]
                    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
                    cubeVertexIndexBuffer.itemSize = 1;
                    cubeVertexIndexBuffer.numItems = 36;
     
                }
     
                ////////////////////////////////////
                //Draw the scene
                var rotationTriangle = 0;
     
                function drawScene() {
                    gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
                    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
     
                    mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
                    mat4.identity(mvMatrix);
                    mat4.translate(mvMatrix, [0.0, 0.0, -5.0]);
     
                    mvPushMatrix();
                    mat4.rotate(mvMatrix, degToRad(rotationTriangle), [1, 1, 0]);
     
                    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
                    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
     
                    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTexCoordbuffer);
                    gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTexCoordbuffer.itemSize, gl.FLOAT, false, 0, 0);
     
                    gl.activeTexture(gl.TEXTURE0);
                    gl.bindTexture(gl.TEXTURE_2D, brickTexture);
                    gl.uniform1i(shaderProgram.samplerUniform, 0);
     
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
                    setMatrixUniforms();
                    gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
     
                    mvPopMatrix();
                }
     
                ////////////////////////////////////
                //conversion function
                function degToRad(degrees) {
                    return degrees * Math.PI / 180;
                }
     
                ////////////////////////////////////
                //upadte Animation
                var lastTime = 0;
                function updateAnimation() {
                    var timeNow = new Date().getTime();
                    if (lastTime != 0) {
                        var elapsed = timeNow - lastTime;
                        rotationTriangle += (90 * elapsed) / 1000.0;
                    }
                    lastTime = timeNow;
                }
     
                ////////////////////////////////////
                //tickfunction
                function tick() {
                    requestAnimFrame(tick);
                    drawScene();
                    updateAnimation();
                }
     
                ////////////////////////////////////
                //Load the texture
                var brickTexture;
                function handleLoadtexture(image) {
                    brickTexture = gl.createTexture();
                    gl.bindTexture(gl.TEXTURE_2D, brickTexture);
                    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
     
                    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
                    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
     
                    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
                    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);               
     
                    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
                }
     
                ////////////////////////////////////
                //Init the texture
     
     
                function initTextures() {
                    image = new Image();
                    image.src = "images/brick.jpg";
                    image.onload = function () {
                        handleLoadtexture(image)
                    }
                }
     
                ////////////////////////////////////
                //Entry point
                function webGLStart() {
                    var canvas = document.getElementById("canvasWebGL");
     
                    initGL(canvas);
                    initShaders();
                    initBuffers();
                    initTextures();
     
                    gl.clearColor(0.14, 0.58, 0.68, 1.0);
                    gl.enable(gl.DEPTH_TEST);
     
                    tick();
                }
            </script>
     
        </head>

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


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 858
    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 858
    Points : 218 577
    Points
    218 577
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    Plusieurs raison peuvent entraîner de telles conséquences.

    Je pense que le fichier n'est pas trouvé, ou encore, que c'est son format JPG (contrairement à GIF du tutoriel de base) qui n'est pas lisible.
    Sinon, c'est avec WebGL et vous pouvez trouver de l'aide en utilisant la fonction gl.GetError() pour savoir si OpenGL ne rentre pas dans un état d'erreur.
    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.

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2012
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mars 2012
    Messages : 6
    Points : 9
    Points
    9
    Par défaut
    Merci de votre réponse, je vais voir ce que cela donne.

Discussions similaires

  1. [OpenSceneGraph] Problème affichage de texture
    Par PP(Team) dans le forum OpenSceneGraph
    Réponses: 3
    Dernier message: 23/01/2007, 16h35
  2. Réponses: 10
    Dernier message: 07/11/2006, 10h45
  3. Pas d'affichage de texture sur certains PC
    Par octopus984 dans le forum GLUT
    Réponses: 7
    Dernier message: 19/04/2005, 13h22
  4. Probleme d affichage de texture
    Par venomelektro dans le forum OpenGL
    Réponses: 1
    Dernier message: 27/09/2004, 15h07
  5. affichage de textures
    Par khayyam90 dans le forum OpenGL
    Réponses: 3
    Dernier message: 05/07/2004, 10h31

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