Advertisement
CzarnyBarszcz

Mieszanie kolorów

Jun 17th, 2021 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. #include <windows.h>
  2. #ifdef __APPLE__
  3. #include <GLUT/glut.h>
  4. #else
  5. #include <GL/glut.h>
  6. #endif
  7.  
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <iostream>
  11. #include <time.h>
  12.  
  13. using namespace std;
  14.  
  15. enum{
  16. LUSTRO,
  17. MGLA,
  18. CIEN,
  19. };
  20.  
  21. static int slices = 16;
  22. static int stacks = 16;
  23.  
  24. double t1 =0.0;
  25. double t2 =0.0;
  26. double t3 =0.0;
  27.  
  28.  
  29. GLdouble PI = 3.14;
  30. GLdouble beta = -PI / 2;
  31. GLdouble skok_point = 10.0;
  32. GLdouble skok_eye = 1.0;
  33.  
  34. GLdouble eyex=0.0;
  35. GLdouble eyey=0.0;
  36. GLdouble eyez=1.0;
  37.  
  38. GLdouble pointx = skok_point * cos (beta);
  39. GLdouble pointy = 0.0;
  40. GLdouble pointz = skok_point * sin (beta);
  41.  
  42. GLdouble fovy =45;
  43. //GLfloat* fPozycja_swiatla_pod = { 2.0f, 5.0f, 5.0f, 0.0f };
  44. GLfloat* fPozycja_swiatla;
  45. GLdouble alfa=1.0;
  46. int lustro =-1;
  47. int mgla = -1;
  48. int cien = -1;
  49. float g_mgly = 0.2f;
  50. /* GLUT callback Handlers */
  51.  
  52. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  53. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  54. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  55. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  56.  
  57. const GLfloat light_position_under[] = { -2.0f, -5.0f, -5.0f, 0.0f };
  58.  
  59. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  60. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  61. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  62. const GLfloat high_shininess[] = { 100.0f };
  63.  
  64. static void resize(int width, int height)
  65. {
  66. const float ar = (float) width / (float) height;
  67.  
  68. glViewport(0, 0, width, height);
  69. glMatrixMode(GL_PROJECTION);
  70. glLoadIdentity();
  71. glFrustum(-ar, ar, -1.0, 1.0, 2.0, 200.0);
  72.  
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity() ;
  75. }
  76.  
  77. void Podloze()
  78. {
  79. glPushMatrix();
  80. //glColor3d(1,0,0);
  81. //glTranslated(0,-3,-5);
  82. //glutSolidCube(5);
  83. if(lustro==LUSTRO || cien == CIEN){
  84. glColor4d(1,0,0,0.9);
  85. }
  86.  
  87. else{
  88. glColor4d(1,0,0,1);
  89. }
  90. glTranslated(0,-3,-5);
  91. glDisable (GL_LIGHTING);
  92. glEnable (GL_BLEND);
  93. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  94. glutSolidCube(5);
  95. glDisable (GL_BLEND);
  96. glEnable (GL_LIGHTING);
  97. glPopMatrix();
  98. }
  99. /*
  100. void Rys_aktorow(int a)
  101. {
  102. glPushMatrix();
  103. //glColor4d(0,1,0,beta);
  104. glColor4d(0,1,0,1);
  105. glTranslated(0,0,-3);
  106. // glScalef(1.0f, -1.0f, 1.0f).
  107. // glDisable (GL_LIGHTING);
  108. // glEnable (GL_BLEND);
  109. // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  110. glutSolidTorus(0.1,0.3,20,20);
  111. // glDisable (GL_BLEND);
  112. // glEnable (GL_LIGHTING);
  113. glPopMatrix();
  114. }
  115. */
  116. /*
  117. void Rys_aktorow(int a)
  118. {
  119. glColor4f (1.0f, 0.0f, 0.0f, 0.4f);
  120.  
  121. glDisable (GL_LIGHTING);
  122. glEnable (GL_BLEND);
  123. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  124. glTranslated(0,0,-3);
  125. glutSolidCube (0.7f);
  126. glDisable (GL_BLEND);
  127. glEnable (GL_LIGHTING);
  128. }
  129. */
  130. void Rys_aktorow(int a)
  131. {
  132. if(a == 0)
  133. {
  134. glPushMatrix();
  135. glColor4d(0,1,0,alfa);
  136. glTranslated(0,0,-3);
  137. glutSolidTorus(0.1,0.3,20,20);
  138. glPopMatrix();
  139. }
  140. if(a == -1)
  141. {
  142. glPushMatrix();
  143. glColor4d(0,1,0,alfa);
  144. glTranslated(0,-1,-3);
  145. glutSolidTorus(0.1,0.3,20,20);
  146. glPopMatrix();
  147. }
  148. if(a == -2)
  149. {
  150. glPushMatrix();
  151. glColor4d(0,0,0,alfa);
  152. glTranslated(-0.2,-0.6,-3);
  153. glScaled(0.7,0.1,0.5);
  154. glutSolidTorus(0.1,0.3,20,20);
  155. glPopMatrix();
  156. }
  157.  
  158. }
  159. static void display(void)
  160. {
  161. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  162. const double a = t*90.0;
  163.  
  164. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  165. glColor3d(1,0,0);
  166.  
  167.  
  168.  
  169.  
  170. /*
  171. if (lustro == LUSTRO)
  172. {
  173. glLightfv (GL_LIGHT0, GL_POSITION, light_position_under); // ustawienie światła pod podłogą
  174. glPushMatrix ();
  175. glFrontFace (GL_CW); // zmiana domyślnego nawinięcia
  176. glScalef (1.0f, -1.0f, 1.0f); // przeskalowanie położenia wszystkich aktorów
  177. Rys_aktorow (0); // narysowanie aktorów pod podłogą
  178. glFrontFace (GL_CCW); // przywrócenie domyślnego nawinięcia
  179. glPopMatrix ();
  180. glDisable (GL_LIGHTING); // wyłączenie oświetlenia
  181. glEnable (GL_BLEND); // włączenie mieszania kolorów
  182. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // równanie mieszania kolorów
  183. Podloze (); // narysowanie podłogi
  184. glDisable (GL_BLEND); // wyłączenie mieszania
  185. glEnable (GL_LIGHTING); // włączenie oświetlenia
  186. glLightfv (GL_LIGHT0, GL_POSITION, light_position); // ustawienie światła nad podłogą
  187. Rys_aktorow (0); // narysowanie aktorów nad podłogą
  188. }
  189.  
  190. glPushMatrix();
  191. glColor4d(0,0,1,1);
  192. glTranslated(0,-1,-3);
  193. glutSolidTorus(0.1,0.3,20,20);
  194. glPopMatrix();
  195. */
  196. if(cien == CIEN){
  197. Rys_aktorow(0);
  198. Rys_aktorow(-2);
  199. Podloze();
  200. }
  201. if(lustro == LUSTRO)
  202. {
  203. Rys_aktorow(0);
  204. Rys_aktorow(-1);
  205. Podloze();
  206.  
  207. }
  208. if(lustro != LUSTRO)
  209. {
  210. Rys_aktorow (0);
  211. Podloze ();
  212. }
  213.  
  214. if (mgla == MGLA){
  215. // ustalenie koloru mgły
  216. GLfloat Kolor_mgly[] = {0.5, 0.5, 0.5, 1.0};
  217. // ustawienia parametrów mgły
  218. glFogfv(GL_FOG_COLOR, Kolor_mgly); // ustawienie koloru mgły
  219. glFogf(GL_FOG_START, 10.0f); // początek mgły
  220. glFogf(GL_FOG_END, 100.0f); // koniec mgły
  221. glFogf(GL_FOG_DENSITY, 0.2f); // gęstość mgły
  222. glFogf(GL_FOG_MODE, GL_EXP); // rozkład mgły
  223. glEnable(GL_FOG); // włączeni emgły
  224. }
  225. glutSwapBuffers();
  226.  
  227. }
  228.  
  229.  
  230. static void key(unsigned char key, int x, int y)
  231. {
  232. switch (key)
  233. {
  234. case 27 :
  235. case 'q':
  236. exit(0);
  237. break;
  238.  
  239. case '+':
  240. if(alfa<1)
  241. {
  242. alfa+=0.1;
  243. }
  244. cout<<"ALFA : "<<alfa<<endl;
  245. break;
  246.  
  247. case '-':
  248. if(alfa>0.1)
  249. {
  250. alfa-=0.1;;
  251. }
  252. cout<<"ALFA : "<<alfa<<endl;
  253. break;
  254. }
  255.  
  256. glutPostRedisplay();
  257. }
  258.  
  259. static void idle(void)
  260. {
  261. glutPostRedisplay();
  262. }
  263.  
  264.  
  265.  
  266. /* Program entry point */
  267.  
  268. void Menu(int val)
  269. {
  270. switch(val)
  271. {
  272. case LUSTRO:
  273. if(lustro == LUSTRO)
  274. {
  275. lustro = -1.0;
  276. }
  277. else{
  278. lustro = LUSTRO;
  279. }
  280.  
  281. break;
  282. case MGLA:
  283.  
  284. if(mgla == MGLA)
  285. {
  286. mgla = -1.0;
  287. glDisable(GL_FOG);
  288. }
  289. else{
  290. mgla = MGLA;
  291. }
  292. case CIEN:
  293.  
  294. if(cien == CIEN)
  295. {
  296. cien = -1.0;
  297. }
  298. else{
  299. cien = CIEN;
  300. }
  301. }
  302. }
  303.  
  304. int main(int argc, char *argv[])
  305. {
  306. glutInit(&argc, argv);
  307. glutInitWindowSize(640,480);
  308. glutInitWindowPosition(10,10);
  309. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  310.  
  311. glutCreateWindow("GLUT Shapes");
  312.  
  313. glutReshapeFunc(resize);
  314. glutDisplayFunc(display);
  315. glutKeyboardFunc(key);
  316. glutIdleFunc(idle);
  317.  
  318. glutCreateMenu (Menu);
  319. glutAddMenuEntry("ODBICIE LUSTRZANE : ", LUSTRO);
  320. glutAddMenuEntry("MGŁA: ", MGLA);
  321. glutAddMenuEntry("CIEN: ", CIEN);
  322. glutAttachMenu (GLUT_RIGHT_BUTTON);
  323.  
  324. glClearColor(1,1,1,1);
  325. glEnable(GL_CULL_FACE);
  326. glCullFace(GL_BACK);
  327.  
  328. glEnable(GL_DEPTH_TEST);
  329. //glEnable(GL_BLEND);
  330. glDepthFunc(GL_LESS);
  331.  
  332. //glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  333.  
  334. glEnable(GL_LIGHT0);
  335. glEnable(GL_NORMALIZE);
  336. glEnable(GL_COLOR_MATERIAL);
  337. glEnable(GL_LIGHTING);
  338.  
  339. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  340. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  341. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  342. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  343.  
  344. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  345. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  346. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  347. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  348.  
  349. glutMainLoop();
  350.  
  351. return EXIT_SUCCESS;
  352. }
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement