dmd 3d cube animation - freetronics forum
TRANSCRIPT
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 1/11
Freetronics ForumFreetronics: Arduino-compatible open source electronics
Skip to content
Search… Search Advanced search
DMD 3D Cube animation
Post ReplyPrint view
Search this topic… Search Advanced search
2 posts • Page 1 of1
DMD 3D Cube animation (#p13715)Quote (./posting.php?mode=quote&f=26&p=13715)
(javascript:void(0);)Postby sinoptik » Sun Jan 11, 2015 8:23 am
(./download/file.php?id=340&mode=view)
Sketch for 3D cube animation.
Original code for OLED display (csconsulting thx) here:
http://forum.freetronics.com/viewtopic.php?f=37&t=5495
(http://forum.freetronics.com/viewtopic.php?f=37&t=5495)
For DMD:
http://youtu.be/d8XMuXJBqMs (http://youtu.be/d8XMuXJBqMs)
// 3D Cube sketch
// Draws a 3d rotating cube on the freetronics OLED screen. // Modified for DMD
#include <SPI.h> //SPI.h must be included as DMD is written by
SPI
#include <DMD.h>
#include <TimerOne.h>
//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 2
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 2/11
float xx,xy,xz;
float yx,yy,yz;
float zx,zy,zz;
int fadeAmount = 1;
float fact;
int Xan,Yan;
int Xoff;
int Yoff;
int Zoff;
int loops = 0;
boolean zoom = false;
struct Point3d
int x;
int y; int z;
;
struct Point2d
int x;
int y;
;
int LinestoRender; // lines to render.
int OldLinestoRender; // lines to render just in case it changes. this
makes sure the old lines all get erased.
struct Line3d
Point3d p0; Point3d p1;
;
struct Line2d
Point2d p0;
Point2d p1;
;
Line3d Lines[20];
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 3/11
Line2d Render[20];
Line2d ORender[20];
/***************************************************************************
// 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;
Xan2 = Xan / fact; // convert degrees to radians.
Yan2 = Yan / fact;
// Zan is assumed to be zero
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)
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 4/11
float zvt1;
int xv1,yv1,zv1;
float zvt2;
int xv2,yv2,zv2;
int rx1,ry1; int rx2,ry2;
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
xv1 = (x1 * xx) + (y1 * xy) + (z1 * xz);
yv1 = (x1 * yx) + (y1 * yy) + (z1 * yz);
zv1 = (x1 * zx) + (y1 * zy) + (z1 * zz);
zvt1 = zv1 ‐ Zoff;
if( zvt1 < ‐5) rx1 = 256 * (xv1 / zvt1) + Xoff;
ry1 = 256 * (yv1 / zvt1) + Yoff;
Ok=1; // ok we are alright for point 1.
xv2 = (x2 * xx) + (y2 * xy) + (z2 * xz);
yv2 = (x2 * yx) + (y2 * yy) + (z2 * yz);
zv2 = (x2 * zx) + (y2 * zy) + (z2 * zz);
zvt2 = zv2 ‐ Zoff;
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 5/11
if( zvt2 < ‐5)
rx2 = 256 * (xv2 / zvt2) + Xoff;
ry2 = 256 * (yv2 / zvt2) + Yoff;
else
Ok=0;
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.
/***************************************************************************
void ScanDMD()
dmd.scanDisplayBySPI();
void setup()
//initialize TimerOne's interrupt/CPU usage used to scan and refresh
the display
Timer1.initialize( 2500 ); //period in microseconds to call
ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to
ScanDMD which goes to dmd.scanDisplayBySPI()
//clear/init the DMD pixels held in RAM
dmd.clearScreen( true ); //true is normal (all pixels off), false is
negative (all pixels on)
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 6/11
fact = 180 / 3.14159259; // conversion from degrees to radians.
Xoff = 16; // positions the center of the 3d conversion space into the
center of the OLED screen. This is usally screen_x_size / 2.
Yoff = 16*DISPLAYS_DOWN/2; // screen_y_size /2
Zoff = 500;
// line segments to draw a cube. basically p0 to p1. p1 to p2. p2 to
p3 so on.
// Front Face.
Lines[0].p0.x=‐15;
Lines[0].p0.y=‐15;
Lines[0].p0.z=15;
Lines[0].p1.x=15; Lines[0].p1.y=‐15;
Lines[0].p1.z=15;
Lines[1].p0.x=15;
Lines[1].p0.y=‐15;
Lines[1].p0.z=15;
Lines[1].p1.x=15;
Lines[1].p1.y=15; Lines[1].p1.z=15;
Lines[2].p0.x=15;
Lines[2].p0.y=15;
Lines[2].p0.z=15;
Lines[2].p1.x=‐15;
Lines[2].p1.y=15; Lines[2].p1.z=15;
Lines[3].p0.x=‐15;
Lines[3].p0.y=15;
Lines[3].p0.z=15;
Lines[3].p1.x=‐15;
Lines[3].p1.y=‐15;
Lines[3].p1.z=15;
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 7/11
//back face.
Lines[4].p0.x=‐15;
Lines[4].p0.y=‐15;
Lines[4].p0.z=‐15; Lines[4].p1.x=15;
Lines[4].p1.y=‐15;
Lines[4].p1.z=‐15;
Lines[5].p0.x=15;
Lines[5].p0.y=‐15;
Lines[5].p0.z=‐15;
Lines[5].p1.x=15; Lines[5].p1.y=15;
Lines[5].p1.z=‐15;
Lines[6].p0.x=15;
Lines[6].p0.y=15;
Lines[6].p0.z=‐15;
Lines[6].p1.x=‐15; Lines[6].p1.y=15;
Lines[6].p1.z=‐15;
Lines[7].p0.x=‐15;
Lines[7].p0.y=15;
Lines[7].p0.z=‐15;
Lines[7].p1.x=‐15;
Lines[7].p1.y=‐15; Lines[7].p1.z=‐15;
// now the 4 edge lines.
Lines[8].p0.x=‐15;
Lines[8].p0.y=‐15;
Lines[8].p0.z=15;
Lines[8].p1.x=‐15;
Lines[8].p1.y=‐15;
Lines[8].p1.z=‐15;
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 8/11
Lines[9].p0.x=15;
Lines[9].p0.y=‐15;
Lines[9].p0.z=15;
Lines[9].p1.x=15;
Lines[9].p1.y=‐15;
Lines[9].p1.z=‐15;
Lines[10].p0.x=‐15;
Lines[10].p0.y=15;
Lines[10].p0.z=15;
Lines[10].p1.x=‐15;
Lines[10].p1.y=15;
Lines[10].p1.z=‐15;
Lines[11].p0.x=15;
Lines[11].p0.y=15;
Lines[11].p0.z=15;
Lines[11].p1.x=15;
Lines[11].p1.y=15;
Lines[11].p1.z=‐15;
LinestoRender=12;
OldLinestoRender=LinestoRender;
/***************************************************************************
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.
for (int i=0; i<OldLinestoRender; i++ )
dmd.drawLine(ORender[i].p0.x,ORender[i].p0.y,ORender[i].p1.x,ORender[i].p1.y
GRAPHICS_NOR); // erase the old lines.
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 9/11
for (int i=0; i<LinestoRender; i++ )
dmd.drawLine(Render[i].p0.x,Render[i].p0.y,Render[i].p1.x,Render[i].p1.y,
GRAPHICS_NORMAL);
OldLinestoRender=LinestoRender;
void RenderInverse( 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.
for (int i=0; i<OldLinestoRender; i++ )
dmd.drawLine(ORender[i].p0.x,ORender[i].p0.y,ORender[i].p1.x,ORender[i].p1.y
GRAPHICS_OR); // erase the old lines.
for (int i=0; i<LinestoRender; i++ )
dmd.drawLine(Render[i].p0.x,Render[i].p0.y,Render[i].p1.x,Render[i].p1.y,
GRAPHICS_INVERSE);
OldLinestoRender=LinestoRender;
/***************************************************************************
void loop()
Xan++;
Yan++;
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 10/11
Yan=Yan % 360;
Xan=Xan % 360; // prevents overflow.
SetVars(); //sets up the global vars to do the conversion.
for(int i=0; i<LinestoRender ; i++)
ORender[i]=Render[i]; // stores the old line segment so we can
delete it later.
ProcessLine(&Render[i],Lines[i]); // converts the 3d line segments
to 2d.
if (loops < 2000)
RenderImage(); // go draw it!
if (loops > 2000)
RenderInverse(); // go draw it! zoom = true;
if (zoom)
Zoff = Zoff ‐ fadeAmount;
if (Zoff == 200 || Zoff == 800)
fadeAmount = ‐fadeAmount ;
loops++;
if (loops > 4000)
loops = 0;
/***************************************************************************
Top
Re: DMD 3D Cube animation (#p13733)Quote (./posting.php?mode=quote&f=26&p=13733)
(javascript:void(0);)Postby johnb » Wed Jan 14, 2015 6:29 am
Awesome ‐ thank you for sharing that with us!
Top
Display posts from previous: All posts Sort by Post time Ascending Go
Post Reply
Print view
2 posts • Page 1 of1Return to “Dot Matrix Display”
Jump to
Arduino-Compatible Boards
ArduPhone
8/20/2019 DMD 3D Cube Animation - Freetronics Forum
http://slidepdf.com/reader/full/dmd-3d-cube-animation-freetronics-forum 11/11
CNCPlotter
Eleven
EtherDue
EtherMega
EtherTen
Goldilocks
KitTen
LeoStick
LeoStick examples, software and fun sketches Pebble v2
StepDuino
USBDroid
Arduino Expansion Shields
BTSH: Bluetooth Shield
DLOCK: RFID Door Lock Shield
ES: Ethernet Shield
HBRIDGE: H-Bridge Motor Driver Shield
LCDK: LCD & Keypad Shield
RX315/RX433: 315MHz and 433MHz Receiver Shields SECSENSE: Security Sensor Shield
SH-RFIDLOCK: RFID Lock Shield
TS: Terminal Shield
Modules, Sensors, & Displays
Module Support
Dot Matrix Display
Experimenters Kit
4x4x4 RGB Cube
OLED128 Display
Raspberry Pi Raspberry Pi Expansion Boards
ArduSat Arduino Satellite
General ArduSat Discussion
ArduSat Payload Processor Module
General Discussion
Random Chit-Chat
Project Showcase
Product / Device Ideas
Practical Arduino
Arduino Workshop
3D Printing SuperHouseTV Home Automation
Arduino Shield List
Shield Discussion
Shield List Site Discussion
Who is onlineUsers browsing this forum: No registered users and 1 guestPowered by phpBB® Forum Software © phpBB Limited