Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.66 KB | None | 0 0
  1. #include "game.h"
  2. #include <iostream>
  3. #include <glm/gtc/matrix_transform.hpp>
  4.  
  5. // Assignment 2
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fstream>
  9. #include <iterator>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. static void printMat(const glm::mat4 mat)
  15. {
  16. std::cout << " matrix:" << std::endl;
  17. for (int i = 0; i < 4; i++)
  18. {
  19. for (int j = 0; j < 4; j++)
  20. std::cout << mat[j][i] << " ";
  21. std::cout << std::endl;
  22. }
  23. }
  24.  
  25. Game::Game() : Scene()
  26. {
  27. }
  28.  
  29. Game::Game(float angle, float relationWH, float near, float far) : Scene(angle, relationWH, near, far)
  30. {
  31. }
  32.  
  33. //parse line to vec4
  34. glm::vec4 Game::parseVec4(const std::string& line)
  35. {
  36. unsigned int tokenLength = line.length();
  37. const char* tokenString = line.c_str();
  38.  
  39. unsigned int vertIndexStart = 2;
  40.  
  41. while (vertIndexStart < tokenLength)
  42. {
  43. if (tokenString[vertIndexStart] != ' ')
  44. break;
  45. vertIndexStart++;
  46. }
  47.  
  48. unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
  49.  
  50. float x = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
  51.  
  52. vertIndexStart = vertIndexEnd + 1;
  53. vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
  54.  
  55. float y = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
  56.  
  57. vertIndexStart = vertIndexEnd + 1;
  58. vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
  59.  
  60. float z = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
  61.  
  62. vertIndexStart = vertIndexEnd + 1;
  63. vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
  64.  
  65. float w = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
  66.  
  67. return glm::vec4(x, y, z, w);
  68.  
  69. //glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()))
  70. }
  71.  
  72. void Game::PrintScene()
  73. {
  74. int positional_index = 0;
  75. std::cout << "coeffs: Num of objects = " << sizes[0] << "\nNum of lights= " << sizes[1] << std::endl;
  76. std::cout << "Objects: " << std::endl;
  77. for (int i = 0; i < sizes[0]; i++)
  78. {
  79. std::cout << "Object: (" << objects[i].x << " , " << objects[i].y << " , " << objects[i].z << " , " << objects[i].w << ")" << std::endl;
  80. std::cout << "Object Ambient:: (" << objColors[i].x << " , " << objColors[i].y << " , " << objColors[i].z << " , " << objColors[i].w << ")" << std::endl;
  81. }
  82. std::cout << "\n" << std::endl;
  83. for (int i = 0; i < sizes[1]; i++) {
  84. std::cout << "Light: (" << lightsDirection[i].x << " , " << lightsDirection[i].y << " , " << lightsDirection[i].z << " , " << lightsDirection[i].w << ")" << std::endl;
  85. std::cout << "Light Intensity:: (" << lightsIntensity[i].x << " , " << lightsIntensity[i].y << " , " << lightsIntensity[i].z << " , " << lightsIntensity[i].w << ")" << std::endl;
  86. if (lightsDirection[i].w != 0) {
  87. std::cout << "Light Position:: (" << lightPosition[positional_index].x << " , " << lightPosition[positional_index].y << " , " << lightPosition[positional_index].z << " , " << lightPosition[positional_index].w << ")" << std::endl;
  88. positional_index++;
  89. }
  90. }
  91.  
  92. std::cout << "\n" << std::endl;
  93. std::cout << "Reflective objects:" << std::endl;
  94. std::cout << "Num of reflective objects: " << sizes[2] << std::endl;
  95. for (int i = 0; i < sizes[2]; i++) {
  96. std::cout << "Index of reflective object: " << objReflective[i].x << std::endl;
  97. std::cout << "Reflective Object: (" << objects[objReflective[i].x].x << " , " << objects[objReflective[i].x].y << " , "
  98. << objects[objReflective[i].x].z << " , " << objects[objReflective[i].x].w << ")" << std::endl;
  99. }
  100.  
  101. }
  102.  
  103. void Game::Init()
  104. {
  105.  
  106.  
  107.  
  108. unsigned int texIDs[1] = { 0 };
  109. AddShader("../res/shaders/pickingShader");
  110. AddShader("../res/shaders/basicShaderAM");
  111.  
  112. AddTexture("../res/textures/box0.bmp", false);
  113. AddMaterial(texIDs, 1);
  114. AddShape(Plane, -1, TRIANGLES);
  115.  
  116. pickedShape = 0;
  117.  
  118. SetShapeMaterial(0, 0);
  119. MoveCamera(0, zTranslate, 10);
  120. pickedShape = -1;
  121.  
  122.  
  123. // -----------------------------------------------Assignment 2-----------------------------------------------
  124.  
  125. std::ifstream file;
  126. file.open("scene.txt");
  127. int ref_index = 0;
  128. std::string line;
  129. if (file.is_open())
  130. {
  131. while (file.good())
  132. {
  133. getline(file, line);
  134.  
  135. unsigned int lineLength = line.length();
  136.  
  137. if (lineLength < 2)
  138. continue;
  139.  
  140. const char* lineCStr = line.c_str();
  141.  
  142. switch (lineCStr[0])
  143. {
  144. case 'e':
  145. eye = parseVec4(line);
  146. break;
  147. case 'a':
  148. ambient = parseVec4(line);
  149. break;
  150. case 'r':
  151. // objReflective.push_back(glm::vec2(ref_index, 0));
  152. case 'o':
  153. case 't':
  154. ref_index++;
  155. objects.push_back(parseVec4(line));
  156. break;
  157. case 'c':
  158. objColors.push_back(parseVec4(line));
  159. break;
  160. case 'd':
  161. lightsDirection.push_back(parseVec4(line));
  162. break;
  163. case 'i':
  164. lightsIntensity.push_back(parseVec4(line));
  165. break;
  166. case 'p':
  167. lightPosition.push_back(parseVec4(line));
  168. break;
  169. }
  170. }
  171. sizes = glm::vec4(objects.size(), lightsDirection.size(), objReflective.size(), 800);
  172. }
  173. else
  174. {
  175. char buf[100];
  176. //std::cout<<"can not open file!"<<std::endl;
  177. strerror_s(buf, errno);
  178. std::cerr << "Error: " << buf;
  179. sizes = glm::vec4(0, 0, 0, 0);
  180. }
  181.  
  182. //ReadPixel(); //uncomment when you are reading from the z-buffer
  183. }
  184.  
  185. void Game::Update(const glm::mat4& MVP, const glm::mat4& Model, const int shaderIndx)
  186. {
  187. Shader* s = shaders[shaderIndx];
  188. int r = ((pickedShape + 1) & 0x000000FF) >> 0;
  189. int g = ((pickedShape + 1) & 0x0000FF00) >> 8;
  190. int b = ((pickedShape + 1) & 0x00FF0000) >> 16;
  191. textures[materials[shapes[pickedShape]->GetMaterial()]->GetTexture(0)]->Bind(0);
  192. s->Bind();
  193. s->SetUniformMat4f("MVP", MVP);
  194. s->SetUniformMat4f("Normal", Model);
  195.  
  196.  
  197.  
  198. //s->SetUniform4fv("eye", &eye, eye.length());
  199. //s->SetUniform4fv("eye", &eye, 4);
  200. s->SetUniform4f("eye", eye.x, eye.y, eye.z, eye.w);
  201. s->SetUniform4f("ambient", ambient.x, ambient.y, ambient.z, ambient.w);
  202. s->SetUniform4f("objects", objects[0].x, objects[0].y, objects[0].z, objects[0].w);
  203. s->SetUniform4f("objColors", objects[0].x, objects[0].y, objects[0].z, objects[0].w);
  204. s->SetUniform4f("lightsDirection", lightsDirection[0].x, lightsDirection[0].y, lightsDirection[0].z, lightsDirection[0].w);
  205. s->SetUniform4f("lightsIntensity", lightsIntensity[0].x, lightsIntensity[0].y, lightsIntensity[0].z, lightsIntensity[0].w);
  206. s->SetUniform4f("lightsPosition", lightPosition[0].x, lightPosition[0].y, lightPosition[0].z, lightPosition[0].w);
  207. s->SetUniform4f("sizes", sizes.x, sizes.y, sizes.z, sizes.w);
  208.  
  209. // TODO: which size do i send?
  210. //s->SetUniform4fv("eye", &eye, eye.length());
  211. //s->SetUniform4fv("eye", &eye, 4);
  212. //s->SetUniform4f("eye", eye.x, eye.y, eye.z, eye.w);
  213. //s->SetUniform4fv("ambient", &ambient, ambient.length());
  214. //s->SetUniform4fv("objects", &objects[0], objects.size());
  215. //s->SetUniform4fv("objColors", &objColors[0], objColors.size());
  216. //s->SetUniform4fv("lightsDirection", &lightsDirection[0], lightsDirection.size());
  217. //s->SetUniform4fv("lightsIntensity", &lightsIntensity[0], lightsIntensity.size());
  218. //s->SetUniform4fv("lightsPosition", &lightPosition[0], lightPosition.size());
  219. //s->SetUniform4fv("sizes", &sizes, sizes.length());
  220. //if (objReflective.size() != 0) {
  221. // s->SetUniform2fv("objReflective", &objReflective, objReflective.size());
  222. //}
  223.  
  224. // -----------------------------------------------Assignment 2 end-----------------------------------------------
  225.  
  226. //s->SetUniform4f("lightDirection", 0.0f , 0.0f, -1.0f, 0.0f);
  227. //if(shaderIndx == 0)
  228. // s->SetUniform4f("lightColor",r/255.0f, g/255.0f, b/255.0f,1.0f);
  229. //else
  230. // s->SetUniform4f("lightColor",0.7f,0.8f,0.1f,1.0f);
  231. s->Unbind();
  232. }
  233.  
  234. void Game::WhenRotate()
  235. {
  236. }
  237.  
  238. void Game::WhenTranslate()
  239. {
  240. PrintScene();
  241. }
  242.  
  243. void Game::Motion()
  244. {
  245. if (isActive)
  246. {
  247. }
  248. }
  249.  
  250. Game::~Game(void)
  251. {
  252. }
  253.  
  254.  
  255.  
  256.  
  257.  
  258. /* BACKUP
  259.  
  260.  
  261. #include "game.h"
  262. #include <iostream>
  263. #include <glm/gtc/matrix_transform.hpp>
  264.  
  265. // Assignment 2
  266. #include <stdio.h>
  267. #include <stdlib.h>
  268. #include <fstream>
  269. #include <iterator>
  270. #include <vector>
  271.  
  272. using namespace std;
  273.  
  274. static void printMat(const glm::mat4 mat)
  275. {
  276. std::cout<<" matrix:"<<std::endl;
  277. for (int i = 0; i < 4; i++)
  278. {
  279. for (int j = 0; j < 4; j++)
  280. std::cout<< mat[j][i]<<" ";
  281. std::cout<<std::endl;
  282. }
  283. }
  284.  
  285. Game::Game() : Scene()
  286. {
  287. }
  288.  
  289. Game::Game(float angle ,float relationWH, float near, float far) : Scene(angle,relationWH,near,far)
  290. {
  291. }
  292.  
  293. void Game::Init()
  294. {
  295.  
  296.  
  297.  
  298. unsigned int texIDs[1] = { 0 };
  299. AddShader("../res/shaders/pickingShader");
  300. AddShader("../res/shaders/basicShader");
  301.  
  302. AddTexture("../res/textures/box0.bmp",false);
  303. AddMaterial(texIDs, 1);
  304. AddShape(Plane,-1,TRIANGLES);
  305.  
  306. pickedShape = 0;
  307.  
  308. SetShapeMaterial(0,0);
  309. MoveCamera(0,zTranslate,10);
  310. pickedShape = -1;
  311.  
  312. //ReadPixel(); //uncomment when you are reading from the z-buffer
  313. }
  314.  
  315. void Game::Update(const glm::mat4 &MVP,const glm::mat4 &Model,const int shaderIndx)
  316. {
  317. Shader *s = shaders[shaderIndx];
  318. int r = ((pickedShape+1) & 0x000000FF) >> 0;
  319. int g = ((pickedShape+1) & 0x0000FF00) >> 8;
  320. int b = ((pickedShape+1) & 0x00FF0000) >> 16;
  321. textures[materials[shapes[pickedShape]->GetMaterial()]->GetTexture(0)]->Bind(0);
  322. s->Bind();
  323. s->SetUniformMat4f("MVP", MVP);
  324. s->SetUniformMat4f("Normal",Model);
  325.  
  326.  
  327. // -----------------------------------------------Assignment 2-----------------------------------------------
  328.  
  329. glm::vec4 coordData = getCoordSize();
  330.  
  331. s->SetUniform1f("theMinusX", coordData.x);
  332. s->SetUniform1f("thePlusX", coordData.y);
  333. s->SetUniform1f("theMinusY", coordData.z);
  334. s->SetUniform1f("thePlusY", coordData.w);
  335.  
  336. FILE* fp;
  337. char buffer[255];
  338.  
  339. char neededBuffer[255];
  340.  
  341. fp = fopen("scene.txt", "r");
  342.  
  343. glm::ivec4 sizes = glm::ivec4(0, 0, 0, 0);
  344.  
  345. std::vector<glm::vec4> objectList;
  346. std::vector<glm::vec4> objectColorList;
  347.  
  348. while (fgets(buffer, 255, (FILE*)fp)) {
  349. char letter;
  350. float x, y, z, w;
  351. sscanf(buffer, "%c %f %f %f %f", &letter, &x, &y, &z, &w);
  352. switch (letter) {
  353. case 'e':
  354. s->SetUniform4f("eye", x, y, z, w);
  355. break;
  356. case 'a':
  357. s->SetUniform4f("ambient", x, y, z, w);
  358. break;
  359. case 'o':
  360. ++sizes.x;
  361. objectList.push_back(glm::vec4(x, y, z, w));
  362. //s->SetUniform4f("objects", x, y, z, w);
  363. break;
  364. case 'r':
  365. break;
  366. case 't':
  367. break;
  368. case 'c':
  369. objectColorList.push_back(glm::vec4(x, y, z, w));
  370. //s->SetUniform4f("objColors", x, y, z, w);
  371. break;
  372. case 'd':
  373. s->SetUniform4f("lightsDirection", x, y, z, w);
  374. ++sizes.y;
  375. break;
  376. case 'p':
  377. s->SetUniform4f("lightPosition", x, y, z, w);
  378. break;
  379. case 'i':
  380. s->SetUniform4f("lightsIntensity", x, y, z, w);
  381. break;
  382.  
  383.  
  384.  
  385. default: break;
  386. }
  387. }
  388. s->SetUniform4i("sizes", objectList.size(), objectColorList.size(), sizes.z, sizes.w);
  389. fclose(fp);
  390. //}
  391. glm::vec4* objects = new glm::vec4[objectList.size()];
  392. glm::vec4* objectsColors = new glm::vec4[objectColorList.size()];
  393.  
  394. int k = 0;
  395. for (auto i = objectList.begin(); i != objectList.end(); ++i) {
  396. objects[k] = *i;
  397. ++k;
  398. }
  399. s->SetUniform4fv("objects", objects, objectList.size());
  400.  
  401. cout << sc
  402. k = 0;
  403. for (auto i = objectColorList.begin(); i != objectColorList.end(); ++i) {
  404. objectsColors[k] = *i;
  405. ++k;
  406. }
  407. s->SetUniform4fv("objColors", objectsColors, objectColorList.size());
  408.  
  409.  
  410. // -----------------------------------------------Assignment 2 end-----------------------------------------------
  411.  
  412. //s->SetUniform4f("lightDirection", 0.0f , 0.0f, -1.0f, 0.0f);
  413. //if(shaderIndx == 0)
  414. // s->SetUniform4f("lightColor",r/255.0f, g/255.0f, b/255.0f,1.0f);
  415. //else
  416. // s->SetUniform4f("lightColor",0.7f,0.8f,0.1f,1.0f);
  417. s->Unbind();
  418. }
  419.  
  420. void Game::WhenRotate()
  421. {
  422. }
  423.  
  424. void Game::WhenTranslate()
  425. {
  426. }
  427.  
  428. void Game::Motion()
  429. {
  430. if(isActive)
  431. {
  432. }
  433. }
  434.  
  435. Game::~Game(void)
  436. {
  437. }
  438.  
  439. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement