Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.82 KB | None | 0 0
  1. #include "Laborator9.h"
  2.  
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6.  
  7. #include <Core/Engine.h>
  8.  
  9. using namespace std;
  10.  
  11. Laborator9::Laborator9()
  12. {
  13. }
  14.  
  15. Laborator9::~Laborator9()
  16. {
  17. }
  18.  
  19. void Laborator9::Init()
  20. {
  21. const string textureLoc = "Source/Laboratoare/Laborator9/Textures/";
  22.  
  23. // Load textures
  24. {
  25. Texture2D* texture = new Texture2D();
  26. texture->Load2D((textureLoc + "santa_diffuse.png").c_str(), GL_REPEAT);
  27. mapTextures["santa"] = texture;
  28. }
  29.  
  30. {
  31. Texture2D* texture = new Texture2D();
  32. texture->Load2D("Resources\Textures\black.png", GL_REPEAT);
  33. mapTextures["black"] = texture;
  34. }
  35.  
  36.  
  37. {
  38. Texture2D* texture = new Texture2D();
  39. texture->Load2D((textureLoc + "blue.jpg").c_str(), GL_REPEAT);
  40. mapTextures["blue"] = texture;
  41. }
  42.  
  43. {
  44. Texture2D* texture = new Texture2D();
  45. texture->Load2D((textureLoc + "grey.jpg").c_str(), GL_REPEAT);
  46. mapTextures["grey"] = texture;
  47. }
  48.  
  49. // Load meshes
  50. {
  51. Mesh* mesh = new Mesh("santa");
  52. mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "santa.obj");
  53. meshes[mesh->GetMeshID()] = mesh;
  54. }
  55.  
  56. {
  57. Mesh* mesh = new Mesh("santa_black");
  58. mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "santa.obj");
  59. meshes[mesh->GetMeshID()] = mesh;
  60. }
  61.  
  62. {
  63. Mesh* mesh = new Mesh("box");
  64. mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "box.obj");
  65. meshes[mesh->GetMeshID()] = mesh;
  66. }
  67.  
  68. {
  69. Mesh* mesh = new Mesh("floor");
  70. mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "plane50.obj");
  71. meshes[mesh->GetMeshID()] = mesh;
  72. }
  73.  
  74. // Create a simple quad
  75. {
  76. vector<glm::vec3> vertices
  77. {
  78. glm::vec3(0.5f, 0.5f, 0.0f), // Top Right
  79. glm::vec3(0.5f, -0.5f, 0.0f), // Bottom Right
  80. glm::vec3(-0.5f, -0.5f, 0.0f), // Bottom Left
  81. glm::vec3(-0.5f, 0.5f, 0.0f), // Top Left
  82. };
  83.  
  84. vector<glm::vec3> normals
  85. {
  86. glm::vec3(0, 1, 1),
  87. glm::vec3(1, 0, 1),
  88. glm::vec3(1, 0, 0),
  89. glm::vec3(0, 1, 0)
  90. };
  91.  
  92.  
  93. // TODO : Complete texture coordinates for the square
  94. vector<glm::vec2> textureCoords
  95. {
  96. glm::vec2(0.0f, 0.0f),
  97. glm::vec2(0.0f, 1.0f),
  98. glm::vec2(1.0f, 1.0f),
  99. glm::vec2(1.0f, 0.0f),
  100.  
  101. };
  102.  
  103. vector<unsigned short> indices =
  104. {
  105. 0, 1, 3,
  106. 1, 2, 3
  107. };
  108.  
  109. Mesh* mesh = new Mesh("square");
  110. mesh->InitFromData(vertices, normals, textureCoords, indices);
  111. meshes[mesh->GetMeshID()] = mesh;
  112. }
  113.  
  114. // Create a shader program for drawing face polygon with the color of the normal
  115. {
  116. Shader *shader = new Shader("ShaderLab9");
  117. shader->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader.glsl", GL_VERTEX_SHADER);
  118. shader->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader.glsl", GL_FRAGMENT_SHADER);
  119. shader->CreateAndLink();
  120. shaders[shader->GetName()] = shader;
  121.  
  122. Shader *shader_toon = new Shader("ShaderLab9Toon");
  123. shader_toon->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader_Toon.glsl", GL_VERTEX_SHADER);
  124. shader_toon->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader_Toon.glsl", GL_FRAGMENT_SHADER);
  125. shader_toon->CreateAndLink();
  126. shaders[shader_toon->GetName()] = shader_toon;
  127.  
  128. Shader *shader_stepped = new Shader("ShaderLab9Stepped");
  129. shader_stepped->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader_Stepped.glsl", GL_VERTEX_SHADER);
  130. shader_stepped->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader_Stepped.glsl", GL_FRAGMENT_SHADER);
  131. shader_stepped->CreateAndLink();
  132. shaders[shader_stepped->GetName()] = shader_stepped;
  133. }
  134. }
  135.  
  136. void Laborator9::FrameStart()
  137. {
  138. // clears the color buffer (using the previously set color) and depth buffer
  139. glClearColor(0, 0, 0, 1);
  140. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  141.  
  142. glm::ivec2 resolution = window->GetResolution();
  143. // sets the screen area where to draw
  144. glViewport(0, 0, resolution.x, resolution.y);
  145. }
  146.  
  147. void Laborator9::Update(float deltaTimeSeconds)
  148. {
  149. glEnable(GL_CULL_FACE);
  150. {
  151. glCullFace(GL_FRONT);
  152. glFrontFace(GL_CW);
  153. glm::mat4 modelMatrix = glm::mat4(1);
  154. modelMatrix = glm::translate(modelMatrix, glm::vec3(-1, 0, -3));
  155. modelMatrix = glm::scale(modelMatrix, glm::vec3(0.01, 0.01, 0.01));
  156. RenderSimpleMesh(meshes["santa"], shaders["ShaderLab9Stepped"], modelMatrix, mapTextures["santa"]);
  157. }
  158.  
  159. {
  160. glCullFace(GL_FRONT);
  161. glFrontFace(GL_CCW);
  162. glm::mat4 modelMatrix = glm::mat4(1);
  163. modelMatrix = glm::translate(modelMatrix, glm::vec3(-1, 0, -3));
  164. modelMatrix = glm::scale(modelMatrix, glm::vec3(0.01, 0.01, 0.01));
  165. RenderSimpleMesh(meshes["santa_black"], shaders["ShaderLab9Toon"], modelMatrix, mapTextures["black"]);
  166. }
  167.  
  168. glDisable(GL_CULL_FACE);
  169.  
  170. {
  171. glm::mat4 modelMatrix = glm::mat4(1);
  172. modelMatrix = glm::translate(modelMatrix, glm::vec3(1, 1, -3));
  173. modelMatrix = glm::scale(modelMatrix, glm::vec3(200));
  174. RenderSimpleMesh(meshes["box"], shaders["ShaderLab9"], modelMatrix, mapTextures["blue"]);
  175. }
  176.  
  177. {
  178. glm::mat4 modelMatrix = glm::mat4(1);
  179. modelMatrix = glm::translate(modelMatrix, glm::vec3(1, -0.1, -3));
  180. modelMatrix = glm::scale(modelMatrix, glm::vec3(2));
  181. RenderSimpleMesh(meshes["floor"], shaders["ShaderLab9"], modelMatrix, mapTextures["grey"]);
  182. }
  183. }
  184.  
  185. void Laborator9::FrameEnd()
  186. {
  187. DrawCoordinatSystem();
  188. }
  189.  
  190. void Laborator9::RenderSimpleMesh(Mesh *mesh, Shader *shader, const glm::mat4 & modelMatrix, Texture2D* texture1, Texture2D* texture2)
  191. {
  192. if (!mesh || !shader || !shader->GetProgramID())
  193. return;
  194.  
  195. // render an object using the specified shader and the specified position
  196. glUseProgram(shader->program);
  197.  
  198. // Bind model matrix
  199. GLint loc_model_matrix = glGetUniformLocation(shader->program, "Model");
  200. glUniformMatrix4fv(loc_model_matrix, 1, GL_FALSE, glm::value_ptr(modelMatrix));
  201.  
  202. // Bind view matrix
  203. glm::mat4 viewMatrix = GetSceneCamera()->GetViewMatrix();
  204. int loc_view_matrix = glGetUniformLocation(shader->program, "View");
  205. glUniformMatrix4fv(loc_view_matrix, 1, GL_FALSE, glm::value_ptr(viewMatrix));
  206.  
  207. // Bind projection matrix
  208. glm::mat4 projectionMatrix = GetSceneCamera()->GetProjectionMatrix();
  209. int loc_projection_matrix = glGetUniformLocation(shader->program, "Projection");
  210. glUniformMatrix4fv(loc_projection_matrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
  211.  
  212. int loc_time = glGetUniformLocation(shader->program, "Time");
  213.  
  214. if (mesh == meshes["santa_black"]) {
  215. glUniform1f(loc_time, Engine::GetElapsedTime());
  216. }
  217. else {
  218.  
  219. glUniform1f(loc_time, 0);
  220.  
  221. }
  222.  
  223. if (texture1)
  224. {
  225. //TODO : activate texture location 0
  226. glActiveTexture(GL_TEXTURE0);
  227.  
  228. //TODO : Bind the texture1 ID
  229. glBindTexture(GL_TEXTURE_2D, texture1->GetTextureID());
  230.  
  231. //TODO : Send texture uniform value
  232. glUniform1i(glGetUniformLocation(shader->program, "texture_1"), 0);
  233. }
  234.  
  235. if (texture2)
  236. {
  237. //TODO : activate texture location 0
  238. glActiveTexture(GL_TEXTURE1);
  239.  
  240. //TODO : Bind the texture1 ID
  241. glBindTexture(GL_TEXTURE_2D, texture2->GetTextureID());
  242.  
  243. //TODO : Send texture uniform value
  244. glUniform1i(glGetUniformLocation(shader->program, "texture_2"), 1);
  245. }
  246.  
  247. // Draw the object
  248. glBindVertexArray(mesh->GetBuffers()->VAO);
  249. glDrawElements(mesh->GetDrawMode(), static_cast<int>(mesh->indices.size()), GL_UNSIGNED_SHORT, 0);
  250. }
  251.  
  252. Texture2D* Laborator9::CreateRandomTexture(unsigned int width, unsigned int height)
  253. {
  254. GLuint textureID = 0;
  255. unsigned int channels = 3;
  256. unsigned int size = width * height * channels;
  257. unsigned char* data = new unsigned char[size];
  258.  
  259. // TODO: generate random texture data
  260. for (int i = 0; i < size; ++i)
  261. data[i] = rand() % 255;
  262.  
  263. // Generate and bind the new texture ID
  264. glGenTextures(1, &textureID);
  265. glActiveTexture(GL_TEXTURE2);
  266.  
  267. glBindTexture(GL_TEXTURE_2D, textureID);
  268.  
  269. // TODO: Set the texture parameters (MIN_FILTER, MAG_FILTER and WRAPPING MODE) using glTexParameteri
  270. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  271.  
  272. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  273.  
  274. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_LINEAR);
  275. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4);
  276.  
  277. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  278. CheckOpenGLError();
  279.  
  280. // TODO: Use glTextImage2D to set the texture data
  281. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  282.  
  283. // TODO: Generate texture mip-maps
  284. glGenerateMipmap(GL_TEXTURE_2D);
  285.  
  286. CheckOpenGLError();
  287.  
  288. // Save the texture into a wrapper Texture2D class for using easier later during rendering phase
  289. Texture2D* texture = new Texture2D();
  290. texture->Init(textureID, width, height, channels);
  291.  
  292. SAFE_FREE_ARRAY(data);
  293. return texture;
  294. }
  295.  
  296. // Documentation for the input functions can be found in: "/Source/Core/Window/InputController.h" or
  297. // https://github.com/UPB-Graphics/Framework-EGC/blob/master/Source/Core/Window/InputController.h
  298.  
  299. void Laborator9::OnInputUpdate(float deltaTime, int mods)
  300. {
  301. float speed = 2;
  302.  
  303. if (!window->MouseHold(GLFW_MOUSE_BUTTON_RIGHT))
  304. {
  305. glm::vec3 up = glm::vec3(0, 1, 0);
  306. glm::vec3 right = GetSceneCamera()->transform->GetLocalOXVector();
  307. glm::vec3 forward = GetSceneCamera()->transform->GetLocalOZVector();
  308. forward = glm::normalize(glm::vec3(forward.x, 0, forward.z));
  309. }
  310. }
  311.  
  312. void Laborator9::OnKeyPress(int key, int mods)
  313. {
  314. // add key press event
  315. }
  316.  
  317. void Laborator9::OnKeyRelease(int key, int mods)
  318. {
  319. // add key release event
  320. }
  321.  
  322. void Laborator9::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
  323. {
  324. // add mouse move event
  325. }
  326.  
  327. void Laborator9::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
  328. {
  329. // add mouse button press event
  330. }
  331.  
  332. void Laborator9::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
  333. {
  334. // add mouse button release event
  335. }
  336.  
  337. void Laborator9::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
  338. {
  339. }
  340.  
  341. void Laborator9::OnWindowResize(int width, int height)
  342. {
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement