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@17:3c9672c6e532, 2019-03-31 (annotated)
- Committer:
- el17cd
- Date:
- Sun Mar 31 18:10:18 2019 +0000
- Revision:
- 17:3c9672c6e532
- Parent:
- 16:64cd7bc094f9
- Child:
- 18:8256546a3cbf
changed from double precision to single (massive performance gain!)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17cd | 1:044238f7bdda | 1 | #include "Rasturizer.h" |
| el17cd | 2:a5bc7b3779f7 | 2 | |
| el17cd | 16:64cd7bc094f9 | 3 | |
| el17cd | 4:759a5c34e239 | 4 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17cd | 3:2e31dfcb712a | 5 | |
| el17cd | 4:759a5c34e239 | 6 | Rasturizer::Rasturizer(){ |
| el17cd | 15:8fbbdefbe720 | 7 | |
| el17cd | 17:3c9672c6e532 | 8 | /*float tempPoints[4][3] = {{43, 2.5, 30}, {43, 2.5, 200}, {43, -2.5, 200}, {43, -2.5, 30}}; |
| el17cd | 14:885915260e25 | 9 | for(int vertex = 0; vertex < 8; vertex++){ |
| el17cd | 14:885915260e25 | 10 | for(int axis = 0; axis < 3; axis++){ |
| el17cd | 14:885915260e25 | 11 | edgePoints[vertex][axis] = tempPoints[vertex][axis]; |
| el17cd | 14:885915260e25 | 12 | } |
| el17cd | 14:885915260e25 | 13 | } |
| el17cd | 14:885915260e25 | 14 | edge.setVerticies(edgePoints); |
| el17cd | 14:885915260e25 | 15 | edge.setVisible(true);*/ |
| el17cd | 14:885915260e25 | 16 | } |
| el17cd | 14:885915260e25 | 17 | |
| el17cd | 15:8fbbdefbe720 | 18 | void Rasturizer::init(){ |
| el17cd | 15:8fbbdefbe720 | 19 | lcd.init(); |
| el17cd | 15:8fbbdefbe720 | 20 | fov = 50; |
| el17cd | 15:8fbbdefbe720 | 21 | } |
| el17cd | 15:8fbbdefbe720 | 22 | |
| el17cd | 17:3c9672c6e532 | 23 | float Rasturizer::xTo2D(float x, float z){ |
| el17cd | 14:885915260e25 | 24 | return x * (fov/z) + 42; |
| el17cd | 14:885915260e25 | 25 | } |
| el17cd | 14:885915260e25 | 26 | |
| el17cd | 17:3c9672c6e532 | 27 | float Rasturizer::yTo2D(float y, float z){ |
| el17cd | 14:885915260e25 | 28 | return y * (fov/z) + 21; |
| el17cd | 4:759a5c34e239 | 29 | } |
| el17cd | 1:044238f7bdda | 30 | |
| el17cd | 17:3c9672c6e532 | 31 | void Rasturizer::drawHorizon(float angle){ |
| el17cd | 11:2cd6341136ca | 32 | lcd.drawLine(0, |
| el17cd | 12:b69657862610 | 33 | 21-rint(angle*50), |
| el17cd | 11:2cd6341136ca | 34 | 84, |
| el17cd | 12:b69657862610 | 35 | 21+rint(angle*50), |
| el17cd | 11:2cd6341136ca | 36 | 1);/* |
| el17cd | 14:885915260e25 | 37 | for( int i = 0; i<4; i++){ |
| el17cd | 14:885915260e25 | 38 | edgePoints[i][0] -= angle*50; |
| el17cd | 14:885915260e25 | 39 | } |
| el17cd | 14:885915260e25 | 40 | edge.setVerticies(edgePoints); |
| el17cd | 14:885915260e25 | 41 | drawFace(edge, angle);*/ |
| el17cd | 11:2cd6341136ca | 42 | } |
| el17cd | 11:2cd6341136ca | 43 | |
| el17cd | 17:3c9672c6e532 | 44 | void Rasturizer::drawFace(Face face, float angle){ |
| el17cd | 17:3c9672c6e532 | 45 | float points[4][3]; |
| el17cd | 4:759a5c34e239 | 46 | for(int vertex = 0; vertex < 4; vertex++){ |
| el17cd | 4:759a5c34e239 | 47 | for(int axis = 0; axis < 3; axis++){ |
| el17cd | 4:759a5c34e239 | 48 | points[vertex][axis] = face.getVertexValue(vertex, axis); |
| el17cd | 4:759a5c34e239 | 49 | } |
| el17cd | 17:3c9672c6e532 | 50 | float y = points[vertex][1]; |
| el17cd | 17:3c9672c6e532 | 51 | float x = points[vertex][0]; |
| el17cd | 11:2cd6341136ca | 52 | |
| el17cd | 11:2cd6341136ca | 53 | points[vertex][0] = x*cos(angle)-y*sin(angle); |
| el17cd | 11:2cd6341136ca | 54 | points[vertex][1] = y*cos(angle)+x*sin(angle); |
| el17cd | 4:759a5c34e239 | 55 | } |
| el17cd | 6:75031d14fc0d | 56 | |
| el17cd | 14:885915260e25 | 57 | if ((points[0][2] > 12 || points[1][2] > 12 || points[2][2] > 12|| points[3][2] > 12) && face.getVisible()){ |
| el17cd | 14:885915260e25 | 58 | int diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]); |
| el17cd | 14:885915260e25 | 59 | int diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]); |
| el17cd | 14:885915260e25 | 60 | int diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]); |
| el17cd | 14:885915260e25 | 61 | int diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]); |
| el17cd | 8:a667bc5050c1 | 62 | |
| el17cd | 17:3c9672c6e532 | 63 | float step = (float)diffY2/(float)diffX2; |
| el17cd | 17:3c9672c6e532 | 64 | float stepSmall1 = (float)diffX1/(float)diffX2; |
| el17cd | 17:3c9672c6e532 | 65 | float stepSmall2 = (float)diffY1/(float)diffX2; |
| el17cd | 9:5915fc800824 | 66 | |
| el17cd | 12:b69657862610 | 67 | for(int s = 0; s< abs(diffX2); s++){ |
| el17cd | 14:885915260e25 | 68 | if(diffX2 > 0){ |
| el17cd | 14:885915260e25 | 69 | lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42, |
| el17cd | 14:885915260e25 | 70 | rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s), |
| el17cd | 14:885915260e25 | 71 | rint(xTo2D(points[3][0], points[3][2])+s), |
| el17cd | 14:885915260e25 | 72 | rint(yTo2D(points[3][1], points[3][2])+step*s), |
| el17cd | 14:885915260e25 | 73 | 0); |
| el17cd | 14:885915260e25 | 74 | } |
| el17cd | 14:885915260e25 | 75 | else{ |
| el17cd | 14:885915260e25 | 76 | lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42, |
| el17cd | 14:885915260e25 | 77 | rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s), |
| el17cd | 14:885915260e25 | 78 | rint(xTo2D(points[3][0], points[3][2])-s), |
| el17cd | 14:885915260e25 | 79 | rint(yTo2D(points[3][1], points[3][2])-step*s), |
| el17cd | 14:885915260e25 | 80 | 0); |
| el17cd | 14:885915260e25 | 81 | } |
| el17cd | 7:15543cb10a14 | 82 | } |
| el17cd | 13:f4de03202477 | 83 | } |
| el17cd | 13:f4de03202477 | 84 | if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){ |
| el17cd | 14:885915260e25 | 85 | for (int i = 0; i < 3; i++){ |
| el17cd | 14:885915260e25 | 86 | lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])), |
| el17cd | 14:885915260e25 | 87 | rint(yTo2D(points[i][1], points[i][2])), |
| el17cd | 14:885915260e25 | 88 | rint(xTo2D(points[i+1][0], points[i+1][2])), |
| el17cd | 14:885915260e25 | 89 | rint(yTo2D(points[i+1][1], points[i+1][2])), |
| el17cd | 14:885915260e25 | 90 | 1); |
| el17cd | 1:044238f7bdda | 91 | } |
| el17cd | 14:885915260e25 | 92 | lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])), |
| el17cd | 14:885915260e25 | 93 | rint(yTo2D(points[0][1], points[0][2])), |
| el17cd | 14:885915260e25 | 94 | rint(xTo2D(points[3][0], points[3][2])), |
| el17cd | 14:885915260e25 | 95 | rint(yTo2D(points[3][1], points[3][2])), |
| el17cd | 14:885915260e25 | 96 | 1); |
| el17cd | 1:044238f7bdda | 97 | } |
| el17cd | 4:759a5c34e239 | 98 | } |
| el17cd | 9:5915fc800824 | 99 | |
| el17cd | 16:64cd7bc094f9 | 100 | void Rasturizer::print(const char *text, int x, int y){ |
| el17cd | 16:64cd7bc094f9 | 101 | lcd.printString(text, x, y); |
| el17cd | 11:2cd6341136ca | 102 | } |
| el17cd | 11:2cd6341136ca | 103 | |
| el17cd | 4:759a5c34e239 | 104 | void Rasturizer::clear(){ |
| el17cd | 4:759a5c34e239 | 105 | lcd.clear(); |
| el17cd | 4:759a5c34e239 | 106 | } |
| el17cd | 4:759a5c34e239 | 107 | |
| el17cd | 4:759a5c34e239 | 108 | void Rasturizer::refresh(){ |
| el17cd | 4:759a5c34e239 | 109 | lcd.refresh(); |
| el17cd | 14:885915260e25 | 110 | } |
| el17cd | 14:885915260e25 | 111 | |
| el17cd | 16:64cd7bc094f9 | 112 | void Rasturizer::drawDeathScreen(bool selection){ |
| el17cd | 16:64cd7bc094f9 | 113 | int x; |
| el17cd | 16:64cd7bc094f9 | 114 | int y; |
| el17cd | 16:64cd7bc094f9 | 115 | int z; |
| el17cd | 16:64cd7bc094f9 | 116 | |
| el17cd | 16:64cd7bc094f9 | 117 | if(selection == true){ |
| el17cd | 16:64cd7bc094f9 | 118 | x = -30; |
| el17cd | 16:64cd7bc094f9 | 119 | y = -3; |
| el17cd | 16:64cd7bc094f9 | 120 | z = 50; |
| el17cd | 16:64cd7bc094f9 | 121 | } |
| el17cd | 16:64cd7bc094f9 | 122 | else{ |
| el17cd | 16:64cd7bc094f9 | 123 | x = -30; |
| el17cd | 16:64cd7bc094f9 | 124 | y = 15; |
| el17cd | 16:64cd7bc094f9 | 125 | z = 50; |
| el17cd | 16:64cd7bc094f9 | 126 | } |
| el17cd | 16:64cd7bc094f9 | 127 | |
| el17cd | 16:64cd7bc094f9 | 128 | lcd.drawRect(24, 14, 45, 11, FILL_WHITE); |
| el17cd | 16:64cd7bc094f9 | 129 | lcd.drawRect(24, 14, 45, 11, FILL_TRANSPARENT); |
| el17cd | 16:64cd7bc094f9 | 130 | lcd.printString("Restart",26,2); |
| el17cd | 16:64cd7bc094f9 | 131 | lcd.drawRect(24, 30, 45, 11, FILL_WHITE); |
| el17cd | 16:64cd7bc094f9 | 132 | lcd.drawRect(24, 30, 45, 11, FILL_TRANSPARENT); |
| el17cd | 16:64cd7bc094f9 | 133 | lcd.printString("Menu",35,4); |
| el17cd | 16:64cd7bc094f9 | 134 | selectionCube.translate(x, y, z); |
| el17cd | 16:64cd7bc094f9 | 135 | for(int i = 0; i < 6; i++){ |
| el17cd | 16:64cd7bc094f9 | 136 | drawFace(selectionCube.getFace(i), 0); |
| el17cd | 16:64cd7bc094f9 | 137 | } |
| el17cd | 16:64cd7bc094f9 | 138 | selectionCube.rotateX(0.1); |
| el17cd | 16:64cd7bc094f9 | 139 | selectionCube.rotateY(0.05); |
| el17cd | 16:64cd7bc094f9 | 140 | selectionCube.rotateZ(-0.08); |
| el17cd | 16:64cd7bc094f9 | 141 | selectionCube.translate(-x, -y, -z); |
| el17cd | 1:044238f7bdda | 142 | } |