@@ -5,122 +5,110 @@ import nme.gl.GL;
55import nme .Assets ;
66import nme .Lib ;
77import nme .utils .Float32Array ;
8- import nme . geom . Matrix3D ;
8+
99import nme .gl .GLProgram ;
10+ import nme .gl .Utils ;
11+
12+ import nme .geom .Matrix3D ;
1013
1114class Main extends Sprite {
12-
13- public function createShader (source : String , type : Int )
14- {
15- var shader = GL .createShader (type );
16- GL .shaderSource (shader , source );
17- GL .compileShader (shader );
18- if (GL .getShaderParameter (shader , GL .COMPILE_STATUS )== 0 )
19- {
20- trace (" --- ERR ---\n " + source );
21- var err = GL .getShaderInfoLog (shader );
22- if (err != " " )
23- throw err ;
24- }
25- return shader ;
26- }
27-
28- public function createProgram (inVertexSource : String , inFragmentSource : String )
29- {
30- var program = GL .createProgram ();
31- var vshader = createShader (inVertexSource , GL .VERTEX_SHADER );
32- var fshader = createShader (inFragmentSource , GL .FRAGMENT_SHADER );
33- GL .attachShader (program , vshader );
34- GL .attachShader (program , fshader );
35- GL .linkProgram (program );
36- if (GL .getProgramParameter (program , GL .LINK_STATUS )== 0 )
37- {
38- var result = GL .getProgramInfoLog (program );
39- if (result != " " )
40- throw result ;
41- }
42-
43- return program ;
44- }
4515
4616
47-
4817 public function new ()
4918 {
5019 super ();
5120
5221 var ogl = new OpenGLView ();
5322 addChild (ogl );
5423
55- var fragShader =
24+ var fragShader : String =
25+ " // Ouput data
26+ " +
27+ Utils .OUT_COLOR (" color" ) +
5628"
57- #version 330 core
58-
59- // Ouput data
60- out vec3 color;
61-
62- void main()
63- {
64- // Output color = red
65- color = vec3(1,0,0);
29+ void main(){
30+ // Output color = red
31+ color = vec4(1.0,0.0,0.0,1.0);
6632}
6733" ;
6834
69- var vertShader =
70- "
71- #version 330 core
7235
73- // Input vertex data, different for all executions of this shader.
74- layout(location = 0) in vec3 vertexPosition_modelspace;
36+ var vertShader : String =
37+ " // Input vertex data, different for all executions of this shader.
38+ " +
39+ Utils .IN (0 ) + " vec3 vertexPosition_modelspace;
7540
7641// Values that stay constant for the whole mesh.
7742uniform mat4 MVP;
7843
7944void main(){
8045 // Output position of the vertex, in clip space : MVP * position
81- gl_Position = MVP * vec4(vertexPosition_modelspace,1);
46+ gl_Position = MVP * vec4(vertexPosition_modelspace,1.0 );
8247}
8348" ;
8449
50+ // Dark blue background: For NME, use "opaqueBackground" instead of "clearColor"
51+ // GL.clearColor(0.0, 0.0, 0.4, 0.0);
52+ nme. Lib .stage .opaqueBackground = 0x000066 ;
8553
86- // Create and compile our GLSL program from the shaders
87- var prog = createProgram ( vertShader , fragShader );
88-
89-
90- // var frameBuffer = GL.createFramebuffer( );
91- // GL.bindFramebuffer(GL.FRAMEBUFFER,frameBuffer);
54+ // GLES3
55+ if ( Utils . isGLES3compat ())
56+ {
57+ var vertexarray = GL . createVertexArray ();
58+ GL .bindVertexArray ( vertexarray );
59+ }
9260
61+ // Create and compile our GLSL program from the shaders
62+ var prog = Utils .createProgram (vertShader ,fragShader );
9363
9464 // Get a handle for our "MVP" uniform
9565 var matrixID = GL .getUniformLocation (prog , " MVP" );
96-
97- var projection = Matrix3D .createOrtho (- 10.0 ,10.0 ,- 10.0 ,10.0 ,0.0 ,100.0 );
9866 var model = new Matrix3D ();
99- var view = new Matrix3D ();
100-
101- var mvp = projection ;
102- mvp .append (model );
67+ #if true
68+ // Ortho camera
69+ var view : Matrix3D = new Matrix3D ();
70+ var projection = Matrix3D .createOrtho (- 10.0 ,10.0 ,- 10.0 ,10.0 ,0.0 ,100.0 );
71+ #else
72+ // Perp camera
73+ var view : Matrix3D = GLM .lookAt (
74+ new Vector3D (4 ,3 ,- 3 ), // Camera is at (4,3,-3), in World Space
75+ new Vector3D (0 ,0 ,0 ), // and looks at the origin
76+ new Vector3D (0 ,1 ,0 ) // Head is up (set to 0,-1,0 to look upside-down)
77+ );
78+
79+ var fov = 45 * Math .PI / 180 ;
80+ var aspect = 4 / 3 ;
81+ var zNear = 0.1 ;
82+ var zFar = 1000 ;
83+ var projection = GLM .perspective (fov , aspect , zNear , zFar );
84+ #end
85+
86+ var mvp = model ;
10387 mvp .append (view );
88+ mvp .append (projection );
10489
10590 var g_vertex_buffer_data = [
106- - 1.0 , - 1.0 , 0.0 , 1.0 ,
107- 1.0 , - 1.0 , 0.0 , 1.0 ,
108- 0.0 , 1.0 , 0.0 , 1.0
109- ];
91+ - 1.0 , - 1.0 , 0.0 , 1.0 , // vertex 0 x,y,z,w
92+ 1.0 , - 1.0 , 0.0 , 1.0 , // vertex 1
93+ 0.0 , 1.0 , 0.0 , 1.0 // vertex2
94+ ]; // 3 vertex of size 4
11095
11196
11297 var vertexbuffer = GL .createBuffer ();
11398 GL .bindBuffer (GL .ARRAY_BUFFER , vertexbuffer );
11499 GL .bufferData (GL .ARRAY_BUFFER , new Float32Array (g_vertex_buffer_data ), GL .STATIC_DRAW );
115100
101+ var posAttrib = 0 ;
102+ if (! Utils .isGLES3compat ())
103+ {
104+ posAttrib = GL .getAttribLocation (prog , " vertexPosition_modelspace" );
105+ }
116106
117107 ogl .render = function (rect : Rectangle )
118108 {
119- // Dark blue background
120- GL .clearColor (0.0 , 0.0 , 0.4 , 0.0 );
121-
109+ // NME already calls GL.clear with "opaqueBackground" color
122110 // Clear the screen.
123- GL .clear (GL .COLOR_BUFFER_BIT );
111+ // GL.clear(GL.COLOR_BUFFER_BIT);
124112
125113 // Use our shader
126114 GL .useProgram (prog );
@@ -130,11 +118,11 @@ void main(){
130118 GL .uniformMatrix4fv (matrixID , false , Float32Array .fromMatrix (mvp ));
131119
132120 // 1rst attribute buffer : vertices
133- GL .enableVertexAttribArray (0 );
121+ GL .enableVertexAttribArray (posAttrib );
134122 GL .bindBuffer (GL .ARRAY_BUFFER , vertexbuffer );
135123 GL .vertexAttribPointer (
136- 0 , // attribute 0. No particular reason for 0, but must match the layout in the shader
137- 4 , // size
124+ posAttrib , // attribute 0. No particular reason for 0, but must match the layout in the shader
125+ 4 , // size
138126 GL .FLOAT , // type
139127 false , // normalized?
140128 0 , // stride
@@ -144,6 +132,8 @@ void main(){
144132 // Draw the triangle !
145133 GL .drawArrays (GL .TRIANGLES , 0 , 3 );
146134 GL .disableVertexAttribArray (0 );
135+
136+ // Swap buffers: is done automatically
147137 }
148138 }
149139
0 commit comments