@@ -26,8 +26,8 @@ Engine3D<VGA14Bit> engine(1337);
26
26
// initial setup
27
27
void setup ()
28
28
{
29
- // need double buffering
30
- vga.setFrameBufferCount (2 );
29
+ // need double buffering
30
+ vga.setFrameBufferCount (2 );
31
31
// initializing i2s vga
32
32
vga.init (vga.MODE200x150 , redPins, greenPins, bluePins, hsyncPin, vsyncPin);
33
33
// setting the font
@@ -36,58 +36,58 @@ void setup()
36
36
37
37
// /a colorful triangle shader actually calculated per triangle
38
38
VGA14Bit::Color myTriangleShader (int trinangleNo, short *v0, short *v1, short *v2, const signed char *normal , VGA14Bit::Color color)
39
- {
39
+ {
40
40
// normals packed in 1 signed byte per axis
41
- const float scaleN = 1 .0f / 127 .0f ;
42
- const float nx = normal [0 ] * scaleN;
43
- const float ny = normal [1 ] * scaleN;
44
- const float nz = normal [2 ] * scaleN;
41
+ const float scaleN = 1 .0f / 127 .0f ;
42
+ const float nx = normal [0 ] * scaleN;
43
+ const float ny = normal [1 ] * scaleN;
44
+ const float nz = normal [2 ] * scaleN;
45
45
// return R5G5B4 color each normal axis controls each color component
46
- return (int (15 * nx + 16 )) | (int (15 * nz + 16 ) << 5 ) | (int (7 * ny + 8 ) << 10 );
47
- }
48
-
46
+ return (int (15 * nx + 16 )) | (int (15 * nz + 16 ) << 5 ) | (int (7 * ny + 8 ) << 10 );
47
+ }
48
+
49
49
// render 3d model
50
50
void drawModel ()
51
51
{
52
- // perspective transformation
53
- static Matrix perspective = Matrix::translation (vga.xres / 2 , vga.yres / 2 , 0 ) * Matrix::scaling (100 * vga.pixelAspect (), 100 , 100 ) * Matrix::perspective (90 , 1 , 10 );
54
- static float u = 0 ;
55
- u += 0.02 ;
56
- // rotate model
57
- Matrix rotation = Matrix::rotation (-1.7 , 1 , 0 , 0 ) * Matrix::rotation (u, 0 , 0 , 1 );
58
- Matrix m0 = perspective * Matrix::translation (0 , 1.7 * 0 , 7 ) * rotation * Matrix::scaling (7 );
59
- // transform the vertices and normals
60
- model.transform (m0, rotation);
61
- // begin adding triangles to render pipeline
62
- engine.begin ();
63
- // add this model to the render pipeline. it will sort the triangles from back to front and remove backfaced. The tiangle shader will determine the color of the tirangle.
64
- // the RGB color gien in the second parameter is not used in this case but could be used for calculations in the triangle shader
65
- model.drawTriangles (engine, vga.RGB (128 , 70 , 20 ), myTriangleShader);
66
- // render all triangles in the pipeline. if you render multiple models you want to do this once at the end
67
- engine.end (vga);
52
+ // perspective transformation
53
+ static Matrix perspective = Matrix::translation (vga.xres / 2 , vga.yres / 2 , 0 ) * Matrix::scaling (100 * vga.pixelAspect (), 100 , 100 ) * Matrix::perspective (90 , 1 , 10 );
54
+ static float u = 0 ;
55
+ u += 0.02 ;
56
+ // rotate model
57
+ Matrix rotation = Matrix::rotation (-1.7 , 1 , 0 , 0 ) * Matrix::rotation (u, 0 , 0 , 1 );
58
+ Matrix m0 = perspective * Matrix::translation (0 , 1.7 * 0 , 7 ) * rotation * Matrix::scaling (7 );
59
+ // transform the vertices and normals
60
+ model.transform (m0, rotation);
61
+ // begin adding triangles to render pipeline
62
+ engine.begin ();
63
+ // add this model to the render pipeline. it will sort the triangles from back to front and remove backfaced. The tiangle shader will determine the color of the tirangle.
64
+ // the RGB color gien in the second parameter is not used in this case but could be used for calculations in the triangle shader
65
+ model.drawTriangles (engine, vga.RGB (128 , 70 , 20 ), myTriangleShader);
66
+ // render all triangles in the pipeline. if you render multiple models you want to do this once at the end
67
+ engine.end (vga);
68
68
}
69
69
70
70
// just draw each frame
71
71
void loop ()
72
72
{
73
- // calculate the milliseconds passed from last pass
74
- static int lastMillis = 0 ;
75
- int t = millis ();
76
- // calculate fps (smooth)
77
- static float oldFps = 0 ;
78
- float fps = oldFps * 0 .9f + 100 .f / (t - lastMillis);
79
- oldFps = fps;
80
- lastMillis = t;
81
- // clear the back buffer
82
- vga.clear (0 );
83
- // draw the model
84
- drawModel ();
85
- // reset the text cursor
86
- vga.setCursor (0 , 0 );
87
- // print the stats
88
- vga.print (" fps: " );
89
- vga.print (fps, 1 , 4 );
90
- vga.print (" tris/s: " );
91
- vga.print (int (fps * model.triangleCount ));
92
- vga.show ();
73
+ // calculate the milliseconds passed from last pass
74
+ static int lastMillis = 0 ;
75
+ int t = millis ();
76
+ // calculate fps (smooth)
77
+ static float oldFps = 0 ;
78
+ float fps = oldFps * 0 .9f + 100 .f / (t - lastMillis);
79
+ oldFps = fps;
80
+ lastMillis = t;
81
+ // clear the back buffer
82
+ vga.clear (0 );
83
+ // draw the model
84
+ drawModel ();
85
+ // reset the text cursor
86
+ vga.setCursor (0 , 0 );
87
+ // print the stats
88
+ vga.print (" fps: " );
89
+ vga.print (fps, 1 , 4 );
90
+ vga.print (" tris/s: " );
91
+ vga.print (int (fps * model.triangleCount ));
92
+ vga.show ();
93
93
}
0 commit comments