Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. #include "pch.h"
  2.  
  3. sf::Clock function = sf::Clock();
  4. float timeElapsed = function.getElapsedTime().asMilliseconds();
  5.  
  6. PFNGLBLENDEQUATIONEXTPROC glBlendEquation = NULL;
  7. PFNGLBLENDEQUATIONEXTPROC glBlendColor = NULL;
  8.  
  9. sf::Texture t1;
  10.  
  11. struct
  12. {
  13. float theta;
  14. float fi;
  15. float r;
  16. } camera;
  17.  
  18. struct
  19. {
  20. float val;
  21. } fov;
  22.  
  23. struct
  24. {
  25. float x;
  26. float y;
  27. float z;
  28. } translate;
  29.  
  30. void translate_init(void)
  31. {
  32. translate.x = 0;
  33. translate.y = 0;
  34. translate.z = 0;
  35. }
  36.  
  37. struct
  38. {
  39. float x;
  40. float y;
  41. float z;
  42. } scale;
  43.  
  44. void scale_init(void)
  45. {
  46. scale.x = 1;
  47. scale.y = 1;
  48. scale.z = 1;
  49. }
  50.  
  51. struct
  52. {
  53. float x;
  54. float y;
  55. float z;
  56. } rotate;
  57.  
  58. void rotate_init(void)
  59. {
  60. rotate.x = 0;
  61. rotate.y = 0;
  62. rotate.z = 0;
  63. }
  64.  
  65. void camera_init(void)
  66. {
  67. fov.val = 45.0;
  68. camera.theta = 1.57f;
  69. camera.fi = -1.57f;
  70. camera.r = 20.f;
  71. glClearColor(1.0, 1.0, 1.0, 0.0);
  72. glEnable(GL_DEPTH_TEST);
  73. glEnable(GL_LIGHTING);
  74. glEnable(GL_LIGHT0);
  75. glEnable(GL_NORMALIZE);
  76. glEnable(GL_COLOR_MATERIAL);
  77. glEnable(GL_TEXTURE_2D);
  78. glBlendEquation = (PFNGLBLENDEQUATIONEXTPROC)wglGetProcAddress("glBlendEquation");
  79. glBlendColor = (PFNGLBLENDEQUATIONEXTPROC)wglGetProcAddress("glBlendColor");
  80. }
  81.  
  82. void reshapeScreen(int w, int h)
  83. {
  84. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  85. glMatrixMode(GL_PROJECTION);
  86. glLoadIdentity();
  87. gluPerspective(fov.val, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);
  88. glMatrixMode(GL_MODELVIEW);
  89. glLoadIdentity();
  90. };
  91.  
  92. GLUquadric *quad;
  93. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  94. GLfloat light_theta;
  95. GLfloat light_fi;
  96.  
  97. void change_light_position(void)
  98. {
  99. light_position[0] = 15 * cos(light_theta)*cos(light_fi);
  100. light_position[1] = 15 * cos(light_theta)*sin(light_fi);
  101. light_position[2] = 15 * sin(light_theta);
  102. }
  103.  
  104. void texture(void)
  105. {
  106. t1.loadFromFile("./grass.png");
  107. sf::Texture::bind(&t1);
  108. }
  109.  
  110. void drawScene()
  111. {
  112. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  113. glLoadIdentity();
  114. float x = camera.r * cos(camera.theta)*cos(camera.fi);
  115. float xp = camera.r * cos(camera.theta+0.001f)*cos(camera.fi);
  116. float y = camera.r * cos(camera.theta)*sin(camera.fi);
  117. float yp = camera.r * cos(camera.theta+0.001f)*sin(camera.fi);
  118. float z = camera.r * sin(camera.theta);
  119. float zp = camera.r * sin(camera.theta + 0.001f);
  120. gluLookAt(x, y, z, 0.0, 0.0, 0.0, xp, yp, zp);
  121. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  122. glMatrixMode(GL_PROJECTION);
  123. glDisable(GL_LIGHTING);
  124. glLineWidth(1.5);
  125. glBegin(GL_LINES);
  126. glColor3f(1.0f, 0.0f, 0.0f);
  127. glVertex3f(0.0f, 0.0f, 0.0f);
  128. glVertex3f(5.0f, 0.0f, 0.0f);
  129. glColor3f(0.0f, 1.0f, 0.0f);
  130. glVertex3f(0.0f, 0.0f, 0.0f);
  131. glVertex3f(0.0f, 5.0f, 0.0f);
  132. glColor3f(0.0f, 0.0f, 1.0f);
  133. glVertex3f(0.0f, 0.0f, 0.0f);
  134. glVertex3f(0.0f, 0.0f, 5.0f);
  135. glEnd();
  136. glEnable(GL_LINE_STIPPLE);
  137. glLineStipple(3, 0xAAAA);
  138. glBegin(GL_LINES);
  139. glColor3f(1.0f, 0.0f, 0.0f);
  140. glVertex3f(0.0f, 0.0f, 0.0f);
  141. glVertex3f(-5.0f, 0.0f, 0.0f);
  142. glColor3f(0.0f, 1.0f, 0.0f);
  143. glVertex3f(0.0f, 0.0f, 0.0f);
  144. glVertex3f(0.0f, -5.0f, 0.0f);
  145. glColor3f(0.0f, 0.0f, 1.0f);
  146. glVertex3f(0.0f, 0.0f, 0.0f);
  147. glVertex3f(0.0f, 0.0f, -5.0f);
  148. glEnd();
  149. glEnable(GL_LIGHTING);
  150. glDisable(GL_LINE_STIPPLE);
  151. glMatrixMode(GL_MODELVIEW);
  152. glTranslatef(translate.x, translate.y, translate.z);
  153. glScalef(scale.x, scale.y, scale.z);
  154. glRotatef(rotate.x, 1, 0, 0);
  155. glRotatef(rotate.y, 0, 1, 0);
  156. glLineWidth(3);
  157. glBegin(GL_LINES);
  158. glColor3f(0.0f, 0.0f, 0.0f);
  159. glVertex3f(2.0f, 2.0f, -2.0f);
  160. glVertex3f(2.0f, 2.0f, 2.0f);
  161. glVertex3f(2.0f, 2.0f, 2.0f);
  162. glVertex3f(2.0f, -2.0f, 2.0f);
  163. glVertex3f(-2.0f, 2.0f, 2.0f);
  164. glVertex3f(2.0f, 2.0f, 2.0f);
  165. glVertex3f(-2.0f, -2.0f, -2.0f);
  166. glVertex3f(-2.0f, -2.0f, 2.0f);
  167. glVertex3f(-2.0f, -2.0f, 2.0f);
  168. glVertex3f(2.0f, -2.0f, 2.0f);
  169. glVertex3f(-2.0f, -2.0f, -2.0f);
  170. glVertex3f(2.0f, -2.0f, -2.0f);
  171. glVertex3f(-2.0f, 2.0f, 2.0f);
  172. glVertex3f(-2.0f, -2.0f, 2.0f);
  173. glVertex3f(2.0f, -2.0f, -2.0f);
  174. glVertex3f(2.0f, -2.0f, 2.0f);
  175. glVertex3f(-2.0f, 2.0f, -2.0f);
  176. glVertex3f(-2.0f, -2.0f, -2.0f);
  177. glVertex3f(-2.0f, 2.0f, -2.0f);
  178. glVertex3f(-2.0f, 2.0f, 2.0f);
  179. glVertex3f(2.0f, 2.0f, -2.0f);
  180. glVertex3f(2.0f, -2.0f, -2.0f);
  181. glVertex3f(-2.0f, 2.0f, -2.0f);
  182. glVertex3f(2.0f, 2.0f, -2.0f);
  183. glEnd();
  184. glBegin(GL_TRIANGLES);
  185. glColor3f(1.0f, 0.0f, 0.0f);
  186. glVertex3f(2.0f, 2.0f, -2.0f);
  187. glColor3f(0.0f, 1.0f, 0.0f);
  188. glVertex3f(-2.0f, 2.0f, 2.0f);
  189. glColor3f(0.0f, .0f, 1.0f);
  190. glVertex3f(2.0f, -2.0f, 2.0f);
  191. glEnd();
  192.  
  193. quad = gluNewQuadric();
  194.  
  195. glDisable(GL_COLOR_MATERIAL);
  196. glPushMatrix();
  197. glMaterialfv(GL_FRONT, GL_AMBIENT, PolishedGoldAmbient);
  198. glMaterialfv(GL_FRONT, GL_DIFFUSE, PolishedGoldDiffuse);
  199. glMaterialfv(GL_FRONT, GL_SPECULAR, PolishedGoldSpecular);
  200. glMaterialfv(GL_FRONT, GL_SHININESS, PolishedGoldShininess);
  201. //glColor3f(1.0f, 0.84f, 0.0f);
  202. glTranslatef(7.0, 0, 0);
  203. gluSphere(quad, 2.0, 15, 15);
  204. glPopMatrix();
  205. glRotatef(timeElapsed, 1, 0, 0);
  206. glPushMatrix();
  207. //glColor3f(0.31f, 0.78f, 0.47f);
  208. glTranslatef(-7.0, 0, 0);
  209.  
  210. glMaterialfv(GL_FRONT, GL_AMBIENT, EmeraldAmbient);
  211. glMaterialfv(GL_FRONT, GL_DIFFUSE, EmeraldDiffuse);
  212. glMaterialfv(GL_FRONT, GL_SPECULAR, EmeraldSpecular);
  213. glMaterialfv(GL_FRONT, GL_SHININESS, EmeraldShininess);
  214. gluCylinder(quad, 3.0, 0.0, 5.0, 15, 15);
  215. glPopMatrix();
  216. glEnable(GL_TEXTURE_2D);
  217. glBegin(GL_QUADS);
  218. glTexCoord2f(0, 0);
  219. glVertex3f(-2.0f, 2.0f, 5.0f);
  220. glTexCoord2f(0, 1);
  221. glVertex3f(-2.0f, -2.0f, 5.0f);
  222. glTexCoord2f(1, 0);
  223. glVertex3f(2.0f, -2.0f, 5.0f);
  224. glTexCoord2f(1, 1);
  225. glVertex3f(2.0f, 2.0f, 5.0f);
  226. glEnd();
  227. glDisable(GL_TEXTURE_2D);
  228. glEnable(GL_COLOR_MATERIAL);
  229. glEnable(GL_BLEND);
  230. glBlendEquation(GL_ONE_MINUS_SRC_ALPHA);
  231. glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
  232. glBegin(GL_QUADS);
  233. glColor4f(1.0f, 0.0f, 0.0f, 0.2f);
  234. glVertex3f(5.0f, 5.0f, -7.0f);
  235. glColor4f(0.0f, 1.0f, 0.0f, 0.2f);
  236. glVertex3f(-5.0f, 5.0f, -7.0f);
  237. glColor4f(1.0f, 0.0f, 1.0f, 0.2f);
  238. glVertex3f(-5.0f, -5.0f, -7.0f);
  239. glColor4f(0.7f, 0.7f, 1.0f, 0.2f);
  240. glVertex3f(5.0f, -5.0f, -7.0f);
  241. glEnd();
  242. glDisable(GL_BLEND);
  243.  
  244.  
  245. }
  246.  
  247. int main()
  248. {
  249. sf::ContextSettings context(24, 0, 0, 4, 5);
  250. sf::RenderWindow window(sf::VideoMode(800, 600), "Open GL", 7U, context);
  251. bool running = true;
  252. translate_init();
  253. camera_init();
  254. scale_init();
  255. texture();
  256. reshapeScreen(window.getSize().x, window.getSize().y);
  257. while (running)
  258. {
  259. sf::Event event;
  260. while (window.pollEvent(event))
  261. {
  262. if (event.type == sf::Event::Closed)
  263. {
  264. running = false;
  265. }
  266. if(event.type == sf::Event::Resized) reshapeScreen(window.getSize().x, window.getSize().y);
  267. }
  268. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))camera.fi -= 0.0005f;
  269. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))camera.fi += 0.0005f;
  270. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))camera.theta -= 0.0005f;
  271. if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))camera.theta += 0.0005f;
  272. if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.y += 0.005f;
  273. if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.y -= 0.005f;
  274. if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)&& sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.z += 0.005f;
  275. if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.z -= 0.005f;
  276. if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))translate.x -= 0.005f;
  277. if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))translate.x += 0.005f;
  278. //if (sf::keyboard::iskeypressed(sf::keyboard::))scale.y += 0.001f;
  279. //if (sf::keyboard::iskeypressed(sf::keyboard::g))scale.y -= 0.001f;
  280. if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
  281. {
  282. fov.val += 0.01f;
  283. reshapeScreen(window.getSize().x, window.getSize().y);
  284. // camera.r += 0.001f;
  285. };
  286. if (sf::Keyboard::isKeyPressed(sf::Keyboard::G))
  287. {
  288. fov.val -= 0.01f;
  289. reshapeScreen(window.getSize().x, window.getSize().y);
  290. // camera.r -= 0.001f;
  291. };
  292. if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))scale.x -= 0.001f;
  293. if (sf::Keyboard::isKeyPressed(sf::Keyboard::H))scale.x += 0.001f;
  294. /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::I))rotate.y += 0.05f;
  295. if (sf::Keyboard::isKeyPressed(sf::Keyboard::K))rotate.y -= 0.05f;
  296. if (sf::Keyboard::isKeyPressed(sf::Keyboard::J))rotate.x -= 0.05f;
  297. if (sf::Keyboard::isKeyPressed(sf::Keyboard::L))rotate.x += 0.05f;*/
  298. if (sf::Keyboard::isKeyPressed(sf::Keyboard::I))
  299. {
  300. light_theta += 0.0005f;
  301. change_light_position();
  302. }
  303. if (sf::Keyboard::isKeyPressed(sf::Keyboard::K))
  304. {
  305. light_theta -= 0.0005f;
  306. change_light_position();
  307. }
  308. if (sf::Keyboard::isKeyPressed(sf::Keyboard::J))
  309. {
  310. light_fi -= 0.0005f;
  311. change_light_position();
  312. }
  313. if (sf::Keyboard::isKeyPressed(sf::Keyboard::L))
  314. {
  315. light_fi += 0.0005f;
  316. change_light_position();
  317. }
  318.  
  319. drawScene();
  320. window.display();
  321.  
  322. }
  323.  
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement