Skip to content

Commit a9cca48

Browse files
committed
fixing dotAdd issues. alpha version
1 parent 607d30a commit a9cca48

File tree

5 files changed

+90
-72
lines changed

5 files changed

+90
-72
lines changed

examples/VGA3DEngine/VGA3DEngine.ino

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Engine3D<VGA14Bit> engine(1337);
2626
//initial setup
2727
void setup()
2828
{
29-
//need double buffering
30-
vga.setFrameBufferCount(2);
29+
//need double buffering
30+
vga.setFrameBufferCount(2);
3131
//initializing i2s vga
3232
vga.init(vga.MODE200x150, redPins, greenPins, bluePins, hsyncPin, vsyncPin);
3333
//setting the font
@@ -36,58 +36,58 @@ void setup()
3636

3737
///a colorful triangle shader actually calculated per triangle
3838
VGA14Bit::Color myTriangleShader(int trinangleNo, short *v0, short *v1, short *v2, const signed char *normal, VGA14Bit::Color color)
39-
{
39+
{
4040
//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;
4545
//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+
4949
//render 3d model
5050
void drawModel()
5151
{
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);
6868
}
6969

7070
//just draw each frame
7171
void loop()
7272
{
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();
9393
}

examples/VGASprites/VGASprites.ino

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ VGA14Bit vga;
2323
//initial setup
2424
void setup()
2525
{
26-
//need double buffering
27-
vga.setFrameBufferCount(2);
28-
//initializing i2s vga
29-
vga.init(vga.MODE200x150, redPins, greenPins, bluePins, hsyncPin, vsyncPin);
30-
//setting the font
31-
vga.setFont(Font6x8);
26+
//need double buffering
27+
vga.setFrameBufferCount(2);
28+
//initializing i2s vga
29+
vga.init(vga.MODE200x150, redPins, greenPins, bluePins, hsyncPin, vsyncPin);
30+
//setting the font
31+
vga.setFont(Font6x8);
3232
}
3333

3434
//just draw each frame
3535
void loop()
3636
{
37-
//draw a background
38-
for(int y = 0; y < vga.yres / 10; y++)
39-
for(int x = 0; x < vga.xres / 10; x++)
40-
vga.fillRect(x * 10, y * 10, 10, 10, (x + y) & 1 ? vga.RGB(0, 127, 0) : vga.RGB(0, 0, 127));
41-
//there are 20 sprites for the explosion. The second parameter is the index of the sprite.
42-
//We used the milliseconds to calculate the current index of the animation.
43-
//the last two parameters is the position. During the conversion of the sprite the origin of each sprite is defined.
44-
//"draw" draws the sprite opaque ignoring any existing alpha channel
45-
explosion.draw(vga, (millis() / 50) % 20, vga.xres / 4, vga.yres / 2);
46-
//"drawMix" uses the alpha channel
47-
explosion.drawMix(vga, (millis() / 50) % 20, vga.xres / 2, vga.yres / 2);
48-
//"drawAdd" adds the color components of the back ground and the sprite
49-
explosion.drawAdd(vga, (millis() / 50) % 20, vga.xres * 3 / 4, vga.yres / 2);
50-
//swap the frame buffers and show the rendering
51-
vga.show();
37+
//draw a background
38+
for (int y = 0; y < vga.yres / 10; y++)
39+
for (int x = 0; x < vga.xres / 10; x++)
40+
vga.fillRect(x * 10, y * 10, 10, 10, (x + y) & 1 ? vga.RGB(0, 128, 0) : vga.RGB(0, 0, 128));
41+
//there are 20 sprites for the explosion. The second parameter is the index of the sprite.
42+
//We used the milliseconds to calculate the current index of the animation.
43+
//the last two parameters is the position. During the conversion of the sprite the origin of each sprite is defined.
44+
//"draw" draws the sprite opaque ignoring any existing alpha channel
45+
explosion.draw(vga, (millis() / 50) % 20, vga.xres / 4, vga.yres / 2);
46+
//"drawMix" uses the alpha channel
47+
explosion.drawMix(vga, (millis() / 50) % 20, vga.xres / 2, vga.yres / 2);
48+
//"drawAdd" adds the color components of the back ground and the sprite
49+
explosion.drawAdd(vga, (millis() / 50) % 20, vga.xres * 3 / 4, vga.yres / 2);
50+
//swap the frame buffers and show the rendering
51+
vga.show();
5252
}

src/Graphics/GraphicsR5G5B4A2.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ class GraphicsR5G5B4A2: public Graphics<unsigned short>
5656

5757
virtual void dotAdd(int x, int y, Color color)
5858
{
59-
//todo repair this
6059
if ((unsigned int)x < xres && (unsigned int)y < yres)
61-
backBuffer[y][x] = color + backBuffer[y][x];
60+
{
61+
int c0 = backBuffer[y][x];
62+
int c1 = color;
63+
int r = (c0 & 0b11111) + (c1 & 0b11111);
64+
if(r > 0b11111) r = 0b11111;
65+
int g = (c0 & 0b1111100000) + (c1 & 0b1111100000);
66+
if(g > 0b1111100000) g = 0b1111100000;
67+
int b = (c0 & 0b11110000000000) + (c1 & 0b11110000000000);
68+
if(b > 0b11110000000000) b = 0b11110000000000;
69+
backBuffer[y][x] = r | (g & 0b1111100000) | (b & 0b11110000000000);
70+
}
6271
}
6372

6473
virtual void dotMix(int x, int y, Color color)

src/Graphics/GraphicsR5G5B4S2Swapped.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ class GraphicsR5G5B4S2Swapped: public Graphics<unsigned short>
6060

6161
virtual void dotAdd(int x, int y, Color color)
6262
{
63-
//todo repair this
6463
if ((unsigned int)x < xres && (unsigned int)y < yres)
65-
backBuffer[y][x^1] = (color + backBuffer[y][x^1]) | SBits;
64+
{
65+
int c0 = backBuffer[y][x^1];
66+
int c1 = color;
67+
int r = (c0 & 0b11111) + (c1 & 0b11111);
68+
if(r > 0b11111) r = 0b11111;
69+
int g = (c0 & 0b1111100000) + (c1 & 0b1111100000);
70+
if(g > 0b1111100000) g = 0b1111100000;
71+
int b = (c0 & 0b11110000000000) + (c1 & 0b11110000000000);
72+
if(b > 0b11110000000000) b = 0b11110000000000;
73+
backBuffer[y][x^1] = r | (g & 0b1111100000) | (b & 0b11110000000000) | SBits;
74+
}
6675
}
6776

6877
virtual void dotMix(int x, int y, Color color)

src/Graphics/Sprites.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Sprites
8080

8181
void drawAdd(Graphics &g, int sprite, int x, int y)
8282
{
83-
sprites[sprite].drawMix(g, x, y);
83+
sprites[sprite].drawAdd(g, x, y);
8484
}
8585

8686
int xres(int sprite) const

0 commit comments

Comments
 (0)