0% found this document useful (0 votes)
276 views11 pages

DMD 3D Cube Animation - Freetronics Forum

The document is a forum post discussing code for animating a 3D cube on a DMD (Digital Micromirror Device) display. It includes code for: 1) Defining the line segments that make up a cube. 2) Transforming the 3D coordinates of these line segments into 2D screen coordinates using rotation variables, offsets and a 3D to 2D transformation process. 3) Continuously re-rendering the transformed lines to create the appearance of rotation.

Uploaded by

apofview
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views11 pages

DMD 3D Cube Animation - Freetronics Forum

The document is a forum post discussing code for animating a 3D cube on a DMD (Digital Micromirror Device) display. It includes code for: 1) Defining the line segments that make up a cube. 2) Transforming the 3D coordinates of these line segments into 2D screen coordinates using rotation variables, offsets and a 3D to 2D transformation process. 3) Continuously re-rendering the transformed lines to create the appearance of rotation.

Uploaded by

apofview
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

Freetronics Forum
Freetronics:Arduinocompatibleopensourceelectronics
Skiptocontent

Search

Search Advancedsearch

DMD 3D Cube animation


PostReply
Printview
Searchthistopic

Search Advancedsearch

2postsPage1of1

DMD 3D Cube animation (#p13715)


Quote(./posting.php?mode=quote&f=26&p=13715)
(javascript:void(0))

PostbysinoptikSunJan11,20158:23am

(./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)

//3DCubesketch
//Drawsa3drotatingcubeonthefreetronicsOLEDscreen.
//ModifiedforDMD

#include<SPI.h>//SPI.hmustbeincludedasDMDiswrittenby
SPI
#include<DMD.h>
#include<TimerOne.h>

//FireuptheDMDlibraryasdmd
#defineDISPLAYS_ACROSS1
#defineDISPLAYS_DOWN2
DMDdmd(DISPLAYS_ACROSS,DISPLAYS_DOWN);
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

1/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

floatxx,xy,xz;
floatyx,yy,yz;
floatzx,zy,zz;
intfadeAmount=1;
floatfact;
intXan,Yan;
intXoff;
intYoff;
intZoff;
intloops=0;
booleanzoom=false;
structPoint3d
{
intx;
inty;
intz;
};
structPoint2d
{
intx;
inty;
};

intLinestoRender;//linestorender.
intOldLinestoRender;//linestorenderjustincaseitchanges.this
makessuretheoldlinesallgeterased.

structLine3d
{
Point3dp0;
Point3dp1;
};

structLine2d
{
Point2dp0;
Point2dp1;
};

Line3dLines[20];
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

2/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

Line2dRender[20];
Line2dORender[20];

/************************************************************************************
//Setstheglobalvarsforthe3dtransform.Anypointssentthrough
"process"willbetransformedusingthesefigures.
//onlyneedstobecalledifXanorYanarechanged.
voidSetVars(void)
{
floatXan2,Yan2,Zan2;
floats1,s2,s3,c1,c2,c3;
Xan2=Xan/fact;//convertdegreestoradians.
Yan2=Yan/fact;
//Zanisassumedtobezero
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);
}

/************************************************************************************
//processesx1,y1,z1andreturnsrx1,ry1transformedbythevariables
setinSetVars()
//fairlyheavyonfloatingpointhere.
//usesabunchofglobalvars.Couldberewrittenwithastructbutnot
worththeeffort.
voidProcessLine(structLine2d*ret,structLine3dvec)
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

3/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

{
floatzvt1;
intxv1,yv1,zv1;
floatzvt2;
intxv2,yv2,zv2;
intrx1,ry1;
intrx2,ry2;
intx1;
inty1;
intz1;
intx2;
inty2;
intz2;
intOk;
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;//defaultstonotOK
xv1=(x1*xx)+(y1*xy)+(z1*xz);
yv1=(x1*yx)+(y1*yy)+(z1*yz);
zv1=(x1*zx)+(y1*zy)+(z1*zz);
zvt1=zv1Zoff;

if(zvt1<5){
rx1=256*(xv1/zvt1)+Xoff;
ry1=256*(yv1/zvt1)+Yoff;
Ok=1;//okwearealrightforpoint1.
}

xv2=(x2*xx)+(y2*xy)+(z2*xz);
yv2=(x2*yx)+(y2*yy)+(z2*yz);
zv2=(x2*zx)+(y2*zy)+(z2*zz);
zvt2=zv2Zoff;

http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

4/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

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;
}
//Theifsherearechecksforoutofbounds.needsabitmorecode
hereto"safe"linesthatwillbewayoutofwhack,sotheydontgetdrawn
andcausescreengarbage.
}

/************************************************************************************
voidScanDMD()
{
dmd.scanDisplayBySPI();
}
voidsetup(){

//initializeTimerOne'sinterrupt/CPUusageusedtoscanandrefresh
thedisplay
Timer1.initialize(2500);//periodinmicrosecondstocall
ScanDMD.Anythinglongerthan5000(5ms)andyoucanseeflicker.
Timer1.attachInterrupt(ScanDMD);//attachtheTimer1interruptto
ScanDMDwhichgoestodmd.scanDisplayBySPI()
//clear/inittheDMDpixelsheldinRAM
dmd.clearScreen(true);//trueisnormal(allpixelsoff),falseis
negative(allpixelson)
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

5/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

fact=180/3.14159259;//conversionfromdegreestoradians.
Xoff=16;//positionsthecenterofthe3dconversionspaceintothe
centeroftheOLEDscreen.Thisisusallyscreen_x_size/2.
Yoff=16*DISPLAYS_DOWN/2;//screen_y_size/2
Zoff=500;

//linesegmentstodrawacube.basicallyp0top1.p1top2.p2to
p3soon.
//FrontFace.
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;

http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

6/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

//backface.
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;

//nowthe4edgelines.
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;
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

7/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

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;

/************************************************************************************
voidRenderImage(void)
{
//rendersallthelinesaftererasingtheoldones.
//inhereistheonlycodeactuallyinterfacingwiththeOLED.soif
youuseadifferentlib,thisiswheretochangeit.

for(inti=0;i<OldLinestoRender;i++)
{

dmd.drawLine(ORender[i].p0.x,ORender[i].p0.y,ORender[i].p1.x,ORender[i].p1.y,
GRAPHICS_NOR);//erasetheoldlines.
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

8/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

for(inti=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;
}
voidRenderInverse(void)
{
//rendersallthelinesaftererasingtheoldones.
//inhereistheonlycodeactuallyinterfacingwiththeOLED.soif
youuseadifferentlib,thisiswheretochangeit.

for(inti=0;i<OldLinestoRender;i++)
{

dmd.drawLine(ORender[i].p0.x,ORender[i].p0.y,ORender[i].p1.x,ORender[i].p1.y,
GRAPHICS_OR);//erasetheoldlines.
}

for(inti=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;

/************************************************************************************
voidloop(){

Xan++;
Yan++;

http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

9/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

Yan=Yan%360;
Xan=Xan%360;//preventsoverflow.

SetVars();//setsuptheglobalvarstodotheconversion.
for(inti=0;i<LinestoRender;i++)
{
ORender[i]=Render[i];//storestheoldlinesegmentsowecan
deleteitlater.
ProcessLine(&Render[i],Lines[i]);//convertsthe3dlinesegments
to2d.
}
if(loops<2000)
RenderImage();//godrawit!
if(loops>2000){
RenderInverse();//godrawit!
zoom=true;
}
if(zoom)
{
Zoff=ZofffadeAmount;

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))

PostbyjohnbWedJan14,20156:29am

Awesome thank you for sharing that with us!


Top
Displaypostsfromprevious: Allposts

Sortby Posttime

Ascending

Go

PostReply
Printview
2postsPage1of1
ReturntoDotMatrixDisplay

Jumpto
ArduinoCompatibleBoards
ArduPhone
http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

10/11

8.7.2015.

DMD3DCubeanimationFreetronicsForum

CNCPlotter
Eleven
EtherDue
EtherMega
EtherTen
Goldilocks
KitTen
LeoStick
LeoStickexamples,softwareandfunsketches
Pebblev2
StepDuino
USBDroid
ArduinoExpansionShields
BTSH:BluetoothShield
DLOCK:RFIDDoorLockShield
ES:EthernetShield
HBRIDGE:HBridgeMotorDriverShield
LCDK:LCD&KeypadShield
RX315/RX433:315MHzand433MHzReceiverShields
SECSENSE:SecuritySensorShield
SHRFIDLOCK:RFIDLockShield
TS:TerminalShield
Modules,Sensors,&Displays
ModuleSupport
DotMatrixDisplay
ExperimentersKit
4x4x4RGBCube
OLED128Display
RaspberryPi
RaspberryPiExpansionBoards
ArduSatArduinoSatellite
GeneralArduSatDiscussion
ArduSatPayloadProcessorModule
GeneralDiscussion
RandomChitChat
ProjectShowcase
Product/DeviceIdeas
PracticalArduino
ArduinoWorkshop
3DPrinting
SuperHouseTVHomeAutomation
ArduinoShieldList
ShieldDiscussion
ShieldListSiteDiscussion

Who is online
Usersbrowsingthisforum:Noregisteredusersand1guest
PoweredbyphpBBForumSoftwarephpBBLimited

http://forum.freetronics.com/viewtopic.php?f=26&t=6165&sid=707270205fb460f7f4804f41b762207f

11/11

You might also like