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 :

OpenTK : portage de javascript vers C#, problème de rendu


Sujet :

OpenGL

  1. #1
    Membre régulier Avatar de maitredede
    Homme Profil pro
    Pisseur de code
    Inscrit en
    Mai 2006
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Pisseur de code

    Informations forums :
    Inscription : Mai 2006
    Messages : 59
    Points : 106
    Points
    106
    Par défaut OpenTK : portage de javascript vers C#, problème de rendu
    Hello World !

    Je suis en train de bricoler une appli qui repose sur OpenTK.

    J'ai tenté de porter un exemple WebGL en C#, mais mon rendu n'est pas très top bien pareil. Genre :

    Nom : cube-moche.png
Affichages : 202
Taille : 10,1 Ko

    La tartine :

    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
     
    private void Init(FrmScreenNative screen)
            {
                //screen.GlControl.MakeCurrent();
                screen.MakeCurrent();
     
                float[] vertices = {
           -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1,
           -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
           -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1,
           1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1,
           -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
           -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
                };
     
                float[] colors = {
                   5, 3, 7, 5, 3, 7, 5, 3, 7, 5, 3, 7,
                   1, 1, 3, 1, 1, 3, 1, 1, 3, 1, 1, 3,
                   0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
                   1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
                   1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
                   0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0
                };
     
                indices = new ushort[]{
                   0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7,
                   8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15,
                   16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23
                };
     
     
                // Create and store data into vertex buffer
                int vertex_buffer = GL.GenBuffer();
                GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer);
                GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length, vertices, BufferUsageHint.StaticDraw);
     
                // Create and store data into color buffer
                int color_buffer = GL.GenBuffer();
                GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer);
                GL.BufferData(BufferTarget.ArrayBuffer, colors.Length, colors, BufferUsageHint.StaticDraw);
     
                // Create and store data into index buffer
                index_buffer = GL.GenBuffer();
                GL.BindBuffer(BufferTarget.ArrayBuffer, index_buffer);
                GL.BufferData(BufferTarget.ArrayBuffer, indices.Length, indices, BufferUsageHint.StaticDraw);
     
                string vertCode = @"
    attribute vec3 position;
    uniform mat4 Pmatrix;
    uniform mat4 Vmatrix;
    uniform mat4 Mmatrix;
    attribute vec3 color;
    varying vec3 vColor;
    void main(void) { 
        gl_Position = Pmatrix*Vmatrix*Mmatrix*vec4(position, 1.);
        vColor = color;
    }";
     
                string fragCode = @"
    precision mediump float;
    varying vec3 vColor;
    void main(void) {
        gl_FragColor = vec4(vColor, 1.);
    }";
     
                int vertShader = LoadShader(ShaderType.VertexShader, vertCode);
                int fragShader = LoadShader(ShaderType.FragmentShader, fragCode);
     
                int shaderProgram = GL.CreateProgram();
                GL.AttachShader(shaderProgram, vertShader);
                GL.AttachShader(shaderProgram, fragShader);
                GL.LinkProgram(shaderProgram);
     
                /* ====== Associating attributes to vertex shader =====*/
                Pmatrix = GL.GetUniformLocation(shaderProgram, "Pmatrix");
                Vmatrix = GL.GetUniformLocation(shaderProgram, "Vmatrix");
                Mmatrix = GL.GetUniformLocation(shaderProgram, "Mmatrix");
     
                GL.BindBuffer(BufferTarget.ArrayBuffer, vertex_buffer);
                int position = GL.GetAttribLocation(shaderProgram, "position");
                GL.VertexAttribPointer(position, 3, VertexAttribPointerType.Float, false, 0, 0);
     
                // Position
                GL.EnableVertexAttribArray(position);
                GL.BindBuffer(BufferTarget.ArrayBuffer, color_buffer);
                int color = GL.GetAttribLocation(shaderProgram, "color");
                GL.VertexAttribPointer(color, 3, VertexAttribPointerType.Float, false, 0, 0);
     
                // Color
                GL.EnableVertexAttribArray(color);
                GL.UseProgram(shaderProgram);
     
                /*==================== MATRIX =====================*/
     
                float fovyDeg = 40;
                float fovyRad = (float)(fovyDeg * Math.PI / 180);
     
                proj_matrix = Matrix4.CreatePerspectiveFieldOfView(fovyRad, (float)screen.Width / (float)screen.Height, 1, 100);
     
                mov_matrix = Matrix4.Identity;
                view_matrix = Matrix4.Identity;
     
                // translating z
                Matrix4 translateZ = Matrix4.CreateTranslation(0, 0, -6);
                view_matrix = view_matrix + translateZ;
     
                //screen.GlControl.Context.MakeCurrent(null);
                screen.Context.MakeCurrent(null);
            }
     
            Matrix4 proj_matrix;
            Matrix4 mov_matrix;
            Matrix4 view_matrix;
     
            int Pmatrix;
            int Vmatrix;
            int Mmatrix;
     
            int index_buffer;
            ushort[] indices;
     
            private System.Diagnostics.Stopwatch m_swatch = new System.Diagnostics.Stopwatch();
     
            private void Draw(FrmScreenNative screen)
            {
                screen.MakeCurrent();
     
                var dt = this.m_swatch.ElapsedMilliseconds;
                Matrix4 rotationX = Matrix4.CreateRotationX(dt * 0.005f);
                Matrix4 rotationY = Matrix4.CreateRotationY(dt * 0.002f);
                Matrix4 rotationZ = Matrix4.CreateRotationZ(dt * 0.003f);
                mov_matrix += rotationX + rotationY + rotationZ;
     
                GL.Enable(EnableCap.DepthTest);
                GL.DepthFunc(DepthFunction.Lequal);
                GL.ClearColor(0.5f, 0.5f, 0.5f, 0.9f);
                GL.ClearDepth(1);
     
                GL.Viewport(0, 0, screen.Width, screen.Height);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                GL.UniformMatrix4(Pmatrix, false, ref proj_matrix);
                GL.UniformMatrix4(Vmatrix, false, ref view_matrix);
                GL.UniformMatrix4(Mmatrix, false, ref mov_matrix);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, index_buffer);
                GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedShort, 0);
     
                screen.SwapBuffers();
                screen.Context.MakeCurrent(null);
            }

  2. #2
    Membre actif Avatar de monwarez
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    144
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 144
    Points : 293
    Points
    293
    Par défaut
    Je pense que ça vient de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    // Create and store data into index buffer
    index_buffer = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, index_buffer); // Attention il faut créer un élément array buffer
    GL.BufferData(BufferTarget.ArrayBuffer, indices.Length, indices, BufferUsageHint.StaticDraw);
    En regardant l'original:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    // Create and store data into index buffer
    var index_buffer = gl.createBuffer ();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);

  3. #3
    Membre régulier Avatar de maitredede
    Homme Profil pro
    Pisseur de code
    Inscrit en
    Mai 2006
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Pisseur de code

    Informations forums :
    Inscription : Mai 2006
    Messages : 59
    Points : 106
    Points
    106
    Par défaut
    Hello,

    Merci d'avoir mis le doigts dessus, mais pas mieux... J'ai ajouté des vérifications avec GL.GetError() après chaque appel, aucune erreur remontée.


  4. #4
    Membre actif Avatar de monwarez
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    144
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 144
    Points : 293
    Points
    293
    Par défaut
    Lors de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length, indices, BufferUsageHint.StaticDraw);
    Il faut remplacer indices.Length par (IntPtr)(indices.Length * sizeof( ushort ) )
    De même pour les autres, voir http://www.opentk.com/doc/graphics/g...buffer-objects (pour plus de détail pour le remplacement, vu que pour les autres c'est des float)

  5. #5
    Membre régulier Avatar de maitredede
    Homme Profil pro
    Pisseur de code
    Inscrit en
    Mai 2006
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Pisseur de code

    Informations forums :
    Inscription : Mai 2006
    Messages : 59
    Points : 106
    Points
    106
    Par défaut
    La vache ! Ca marche vachement mieux !!

    Bon, c'est un peu déformé (rapport aux dimensions que je donne au viewport), mais ça déchire presque...
    J'adore quand un "Hello world !" fonctionne

    Merciiiiiiiiiiiiiiiiiiiiiiiiiiiiii


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

Discussions similaires

  1. Problème portage code Linux vers Windows
    Par scary dans le forum Windows
    Réponses: 14
    Dernier message: 11/01/2010, 17h56
  2. Portage C++ vers java, problème de pointeur
    Par rimas2009 dans le forum C++
    Réponses: 1
    Dernier message: 28/04/2009, 17h00
  3. Portage requete Access vers SQL Server (Iif)...
    Par cmousset dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 14/06/2005, 16h38
  4. [Kylix] Portage d'application Delphi vers Kylix
    Par BONNEFOI Patrick dans le forum EDI
    Réponses: 4
    Dernier message: 03/05/2005, 22h35
  5. Adaptation d'un code TMemo vers TRichEdit : problème de ScrollBars
    Par Droïde Système7 dans le forum Composants VCL
    Réponses: 2
    Dernier message: 21/01/2005, 15h06

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