Guest User

Untitled

a guest
Jun 24th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.90 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. }
  373. cleft=topvertex;
  374. cright=topvertex;
  375.  
  376. nleft=cleft-1;
  377. nright=cright+1;
  378. //after Blitz
  379. if (nleft<0)
  380. {
  381. nleft=p->nv-1;
  382. }
  383.  
  384. if (cright>p->nv)
  385. {
  386. nright=0;
  387. }
  388.  
  389. //
  390.  
  391. float dx,dy,dx2,dy2;
  392. dx=p->vert[nright].x-p->vert[cright].x;
  393. dy=p->vert[nright].y-p->vert[cright].y;
  394. dx2=p->vert[nleft].x-p->vert[cleft].x;
  395. dy2=p->vert[nleft].y-p->vert[cleft].y;
  396.  
  397. if (dy!=0)
  398. {
  399. sloperight=dx/dy;
  400.  
  401. }
  402. if (dy2!=0)
  403. {
  404. slopeleft=dx2/dy2;
  405. }
  406.  
  407. bool test;
  408. if(p->vert[nleft].y > p->vert[nright].y)
  409. {
  410. yend=p->vert[nright].y;
  411. test= true;
  412.  
  413. }
  414. else
  415. {
  416. yend=p->vert[nleft].y;
  417. test= false;
  418. }
  419.  
  420. xstart=p->vert[cleft].x;//p->vert[cright].x;
  421. xend=p->vert[cright].x;//p->vert[cleft].x;
  422. ytop=p->vert[topvertex].y;
  423.  
  424. DrawSquare(p->colour,ytop,yend,xstart,xend,sloperight,slopeleft);
  425.  
  426. ///trap2 part3
  427. do
  428. {
  429. ytop=yend;
  430.  
  431. if(test==true)
  432. {
  433. //cright=topvertex;
  434. //cleft = nleft;
  435. cright = nright;
  436. nright=cright-1;
  437. if(nright > p->nv)
  438. {
  439. nright = 0;
  440. }
  441. if((p->vert[nright].y -p->vert[cright].y) !=0)
  442. {
  443. dx=p->vert[nright].x-p->vert[cright].x;
  444. dy=p->vert[nright].y-p->vert[cright].y;
  445. slopeleft = dx/dy;
  446. }
  447. /*if(p->vert[nleft].y > p->vert[nright].y)
  448. {
  449. yend=p->vert[nright].y;
  450. test= true;
  451.  
  452. }
  453. else
  454. {
  455. yend=p->vert[nleft].y;
  456. test= false;
  457. }*/
  458.  
  459.  
  460. }
  461. if (test==false)
  462. {
  463. //cleft=topvertex;
  464. cleft = nleft;
  465. nleft=cleft+1;
  466. if (nleft < 0)
  467. {
  468. nleft = p->nv-1;
  469. }
  470. if((p->vert[nleft].y -p->vert[cleft].y) !=0)
  471. {
  472. dx2=p->vert[nleft].x-p->vert[cleft].x;
  473. dy2=p->vert[nleft].y-p->vert[cleft].y;
  474. sloperight = dx2/dy2;
  475. }
  476. /*if(p->vert[nleft].y > p->vert[nright].y)
  477. {
  478. yend=p->vert[nright].y;
  479. test= true;
  480.  
  481. } else {
  482. yend=p->vert[nleft].y;
  483. test= false;
  484. }*/
  485.  
  486. }
  487. test = true;
  488. yend = p->vert[nleft].y;
  489. if (p->vert[nleft].y > p->vert[nright].y)
  490. {
  491. yend = p->vert[nright].y;
  492. test = false;
  493. }
  494. DrawSquare(p->colour,ytop,yend,xstart,xend,sloperight,slopeleft);
  495. }
  496. while(nleft!=nright);
  497. //
  498. }
  499.  
  500.  
  501.  
  502. //-----------------------------------------------------------------------------
  503. // Name: ClipPolyXLow
  504. // Desc: Clipping Routine for lower x boundary
  505. //-----------------------------------------------------------------------------
  506. int ClipPolyXLow(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
  507. {
  508. // Tell calling routine how many vertices in pOutput array
  509. return pOutput->nv;
  510. }
  511.  
  512. //-----------------------------------------------------------------------------
  513. // Name: ClipPolyYLow
  514. // Desc: Clipping Routine for lower y boundary
  515. //-----------------------------------------------------------------------------
  516. int ClipPolyYLow(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
  517. {
  518. // Tell calling routine how many vertices in pOutput array
  519. return pOutput->nv;
  520. }
  521.  
  522. //-----------------------------------------------------------------------------
  523. // Name: ClipPolyXHi
  524. // Desc: Clipping Routine for upper x boundary
  525. //-----------------------------------------------------------------------------
  526. int ClipPolyXHigh(POLYGON *pIinput, POLYGON *pOutput, int iXBound)
  527. {
  528. // Tell calling routine how many vertices in pOutput array
  529. return pOutput->nv;
  530. }
  531.  
  532. //-----------------------------------------------------------------------------
  533. // Name: ClipPolyYHi
  534. // Desc: Clipping Routine for upper y boundary
  535. //-----------------------------------------------------------------------------
  536. int ClipPolyYHigh(POLYGON *pIinput, POLYGON *pOutput, int iYBound)
  537. {
  538. // Tell calling routine how many vertices in pOutput array
  539. return pOutput->nv;
  540. }
  541.  
  542. //-----------------------------------------------------------------------------
  543. // Name: Init
  544. // Desc: Initialises Direct3D etc.
  545. // This is called before switch to graphics mode,
  546. // example of z buffer initialisation shown in comments,
  547. // ignore for parts a/b.
  548. //-----------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment