- #include "VGA.h"
- #include <GfxWrapper.h>
-
- VGA vga;
- //Mode mode = Mode::MODE_320x240x60;
- Mode mode = Mode::MODE_640x480x60;//MODE_640x480x60;
- GfxWrapper<VGA> gfx(vga, mode.hRes, mode.vRes);
-
- int CurrentX, CurrentY;
- void moveto(int X, int Y) {
- CurrentX = X;
- CurrentY = Y;
- }
- void lineto(int X, int Y) {
- gfx.drawLine(CurrentX, CurrentY, X, Y, 0x700);
- moveto(X, Y);
- }
-
- void HideSphere(float R, int alfa, int beta, int HideFlag)
- {
- int i, j, k;
- float x[4], y[4], z[4], x1[4], y1[4], z1[4], sx[4], sy[4];
- double a1, a2, b1, b2, c, d, xn, yn, zn, vn;
- c = alfa * PI / 180.0;
- d = beta * PI / 180.0;
- for (j = 0; j < 180; j = j + 5)
- {
- a1 = j * PI / 180.0;
- a2 = (j + 5) * PI / 180.0;
- for (i = 0; i < 360; i = i + 5)
- {
- b1 = i * PI / 180.0;
- b2 = (i + 5) * PI / 180.0;
- x[0] = R * sin(a1) * cos(b1); y[0] = R * sin(a1) * sin(b1); z[0] = R * cos(a1);
- x[1] = R * sin(a2) * cos(b1); y[1] = R * sin(a2) * sin(b1); z[1] = R * cos(a2);
- x[2] = R * sin(a2) * cos(b2); y[2] = R * sin(a2) * sin(b2); z[2] = R * cos(a2);
- x[3] = R * sin(a1) * cos(b2); y[3] = R * sin(a1) * sin(b2); z[3] = R * cos(a1);
- for (k = 0; k < 4; k++)
- {
- x1[k] = x[k] * cos(c) - y[k] * sin(c);
- y1[k] = x[k] * sin(c) * cos(d) + y[k] * cos(c) * sin(d) + z[k] * sin(d);
- z1[k] = -x[k] * sin(c) * sin(d) - y[k] * cos(c) * sin(d) + z[k] * cos(d);
- sx[k] = 320 - x1[k];
- sy[k] = 240 - z1[k];
- }
- xn = (y1[2] - y1[0]) * (z1[3] - z1[1]) - (y1[3] - y1[1]) * (z1[2] - z1[0]);
- yn = -(x1[2] - x1[0]) * (z1[3] - z1[1]) + (x1[3] - x1[1]) * (z1[2] - z1[0]);
- zn = (x1[2] - x1[0]) * (y1[3] - y1[1]) - (x1[3] - x1[1]) * (y1[2] - y1[0]);
- vn = sqrt(xn * xn + yn * yn + zn * zn);
- xn = xn / vn;
- yn = yn / vn;
- zn = zn / vn;
- if (!HideFlag || yn >= 0.0)
- {
- moveto(sx[0], sy[0]);
- lineto(sx[1], sy[1]);
- lineto(sx[2], sy[2]);
- lineto(sx[3], sy[3]);
- lineto(sx[0], sy[0]);
- }
- }
- }
- }
-
-
- void setup()
- {
- Serial.begin(115200);
- delay(5000);
- if (psramInit()) {
- log_d("Total heap: %d", ESP.getHeapSize());
- log_d("Free heap: %d", ESP.getFreeHeap());
- log_d("Total PSRAM: %d", ESP.getPsramSize());
- log_d("Free PSRAM: %d", ESP.getFreePsram());
- }
- //const PinConfig pins(4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 21, 1, 2);
- const PinConfig pins(-1,-1,6,7,8, -1,-1,-1,12,13,14, -1,-1,-1,18,21, 1,2); // R G B h v
-
-
- vga.bufferCount = 2;
- if(!vga.init(pins, mode, 8)) while(1) delay(1);
-
- vga.start();
- }
-
- void loop()
- {
-
- while (true)
- {
- for (int i = 0; i < 180; i++)
- {
- HideSphere(200, 45 + i, 30 + i, 1);
- vga.show();
- delay(20);
- vga.clear(0);
- }
- }
- }
复制代码
最终的效果还不错,有兴趣的朋友可以在下面看到:
这次使用到的库
|