Guest User

Untitled

a guest
Jun 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.98 KB | None | 0 0
  1. //--------------------------------------------------------------------------------------------
  2. // File: MyCode.cpp
  3. // Version: V2.0
  4. // Author: Daniel Rhodes
  5. // Description: Your code goes here
  6. // Notes: For use with OpenGl 2.0 / DirectX9.0c or higher
  7. //--------------------------------------------------------------------------------------------
  8.  
  9. // System header files
  10. #include <math.h>
  11.  
  12. // Windows header files
  13. // --
  14.  
  15. // Custom header files
  16. #include "MyCode.h"
  17. #include "resource.h"
  18. extern HINSTANCE g_hInstance;
  19. // For dynamic keyboard control
  20. int m_iMoveX;
  21. int m_iMoveY;
  22. int m_iMoveZ;
  23. float m_dThetaX;
  24. float m_dThetaY;
  25. float m_dThetaZ;
  26. double m_dViewAngle;
  27. //debugging control variables
  28. int iFaceSelection; // FaceSelection > 0: enables one face (with index selecton - 1)
  29. // to be drawn on its own to aid debugging.
  30. bool bDebug; // Switches on debug output to file when true.
  31. // number of polygons read in
  32. int m_iNumOfPolys;
  33. //Frame Count
  34. int m_iOurFrameCount;
  35. // Input filename
  36. char m_sFilename[30];
  37. // Viewpoint data, use for parts B/C
  38. VECTOR m_vDisp, m_vLight; // Displacement of object coordinates relative
  39. // to viewing coordinates and light direction
  40. //Debug File Handle
  41. FILE *debugfile;
  42.  
  43.  
  44.  
  45. // Drawing area dimensions: width and height
  46. int m_iWidth;
  47. int m_iHeight;
  48.  
  49. //Drawing Surface Handle
  50. float *m_fDrawingSurface;
  51. // Database data, for the polygons
  52. POLYGON polylist[2000]; // Array to store a list of polygons.
  53.  
  54. COLOUR Default_Colour={0.5,0.5,0.5};//colours are floats these days
  55. //-----------------------------------------------------------------------------
  56. // Name: KeyboardControl
  57. // Desc: Enable Keyboard Control
  58. //-----------------------------------------------------------------------------
  59. void KeyboardControl( WPARAM wParam )
  60. {
  61. switch( wParam )
  62. {
  63. case VK_ESCAPE: // Exit application
  64. PostQuitMessage( 0 );
  65. break;
  66. case VK_LEFT:
  67. m_iMoveX--;
  68. break;
  69. case VK_RIGHT:
  70. m_iMoveX++;
  71. break;
  72. case VK_UP:
  73. m_iMoveY--;
  74. break;
  75. case VK_DOWN:
  76. m_iMoveY++;
  77. break;
  78. case VK_ADD:
  79. m_iMoveZ++;
  80. break;
  81. case VK_SUBTRACT:
  82. m_iMoveZ--;
  83. break;
  84. case VK_D:
  85. bDebug = !bDebug; // Toggle Debug output
  86. break;
  87. case VK_Q: DialogBoxParam( g_hInstance, MAKEINTRESOURCE(IDD_INIT),run->m_hWindow, run->DialogMessageHandlerStatic, (LPARAM)(run));
  88. break;
  89. case VK_X:
  90. m_dThetaX++;
  91. break;
  92. case VK_Y:
  93. m_dThetaY++;
  94. break;
  95. case VK_Z:
  96. m_dThetaZ++;
  97. break;
  98.  
  99.  
  100. }
  101. }
  102.  
  103. //-----------------------------------------------------------------------------
  104. // Name: KeyboardControl
  105. // Desc: Enable Keyboard Control
  106. //-----------------------------------------------------------------------------
  107. void MouseControl( POINT currentMousePos, POINT oldMousePos )
  108. {
  109. m_dThetaX -= ( currentMousePos.x - oldMousePos.x );
  110. m_dThetaY -= ( currentMousePos.y - oldMousePos.y );
  111. }
  112.  
  113.  
  114. //-----------------------------------------------------------------------------
  115. // Name: displayFinal
  116. // Desc: Routine to display useful info after program exit
  117. //-----------------------------------------------------------------------------
  118. void displayFinal( )
  119. {
  120. char sDispString[50];
  121. sprintf( sDispString, "Total Framecount %d", m_iOurFrameCount );
  122. run->Alert( "Finished", sDispString );
  123. }
  124.  
  125. //-----------------------------------------------------------------------------
  126. // Name: displayReadInfo
  127. // Desc: Routine to display useful info after file read, shows light vector
  128. // as an example, modify and use for debugging as required
  129. //-----------------------------------------------------------------------------
  130. void displayReadInfo( )
  131. {
  132. char sDispString[50];
  133. sprintf( sDispString, "%d polygons read", m_iNumOfPolys );
  134. run->Alert( m_sFilename, sDispString );
  135. sprintf( sDispString , "Light Vector %f %f %f", m_vLight.x, m_vLight.y, m_vLight.z );
  136. run->Alert( "Start Values:", sDispString );
  137. }
  138.  
  139.  
  140. //-----------------------------------------------------------------------------
  141. // Name: LoadPolys
  142. // Desc: Read polygon info from file
  143. //-----------------------------------------------------------------------------
  144. int LoadPolys( FILE *infile )
  145. {
  146. char cInString[1000];
  147. int iNumPolys = 0;
  148. float fLength;
  149. float fR, fG, fB; // red, green, blue values
  150.  
  151. do
  152. {
  153. fgets( cInString, 1000, infile); // Read first/next line of file
  154. sscanf( cInString, "%d", &polylist[iNumPolys].nv); // Get number of vertices
  155. fprintf( debugfile, "number of vertices: %d\n", polylist[iNumPolys].nv); // print number of vertices to debug file
  156. if (polylist[iNumPolys].nv == 0)
  157. break; // Get out if terminating zero found
  158.  
  159. // Only allocate the memory we need - ALWAYS remember to delete on shutdown
  160.  
  161. for (int i = 0; i < polylist[iNumPolys].nv; i++)
  162. {
  163. // Read next line of file
  164. fgets(cInString, 1000, infile);
  165. //Get Coordinates
  166. sscanf(cInString, "%f%f%f", &( polylist[iNumPolys].vert[i].x ),
  167. &( polylist[iNumPolys].vert[i].y ),
  168. &( polylist[iNumPolys].vert[i].z ) );
  169. }
  170.  
  171. polylist[iNumPolys].normal = Cross( VectorDiff( polylist[iNumPolys].vert[0], polylist[iNumPolys].vert[1] ),
  172. VectorDiff( polylist[iNumPolys].vert[0],polylist[iNumPolys].vert[2] ) );
  173. fLength = (float)sqrt( Dot( polylist[iNumPolys].normal, polylist[iNumPolys].normal ) ); // Calculate length of vector
  174.  
  175. polylist[iNumPolys].normal.x /= fLength; // Normalise
  176. polylist[iNumPolys].normal.y /= fLength; // each
  177. polylist[iNumPolys].normal.z /= fLength; // component
  178. fgets(cInString, 1000, infile); // Read next line of file
  179. sscanf( cInString, "%f%f%f", &fR, &fG, &fB ); // Get Colour, texture
  180. if (fR>1.0 ||fG>1.0||fB>1.0)//cope with either Open Gll 0-1.0 colours or old style 0-255 colours
  181. {
  182. polylist[iNumPolys].colour.r = fR/255.0;
  183. polylist[iNumPolys].colour.g = fG/255.0;
  184. polylist[iNumPolys].colour.b = fB/255.0;
  185. }
  186. iNumPolys++;
  187. } while( 1 );
  188.  
  189. return iNumPolys; //Return number of polygons read
  190. }
  191.  
  192.  
  193. //-----------------------------------------------------------------------------
  194. // Name: ReadFile
  195. // Desc: Read polygon info from file
  196. //-----------------------------------------------------------------------------
  197. void ReadFile()
  198. {
  199. FILE *flInFile;
  200. flInFile = fopen( m_sFilename, "r" );
  201. m_iNumOfPolys = LoadPolys( flInFile );
  202. displayReadInfo();
  203. fclose(flInFile);
  204. }
  205.  
  206.  
  207. //-----------------------------------------------------------------------------
  208. // Name: Plotpix
  209. // Desc: Draw a pixel - Calls nRGBAImage::SetColour(...),
  210. // m_kImage MUST be initialised before use!
  211. // Example usage: Plotpix( x, y, 255, 0, 0 );
  212. //-----------------------------------------------------------------------------
  213. inline void Plotpix( DWORD dwX, DWORD dwY, float fR, float fG, float fB )
  214. {
  215. DWORD dwYtemp;
  216.  
  217. // If using OpenGL we need to mirror the Y coordinates,
  218. // as OpenGL uses the opposite coordinate system to us and DirectX
  219. #ifdef DIRECTX
  220. dwYtemp = dwY;
  221. #else //OPENGL should be defined
  222. dwYtemp = ( m_iHeight - 1 ) - dwY;
  223. #endif
  224.  
  225. int index = 4 * ( dwYtemp * m_iWidth + dwX );
  226. m_fDrawingSurface[index] = fR; // Red Channel
  227. m_fDrawingSurface[index + 1] = fG; // Green Channel
  228. m_fDrawingSurface[index + 2] = fB; // Blue Channel
  229. m_fDrawingSurface[index + 3] = 0.0; // Alpha Channel
  230. }
  231.  
  232. //-----------------------------------------------------------------------------
  233. // Name: DrawImage
  234. // Desc: Draws the image
  235. //-----------------------------------------------------------------------------
  236. void DrawImage( )
  237. {
  238. TRANSFORM object_transformation;
  239. object_transformation =BuildTrans(m_dThetaX, m_dThetaY,m_dThetaZ,m_vDisp);
  240.  
  241. POLYGON polyTempP, polyTempQ, polyTempQT; // Temporary polygons for copying transformed, projected / clipped
  242. // versions of scene polys before drawing them.
  243. int iCurrentPoly;
  244. VECTOR tmp1,tmp2;
  245. int xpix,ypix;
  246. float xtop=50+m_iMoveX;
  247. float xbottom=m_iMoveX;
  248. // Temporary polygons for copying transformed, projected / clipped
  249. // versions of scene polys before drawing them.
  250. if ( m_iNumOfPolys <= 0 )
  251. DrawSquare(Default_Colour,m_iMoveY,50+m_iMoveY,xbottom,xtop,2,-2); // This draws the square you see, replace with Trapezium, and later Polygon
  252. else
  253. m_iOurFrameCount++; // Increment frame counter if we have a polygon to draw
  254.  
  255. for ( iCurrentPoly = 0; iCurrentPoly < m_iNumOfPolys; iCurrentPoly++ ) // for each polygon
  256. {
  257. if ( iFaceSelection > m_iNumOfPolys )
  258. iFaceSelection = m_iNumOfPolys; //Keep debug selector in range
  259.  
  260.  
  261. if ( iFaceSelection && ( iCurrentPoly + 1 ) != iFaceSelection)
  262. continue; // Reject unselected polygons if debug selection active.
  263.  
  264.  
  265. polyTempP = polylist[iCurrentPoly]; //copy static data into temp poly structure
  266.  
  267. // Copy each vertex in polygon, add displacement to allow shift
  268. for (int i = 0; i < polylist[iCurrentPoly].nv; i++)
  269. {
  270. //5
  271. /* tmp1=DoTransform(polylist[iCurrentPoly].vert[i],object_transformation);
  272. tmp2=Project(tmp1,m_dViewAngle);
  273. xpix = ((tmp2.x+1)*m_iWidth)/2;
  274. ypix = ((tmp2.y+1)*m_iWidth)/2;
  275. polylist[iCurrentPoly].vert[i].x=xpix;
  276. polylist[iCurrentPoly].vert[i].y=ypix;*/
  277. //5
  278.  
  279. polyTempP.vert[i].x = polylist[iCurrentPoly].vert[i].x + m_iMoveX;
  280. polyTempP.vert[i].y = polylist[iCurrentPoly].vert[i].y + m_iMoveY;
  281. }
  282.  
  283. if ( bDebug )
  284. fprintf( debugfile, " number of vertices: %d\n", polyTempP.nv); // print number of vertices
  285.  
  286. fflush( debugfile );
  287.  
  288. if ( bDebug ) // Print out current poly specs if debug active
  289. {
  290. for (int i = 0; i < polyTempP.nv; i++)
  291. {
  292. fprintf( debugfile, "before clipping Polygon %d, Vertex %d values: %7.2f, %7.2f, %11.6f\n",
  293. iCurrentPoly, i, polyTempP.vert[i].x, polyTempP.vert[i].y, polyTempP.vert[i].z );
  294. }
  295. fflush( debugfile );
  296.  
  297. }
  298. // The section below calls clipping and polygon draw routines, commented out to allow the
  299. // program to work without them. You may re-instate once you have appropriate routines,
  300. // or replace with your own code.
  301. /*
  302. ClipPolyXHigh( &polyTempP, &polyTempQT, WINDOWWIDTH ); // Clip against upper x boundary
  303. ClipPolyYHigh( &polyTempQT, &polyTempQ, WINDOWHEIGHT ); // Clip against upper y boundary (bottom of screen)
  304. ClipPolyXLow( &polyTempQ, &polyTempQT, 0); // Clip against lower x boundary
  305. ClipPolyYLow( &polyTempQT, &polyTempQ, 0); // Clip against lower y boundary (bottom of screen)
  306.  
  307. if ( bDebug ) // Print out current poly specs if debug active
  308. {
  309. for ( int i = 0; i < polyTempQ.nv; i++ )
  310. fprintf( debugfile, "after clipping Polygon %d Vertex %d values:y %7.2f %7.2f %11.6f\n",
  311. iCurrentPoly, i, polyTempQ.vert[i].x, polyTempQ.vert[i].y, polyTempQ.vert[i].z );
  312.  
  313. fflush(debugfile);
  314. }
  315. */
  316. DrawPolygon(&polyTempP); // Call the drawing routine
  317.  
  318. }
  319. if ( m_iNumOfPolys > 0 )
  320. bDebug = false; // Switch debug off after first run - switch on again via keyboard control if needed
  321.  
  322.  
  323. }
  324.  
  325. //-----------------------------------------------------------------------------
  326. // Name: DrawSquare
  327. // Desc: Draw a sqaure
  328. //-----------------------------------------------------------------------------
  329. void DrawSquare(COLOUR c,int ytop,int yend,float& xstart,float& xend,float sloperight,float slopeleft)
  330. {
  331. //Note no protection to keep in screen bounds...
  332. for( int j = ytop; j < yend; j++)
  333. {
  334.  
  335. for (float i = xstart; i < xend; i++)
  336. {
  337. Plotpix( i, j, c.r, c.g, c.b );
  338. }
  339. xend=xend+sloperight;
  340. xstart=xstart+slopeleft;
  341. }
  342. }
  343.  
  344. /*
  345. //-----------------------------------------------------------------------------
  346. // Name: DrawPolygon
  347. // Desc: Draw a polygon
  348. //-----------------------------------------------------------------------------
  349. */
  350. void DrawPolygon(POLYGON *p)
  351. {
  352. int topvertex;
  353. int cleft;
  354. int cright;
  355. int nleft;
  356. int nright;
  357. float sloperight;
  358. float slopeleft;
  359. //
  360. float xend;
  361. float xstart;
  362. float yend;
  363. //
  364. float ytop;
  365. for(int i=0; i<p->nv;i++)
  366. {
  367. if(p->vert[topvertex].y > p->vert[i].y)
  368. {
  369. topvertex=i;
  370. }
  371. }
  372. cleft=topvertex;
  373. cright=topvertex;
  374.  
  375. nleft=cleft-1;
  376. nright=cright+1;
  377. //after Blitz
  378. if (nleft<0)
  379. {
  380. nleft=p->nv-1;
  381. }
  382.  
  383. if (cright>p->nv)
  384. {
  385. nright=0;
  386. }
  387.  
  388. //
  389.  
  390. float dx,dy,dx2,dy2;
  391. dx=p->vert[nright].x-p->vert[cright].x;
  392. dy=p->vert[nright].y-p->vert[cright].y;
  393. dx2=p->vert[nleft].x-p->vert[cleft].x;
  394. dy2=p->vert[nleft].y-p->vert[cleft].y;
  395.  
  396. if (dy!=0)
  397. {
  398. sloperight=dx/dy;
  399.  
  400. }
  401. if (dy2!=0)
  402. {
  403. slopeleft=dx2/dy2;
  404. }
  405.  
  406. bool test;
  407. if(p->vert[nleft].y > p->vert[nright].y)
  408. {
  409. yend=p->vert[nright].y;
  410. test= true;
  411.  
  412. }
  413. else
  414. {
  415. yend=p->vert[nleft].y;
  416. test= false;
  417. }
  418.  
  419. xstart=p->vert[cleft].x;//p->vert[cright].x;
  420. xend=p->vert[cright].x;//p->vert[cleft].x;
  421. ytop=p->vert[topvertex].y;
  422.  
  423. DrawSquare(p->colour,ytop,yend,xstart,xend,sloperight,slopeleft);
  424.  
  425. ///trap2 part3
  426. do
  427. {
  428. ytop=yend;
  429.  
  430. if(test==true)
  431. {
  432. //cright=topvertex;
  433. //cleft = nleft;
  434. cright = nright;
  435. nright=cright+1;
  436. if(nright > p->nv)
  437. {
  438. nright = 0;
  439. }
  440. if((p->vert[nright].y -p->vert[cright].y) !=0)
  441. {
  442. dx=p->vert[nright].x-p->vert[cright].x;
  443. dy=p->vert[nright].y-p->vert[cright].y;
  444. sloperight = dx/dy;
  445. }
  446. /*if(p->vert[nleft].y > p->vert[nright].y)
  447. {
  448. yend=p->vert[nright].y;
  449. test= true;
  450.  
  451. }
  452. else
  453. {
  454. yend=p->vert[nleft].y;
  455. test= false;
  456. }*/
  457.  
  458.  
  459. }
  460. if (test==false)
  461. {
  462. //cleft=topvertex;
  463. cleft = nleft;
  464. nleft=cleft-1;
  465. if (nleft > 0)
  466. {
  467. nleft = p->nv-1;
  468. }
  469. if((p->vert[nleft].y -p->vert[cleft].y) !=0)
  470. {
  471. dx2=p->vert[nleft].x-p->vert[cleft].x;
  472. dy2=p->vert[nleft].y-p->vert[cleft].y;
  473. slopeleft = dx2/dy2;
  474. }
  475. /*if(p->vert[nleft].y > p->vert[nright].y)
  476. {
  477. yend=p->vert[nright].y;
  478. test= true;
  479.  
  480. } else {
  481. yend=p->vert[nleft].y;
  482. test= false;
  483. }*/
  484.  
  485. }
  486. test = true;
  487. yend = p->vert[nleft].y;
  488. if (p->vert[nleft].y > p->vert[nright].y)
  489. {
  490. yend = p->vert[nright].y;
  491. test = false;
  492. }
  493. //
  494. else
  495. {
  496. yend = p->vert[nleft].y;
  497. test = true;
  498. }
  499. //
  500. DrawSquare(p->colour,ytop,yend,xstart,xend,sloperight,slopeleft);
  501. }
  502. while(nleft!=nright);
  503. //
  504. }
  505.  
  506.  
  507.  
  508. //-----------------------------------------------------------------------------
  509. // Name: ClipPolyXLow
  510. // Desc: Clipping Routine for lower x boundary
  511. //-----------------------------------------------------------------------------
  512. int ClipPolyXLow(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
  513. {
  514. // Tell calling routine how many vertices in pOutput array
  515. return pOutput->nv;
  516. }
  517.  
  518. //-----------------------------------------------------------------------------
  519. // Name: ClipPolyYLow
  520. // Desc: Clipping Routine for lower y boundary
  521. //-----------------------------------------------------------------------------
  522. int ClipPolyYLow(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
  523. {
  524. // Tell calling routine how many vertices in pOutput array
  525. return pOutput->nv;
  526. }
  527.  
  528. //-----------------------------------------------------------------------------
  529. // Name: ClipPolyXHi
  530. // Desc: Clipping Routine for upper x boundary
  531. //-----------------------------------------------------------------------------
  532. int ClipPolyXHigh(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
  533. {
  534. // Tell calling routine how many vertices in pOutput array
  535. return pOutput->nv;
  536. }
  537.  
  538. //-----------------------------------------------------------------------------
  539. // Name: ClipPolyYHi
  540. // Desc: Clipping Routine for upper y boundary
  541. //-----------------------------------------------------------------------------
  542. int ClipPolyYHigh(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
  543. {
  544. // Tell calling routine how many vertices in pOutput array
  545. return pOutput->nv;
  546. }
  547.  
  548. //-----------------------------------------------------------------------------
  549. // Name: Init
  550. // Desc: Initialises Direct3D etc.
  551. // This is called before switch to graphics mode,
  552. // example of z buffer initialisation shown in comments,
  553. // ignore for parts a/b.
  554. //-----------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment