PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/example_0.cpp

https://github.com/ksimons221/CS-184-Project1
C++ | 489 lines | 293 code | 144 blank | 52 comment | 50 complexity | d9a94ab5624f42c4ad53c5769a748663 MD5 | raw file
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cmath>
  5. #include <string>
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #else
  9. #include <sys/time.h>
  10. #endif
  11. #ifdef OSX
  12. #include <GLUT/glut.h>
  13. #include <OpenGL/glu.h>
  14. #else
  15. #include <GL/glut.h>
  16. #include <GL/glu.h>
  17. #endif
  18. #include <time.h>
  19. #include <math.h>
  20. #include <GL/glut.h>
  21. #define PI 3.14159265 // Should be used from mathlib
  22. inline float sqr(float x) { return x*x; }
  23. using namespace std;
  24. struct lightSource
  25. {
  26. float xValue;
  27. float yValue;
  28. float zValue;
  29. float redValue;
  30. float greenValue;
  31. float blueValue;
  32. };
  33. float centerXSphere = 0;
  34. float centerYSphere = 0;
  35. float centerZSphere = 0;
  36. float kaR = 0;
  37. float kaG = 0;
  38. float kaB = 0;
  39. float kdR = 0;
  40. float kdG = 0;
  41. float kdB = 0;
  42. float ksR = 0;
  43. float ksG = 0;
  44. float ksB = 0;
  45. float powerCoefficient = 0;
  46. int numPointLights = 0;
  47. int numDirectionalLights = 0;
  48. lightSource pointLights[5];
  49. lightSource directionalLights[5];
  50. //****************************************************
  51. // Some Classes
  52. //****************************************************
  53. class Viewport;
  54. class Viewport {
  55. public:
  56. int w, h; // width and height
  57. };
  58. //****************************************************
  59. // Global Variables
  60. //****************************************************
  61. Viewport viewport;
  62. //****************************************************
  63. // Simple init function
  64. //****************************************************
  65. void initScene(){
  66. // Nothing to do here for this simple example.
  67. }
  68. //****************************************************
  69. // reshape viewport if the window is resized
  70. //****************************************************
  71. void myReshape(int w, int h) {
  72. viewport.w = w;
  73. viewport.h = h;
  74. glViewport (0,0,viewport.w,viewport.h);
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77. gluOrtho2D(0, viewport.w, 0, viewport.h);
  78. }
  79. //****************************************************
  80. // A routine to set a pixel by drawing a GL point. This is not a
  81. // general purpose routine as it assumes a lot of stuff specific to
  82. // this example.
  83. //****************************************************
  84. void setPixel(int x, int y, GLfloat r, GLfloat g, GLfloat b) {
  85. glColor3f(r, g, b);
  86. glVertex2f(x + 0.5, y + 0.5); // The 0.5 is to target pixel
  87. // centers
  88. // Note: Need to check for gap
  89. // bug on inst machines.
  90. }
  91. //****************************************************
  92. // Draw a filled circle.
  93. //****************************************************
  94. void circle(float centerX, float centerY, float radius) {
  95. // Draw inner circle
  96. glBegin(GL_POINTS);
  97. // We could eliminate wasted work by only looping over the pixels
  98. // inside the sphere's radius. But the example is more clear this
  99. // way. In general drawing an object by loopig over the whole
  100. // screen is wasteful.
  101. int iStep,jStep; // Pixel indices
  102. int minI = max(0,(int)floor(centerX-radius));
  103. int maxI = min(viewport.w-1,(int)ceil(centerX+radius));
  104. int minJ = max(0,(int)floor(centerY-radius));
  105. int maxJ = min(viewport.h-1,(int)ceil(centerY+radius));
  106. for (iStep=0;iStep<viewport.w;iStep++) {
  107. for (jStep=0;jStep<viewport.h;jStep++) {
  108. // Location of the center of pixel relative to center of sphere
  109. float x = (iStep+0.5-centerX);
  110. float y = (jStep+0.5-centerY);
  111. float dist = sqrt(sqr(x) + sqr(y));
  112. if (dist<=radius) {
  113. // This is the front-facing Z coordinate
  114. float z = sqrt(radius*radius-dist*dist);
  115. //Normal
  116. float normalX = x / sqrt(sqr(x)+sqr(y)+sqr(z));
  117. float normalY = y / sqrt(sqr(x)+sqr(y)+sqr(z));
  118. float normalZ = z / sqrt(sqr(x)+sqr(y)+sqr(z));
  119. //Specular
  120. float specularRed = 0;
  121. float specularGreen = 0;
  122. float specularBlue = 0;
  123. for (int i = 0; i< numPointLights; i++) {
  124. float pointLightX = (pointLights[i].xValue * radius) -x;
  125. float pointLightY = (pointLights[i].yValue * radius)- y;
  126. float pointLightZ = (pointLights[i].zValue * radius)- z;
  127. float pointLightTotal = sqrt(sqr(pointLightX)+sqr(pointLightY)+sqr(pointLightZ));
  128. pointLightX = pointLightX / pointLightTotal;
  129. pointLightY = pointLightY / pointLightTotal;
  130. pointLightZ = pointLightZ / pointLightTotal;
  131. float dotProductResult = pointLightX * normalX + pointLightY * normalY + pointLightZ * normalZ;
  132. float scaledNormalX = dotProductResult*2*normalX;
  133. float scaledNormalY = dotProductResult*2*normalY;
  134. float scaledNormalZ = dotProductResult*2*normalZ;
  135. float reflectiveX = scaledNormalX + (pointLightX * -1);
  136. float reflectiveY = scaledNormalY + (pointLightY * -1);
  137. float reflectiveZ = scaledNormalZ + (pointLightZ * -1);
  138. // assume view = (0,0,1)
  139. float dotOfReflectiveViewer= pow(max(reflectiveZ , 0), powerCoefficient);
  140. specularRed = specularRed + pointLights[i].redValue * ksR * dotOfReflectiveViewer;
  141. specularGreen = specularGreen+ pointLights[i].greenValue * ksG * dotOfReflectiveViewer;
  142. specularBlue = specularBlue+ pointLights[i].blueValue * ksB * dotOfReflectiveViewer;
  143. }
  144. // Directional
  145. for (int i = 0; i< numDirectionalLights; i++) {
  146. float directionLightX = centerXSphere - directionalLights[i].xValue;
  147. float directionLightY = centerYSphere - directionalLights[i].yValue;
  148. float directionLightZ = centerZSphere - directionalLights[i].zValue;
  149. float directionLightTotal = sqrt(sqr(directionLightX)+sqr(directionLightY)+sqr(directionLightZ));
  150. directionLightX = directionLightX / directionLightTotal;
  151. directionLightY = directionLightY / directionLightTotal;
  152. directionLightZ = directionLightZ / directionLightTotal;
  153. float dotProductResult = directionLightX * normalX + directionLightY * normalY + directionLightZ * normalZ;
  154. float scaledNormalX = dotProductResult*2*normalX;
  155. float scaledNormalY = dotProductResult*2*normalY;
  156. float scaledNormalZ = dotProductResult*2*normalZ;
  157. float reflectiveX = scaledNormalX + (directionLightX * -1);
  158. float reflectiveY = scaledNormalY + (directionLightY * -1);
  159. float reflectiveZ = scaledNormalZ + (directionLightZ * -1);
  160. // assume view = (0,0,1)
  161. float dotOfReflectiveViewer= pow(max(reflectiveZ , 0), powerCoefficient);
  162. specularRed = specularRed + directionalLights[i].redValue * ksR * dotOfReflectiveViewer;
  163. specularGreen = specularGreen+ directionalLights[i].greenValue * ksG * dotOfReflectiveViewer;
  164. specularBlue = specularBlue+ directionalLights[i].blueValue * ksB * dotOfReflectiveViewer;
  165. }
  166. /// Diffuse
  167. float diffuseRed = 0;
  168. float diffuseGreen = 0;
  169. float diffuseBlue = 0;
  170. /// Point Source
  171. for (int i = 0; i< numPointLights; i++) {
  172. float pointLightX = (pointLights[i].xValue * radius) -x;
  173. float pointLightY = (pointLights[i].yValue * radius)- y;
  174. float pointLightZ = (pointLights[i].zValue * radius)- z;
  175. float pointLightTotal = sqrt(sqr(pointLightX)+sqr(pointLightY)+sqr(pointLightZ));
  176. pointLightX = pointLightX / pointLightTotal;
  177. pointLightY = pointLightY / pointLightTotal;
  178. pointLightZ = pointLightZ / pointLightTotal;
  179. float dotProductResult = max(0, pointLightX * normalX + pointLightY * normalY + pointLightZ * normalZ);
  180. diffuseRed = diffuseRed + pointLights[i].redValue * kdR * dotProductResult;
  181. diffuseGreen = diffuseGreen+ pointLights[i].greenValue * kdG * dotProductResult;
  182. diffuseBlue = diffuseBlue+ pointLights[i].blueValue * kdB * dotProductResult;
  183. }
  184. //Directional
  185. for (int i = 0; i< numDirectionalLights; i++) {
  186. float directionLightX = centerXSphere - directionalLights[i].xValue;
  187. float directionLightY = centerYSphere - directionalLights[i].yValue;
  188. float directionLightZ = centerZSphere - directionalLights[i].zValue;
  189. float directionLightTotal = sqrt(sqr(directionLightX)+sqr(directionLightY)+sqr(directionLightZ));
  190. directionLightX = directionLightX / directionLightTotal;
  191. directionLightY = directionLightY / directionLightTotal;
  192. directionLightZ = directionLightZ / directionLightTotal;
  193. float dotProductResult = max(0, directionLightX * normalX + directionLightY * normalY + directionLightZ * normalZ);
  194. diffuseRed = diffuseRed + directionalLights[i].redValue * kdR * dotProductResult;
  195. diffuseGreen = diffuseGreen+ directionalLights[i].greenValue * kdG * dotProductResult;
  196. diffuseBlue = diffuseBlue+ directionalLights[i].blueValue * kdB * dotProductResult;
  197. }
  198. //// Ambient Term
  199. float ambientRed = 0;
  200. float ambientGreen = 0;
  201. float ambientBlue = 0;
  202. for (int i = 0; i< numPointLights; i++) {
  203. ambientRed = ambientRed + ( kaR * pointLights[i].redValue);
  204. ambientGreen = ambientGreen+ (kaG * pointLights[i].greenValue);
  205. ambientBlue = ambientBlue +(kaB * pointLights[i].blueValue);
  206. }
  207. for (int i = 0; i< numDirectionalLights; i++) {
  208. ambientRed = ambientRed + ( kaR * directionalLights[i].redValue);
  209. ambientGreen = ambientGreen+ (kaG * directionalLights[i].greenValue);
  210. ambientBlue = ambientBlue +(kaB * directionalLights[i].blueValue);
  211. }
  212. setPixel(iStep,jStep, (specularRed+ambientRed+diffuseRed)/255,(specularGreen+ambientGreen+diffuseGreen)/255, (specularBlue+ambientBlue+diffuseBlue)/255);
  213. }
  214. }
  215. }
  216. glEnd();
  217. }
  218. //****************************************************
  219. // function that does the actual drawing of stuff
  220. //***************************************************
  221. void myDisplay() {
  222. glClear(GL_COLOR_BUFFER_BIT); // clear the color buffer
  223. glMatrixMode(GL_MODELVIEW); // indicate we are specifying camera transformations
  224. glLoadIdentity(); // make sure transformation is "zero'd"
  225. // Start drawing
  226. circle(viewport.w / 2.0 , viewport.h / 2.0 , min(viewport.w, viewport.h) / 3.0);
  227. glFlush();
  228. glutSwapBuffers(); // swap buffers (we earlier set double buffer)
  229. }
  230. void keyPressed (unsigned char key, int x, int y) {
  231. if (key == ' ') {
  232. exit(0);
  233. }
  234. }
  235. //****************************************************
  236. // the usual stuff, nothing exciting here
  237. //****************************************************
  238. int main(int argc, char *argv[]) { // first argument is the program running
  239. //This initializes glut
  240. glutInit(&argc, argv);
  241. int i = 1;
  242. while (i < argc) {
  243. string lineArg = argv [i];
  244. if (lineArg == "-ka" || lineArg == "-kd" || lineArg == "-ks") {
  245. if (i + 3 < argc == false) {
  246. cout << "Badly formatted command line arguments for ka, kd, or ks" << endl;
  247. break;
  248. }
  249. i++;
  250. string redString = argv [i];
  251. float redValue = 255* (float)atof(redString.c_str());
  252. i++;
  253. string greenString = argv [i];
  254. float greenValue = 255* (float)atof(greenString.c_str());
  255. i++;
  256. string blueString = argv [i];
  257. float blueValue = 255* (float)atof(blueString.c_str());
  258. if (redValue < 0 || redValue > 255 || greenValue < 0 || greenValue > 255 || blueValue < 0 || blueValue > 255) {
  259. cout << "Badly formatted command line arguments. ka, kd, ks value not within range" << endl;
  260. break;
  261. }
  262. if (lineArg == "-ka") {
  263. kaR = redValue;
  264. kaG = greenValue;
  265. kaB = blueValue;
  266. } else if ( lineArg == "-kd") {
  267. kdR = redValue;
  268. kdG = greenValue;
  269. kdB = blueValue;
  270. } else { //-ks
  271. ksR = redValue;
  272. ksG = greenValue;
  273. ksB = blueValue;
  274. }
  275. } else if (lineArg == "-sp") {
  276. if (i + 1 < argc == false) {
  277. cout << "Badly formatted command line arguments for sp" << endl;
  278. break;
  279. }
  280. i++;
  281. string powerString = argv [i];
  282. float powerValue = (float)atof(powerString.c_str());
  283. powerCoefficient = powerValue;
  284. } else if (lineArg == "-pl" || lineArg == "-dl" ) {
  285. if (i + 6 < argc == false) {
  286. cout << "Badly formatted command line arguments for pl or dl" << endl;
  287. break;
  288. }
  289. i++;
  290. string xString = argv [i];
  291. float xValue = (float)atof(xString.c_str());
  292. i++;
  293. string yString = argv [i];
  294. float yValue = (float)atof(yString.c_str());
  295. i++;
  296. string zString = argv [i];
  297. float zValue = (float)atof(zString.c_str());
  298. i++;
  299. string redString = argv [i];
  300. float redValue = (float)atof(redString.c_str());
  301. i++;
  302. string greenString = argv [i];
  303. float greenValue = (float)atof(greenString.c_str());
  304. i++;
  305. string blueString = argv [i];
  306. float blueValue = (float)atof(blueString.c_str());
  307. lightSource s = {xValue, yValue, zValue, redValue, greenValue, blueValue};
  308. if (lineArg == "-pl") {
  309. if (numPointLights > 4) {
  310. cout << "Badly formatted command line arguments. " << endl;
  311. break;
  312. }
  313. pointLights[numPointLights] = s;
  314. numPointLights++;
  315. } else { //-dl
  316. if (numDirectionalLights > 4) {
  317. cout << "Badly formatted command line arguments. " << endl;
  318. break;
  319. }
  320. directionalLights[numDirectionalLights] = s;
  321. numDirectionalLights++;
  322. }
  323. } else {
  324. cout << "Badly formatted command line arguments. " << endl;
  325. break;
  326. }
  327. i++;
  328. }
  329. //This tells glut to use a double-buffered window with red, green, and blue channels
  330. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  331. // Initalize theviewport size
  332. viewport.w = 400;
  333. viewport.h = 400;
  334. //The size and position of the window
  335. glutInitWindowSize(viewport.w, viewport.h);
  336. glutInitWindowPosition(0,0);
  337. glutCreateWindow(argv[0]);
  338. glutKeyboardFunc(keyPressed);
  339. initScene(); // quick function to set up scene
  340. glutDisplayFunc(myDisplay); // function to run when its time to draw something
  341. glutReshapeFunc(myReshape); // function to run when the window gets resized
  342. glutMainLoop(); // infinite loop that will keep drawing and resizing
  343. // and whatever else
  344. return 0;
  345. }