Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <glm/glm.hpp>
  3. #include <SDL.h>
  4. #include "SDLauxiliary.h"
  5. #include "TestModel.h"
  6.  
  7.  
  8. using namespace std;
  9. using glm::vec2;
  10. using glm::vec3;
  11. using glm::ivec2;
  12. using glm::mat3;
  13. using glm::max;
  14.  
  15. // ----------------------------------------------------------------------------
  16. // GLOBAL VARIABLES
  17.  
  18. int cc = 0;
  19.  
  20. const int SCREEN_WIDTH = 500;
  21. const int SCREEN_HEIGHT = 500;
  22. SDL_Surface* screen;
  23. int t;
  24. vector<Triangle> triangles;
  25. vec3 cameraPos(0, 0, -3.001);
  26. float f = 500;
  27.  
  28. double yaw = 0;
  29. vec3 c1(cos(yaw), 0, -sin(yaw));
  30. vec3 c2(0, 1, 0);
  31. vec3 c3(sin(yaw), 0, cos(yaw));
  32. glm::mat3 R(c1, c2, c3);
  33.  
  34. float translation = 0.1; // use this to set translation increment
  35.  
  36. const float PI = 3.1415927;
  37. vec3 currentColor;
  38. float depthBuffer[SCREEN_HEIGHT][SCREEN_WIDTH];
  39.  
  40. // ----------------------------------------------------------------------------
  41. // STUCTURES
  42.  
  43. struct Pixel
  44. {
  45. int x;
  46. int y;
  47. float zinv;
  48. }pixel;
  49.  
  50. // ----------------------------------------------------------------------------
  51. // FUNCTIONS
  52.  
  53. void Update();
  54. void Draw();
  55. void VertexShader(const vec3& v, Pixel& p);
  56. void Interpolate(ivec2 a, ivec2 b, vector<ivec2>& result);
  57. void DrawLineSDL(SDL_Surface* surface, ivec2 a, ivec2 b, vec3 color);
  58. void DrawPolygonEdges(const vector<vec3>& vertices);
  59. void ComputePolygonRows(const vector<Pixel>& vertexPixels, vector<Pixel>& leftPixels, vector<Pixel>& rightPixels);
  60. void DrawPolygonRows(const vector<Pixel>& leftPixels, const vector<Pixel>& rightPixels);
  61. void DrawPolygon(const vector<vec3>& vertices);
  62. void Interpolate2(Pixel a, Pixel b, vector<Pixel>& result);
  63.  
  64.  
  65. int main(int argc, char* argv[])
  66. {
  67. LoadTestModel(triangles);
  68. screen = InitializeSDL(SCREEN_WIDTH, SCREEN_HEIGHT);
  69. t = SDL_GetTicks(); // Set start value for timer.
  70.  
  71. while (NoQuitMessageSDL())
  72. {
  73. Draw();
  74. }
  75.  
  76. //Draw();
  77. //cin.get();
  78.  
  79. SDL_SaveBMP(screen, "screenshot.bmp");
  80. return 0;
  81. }
  82.  
  83.  
  84. void Draw()
  85. {
  86. SDL_FillRect(screen, 0, 0);
  87.  
  88. if (SDL_MUSTLOCK(screen))
  89. SDL_LockSurface(screen);
  90.  
  91. for (int y = 0; y<SCREEN_HEIGHT; ++y)
  92. for (int x = 0; x<SCREEN_WIDTH; ++x)
  93. depthBuffer[y][x] = 0;
  94.  
  95. for (int i = 0; i<triangles.size(); ++i)
  96. {
  97. currentColor = triangles[i].color;
  98. vector<vec3> vertices(3);
  99. int aa = 24;
  100. vertices[0] = triangles[i].v0;
  101. vertices[1] = triangles[i].v1;
  102. vertices[2] = triangles[i].v2;
  103. DrawPolygon(vertices);
  104. }
  105.  
  106. if (SDL_MUSTLOCK(screen))
  107. SDL_UnlockSurface(screen);
  108.  
  109. SDL_UpdateRect(screen, 0, 0, 0, 0);
  110. }
  111.  
  112. void VertexShader(const vec3& v, Pixel& p)
  113. {
  114. vec3 vPrime = (v - cameraPos)*R;
  115. pixel.zinv = 1 / vPrime.z;
  116. p.x = f * vPrime.x / vPrime.z + SCREEN_WIDTH / 2;
  117. p.y = f * vPrime.y / vPrime.z + SCREEN_HEIGHT / 2;
  118. //cout << p.x << " this is it " << p.y << endl;
  119. depthBuffer[p.x][p.y] = pixel.zinv;
  120. }
  121.  
  122. void ComputePolygonRows(const vector<Pixel>& vertexPixels,
  123. vector<Pixel>& leftPixels, vector<Pixel>& rightPixels)
  124. {
  125. // Find y-min,max for the 3 vertices
  126. vec3 vp(vertexPixels[0].y, vertexPixels[1].y, vertexPixels[2].y);
  127. Pixel start; Pixel end; Pixel middle;
  128. int yMin = 1000;
  129. int yMax = -1000;
  130. int w=0; int s=0;
  131. for (int k = 0; k < vertexPixels.size(); ++k)
  132. {
  133. if (vp[k] <= yMin)
  134. {
  135. yMin = vp[k];
  136. end = vertexPixels[k];
  137. w = k;
  138. }
  139. }
  140. for (int k = 0; k < vertexPixels.size(); ++k)
  141. {
  142. if (vp[k] >= yMax)
  143. {
  144. yMax = vp[k];
  145. start = vertexPixels[k];
  146. s = k;
  147. }
  148. }
  149. for (int k = 0; k < vertexPixels.size(); ++k)
  150. {
  151. if (vertexPixels[k].y != start.y
  152. && vertexPixels[k].y != end.y)
  153. {
  154. middle = vertexPixels[k];
  155. }
  156. if (w!= k && s!= k)
  157. {
  158. middle = vertexPixels[k];
  159. }
  160. }
  161.  
  162. int ROWS = yMax - yMin + 1;
  163.  
  164. leftPixels.resize(ROWS);
  165. rightPixels.resize(ROWS);
  166.  
  167. for (int i = 0; i<ROWS; ++i)
  168. {
  169. leftPixels[i].x = +numeric_limits<int>::max();
  170. rightPixels[i].x = -numeric_limits<int>::max();
  171. }
  172.  
  173. int pixels1 = glm::abs(start.y - end.y) + 1;
  174. vector<Pixel> line1(pixels1);
  175. Interpolate2(end, start, line1);
  176.  
  177. int pixels2 = glm::abs(end.y - middle.y) + 1;
  178. vector<Pixel> line2(pixels2);
  179. Interpolate2(end, middle, line2);
  180.  
  181. int pixels3 = glm::abs(middle.y - start.y) + 1;
  182. vector<Pixel> line3(pixels3);
  183. Interpolate2(middle, start, line3);
  184.  
  185. vector<Pixel> side1(ROWS);
  186. for (int i = 0; i < line2.size(); ++i)
  187. {
  188. side1[i] = line2[i];
  189. }
  190. for (int i = 0; i < line3.size(); ++i)
  191. {
  192. side1[line2.size()+i-1] = line3[i];
  193.  
  194. }
  195.  
  196. for (int i = 0; i < ROWS; ++i)
  197. {
  198. if (line1[i].x < leftPixels[i].x)
  199. {
  200. leftPixels[i] = line1[i];
  201. }
  202. if (line1[i].x > rightPixels[i].x)
  203. {
  204. rightPixels[i] = line1[i];
  205. }
  206. if (side1[i].x < leftPixels[i].x)
  207. {
  208. leftPixels[i] = side1[i];
  209. }
  210. if (side1[i].x > rightPixels[i].x)
  211. {
  212. rightPixels[i] = side1[i];
  213. }
  214. }
  215.  
  216. }
  217.  
  218. void DrawPolygonRows(const vector<Pixel>& leftPixels, const vector<Pixel>& rightPixels)
  219. {
  220. //cout << cc++ << endl;
  221. for (int k = 0; k < leftPixels.size(); ++k)
  222. {
  223. int pixels = glm::abs(leftPixels[k].x - rightPixels[k].x) + 1;
  224. vector<Pixel> row(pixels);
  225. Interpolate2(leftPixels[k], rightPixels[k], row);
  226. for (int i = 0; i < pixels; ++i)
  227. {
  228. if (depthBuffer[row[i].x][row[i].y] > row[i].zinv)
  229. {
  230. PutPixelSDL(screen, row[i].x, row[i].y, currentColor);
  231. depthBuffer[row[i].x][row[i].y] = row[i].zinv;
  232. }
  233. }
  234. }
  235. }
  236.  
  237. void DrawPolygon(const vector<vec3>& vertices)
  238. {
  239. int V = vertices.size();
  240. vector<Pixel> vertexPixels(V);
  241. for (int i = 0; i<V; ++i)
  242. VertexShader(vertices[i], vertexPixels[i]);
  243. vector<Pixel> leftPixels;
  244. vector<Pixel> rightPixels;
  245. ComputePolygonRows(vertexPixels, leftPixels, rightPixels);
  246. DrawPolygonRows(leftPixels, rightPixels);
  247. }
  248.  
  249. void Interpolate2(Pixel a, Pixel b, vector<Pixel>& result)
  250. {
  251. int N = result.size();
  252. float stepx = (b.x - a.x) / float(glm::max(N - 1, 1));
  253. float stepy = (b.y - a.y) / float(glm::max(N - 1, 1));
  254. float stepz = (b.zinv - a.zinv) / float(glm::max(N - 1, 1));
  255. float currentx = a.x;
  256. float currenty = a.y;
  257. float currentz = a.zinv;
  258. for (int i = 0; i<N; ++i)
  259. {
  260. result[i].x = currentx;
  261. result[i].y = currenty;
  262. result[i].zinv = currentz;
  263. float currentx = a.x;
  264. float currenty = a.y;
  265. float currentz = a.zinv;
  266. currentx += stepx;
  267. currenty += stepy;
  268. currentz += stepz;
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement