Skip to content

Commit de3d73f

Browse files
author
bitluni
committed
starfield example added
1 parent 5df7e23 commit de3d73f

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <ESP32Lib.h>
2+
#include <Ressources/Font6x8.h>
3+
#include "VGA/VGA6MonochromeVGAMadnessMultimonitor.h"
4+
5+
//pin configuration
6+
//red pins
7+
const int redPin0 = 17;
8+
const int redPin1 = 2;
9+
const int redPin2 = 19;
10+
const int redPin3 = 12;
11+
const int redPin4 = 23;
12+
const int redPin5 = 26;
13+
//green pins
14+
const int greenPin0 = 16;
15+
const int greenPin1 = 15;
16+
const int greenPin2 = 18;
17+
const int greenPin3 = 14;
18+
const int greenPin4 = 22;
19+
const int greenPin5 = 25;
20+
//blue pins
21+
const int bluePin0 = 4;
22+
const int bluePin1 = 13;
23+
const int bluePin2 = 5;
24+
const int bluePin3 = 27;
25+
const int bluePin4 = 21;
26+
const int bluePin5 = 0;
27+
const int hsyncPin = 32;
28+
const int vsyncPin = 33;
29+
30+
//VGA Device
31+
VGA6MonochromeVGAMadnessMultimonitor gfx;
32+
33+
const int xres = 320 * 3;
34+
const int yres = 200 * 2;
35+
36+
const int starCount = 4000;
37+
float stars[starCount][3];
38+
Mesh<VGA6MonochromeVGAMadnessMultimonitor> model(starCount, stars, 0, 0, 0, 0, 0);
39+
40+
void initStars()
41+
{
42+
for(int i = 0; i < starCount; i++)
43+
{
44+
stars[i][0] = random(201) - 100;
45+
stars[i][1] = random(201) - 100;
46+
stars[i][2] = random(201) - 100;
47+
}
48+
}
49+
50+
//3D engine
51+
Engine3D<VGA6MonochromeVGAMadnessMultimonitor> engine(1);
52+
53+
void setup()
54+
{
55+
const int pinCount = 3 * 6 + 2;
56+
Serial.begin(115200);
57+
//initializing vga at the specified pins
58+
gfx.setFrameBufferCount(2);
59+
gfx.init(VGAMode::MODE320x240,
60+
redPin0,greenPin0,bluePin0,
61+
redPin1,greenPin1,bluePin1,
62+
redPin2,greenPin2,bluePin2,
63+
redPin3,greenPin3,bluePin3,
64+
redPin4,greenPin4,bluePin4,
65+
redPin5,greenPin5,bluePin5,
66+
hsyncPin, vsyncPin, -1, 3, 2);
67+
//selecting the font
68+
gfx.setFont(Font6x8);
69+
initStars();
70+
}
71+
72+
73+
void loop()
74+
{
75+
gfx.clear();
76+
//perspective transformation
77+
static Matrix perspective = Matrix::translation(gfx.xres / 2, gfx.yres / 2, 0) * Matrix::scaling(1000 * gfx.pixelAspect(), 1000, 1000) * Matrix::perspective(90, 1, 10);
78+
static float u = 0;
79+
static float v = 0;
80+
static float w = 0;
81+
u += 0.02;
82+
v += 0.01;
83+
w += 0.005;
84+
//rotate model
85+
Matrix rotation = Matrix::rotation(u, 1, 0, 0) * Matrix::rotation(v, 0, 1, 0) * Matrix::rotation(w, 0, 0, 1);
86+
Matrix m0 = perspective * Matrix::translation(0, 1.7 * 0, 200) * rotation;
87+
//transform the vertices and normals
88+
model.transform(m0, rotation);
89+
//begin adding triangles to render pipeline
90+
//engine.begin();
91+
//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.
92+
//the RGB color gien in the second parameter is not used in this case but could be used for calculations in the triangle shader
93+
model.drawVertices(gfx, gfx.RGB(255, 255, 255));
94+
//render all triangles in the pipeline. if you render multiple models you want to do this once at the end
95+
//engine.end(gfx);
96+
gfx.show();
97+
}

0 commit comments

Comments
 (0)