Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.72 KB | None | 0 0
  1. /************************************************************
  2. Grafika OpenGL
  3. *************************************************************/
  4. #include <windows.h>
  5. #include <gl\gl.h>
  6. #include <gl\glu.h>
  7.  
  8. #include "grafika.h"
  9. //#include "wektor.h"
  10. //#include "kwaternion.h"
  11. #include "obiekty.h"
  12.  
  13. extern GLfloat teamColors[][4]; // pobranie tablicy kolorów
  14.  
  15. extern Wektor3 kierunek_kamery;
  16. extern Wektor3 pol_kamery;
  17. extern Wektor3 pion_kamery;
  18. extern bool sledzenie;
  19. extern float oddalenie;
  20. extern float zoom;
  21. extern float kat_kam_z;
  22. extern char napis1[], napis2[];
  23. extern char ** napisyLicytacja;
  24.  
  25. extern FILE *f;
  26. extern ObiektRuchomy *pMojObiekt; // obiekt przypisany do tej aplikacji
  27. extern int iLiczbaCudzychOb; // liczba innych obiektow
  28. extern ObiektRuchomy *CudzeObiekty[1000]; // obiekty z innych aplikacji lub inne obiekty niz pCraft
  29. extern int IndeksyOb[1000]; // tablica indeksow innych obiektow ulatwiajaca wyszukiwanie
  30. extern Teren teren;
  31.  
  32. int g_GLPixelIndex = 0;
  33. HGLRC g_hGLContext = NULL;
  34. unsigned int font_base;
  35.  
  36.  
  37. extern void TworzListyWyswietlania(); // definiujemy listy tworzące labirynt
  38. extern void RysujGlobalnyUkladWsp();
  39.  
  40.  
  41. int InicjujGrafike(HDC g_context)
  42. {
  43.  
  44. if (SetWindowPixelFormat(g_context)==FALSE)
  45. return FALSE;
  46.  
  47. if (CreateViewGLContext(g_context)==FALSE)
  48. return 0;
  49. BuildFont(g_context);
  50.  
  51. glEnable(GL_DEPTH_TEST);
  52. glDepthFunc(GL_LEQUAL);
  53.  
  54.  
  55. TworzListyWyswietlania(); // definiujemy listy tworzące różne elementy sceny
  56. teren.PoczatekGrafiki();
  57. }
  58.  
  59. void RysujScene()
  60. {
  61. GLfloat BlueSurface[] = { 0.3f, 0.0f, 0.7f, 0.7f };
  62. GLfloat BlueSurfaceTr[] = { 0.6f, 0.0f, 0.9f, 0.3f };
  63.  
  64. GLfloat DGreenSurface[] = { 0.3f, 0.6f, 0.1f, 0.5f };
  65. GLfloat DGreenSurface_z[] = { 0.1f, 0.6f, 0.3f, 0.25f };
  66. GLfloat DRedSurface_z[] = { 0.6f, 0.1f, 0.3f, 0.25f };
  67.  
  68. GLfloat RedSurface[] = { 0.8f, 0.2f, 0.1f, 0.5f };
  69. GLfloat GreenSurface[] = { 0.5f, 0.7f, 0.15f, 1.0f };
  70. GLfloat YellowSurface[] = { 0.45f, 0.75f, 0.0f, 1.0f };
  71. GLfloat YellowLight[] = { 2.0f, 2.0f, 1.0f, 1.0f };
  72.  
  73. GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 0.1f };
  74. GLfloat LightDiffuse[] = { 0.7f, 0.7f, 0.7f, 0.7f };
  75. GLfloat LightPosition[] = { 5.0f, 5.0f, 5.0f, 0.0f };
  76.  
  77. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  79.  
  80. glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient); //1 składowa: światło otaczające (bezkierunkowe)
  81. glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); //2 składowa: światło rozproszone (kierunkowe)
  82. glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
  83. glEnable(GL_LIGHT0);
  84.  
  85. // glPushMatrix();
  86. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, YellowLight);
  87.  
  88.  
  89.  
  90. glLoadIdentity();
  91. glClearColor(0.05, 0.05, 0.2, 0.8); // ustawienie nieczarnego koloru tła
  92. glTranslatef(-24,24,-40);
  93. glRasterPos2f(4.0,-4.0);
  94.  
  95. glPrint("%s",napis1);
  96.  
  97. glRasterPos2f(-5.0, -5.0);
  98. glPrint("%s",napis2);
  99.  
  100. for (int i = 0; i < 22; i++) {
  101. glRasterPos2f(-5.0, -6.0 - i);
  102. glPrint("%s", napisyLicytacja[i]);
  103. }
  104.  
  105.  
  106. glLoadIdentity();
  107.  
  108. glClearColor( 0.15, 0.15, 0.8, 0.7);
  109.  
  110. Wektor3 kierunek_k,pion_k,pol_k;
  111. if (sledzenie)
  112. {
  113. kierunek_k = pMojObiekt->qOrient.obroc_wektor(Wektor3(1,0,0));
  114. pion_k = pMojObiekt->qOrient.obroc_wektor(Wektor3(0,1,0));
  115. Wektor3 prawo_kamery = pMojObiekt->qOrient.obroc_wektor(Wektor3(0,0,1));
  116.  
  117. pion_k = pion_k.obrot(kat_kam_z,prawo_kamery.x,prawo_kamery.y,prawo_kamery.z);
  118. kierunek_k = kierunek_k.obrot(kat_kam_z,prawo_kamery.x,prawo_kamery.y,prawo_kamery.z);
  119. pol_k = pMojObiekt->wPol - kierunek_k*pMojObiekt->dlugosc*0 +
  120. pion_k.znorm()*pMojObiekt->wysokosc*5;
  121. pion_kamery = pion_k;
  122. kierunek_kamery = kierunek_k;
  123. pol_kamery = pol_k;
  124. }
  125. else
  126. {
  127. pion_k = pion_kamery;
  128. kierunek_k = kierunek_kamery;
  129. pol_k = pol_kamery;
  130. Wektor3 prawo_kamery = (kierunek_k*pion_k).znorm();
  131. pion_k = pion_k.obrot(kat_kam_z/20,prawo_kamery.x,prawo_kamery.y,prawo_kamery.z);
  132. kierunek_k = kierunek_k.obrot(kat_kam_z/20,prawo_kamery.x,prawo_kamery.y,prawo_kamery.z);
  133. }
  134.  
  135.  
  136. gluLookAt(pol_k.x - oddalenie*kierunek_k.x,
  137. pol_k.y - oddalenie*kierunek_k.y, pol_k.z - oddalenie*kierunek_k.z,
  138. pol_k.x + kierunek_k.x, pol_k.y + kierunek_k.y, pol_k.z + kierunek_k.z ,
  139. pion_k.x, pion_k.y, pion_k.z);
  140.  
  141. //glRasterPos2f(0.30,-0.27);
  142. //glPrint("MojObiekt->iID = %d",pMojObiekt->iID );
  143.  
  144. RysujGlobalnyUkladWsp();
  145.  
  146.  
  147. for (int w = -1;w<2;w++)
  148. for (int k = -1;k<2;k++)
  149. {
  150. glPushMatrix();
  151.  
  152. glTranslatef(teren.lkolumn*teren.rozmiar_pola*k,0,teren.lwierszy*teren.rozmiar_pola*w);
  153.  
  154. ///////////////////// Zmiana kolorów
  155. int x = pMojObiekt->id_Koloru;
  156. if (x >= 0 && x < 4)
  157. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, teamColors[x]);
  158. else
  159. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, BlueSurface);
  160. glEnable(GL_BLEND);
  161. pMojObiekt->Rysuj();
  162.  
  163. glEnable(GL_BLEND);
  164.  
  165. for (int i = 0; i<iLiczbaCudzychOb; i++)
  166. {
  167. x = CudzeObiekty[i]->id_Koloru;
  168. if (x >= 0 && x < 4)
  169. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, teamColors[x]);
  170. else
  171. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, GreenSurface);
  172. CudzeObiekty[i]->Rysuj();
  173. }
  174. ////////////////// koniec zmiany kolorów
  175.  
  176. //glmaterialfv(gl_front_and_back, gl_ambient_and_diffuse, bluesurface);
  177. //glenable(gl_blend);
  178.  
  179. //pmojobiekt->rysuj();
  180.  
  181. //for (int i=0;i<iliczbacudzychob;i++)
  182. //{
  183. //glmaterialfv(gl_front_and_back, gl_ambient_and_diffuse, dgreensurface);
  184. //cudzeobiekty[i]->rysuj();
  185. //}
  186.  
  187. glDisable(GL_BLEND);
  188. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, GreenSurface);
  189. int szczegolowosc = 0;
  190. if ((w==0)&&(k==0)) szczegolowosc = 1;
  191. teren.Rysuj(szczegolowosc);
  192. glPopMatrix();
  193. }
  194. glPopMatrix();
  195. glFlush();
  196. }
  197.  
  198.  
  199.  
  200.  
  201. void ZmianaRozmiaruOkna(int cx,int cy)
  202. {
  203. GLsizei width, height;
  204. GLdouble aspect;
  205. width = cx;
  206. height = cy;
  207.  
  208. if (cy==0)
  209. aspect = (GLdouble)width;
  210. else
  211. aspect = (GLdouble)width/(GLdouble)height;
  212.  
  213. glViewport(0, 0, width, height);
  214.  
  215.  
  216. glMatrixMode(GL_PROJECTION);
  217. glLoadIdentity();
  218. gluPerspective(55*zoom, aspect, 1, 10000.0);
  219.  
  220. glMatrixMode(GL_MODELVIEW);
  221. glLoadIdentity();
  222.  
  223. glDrawBuffer(GL_BACK);
  224.  
  225. glEnable(GL_LIGHTING);
  226.  
  227. glEnable(GL_DEPTH_TEST);
  228.  
  229. }
  230.  
  231.  
  232. void ZakonczenieGrafiki()
  233. {
  234. if(wglGetCurrentContext()!=NULL)
  235. {
  236. // dezaktualizacja kontekstu renderującego
  237. wglMakeCurrent(NULL, NULL) ;
  238. }
  239. if (g_hGLContext!=NULL)
  240. {
  241. wglDeleteContext(g_hGLContext);
  242. g_hGLContext = NULL;
  243. }
  244. glDeleteLists(font_base, 96);
  245. }
  246.  
  247. BOOL SetWindowPixelFormat(HDC hDC)
  248. {
  249. PIXELFORMATDESCRIPTOR pixelDesc;
  250.  
  251. pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
  252. pixelDesc.nVersion = 1;
  253. pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER |PFD_STEREO_DONTCARE;
  254. pixelDesc.iPixelType = PFD_TYPE_RGBA;
  255. pixelDesc.cColorBits = 32;
  256. pixelDesc.cRedBits = 8;
  257. pixelDesc.cRedShift = 16;
  258. pixelDesc.cGreenBits = 8;
  259. pixelDesc.cGreenShift = 8;
  260. pixelDesc.cBlueBits = 8;
  261. pixelDesc.cBlueShift = 0;
  262. pixelDesc.cAlphaBits = 0;
  263. pixelDesc.cAlphaShift = 0;
  264. pixelDesc.cAccumBits = 64;
  265. pixelDesc.cAccumRedBits = 16;
  266. pixelDesc.cAccumGreenBits = 16;
  267. pixelDesc.cAccumBlueBits = 16;
  268. pixelDesc.cAccumAlphaBits = 0;
  269. pixelDesc.cDepthBits = 32;
  270. pixelDesc.cStencilBits = 8;
  271. pixelDesc.cAuxBuffers = 0;
  272. pixelDesc.iLayerType = PFD_MAIN_PLANE;
  273. pixelDesc.bReserved = 0;
  274. pixelDesc.dwLayerMask = 0;
  275. pixelDesc.dwVisibleMask = 0;
  276. pixelDesc.dwDamageMask = 0;
  277. g_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
  278.  
  279. if (g_GLPixelIndex==0)
  280. {
  281. g_GLPixelIndex = 1;
  282.  
  283. if (DescribePixelFormat(hDC, g_GLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
  284. {
  285. return FALSE;
  286. }
  287. }
  288.  
  289. if (SetPixelFormat( hDC, g_GLPixelIndex, &pixelDesc)==FALSE)
  290. {
  291. return FALSE;
  292. }
  293.  
  294. return TRUE;
  295. }
  296. BOOL CreateViewGLContext(HDC hDC)
  297. {
  298. g_hGLContext = wglCreateContext(hDC);
  299.  
  300. if (g_hGLContext == NULL)
  301. {
  302. return FALSE;
  303. }
  304.  
  305. if (wglMakeCurrent(hDC, g_hGLContext)==FALSE)
  306. {
  307. return FALSE;
  308. }
  309.  
  310. return TRUE;
  311. }
  312.  
  313. GLvoid BuildFont(HDC hDC) // Build Our Bitmap Font
  314. {
  315. HFONT font; // Windows Font ID
  316. HFONT oldfont; // Used For Good House Keeping
  317.  
  318. font_base = glGenLists(96); // Storage For 96 Characters
  319.  
  320. font = CreateFont( -18, // Height Of Font
  321. 0, // Width Of Font
  322. 0, // Angle Of Escapement
  323. 0, // Orientation Angle
  324. FW_NORMAL, // Font Weight
  325. FALSE, // Italic
  326. FALSE, // Underline
  327. FALSE, // Strikeout
  328. ANSI_CHARSET, // Character Set Identifier
  329. OUT_TT_PRECIS, // Output Precision
  330. CLIP_DEFAULT_PRECIS, // Clipping Precision
  331. ANTIALIASED_QUALITY, // Output Quality
  332. FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
  333. "Courier New"); // Font Name
  334.  
  335. oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
  336. wglUseFontBitmaps(hDC, 31, 96, font_base); // Builds 96 Characters Starting At Character 32
  337. SelectObject(hDC, oldfont); // Selects The Font We Want
  338. DeleteObject(font); // Delete The Font
  339. }
  340.  
  341. // Napisy w OpenGL
  342. GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
  343. {
  344. char text[256]; // Holds Our String
  345. va_list ap; // Pointer To List Of Arguments
  346.  
  347. if (fmt == NULL) // If There's No Text
  348. return; // Do Nothing
  349.  
  350. va_start(ap, fmt); // Parses The String For Variables
  351. vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
  352. va_end(ap); // Results Are Stored In Text
  353.  
  354. glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
  355. glListBase(font_base - 31); // Sets The Base Character to 32
  356. glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
  357. glPopAttrib(); // Pops The Display List Bits
  358. }
  359.  
  360.  
  361. void TworzListyWyswietlania()
  362. {
  363. glNewList(Wall1,GL_COMPILE); // GL_COMPILE - lista jest kompilowana, ale nie wykonywana
  364.  
  365. glBegin(GL_QUADS ); // inne opcje: GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
  366. // GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUAD_STRIP, GL_POLYGON
  367. glNormal3f( -1.0, 0.0, 0.0);
  368. glVertex3f( -1.0, -1.0, 1.0);
  369. glVertex3f( -1.0, 1.0, 1.0);
  370. glVertex3f( -1.0, 1.0, -1.0);
  371. glVertex3f( -1.0, -1.0, -1.0);
  372. glEnd();
  373. glEndList();
  374.  
  375. glNewList(Wall2,GL_COMPILE);
  376. glBegin(GL_QUADS);
  377. glNormal3f( 1.0, 0.0, 0.0);
  378. glVertex3f( 1.0, -1.0, 1.0);
  379. glVertex3f( 1.0, 1.0, 1.0);
  380. glVertex3f( 1.0, 1.0, -1.0);
  381. glVertex3f( 1.0, -1.0, -1.0);
  382. glEnd();
  383. glEndList();
  384.  
  385. /*glNewList(Floor,GL_COMPILE);
  386. glBegin(GL_POLYGON);
  387. glNormal3f( 0.0, 1.0, 0.0);
  388. glVertex3f( -100, 0, -300.0);
  389. glVertex3f( -100, 0, 100.0);
  390. glVertex3f( 100, 0, 100.0);
  391. glVertex3f( 100, 0, -300.0);
  392. glEnd();
  393. glEndList();*/
  394.  
  395. glNewList(Cube,GL_COMPILE);
  396. glBegin(GL_QUADS);
  397. // przod
  398. glNormal3f( 0.0, 0.0, 1.0);
  399. glVertex3f( 0,0,1);
  400. glVertex3f( 0,1,1);
  401. glVertex3f( 1,1,1);
  402. glVertex3f( 1,0,1);
  403. // tyl
  404. glNormal3f( 0.0, 0.0, -1.0);
  405. glVertex3f( 0,0,0);
  406. glVertex3f( 1,0,0);
  407. glVertex3f( 1,1,0);
  408. glVertex3f( 0,1,0);
  409. // gora
  410. glNormal3f( 0.0, 1.0, 0.0);
  411. glVertex3f( 0,1,0);
  412. glVertex3f( 0,1,1);
  413. glVertex3f( 1,1,1);
  414. glVertex3f( 1,1,0);
  415. // dol
  416. glNormal3f( 0.0, -1.0, 0.0);
  417. glVertex3f( 0,0,0);
  418. glVertex3f( 1,0,0);
  419. glVertex3f( 1,0,1);
  420. glVertex3f( 0,0,1);
  421. // prawo
  422. glNormal3f( 1.0, 0.0, 0.0);
  423. glVertex3f( 1,0,0);
  424. glVertex3f( 1,0,1);
  425. glVertex3f( 1,1,1);
  426. glVertex3f( 1,1,0);
  427. // lewo
  428. glNormal3f( -1.0, 0.0, 0.0);
  429. glVertex3f( 0,0,0);
  430. glVertex3f( 0,1,0);
  431. glVertex3f( 0,1,1);
  432. glVertex3f( 0,0,1);
  433.  
  434. glEnd();
  435. glEndList();
  436.  
  437. }
  438.  
  439.  
  440. void RysujGlobalnyUkladWsp(void)
  441. {
  442.  
  443. glColor3f(1,0,0);
  444. glBegin(GL_LINES);
  445. glVertex3f(0,0,0);
  446. glVertex3f(2,0,0);
  447. glVertex3f(2,-0.25,0.25);
  448. glVertex3f(2,0.25,-0.25);
  449. glVertex3f(2,-0.25,-0.25);
  450. glVertex3f(2,0.25,0.25);
  451.  
  452. glEnd();
  453. glColor3f(0,1,0);
  454. glBegin(GL_LINES);
  455. glVertex3f(0,0,0);
  456. glVertex3f(0,2,0);
  457. glVertex3f(0,2,0);
  458. glVertex3f(0.25,2,0);
  459. glVertex3f(0,2,0);
  460. glVertex3f(-0.25,2,0.25);
  461. glVertex3f(0,2,0);
  462. glVertex3f(-0.25,2,-0.25);
  463.  
  464. glEnd();
  465. glColor3f(0,0,1);
  466. glBegin(GL_LINES);
  467. glVertex3f(0,0,0);
  468. glVertex3f(0,0,2);
  469. glVertex3f(-0.25,-0.25,2);
  470. glVertex3f(0.25,0.25,2);
  471. glVertex3f(-0.25,-0.25,2);
  472. glVertex3f(0.25,-0.25,2);
  473. glVertex3f(-0.25,0.25,2);
  474. glVertex3f(0.25,0.25,2);
  475.  
  476. glEnd();
  477.  
  478. glColor3f(1,1,1);
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement