Professional Documents
Culture Documents
Demo 3 Dcube
Demo 3 Dcube
Demo 3 Dcube
*/
#include <SPI.h>
int16_t h;
int16_t w;
float fact;
int Xan, Yan;
int Xoff;
int Yoff;
int Zoff;
struct Point3d
{
int x;
int y;
int z;
};
struct Point2d
{
int x;
int y;
};
struct Line3d
{
Point3d p0;
Point3d p1;
};
struct Line2d
{
Point2d p0;
Point2d p1;
};
Line3d Lines[20];
Line2d Render[20];
Line2d ORender[20];
/***********************************************************************************************************************************/
void setup() {
tft.init();
h = tft.height();
w = tft.width();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
cube();
Xoff = 240; // Position the center of the 3d conversion space into the center of the TFT screen.
Yoff = 160;
Zoff = 550; // Z offset in 3D space (smaller = closer and bigger rendering)
}
/***********************************************************************************************************************************/
void loop() {
delay(14); // Delay to reduce loop rate (reduces flicker caused by aliasing with TFT screen refresh rate)
}
/***********************************************************************************************************************************/
void RenderImage( void)
{
// renders all the lines after erasing the old ones.
// in here is the only code actually interfacing with the OLED. so if you use a different lib, this is where to change it.
/***********************************************************************************************************************************/
// Sets the global vars for the 3d transform. Any points sent through "process" will be transformed using these figures.
// only needs to be called if Xan or Yan are changed.
void SetVars(void)
{
float Xan2, Yan2, Zan2;
float s1, s2, s3, c1, c2, c3;
s1 = sin(Yan2);
s2 = sin(Xan2);
c1 = cos(Yan2);
c2 = cos(Xan2);
xx = c1;
xy = 0;
xz = -s1;
yx = (s1 * s2);
yy = c2;
yz = (c1 * s2);
zx = (s1 * c2);
zy = -s2;
zz = (c1 * c2);
}
/***********************************************************************************************************************************/
// processes x1,y1,z1 and returns rx1,ry1 transformed by the variables set in SetVars()
// fairly heavy on floating point here.
// uses a bunch of global vars. Could be rewritten with a struct but not worth the effort.
void ProcessLine(struct Line2d *ret, struct Line3d vec)
{
float zvt1;
int xv1, yv1, zv1;
float zvt2;
int xv2, yv2, zv2;
int x1;
int y1;
int z1;
int x2;
int y2;
int z2;
int Ok;
x1 = vec.p0.x;
y1 = vec.p0.y;
z1 = vec.p0.z;
x2 = vec.p1.x;
y2 = vec.p1.y;
z2 = vec.p1.z;
Ok = 0; // defaults to not OK
if (Ok == 1) {
ret->p0.x = rx1;
ret->p0.y = ry1;
ret->p1.x = rx2;
ret->p1.y = ry2;
}
// The ifs here are checks for out of bounds. needs a bit more code here to "safe" lines that will be way out of whack, so they dont
get drawn and cause screen garbage.
/***********************************************************************************************************************************/
// line segments to draw a cube. basically p0 to p1. p1 to p2. p2 to p3 so on.
void cube(void)
{
// Front Face.
Lines[0].p0.x = -50;
Lines[0].p0.y = -50;
Lines[0].p0.z = 50;
Lines[0].p1.x = 50;
Lines[0].p1.y = -50;
Lines[0].p1.z = 50;
Lines[1].p0.x = 50;
Lines[1].p0.y = -50;
Lines[1].p0.z = 50;
Lines[1].p1.x = 50;
Lines[1].p1.y = 50;
Lines[1].p1.z = 50;
Lines[2].p0.x = 50;
Lines[2].p0.y = 50;
Lines[2].p0.z = 50;
Lines[2].p1.x = -50;
Lines[2].p1.y = 50;
Lines[2].p1.z = 50;
Lines[3].p0.x = -50;
Lines[3].p0.y = 50;
Lines[3].p0.z = 50;
Lines[3].p1.x = -50;
Lines[3].p1.y = -50;
Lines[3].p1.z = 50;
//back face.
Lines[4].p0.x = -50;
Lines[4].p0.y = -50;
Lines[4].p0.z = -50;
Lines[4].p1.x = 50;
Lines[4].p1.y = -50;
Lines[4].p1.z = -50;
Lines[5].p0.x = 50;
Lines[5].p0.y = -50;
Lines[5].p0.z = -50;
Lines[5].p1.x = 50;
Lines[5].p1.y = 50;
Lines[5].p1.z = -50;
Lines[6].p0.x = 50;
Lines[6].p0.y = 50;
Lines[6].p0.z = -50;
Lines[6].p1.x = -50;
Lines[6].p1.y = 50;
Lines[6].p1.z = -50;
Lines[7].p0.x = -50;
Lines[7].p0.y = 50;
Lines[7].p0.z = -50;
Lines[7].p1.x = -50;
Lines[7].p1.y = -50;
Lines[7].p1.z = -50;
Lines[8].p0.x = -50;
Lines[8].p0.y = -50;
Lines[8].p0.z = 50;
Lines[8].p1.x = -50;
Lines[8].p1.y = -50;
Lines[8].p1.z = -50;
Lines[9].p0.x = 50;
Lines[9].p0.y = -50;
Lines[9].p0.z = 50;
Lines[9].p1.x = 50;
Lines[9].p1.y = -50;
Lines[9].p1.z = -50;
Lines[10].p0.x = -50;
Lines[10].p0.y = 50;
Lines[10].p0.z = 50;
Lines[10].p1.x = -50;
Lines[10].p1.y = 50;
Lines[10].p1.z = -50;
Lines[11].p0.x = 50;
Lines[11].p0.y = 50;
Lines[11].p0.z = 50;
Lines[11].p1.x = 50;
Lines[11].p1.y = 50;
Lines[11].p1.z = -50;
LinestoRender = 12;
OldLinestoRender = LinestoRender;