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@11:2cd6341136ca, 2019-03-24 (annotated)
- Committer:
- el17cd
- Date:
- Sun Mar 24 12:31:38 2019 +0000
- Revision:
- 11:2cd6341136ca
- Parent:
- 10:07a23afd5088
- Child:
- 12:b69657862610
added horizon, rotating field of view when moving and collision detection
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17cd | 1:044238f7bdda | 1 | #include "mbed.h" |
| el17cd | 1:044238f7bdda | 2 | #include "Rasturizer.h" |
| el17cd | 2:a5bc7b3779f7 | 3 | |
| el17cd | 4:759a5c34e239 | 4 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17cd | 3:2e31dfcb712a | 5 | |
| el17cd | 3:2e31dfcb712a | 6 | |
| el17cd | 4:759a5c34e239 | 7 | Rasturizer::Rasturizer(){ |
| el17cd | 3:2e31dfcb712a | 8 | lcd.init(); |
| el17cd | 4:759a5c34e239 | 9 | } |
| el17cd | 1:044238f7bdda | 10 | |
| el17cd | 11:2cd6341136ca | 11 | void Rasturizer::drawHorizon(double angle){ |
| el17cd | 11:2cd6341136ca | 12 | lcd.drawLine(0, |
| el17cd | 11:2cd6341136ca | 13 | 21, |
| el17cd | 11:2cd6341136ca | 14 | 84, |
| el17cd | 11:2cd6341136ca | 15 | 21, |
| el17cd | 11:2cd6341136ca | 16 | 1);/* |
| el17cd | 11:2cd6341136ca | 17 | double floorPoints[14][3] = {{-120,5, 5}, {-120,0, 1000}, {-80,5, 5}, {-80,0, 1000}, {-40,5, 5}, {-40,0, 1000}, {0,5, 5}, {0,0, 1000}, {40,5, 5}, {40,0, 1000}, {80,5, 5}, {80,0, 1000}, {120,5, 5}, {120,0, 1000}};//{-10,20, 10}, {-10,0, 100}, {0,20, 10}, {0,0, 100}, {10,20, 10}, {10,0, 100}, {20,20, 10}, {20,0, 100},{30,20, 10}, {30,0, 100}}; |
| el17cd | 11:2cd6341136ca | 18 | for(int i = 0; i < 14; i += 2){ |
| el17cd | 11:2cd6341136ca | 19 | lcd.drawLine(rint(floorPoints[i][0]*(40/floorPoints[i][2]))+42, |
| el17cd | 11:2cd6341136ca | 20 | rint(floorPoints[i][1]*(40/floorPoints[i][2]))+21, |
| el17cd | 11:2cd6341136ca | 21 | rint(floorPoints[i+1][0]*(40/floorPoints[i+1][2]))+42, |
| el17cd | 11:2cd6341136ca | 22 | rint(floorPoints[i+1][1]*(40/floorPoints[i+1][2]))+21, |
| el17cd | 11:2cd6341136ca | 23 | 1); |
| el17cd | 11:2cd6341136ca | 24 | }*/ |
| el17cd | 11:2cd6341136ca | 25 | } |
| el17cd | 11:2cd6341136ca | 26 | |
| el17cd | 11:2cd6341136ca | 27 | void Rasturizer::drawFace(Face face, double angle){ |
| el17cd | 7:15543cb10a14 | 28 | double points[4][3]; |
| el17cd | 4:759a5c34e239 | 29 | for(int vertex = 0; vertex < 4; vertex++){ |
| el17cd | 4:759a5c34e239 | 30 | for(int axis = 0; axis < 3; axis++){ |
| el17cd | 4:759a5c34e239 | 31 | points[vertex][axis] = face.getVertexValue(vertex, axis); |
| el17cd | 4:759a5c34e239 | 32 | } |
| el17cd | 11:2cd6341136ca | 33 | double y = points[vertex][1]; |
| el17cd | 11:2cd6341136ca | 34 | double x = points[vertex][0]; |
| el17cd | 11:2cd6341136ca | 35 | |
| el17cd | 11:2cd6341136ca | 36 | points[vertex][0] = x*cos(angle)-y*sin(angle); |
| el17cd | 11:2cd6341136ca | 37 | points[vertex][1] = y*cos(angle)+x*sin(angle); |
| el17cd | 4:759a5c34e239 | 38 | } |
| el17cd | 6:75031d14fc0d | 39 | |
| el17cd | 11:2cd6341136ca | 40 | |
| el17cd | 11:2cd6341136ca | 41 | if (points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10){ |
| el17cd | 10:07a23afd5088 | 42 | |
| el17cd | 7:15543cb10a14 | 43 | int diffX1 = points[0][0]-points[1][0]; |
| el17cd | 7:15543cb10a14 | 44 | int diffY1 = points[0][1]-points[1][1]; |
| el17cd | 7:15543cb10a14 | 45 | int diffX2 = points[2][0]-points[3][0]; |
| el17cd | 9:5915fc800824 | 46 | int diffY2 = points[2][1]-points[3][1]; |
| el17cd | 8:a667bc5050c1 | 47 | |
| el17cd | 9:5915fc800824 | 48 | double step = (double)diffY2/(double)diffX2; |
| el17cd | 9:5915fc800824 | 49 | double stepSmall1 = (double)diffX1/(double)diffX2; |
| el17cd | 9:5915fc800824 | 50 | double stepSmall2 = (double)diffY1/(double)diffX2; |
| el17cd | 9:5915fc800824 | 51 | |
| el17cd | 9:5915fc800824 | 52 | for(int s = 0; s<= abs(diffX2)-1; s++){ |
| el17cd | 9:5915fc800824 | 53 | lcd.drawLine(rint((points[0][0]-stepSmall1*s)*(40/points[0][2]))+42, rint((points[0][1]-stepSmall2*s)*(40/points[0][2]))+21,rint((points[3][0]+s)*(40/points[3][2]))+42, rint((points[3][1]+step*s)*(40/points[3][2]))+21, 0); |
| el17cd | 7:15543cb10a14 | 54 | } |
| el17cd | 1:044238f7bdda | 55 | |
| el17cd | 7:15543cb10a14 | 56 | for (int i = 0; i < 3; i++){ |
| el17cd | 6:75031d14fc0d | 57 | |
| el17cd | 7:15543cb10a14 | 58 | lcd.drawLine(rint(points[i][0]*(40/points[i][2]))+42, |
| el17cd | 7:15543cb10a14 | 59 | rint(points[i][1]*(40/points[i][2]))+21, |
| el17cd | 7:15543cb10a14 | 60 | rint(points[i+1][0]*(40/points[i+1][2]))+42, |
| el17cd | 7:15543cb10a14 | 61 | rint(points[i+1][1]*(40/points[i+1][2]))+21, |
| el17cd | 7:15543cb10a14 | 62 | 1); |
| el17cd | 7:15543cb10a14 | 63 | |
| el17cd | 1:044238f7bdda | 64 | } |
| el17cd | 7:15543cb10a14 | 65 | lcd.drawLine(rint(points[0][0]*(40/points[0][2]))+42, |
| el17cd | 7:15543cb10a14 | 66 | rint(points[0][1]*(40/points[0][2]))+21, |
| el17cd | 7:15543cb10a14 | 67 | rint(points[3][0]*(40/points[3][2]))+42, |
| el17cd | 7:15543cb10a14 | 68 | rint(points[3][1]*(40/points[3][2]))+21, |
| el17cd | 7:15543cb10a14 | 69 | 1); |
| el17cd | 1:044238f7bdda | 70 | } |
| el17cd | 4:759a5c34e239 | 71 | } |
| el17cd | 9:5915fc800824 | 72 | |
| el17cd | 11:2cd6341136ca | 73 | void Rasturizer::print(const char *text){ |
| el17cd | 11:2cd6341136ca | 74 | lcd.printString(text, 0, 0); |
| el17cd | 11:2cd6341136ca | 75 | } |
| el17cd | 11:2cd6341136ca | 76 | |
| el17cd | 4:759a5c34e239 | 77 | void Rasturizer::clear(){ |
| el17cd | 4:759a5c34e239 | 78 | lcd.clear(); |
| el17cd | 4:759a5c34e239 | 79 | } |
| el17cd | 4:759a5c34e239 | 80 | |
| el17cd | 4:759a5c34e239 | 81 | void Rasturizer::refresh(){ |
| el17cd | 4:759a5c34e239 | 82 | lcd.refresh(); |
| el17cd | 1:044238f7bdda | 83 | } |