Skip to content

Commit 02e12ab

Browse files
committed
Sync tutorials 01 to 04
Uses haxenme/nme#496
1 parent bd06dbb commit 02e12ab

File tree

4 files changed

+140
-172
lines changed
  • tutorial01_first_window/Source
  • tutorial02_red_triangle/Source
  • tutorial03_matrices/Source
  • tutorial04_a_colored_cube/Source

4 files changed

+140
-172
lines changed

tutorial01_first_window/Source/Main.hx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import nme.Lib;
77

88

99
class Main extends Sprite {
10-
11-
10+
11+
1212
public function new ()
1313
{
1414
super ();
1515

1616
var ogl = new OpenGLView();
17+
addChild(ogl);
1718

19+
// Dark blue background: For NME, use "opaqueBackground" instead of "clearColor"
20+
//GL.clearColor(0.0, 0.0, 0.4, 0.0);
21+
nme.Lib.stage.opaqueBackground = 0x000066;
22+
1823
ogl.render = function(rect:Rectangle)
1924
{
20-
// Dark blue background
21-
GL.clearColor(0.0, 0.0, 0.4, 0.0);
22-
2325
// Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
24-
GL.clear(GL.COLOR_BUFFER_BIT);
26+
//NME already calls GL.clear with "opaqueBackground" color
27+
//GL.clear(GL.COLOR_BUFFER_BIT);
28+
2529
}
2630
}
2731

tutorial02_red_triangle/Source/Main.hx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class Main extends Sprite {
2121

2222
var fragShader:String =
2323
"// Ouput data
24-
"+Utils.OUT_COLOR("color")+"
25-
26-
void main()
27-
{
24+
" +
25+
Utils.OUT_COLOR("color") +
26+
"
27+
void main(){
2828
// Output color = red
2929
color = vec4(1.0,0.0,0.0,1.0);
3030
}
@@ -33,7 +33,8 @@ void main()
3333

3434
var vertShader:String =
3535
"// Input vertex data, different for all executions of this shader.
36-
"+Utils.IN(0)+" vec3 vertexPosition_modelspace;
36+
" +
37+
Utils.IN(0) + " vec3 vertexPosition_modelspace;
3738
3839
void main(){
3940
gl_Position.xyz = vertexPosition_modelspace;

tutorial03_matrices/Source/Main.hx

Lines changed: 64 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,110 @@ import nme.gl.GL;
55
import nme.Assets;
66
import nme.Lib;
77
import nme.utils.Float32Array;
8-
import nme.geom.Matrix3D;
8+
99
import nme.gl.GLProgram;
10+
import nme.gl.Utils;
11+
12+
import nme.geom.Matrix3D;
1013

1114
class 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.
7742
uniform mat4 MVP;
7843
7944
void 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

Comments
 (0)