Advertisement
Guest User

farzana

a guest
Jan 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. void stage3()
  2. {
  3. if (near == far) return;
  4. ifstream stage2;
  5. ofstream stage3;
  6. stage2.open ("stage2.txt");
  7. stage3.open ("stage3.txt");
  8. stage3 << std::fixed;
  9. stage3 << std::setprecision(7);
  10.  
  11. // process input from stage2 and write to stage3
  12. fov_x = fov_y * aspectRatio;
  13. double r, t;
  14. t = near*tan(pi*fov_y/(2*180));
  15. r = near*tan(pi*fov_x/(2*180));
  16.  
  17. matrix proj = matrix::make_identity(4);
  18. proj.values[0][0] = near/r;
  19. proj.values[1][1] = near/t;
  20. proj.values[2][2] = -(far+near)/(far-near);
  21. proj.values[2][3] = -(2*far*near)/(far-near);
  22. proj.values[3][3] = 0;
  23. proj.values[3][2] = -1;
  24.  
  25. int tc = 0;
  26.  
  27. while(tc < triangleCount)
  28. {
  29. tc++;
  30. homogeneous_point input[3];
  31. homogeneous_point output[3];
  32. homogeneous_point outfinal[3];
  33.  
  34. for(int i=0; i<3; i++)
  35. {
  36. double x,y,z;
  37. stage2 >> x >> y >> z;
  38. homogeneous_point h(x,y,z);
  39. input[i]=h;
  40. }
  41.  
  42.  
  43. int oput = 0;
  44. for(int i=0; i<3; i++)
  45. {
  46. homogeneous_point s = input[i];
  47. homogeneous_point p = input[(i+1)%3];
  48.  
  49. if(s.z < -near && p.z < -near)
  50. {
  51. output[oput] = p;
  52. oput++;
  53. }
  54.  
  55. // s inside and p outside
  56. else if(s.z < -near && p.z >= -near)
  57. {
  58. homogeneous_point temp(s.x, s.y, -near);
  59. output[oput] = temp;
  60. oput++;
  61. }
  62.  
  63. // s outside and p inside
  64. else if(s.z >= -near && p.z < -near)
  65. {
  66. homogeneous_point temp(p.x, p.y, -near);
  67. output[oput] = temp;
  68. oput++;
  69. output[oput] = p;
  70. oput++;
  71. }
  72.  
  73. // both outside
  74. else
  75. {
  76.  
  77. }
  78. }
  79.  
  80. //clipping by far plane
  81.  
  82. oput = 0;
  83. for(int i=0; i<3; i++)
  84. {
  85. homogeneous_point s = output[i];
  86. homogeneous_point p = output[(i+1)%3];
  87.  
  88. // both inside the clip region
  89.  
  90. if(s.z >= -far && p.z >= -far)
  91. {
  92. outfinal[oput] = p;
  93. oput++;
  94. }
  95.  
  96. // s inside and p outside
  97. else if(s.z >= -far && p.z < -far)
  98. {
  99. homogeneous_point temp(s.x, s.y, -far);
  100. outfinal[oput] = temp;
  101. oput++;
  102. }
  103.  
  104. // s outside and p inside
  105. else if(s.z < -far && p.z >= -far)
  106. {
  107. homogeneous_point temp(p.x, p.y, -far);
  108. outfinal[oput] = temp;
  109. oput++;
  110. outfinal[oput] = p;
  111. oput++;
  112. }
  113.  
  114. // both outside
  115. else
  116. {
  117.  
  118. }
  119. }
  120. // final output write to stage3 file
  121.  
  122. for(int i=1; i<4; i++)
  123. {
  124. homogeneous_point now = proj*outfinal[i%3];
  125. stage3 << now.x <<" "<<now.y<< " "<<now.z<<" "<<endl;
  126. }
  127. stage3<<endl;
  128. }
  129.  
  130. stage3.close();
  131. stage2.close();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement