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 : 239
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);
        }