Posted By

ozkriff on 12/14/08


Tagged


Versions (?)

3d model RAW test(OpenGL/GLUT)


 / Published in: C++
 

  1. #include <GL/glut.h>
  2.  
  3. #include <GL/glu.h>
  4.  
  5. #include <cmath>
  6.  
  7. #include <fstream>
  8.  
  9. #include <string>
  10.  
  11. #include <iostream>
  12.  
  13. using namespace::std;
  14.  
  15.  
  16.  
  17. bool IsPerspective=true;
  18.  
  19.  
  20.  
  21. struct point
  22.  
  23. {
  24.  
  25. GLfloat x;
  26.  
  27. GLfloat y;
  28.  
  29. GLfloat z;
  30.  
  31. };
  32.  
  33.  
  34.  
  35. struct face
  36.  
  37. {
  38.  
  39. point vertex[3];
  40.  
  41. };
  42.  
  43.  
  44.  
  45. class model
  46.  
  47. {
  48.  
  49. public:
  50.  
  51. face faces_list[200];
  52.  
  53.  
  54.  
  55. model()
  56.  
  57. {
  58.  
  59. char str[]="vert_100-faces_196.raw";
  60.  
  61. read_from_file(str);
  62.  
  63. };
  64.  
  65.  
  66.  
  67. void draw_test();
  68.  
  69. void draw();
  70.  
  71. int read_from_file(char*);
  72.  
  73. };
  74.  
  75.  
  76.  
  77. void model::draw()
  78.  
  79. {
  80.  
  81. int ii, jj;
  82.  
  83. bool bb=0;
  84.  
  85. glBegin(GL_TRIANGLES);
  86.  
  87. for (ii=0; ii<197; ii++)
  88.  
  89. {
  90.  
  91. for (jj=0; jj<3; jj++)
  92.  
  93. {
  94.  
  95. glVertex3f
  96.  
  97. (
  98.  
  99. faces_list[ii].vertex[jj].x,
  100.  
  101. faces_list[ii].vertex[jj].y,
  102.  
  103. faces_list[ii].vertex[jj].z
  104.  
  105. );
  106.  
  107. if ( !bb )
  108.  
  109. {
  110.  
  111. glColor3f(0.0f, 1.0f, 0.0f);
  112.  
  113. bb=1;
  114.  
  115. }
  116.  
  117. else
  118.  
  119. {
  120.  
  121. glColor3f(1.0f, 0.0f, 0.0f);
  122.  
  123. bb=0;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. }
  130.  
  131. glEnd();
  132.  
  133. }
  134.  
  135.  
  136.  
  137. int model::read_from_file(char* string_name)
  138.  
  139. {
  140.  
  141. int ii,jj;
  142.  
  143. ifstream input_file( string_name );
  144.  
  145.  
  146.  
  147. if (!input_file )
  148.  
  149. {cout << "oblom/ glucky s failom...\n";}
  150.  
  151. else
  152.  
  153. {
  154.  
  155. while (input_file )
  156.  
  157. {
  158.  
  159. for (ii=0; ii<197; ii++)
  160.  
  161. for (jj=0; jj<3; jj++)
  162.  
  163. input_file
  164.  
  165. >> faces_list[ii].vertex[jj].x
  166.  
  167. >> faces_list[ii].vertex[jj].y
  168.  
  169. >> faces_list[ii].vertex[jj].z;
  170.  
  171. }
  172.  
  173.  
  174.  
  175. cout<<faces_list[1].vertex[0].x<<endl;
  176.  
  177.  
  178.  
  179. for (jj=0; jj<3; jj++)
  180.  
  181. cout
  182.  
  183. <<faces_list[0].vertex[jj].x
  184.  
  185. <<" "
  186.  
  187. <<faces_list[0].vertex[jj].y
  188.  
  189. <<" "
  190.  
  191. <<faces_list[0].vertex[ii].z
  192.  
  193. <<" ";
  194.  
  195. cout<<endl;
  196.  
  197. }
  198.  
  199. return 0;
  200.  
  201. };
  202.  
  203.  
  204.  
  205.  
  206.  
  207. void model::draw_test()
  208.  
  209. {
  210.  
  211. glBegin(GL_TRIANGLES);
  212.  
  213. glColor3f(0.0f, 1.0f, 0.0f);
  214.  
  215.  
  216.  
  217. glVertex3f(0.0f, 0.0f, 0.0f);
  218.  
  219. glVertex3f(50.0f, 0.0f, 0.0f);
  220.  
  221. glVertex3f(0.0f, 50.0f, 0.0f);
  222.  
  223.  
  224.  
  225. glColor3f(1.0f, 0.0f, 0.0f);
  226.  
  227.  
  228.  
  229. glVertex3f(0.0f, 0.0f, 0.0f);
  230.  
  231. glVertex3f(-50.0f, 0.0f, 0.0f);
  232.  
  233. glVertex3f(0.0f, -50.0f,50.0f);
  234.  
  235.  
  236.  
  237. glColor3f(0.0f, 0.0f, 1.0f);
  238.  
  239.  
  240.  
  241. glVertex3f(0.0f, 50.0f, 0.0f);
  242.  
  243. glVertex3f(0.0f, 50.0f, 50.0f);
  244.  
  245. glVertex3f(0.0f, 0.0f,0.0f);
  246.  
  247. glEnd();
  248.  
  249. };
  250.  
  251.  
  252.  
  253. ////////////
  254.  
  255. // GLOBALS
  256.  
  257. ////////////
  258.  
  259. model man;
  260.  
  261.  
  262.  
  263. // Lighting values
  264.  
  265. GLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
  266.  
  267. GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  268.  
  269. GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  270.  
  271.  
  272.  
  273. // Rotation amounts
  274.  
  275. static GLfloat xRot = 0.0f;
  276.  
  277. static GLfloat yRot = 0.0f;
  278.  
  279.  
  280.  
  281. // Flags for effects
  282.  
  283. // Flags for effects
  284.  
  285. int iCull = 0;
  286.  
  287. int iOutline = 0;
  288.  
  289. int iDepth = 0;
  290.  
  291.  
  292.  
  293. ///////////////////////////////////////////////////////////////////////////////
  294.  
  295. // Reset flags as appropriate in response to menu selections
  296.  
  297. void ProcessMenu(int value)
  298.  
  299. {
  300.  
  301. switch(value)
  302.  
  303. {
  304.  
  305. case 1:
  306.  
  307. iDepth = !iDepth;
  308.  
  309. break;
  310.  
  311.  
  312.  
  313. case 2:
  314.  
  315. iCull = !iCull;
  316.  
  317. break;
  318.  
  319.  
  320.  
  321. case 3:
  322.  
  323. iOutline = !iOutline;
  324.  
  325. default:
  326.  
  327. break;
  328.  
  329. }
  330.  
  331.  
  332.  
  333. glutPostRedisplay();
  334.  
  335. }
  336.  
  337.  
  338.  
  339. // Called to draw scene
  340.  
  341. void RenderScene(void)
  342.  
  343. {
  344.  
  345. GLfloat x,y,angle; // Storage for coordinates and angles
  346.  
  347. int iPivot = 1; // Used to flag alternating colors
  348.  
  349.  
  350.  
  351. // Clear the window and the depth buffer
  352.  
  353. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  354.  
  355.  
  356.  
  357. // Turn culling on if flag is set
  358.  
  359. if(iCull)
  360.  
  361. glEnable(GL_CULL_FACE);
  362.  
  363. else
  364.  
  365. glDisable(GL_CULL_FACE);
  366.  
  367.  
  368.  
  369. // Enable depth testing if flag is set
  370.  
  371. if(iDepth)
  372.  
  373. glEnable(GL_DEPTH_TEST);
  374.  
  375. else
  376.  
  377. glDisable(GL_DEPTH_TEST);
  378.  
  379.  
  380.  
  381. // Draw back side as a polygon only, if flag is set
  382.  
  383. if(iOutline)
  384.  
  385. glPolygonMode(GL_BACK,GL_LINE);
  386.  
  387. else
  388.  
  389. glPolygonMode(GL_BACK,GL_FILL);
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397. // Save matrix state and do the rotation
  398.  
  399. glPushMatrix();
  400.  
  401.  
  402.  
  403. glTranslatef(0.0f, 0.0f, -150.0f);
  404.  
  405.  
  406.  
  407. glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  408.  
  409. glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  410.  
  411.  
  412.  
  413. ////////////
  414.  
  415. // start
  416.  
  417. ////////////
  418.  
  419.  
  420.  
  421. float scale=20.0f;
  422.  
  423. glScalef(scale,scale,scale);
  424.  
  425. glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
  426.  
  427.  
  428.  
  429. //~ man.draw_test();
  430.  
  431. man.draw();
  432.  
  433. ////////////
  434.  
  435. // end
  436.  
  437. ////////////
  438.  
  439.  
  440.  
  441. // Restore transformations
  442.  
  443. glPopMatrix();
  444.  
  445.  
  446.  
  447.  
  448.  
  449. glutSwapBuffers();
  450.  
  451. }
  452.  
  453.  
  454.  
  455. // This function does any needed initialization on the rendering
  456.  
  457. // context.
  458.  
  459. void SetupRC()
  460.  
  461. {
  462.  
  463. // Black background
  464.  
  465. glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  466.  
  467.  
  468.  
  469. // Set drawing color to green
  470.  
  471. glColor3f(0.0f, 1.0f, 0.0f);
  472.  
  473.  
  474.  
  475. // Set color shading model to flat
  476.  
  477. glShadeModel(GL_FLAT);
  478.  
  479.  
  480.  
  481. // Clock wise wound polygons are front facing, this is reversed
  482.  
  483. // because we are using triangle fans
  484.  
  485. //~ glFrontFace(GL_CW);
  486.  
  487.  
  488.  
  489. }
  490.  
  491.  
  492.  
  493. void SpecialKeys(int key, int x, int y)
  494.  
  495. {
  496.  
  497. if(key == GLUT_KEY_UP)
  498.  
  499. xRot-= 5.0f;
  500.  
  501.  
  502.  
  503. if(key == GLUT_KEY_DOWN)
  504.  
  505. xRot += 5.0f;
  506.  
  507.  
  508.  
  509. if(key == GLUT_KEY_LEFT)
  510.  
  511. yRot -= 5.0f;
  512.  
  513.  
  514.  
  515. if(key == GLUT_KEY_RIGHT)
  516.  
  517. yRot += 5.0f;
  518.  
  519.  
  520.  
  521. // Refresh the Window
  522.  
  523. glutPostRedisplay();
  524.  
  525. }
  526.  
  527.  
  528.  
  529.  
  530.  
  531. void ChangeSize(int w, int h)
  532.  
  533. {
  534.  
  535. if ( !IsPerspective )
  536.  
  537. {
  538.  
  539. GLfloat nRange = 100.0f;
  540.  
  541.  
  542.  
  543. // Prevent a divide by zero
  544.  
  545. if(h == 0)
  546.  
  547. h = 1;
  548.  
  549.  
  550.  
  551. // Set Viewport to window dimensions
  552.  
  553. glViewport(0, 0, w, h);
  554.  
  555.  
  556.  
  557. // Reset projection matrix stack
  558.  
  559. glMatrixMode(GL_PROJECTION);
  560.  
  561. glLoadIdentity();
  562.  
  563.  
  564.  
  565. // Establish clipping volume (left, right, bottom, top, near, far)
  566.  
  567. if (w <= h)
  568.  
  569. glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
  570.  
  571. else
  572.  
  573. glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
  574.  
  575.  
  576.  
  577. // Reset Model view matrix stack
  578.  
  579. glMatrixMode(GL_MODELVIEW);
  580.  
  581. glLoadIdentity();
  582.  
  583. }
  584.  
  585. else
  586.  
  587. {
  588.  
  589.  
  590.  
  591. GLfloat fAspect;
  592.  
  593.  
  594.  
  595. // Prevent a divide by zero
  596.  
  597. if(h == 0)
  598.  
  599. h = 1;
  600.  
  601.  
  602.  
  603. // Set Viewport to window dimensions
  604.  
  605. glViewport(0, 0, w, h);
  606.  
  607.  
  608.  
  609. fAspect = (GLfloat)w/(GLfloat)h;
  610.  
  611.  
  612.  
  613. // Reset coordinate system
  614.  
  615. glMatrixMode(GL_PROJECTION);
  616.  
  617. glLoadIdentity();
  618.  
  619.  
  620.  
  621. // Produce the perspective projection
  622.  
  623. gluPerspective(60.0f, fAspect, 1.0, 0.0);
  624.  
  625.  
  626.  
  627. glMatrixMode(GL_MODELVIEW);
  628.  
  629. glLoadIdentity();
  630.  
  631. }
  632.  
  633. }
  634.  
  635.  
  636.  
  637. int main(int argc, char* argv[])
  638.  
  639. {
  640.  
  641. glutInit(&argc, argv);
  642.  
  643. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  644.  
  645. glutCreateWindow("riff's programm");
  646.  
  647. glutInitWindowSize(480, 640);
  648.  
  649.  
  650.  
  651. // Create the Menu
  652.  
  653. glutCreateMenu(ProcessMenu);
  654.  
  655. glutAddMenuEntry("Toggle depth test",1);
  656.  
  657. glutAddMenuEntry("Toggle cull backface",2);
  658.  
  659. glutAddMenuEntry("Toggle outline back",3);
  660.  
  661. glutAttachMenu(GLUT_RIGHT_BUTTON);
  662.  
  663.  
  664.  
  665. glutReshapeFunc(ChangeSize);
  666.  
  667. glutSpecialFunc(SpecialKeys);
  668.  
  669. glutDisplayFunc(RenderScene);
  670.  
  671. SetupRC();
  672.  
  673. glutMainLoop();
  674.  
  675.  
  676.  
  677. return 0;
  678.  
  679. }

Report this snippet  

You need to login to post a comment.