Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Rasturizer/Rasturizer.cpp
- Committer:
- el17cd
- Date:
- 2019-04-02
- Revision:
- 21:6b5d2d75e083
- Parent:
- 20:3ca430241df0
- Child:
- 22:236319885874
File content as of revision 21:6b5d2d75e083:
#include "Rasturizer.h"
Serial pc(USBTX, USBRX); // tx, rx
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Rasturizer::Rasturizer(){
for(int i = 0; i < 6; i++){
selectionCube.getFace(i).setVisible(true);
}
}
void Rasturizer::init(){
lcd.init();
fov = 50;
}
float Rasturizer::xTo2D(float x, float z){
return x * (fov/z) + 42;
}
float Rasturizer::yTo2D(float y, float z){
return y * (fov/z) + 21;
}
void Rasturizer::drawHorizon(float angle){
lcd.drawLine(0,
21-rint(angle*40),
84,
21+rint(angle*40),
1);
}
void Rasturizer::drawFace(Face *face, float angle){
float points[4][3];
float y;
float x;
int diffX1;
int diffY1;
int diffX2;
int diffY2;
float step;
float stepSmall1;
float stepSmall2;
for(int vertex = 0; vertex < 4; vertex++){
for(int axis = 0; axis < 3; axis++){
points[vertex][axis] = face->getVertexValue(vertex, axis);
}
y = points[vertex][1];
x = points[vertex][0];
points[vertex][0] = x*cos(angle)-y*sin(angle);
points[vertex][1] = y*cos(angle)+x*sin(angle);
}
if (checkOnScreen(points)){
diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]);
diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]);
diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]);
diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]);
if(diffX2 != 0 && face->getVisible()){
step = (float)diffY2/(float)diffX2;
stepSmall1 = (float)diffX1/(float)diffX2;
stepSmall2 = (float)diffY1/(float)diffX2;
for(int s = 0; s< abs(diffX2); s++){
if(diffX2 > 0){
lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s),
rint(xTo2D(points[3][0], points[3][2])+s),
rint(yTo2D(points[3][1], points[3][2])+step*s),
0);
}
else{
lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s),
rint(xTo2D(points[3][0], points[3][2])-s),
rint(yTo2D(points[3][1], points[3][2])-step*s),
0);
}
}
}
for (int i = 0; i < 3; i++){
lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])),
rint(yTo2D(points[i][1], points[i][2])),
rint(xTo2D(points[i+1][0], points[i+1][2])),
rint(yTo2D(points[i+1][1], points[i+1][2])),
1);
}
lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])),
rint(yTo2D(points[0][1], points[0][2])),
rint(xTo2D(points[3][0], points[3][2])),
rint(yTo2D(points[3][1], points[3][2])),
1);
}
//if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){
//}
}
void Rasturizer::drawAllFaces(Face *faceArray, int noOfCubes, float angle){
Face temp;
for (int f = 0; f< (noOfCubes*6)-1; f++){
for (int f2 = 0; f2< (noOfCubes*6)-f-1; f2++){
if(faceArray[f2].getAvgZ() < faceArray[f2+1].getAvgZ()){
temp = faceArray[f2+1];
faceArray[f2+1] = faceArray[f2];
faceArray[f2] = temp;
}
}
}
for (int f = 0; f< noOfCubes*6 ; f++){
drawFace(&faceArray[f], angle/15);
}
}
bool Rasturizer::checkOnScreen(float (&points)[4][3]){
if (points[0][2] < 6 || points[1][2] < 6 || points[2][2] < 6 || points[3][2] < 6){
return false;
}
else if ((xTo2D(points[0][0], points[0][2]) < 0 || xTo2D(points[0][0], points[0][2]) > 84)
&& (xTo2D(points[1][0], points[1][2]) < 0 || xTo2D(points[1][0], points[1][2]) > 84)
&& (xTo2D(points[2][0], points[2][2]) < 0 || xTo2D(points[2][0], points[2][2]) > 84)
&& (xTo2D(points[3][0], points[3][2]) < 0 || xTo2D(points[3][0], points[3][2]) > 84)){
return false;
}
return true;
}
void Rasturizer::print(const char *text, int x, int y){
lcd.printString(text, x, y);
}
void Rasturizer::clear(){
lcd.clear();
}
void Rasturizer::refresh(){
lcd.refresh();
}
void Rasturizer::drawDeathScreen(bool selection){
Face faces[6];
int x;
int y;
int z;
if(selection == true){
x = -30;
y = -3;
z = 50;
}
else{
x = -30;
y = 15;
z = 50;
}
lcd.drawRect(24, 14, 45, 11, FILL_WHITE);
lcd.drawRect(24, 14, 45, 11, FILL_TRANSPARENT);
lcd.printString("Restart",26,2);
lcd.drawRect(24, 30, 45, 11, FILL_WHITE);
lcd.drawRect(24, 30, 45, 11, FILL_TRANSPARENT);
lcd.printString("Menu",35,4);
selectionCube.translate(x, y, z);
for(int i = 0; i < 6; i++){
faces[i] = selectionCube.getFace(i);
}
drawAllFaces(faces, 1, 0);
selectionCube.rotateX(0.1);
selectionCube.rotateY(0.05);
selectionCube.rotateZ(-0.08);
selectionCube.translate(-x, -y, -z);
}
void Rasturizer::drawHomeScreen(int selection){
Face faces[6];
int x;
int y;
int z;
if(selection == 0){
x = -30;
y = -12;
z = 50;
}
else if(selection == 1){
x = -30;
y = 5;
z = 50;
}
else{
x = -30;
y = 22;
z = 50;
}
lcd.drawRect(24, 6, 45, 10, FILL_WHITE);
lcd.printString("Play",35,1);
lcd.drawRect(24, 6, 45, 10, FILL_TRANSPARENT);
lcd.drawRect(24, 22, 45, 10, FILL_WHITE);
lcd.printString("Help",35,3);
lcd.drawRect(24, 22, 45, 10, FILL_TRANSPARENT);
lcd.drawRect(24, 38, 45, 10, FILL_WHITE);
lcd.printString("Quit",35,5);
lcd.drawRect(24, 38, 45, 10, FILL_TRANSPARENT);
selectionCube.translate(x, y, z);
for(int i = 0; i < 6; i++){
faces[i] = selectionCube.getFace(i);
}
drawAllFaces(faces, 1, 0);
selectionCube.rotateX(-0.1);
selectionCube.rotateY(-0.05);
selectionCube.rotateZ(0.08);
selectionCube.translate(-x, -y, -z);
}