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@20:3ca430241df0, 2019-04-01 (annotated)
- Committer:
- el17cd
- Date:
- Mon Apr 01 17:11:40 2019 +0000
- Revision:
- 20:3ca430241df0
- Parent:
- 19:ec4cb22accb0
- Child:
- 21:6b5d2d75e083
Made renderer sort all faces by z axis and draw instead of being done in game class
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 | 20:3ca430241df0 | 7 | for(int i = 0; i < 6; i++){ |
| el17cd | 20:3ca430241df0 | 8 | selectionCube.getFace(i).setVisible(true); |
| el17cd | 20:3ca430241df0 | 9 | } |
| el17cd | 20:3ca430241df0 | 10 | } |
| el17cd | 15:8fbbdefbe720 | 11 | |
| el17cd | 14:885915260e25 | 12 | |
| el17cd | 15:8fbbdefbe720 | 13 | void Rasturizer::init(){ |
| el17cd | 15:8fbbdefbe720 | 14 | lcd.init(); |
| el17cd | 15:8fbbdefbe720 | 15 | fov = 50; |
| el17cd | 15:8fbbdefbe720 | 16 | } |
| el17cd | 15:8fbbdefbe720 | 17 | |
| el17cd | 17:3c9672c6e532 | 18 | float Rasturizer::xTo2D(float x, float z){ |
| el17cd | 14:885915260e25 | 19 | return x * (fov/z) + 42; |
| el17cd | 14:885915260e25 | 20 | } |
| el17cd | 14:885915260e25 | 21 | |
| el17cd | 17:3c9672c6e532 | 22 | float Rasturizer::yTo2D(float y, float z){ |
| el17cd | 14:885915260e25 | 23 | return y * (fov/z) + 21; |
| el17cd | 4:759a5c34e239 | 24 | } |
| el17cd | 1:044238f7bdda | 25 | |
| el17cd | 17:3c9672c6e532 | 26 | void Rasturizer::drawHorizon(float angle){ |
| el17cd | 11:2cd6341136ca | 27 | lcd.drawLine(0, |
| el17cd | 12:b69657862610 | 28 | 21-rint(angle*50), |
| el17cd | 11:2cd6341136ca | 29 | 84, |
| el17cd | 12:b69657862610 | 30 | 21+rint(angle*50), |
| el17cd | 20:3ca430241df0 | 31 | 1); |
| el17cd | 11:2cd6341136ca | 32 | } |
| el17cd | 11:2cd6341136ca | 33 | |
| el17cd | 17:3c9672c6e532 | 34 | void Rasturizer::drawFace(Face face, float angle){ |
| el17cd | 17:3c9672c6e532 | 35 | float points[4][3]; |
| el17cd | 4:759a5c34e239 | 36 | for(int vertex = 0; vertex < 4; vertex++){ |
| el17cd | 4:759a5c34e239 | 37 | for(int axis = 0; axis < 3; axis++){ |
| el17cd | 4:759a5c34e239 | 38 | points[vertex][axis] = face.getVertexValue(vertex, axis); |
| el17cd | 4:759a5c34e239 | 39 | } |
| el17cd | 17:3c9672c6e532 | 40 | float y = points[vertex][1]; |
| el17cd | 17:3c9672c6e532 | 41 | float x = points[vertex][0]; |
| el17cd | 11:2cd6341136ca | 42 | |
| el17cd | 11:2cd6341136ca | 43 | points[vertex][0] = x*cos(angle)-y*sin(angle); |
| el17cd | 11:2cd6341136ca | 44 | points[vertex][1] = y*cos(angle)+x*sin(angle); |
| el17cd | 4:759a5c34e239 | 45 | } |
| el17cd | 6:75031d14fc0d | 46 | |
| el17cd | 19:ec4cb22accb0 | 47 | if (checkOnScreen(points) && face.getVisible()){ |
| el17cd | 14:885915260e25 | 48 | int diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]); |
| el17cd | 14:885915260e25 | 49 | int diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]); |
| el17cd | 14:885915260e25 | 50 | int diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]); |
| el17cd | 14:885915260e25 | 51 | int diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]); |
| el17cd | 8:a667bc5050c1 | 52 | |
| el17cd | 17:3c9672c6e532 | 53 | float step = (float)diffY2/(float)diffX2; |
| el17cd | 17:3c9672c6e532 | 54 | float stepSmall1 = (float)diffX1/(float)diffX2; |
| el17cd | 17:3c9672c6e532 | 55 | float stepSmall2 = (float)diffY1/(float)diffX2; |
| el17cd | 9:5915fc800824 | 56 | |
| el17cd | 12:b69657862610 | 57 | for(int s = 0; s< abs(diffX2); s++){ |
| el17cd | 14:885915260e25 | 58 | if(diffX2 > 0){ |
| el17cd | 14:885915260e25 | 59 | 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 | 60 | rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s), |
| el17cd | 14:885915260e25 | 61 | rint(xTo2D(points[3][0], points[3][2])+s), |
| el17cd | 14:885915260e25 | 62 | rint(yTo2D(points[3][1], points[3][2])+step*s), |
| el17cd | 14:885915260e25 | 63 | 0); |
| el17cd | 14:885915260e25 | 64 | } |
| el17cd | 14:885915260e25 | 65 | else{ |
| el17cd | 14:885915260e25 | 66 | 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 | 67 | rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s), |
| el17cd | 14:885915260e25 | 68 | rint(xTo2D(points[3][0], points[3][2])-s), |
| el17cd | 14:885915260e25 | 69 | rint(yTo2D(points[3][1], points[3][2])-step*s), |
| el17cd | 14:885915260e25 | 70 | 0); |
| el17cd | 14:885915260e25 | 71 | } |
| el17cd | 7:15543cb10a14 | 72 | } |
| el17cd | 13:f4de03202477 | 73 | } |
| el17cd | 13:f4de03202477 | 74 | if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){ |
| el17cd | 14:885915260e25 | 75 | for (int i = 0; i < 3; i++){ |
| el17cd | 14:885915260e25 | 76 | lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])), |
| el17cd | 14:885915260e25 | 77 | rint(yTo2D(points[i][1], points[i][2])), |
| el17cd | 14:885915260e25 | 78 | rint(xTo2D(points[i+1][0], points[i+1][2])), |
| el17cd | 14:885915260e25 | 79 | rint(yTo2D(points[i+1][1], points[i+1][2])), |
| el17cd | 14:885915260e25 | 80 | 1); |
| el17cd | 1:044238f7bdda | 81 | } |
| el17cd | 14:885915260e25 | 82 | lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])), |
| el17cd | 14:885915260e25 | 83 | rint(yTo2D(points[0][1], points[0][2])), |
| el17cd | 14:885915260e25 | 84 | rint(xTo2D(points[3][0], points[3][2])), |
| el17cd | 14:885915260e25 | 85 | rint(yTo2D(points[3][1], points[3][2])), |
| el17cd | 14:885915260e25 | 86 | 1); |
| el17cd | 1:044238f7bdda | 87 | } |
| el17cd | 4:759a5c34e239 | 88 | } |
| el17cd | 20:3ca430241df0 | 89 | void Rasturizer::drawAllFaces(Face faceArray[], int noOfCubes, float angle){ |
| el17cd | 20:3ca430241df0 | 90 | for (int f = 0; f< (noOfCubes*6)-1; f++){ |
| el17cd | 20:3ca430241df0 | 91 | for (int f2 = 0; f2< (noOfCubes*6)-f-1; f2++){ |
| el17cd | 20:3ca430241df0 | 92 | if(faceArray[f2].getAvgZ() < faceArray[f2+1].getAvgZ()){ |
| el17cd | 20:3ca430241df0 | 93 | Face temp = faceArray[f2+1]; |
| el17cd | 20:3ca430241df0 | 94 | faceArray[f2+1] = faceArray[f2]; |
| el17cd | 20:3ca430241df0 | 95 | faceArray[f2] = temp; |
| el17cd | 20:3ca430241df0 | 96 | } |
| el17cd | 20:3ca430241df0 | 97 | } |
| el17cd | 20:3ca430241df0 | 98 | } |
| el17cd | 20:3ca430241df0 | 99 | for (int f = 0; f< noOfCubes*6 ; f++){ |
| el17cd | 20:3ca430241df0 | 100 | drawFace(faceArray[f], angle/15); |
| el17cd | 20:3ca430241df0 | 101 | } |
| el17cd | 20:3ca430241df0 | 102 | } |
| el17cd | 9:5915fc800824 | 103 | |
| el17cd | 19:ec4cb22accb0 | 104 | bool Rasturizer::checkOnScreen(float (&points)[4][3]){ |
| el17cd | 19:ec4cb22accb0 | 105 | if (points[0][2] < 10 || points[1][2] < 10 || points[2][2] < 10 || points[3][2] < 10){ |
| el17cd | 19:ec4cb22accb0 | 106 | return false; |
| el17cd | 19:ec4cb22accb0 | 107 | } |
| el17cd | 19:ec4cb22accb0 | 108 | else if ((xTo2D(points[0][0], points[0][2]) < 0 || xTo2D(points[0][0], points[0][2]) > 84) |
| el17cd | 19:ec4cb22accb0 | 109 | && (xTo2D(points[1][0], points[1][2]) < 0 || xTo2D(points[1][0], points[1][2]) > 84) |
| el17cd | 19:ec4cb22accb0 | 110 | && (xTo2D(points[2][0], points[2][2]) < 0 || xTo2D(points[2][0], points[2][2]) > 84) |
| el17cd | 19:ec4cb22accb0 | 111 | && (xTo2D(points[3][0], points[3][2]) < 0 || xTo2D(points[3][0], points[3][2]) > 84)){ |
| el17cd | 19:ec4cb22accb0 | 112 | return false; |
| el17cd | 19:ec4cb22accb0 | 113 | } |
| el17cd | 19:ec4cb22accb0 | 114 | return true; |
| el17cd | 19:ec4cb22accb0 | 115 | } |
| el17cd | 19:ec4cb22accb0 | 116 | |
| el17cd | 16:64cd7bc094f9 | 117 | void Rasturizer::print(const char *text, int x, int y){ |
| el17cd | 16:64cd7bc094f9 | 118 | lcd.printString(text, x, y); |
| el17cd | 11:2cd6341136ca | 119 | } |
| el17cd | 11:2cd6341136ca | 120 | |
| el17cd | 4:759a5c34e239 | 121 | void Rasturizer::clear(){ |
| el17cd | 4:759a5c34e239 | 122 | lcd.clear(); |
| el17cd | 4:759a5c34e239 | 123 | } |
| el17cd | 4:759a5c34e239 | 124 | |
| el17cd | 4:759a5c34e239 | 125 | void Rasturizer::refresh(){ |
| el17cd | 4:759a5c34e239 | 126 | lcd.refresh(); |
| el17cd | 14:885915260e25 | 127 | } |
| el17cd | 14:885915260e25 | 128 | |
| el17cd | 16:64cd7bc094f9 | 129 | void Rasturizer::drawDeathScreen(bool selection){ |
| el17cd | 20:3ca430241df0 | 130 | Face faces[6]; |
| el17cd | 16:64cd7bc094f9 | 131 | int x; |
| el17cd | 16:64cd7bc094f9 | 132 | int y; |
| el17cd | 16:64cd7bc094f9 | 133 | int z; |
| el17cd | 16:64cd7bc094f9 | 134 | |
| el17cd | 16:64cd7bc094f9 | 135 | if(selection == true){ |
| el17cd | 16:64cd7bc094f9 | 136 | x = -30; |
| el17cd | 16:64cd7bc094f9 | 137 | y = -3; |
| el17cd | 16:64cd7bc094f9 | 138 | z = 50; |
| el17cd | 16:64cd7bc094f9 | 139 | } |
| el17cd | 16:64cd7bc094f9 | 140 | else{ |
| el17cd | 16:64cd7bc094f9 | 141 | x = -30; |
| el17cd | 16:64cd7bc094f9 | 142 | y = 15; |
| el17cd | 16:64cd7bc094f9 | 143 | z = 50; |
| el17cd | 16:64cd7bc094f9 | 144 | } |
| el17cd | 16:64cd7bc094f9 | 145 | |
| el17cd | 16:64cd7bc094f9 | 146 | lcd.drawRect(24, 14, 45, 11, FILL_WHITE); |
| el17cd | 16:64cd7bc094f9 | 147 | lcd.drawRect(24, 14, 45, 11, FILL_TRANSPARENT); |
| el17cd | 16:64cd7bc094f9 | 148 | lcd.printString("Restart",26,2); |
| el17cd | 16:64cd7bc094f9 | 149 | lcd.drawRect(24, 30, 45, 11, FILL_WHITE); |
| el17cd | 16:64cd7bc094f9 | 150 | lcd.drawRect(24, 30, 45, 11, FILL_TRANSPARENT); |
| el17cd | 16:64cd7bc094f9 | 151 | lcd.printString("Menu",35,4); |
| el17cd | 16:64cd7bc094f9 | 152 | selectionCube.translate(x, y, z); |
| el17cd | 16:64cd7bc094f9 | 153 | for(int i = 0; i < 6; i++){ |
| el17cd | 20:3ca430241df0 | 154 | faces[i] = selectionCube.getFace(i); |
| el17cd | 16:64cd7bc094f9 | 155 | } |
| el17cd | 20:3ca430241df0 | 156 | drawAllFaces(faces, 1, 0); |
| el17cd | 16:64cd7bc094f9 | 157 | selectionCube.rotateX(0.1); |
| el17cd | 16:64cd7bc094f9 | 158 | selectionCube.rotateY(0.05); |
| el17cd | 16:64cd7bc094f9 | 159 | selectionCube.rotateZ(-0.08); |
| el17cd | 16:64cd7bc094f9 | 160 | selectionCube.translate(-x, -y, -z); |
| el17cd | 18:8256546a3cbf | 161 | } |
| el17cd | 18:8256546a3cbf | 162 | |
| el17cd | 18:8256546a3cbf | 163 | void Rasturizer::drawHomeScreen(int selection){ |
| el17cd | 20:3ca430241df0 | 164 | Face faces[6]; |
| el17cd | 18:8256546a3cbf | 165 | int x; |
| el17cd | 18:8256546a3cbf | 166 | int y; |
| el17cd | 18:8256546a3cbf | 167 | int z; |
| el17cd | 18:8256546a3cbf | 168 | |
| el17cd | 18:8256546a3cbf | 169 | if(selection == 0){ |
| el17cd | 18:8256546a3cbf | 170 | x = -30; |
| el17cd | 18:8256546a3cbf | 171 | y = -12; |
| el17cd | 18:8256546a3cbf | 172 | z = 50; |
| el17cd | 18:8256546a3cbf | 173 | } |
| el17cd | 18:8256546a3cbf | 174 | else if(selection == 1){ |
| el17cd | 18:8256546a3cbf | 175 | x = -30; |
| el17cd | 18:8256546a3cbf | 176 | y = 5; |
| el17cd | 18:8256546a3cbf | 177 | z = 50; |
| el17cd | 18:8256546a3cbf | 178 | } |
| el17cd | 18:8256546a3cbf | 179 | else{ |
| el17cd | 18:8256546a3cbf | 180 | x = -30; |
| el17cd | 18:8256546a3cbf | 181 | y = 22; |
| el17cd | 18:8256546a3cbf | 182 | z = 50; |
| el17cd | 18:8256546a3cbf | 183 | } |
| el17cd | 18:8256546a3cbf | 184 | |
| el17cd | 18:8256546a3cbf | 185 | lcd.drawRect(24, 6, 45, 10, FILL_WHITE); |
| el17cd | 18:8256546a3cbf | 186 | lcd.printString("Play",35,1); |
| el17cd | 18:8256546a3cbf | 187 | lcd.drawRect(24, 6, 45, 10, FILL_TRANSPARENT); |
| el17cd | 18:8256546a3cbf | 188 | |
| el17cd | 18:8256546a3cbf | 189 | lcd.drawRect(24, 22, 45, 10, FILL_WHITE); |
| el17cd | 18:8256546a3cbf | 190 | lcd.printString("Help",35,3); |
| el17cd | 18:8256546a3cbf | 191 | lcd.drawRect(24, 22, 45, 10, FILL_TRANSPARENT); |
| el17cd | 18:8256546a3cbf | 192 | |
| el17cd | 18:8256546a3cbf | 193 | |
| el17cd | 18:8256546a3cbf | 194 | lcd.drawRect(24, 38, 45, 10, FILL_WHITE); |
| el17cd | 18:8256546a3cbf | 195 | lcd.printString("Quit",35,5); |
| el17cd | 18:8256546a3cbf | 196 | lcd.drawRect(24, 38, 45, 10, FILL_TRANSPARENT); |
| el17cd | 18:8256546a3cbf | 197 | selectionCube.translate(x, y, z); |
| el17cd | 18:8256546a3cbf | 198 | |
| el17cd | 18:8256546a3cbf | 199 | for(int i = 0; i < 6; i++){ |
| el17cd | 20:3ca430241df0 | 200 | faces[i] = selectionCube.getFace(i); |
| el17cd | 18:8256546a3cbf | 201 | } |
| el17cd | 20:3ca430241df0 | 202 | drawAllFaces(faces, 1, 0); |
| el17cd | 18:8256546a3cbf | 203 | selectionCube.rotateX(-0.1); |
| el17cd | 18:8256546a3cbf | 204 | selectionCube.rotateY(-0.05); |
| el17cd | 18:8256546a3cbf | 205 | selectionCube.rotateZ(0.08); |
| el17cd | 18:8256546a3cbf | 206 | selectionCube.translate(-x, -y, -z); |
| el17cd | 1:044238f7bdda | 207 | } |