Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Laborator9.h"
- #include <vector>
- #include <string>
- #include <iostream>
- #include <Core/Engine.h>
- using namespace std;
- Laborator9::Laborator9()
- {
- }
- Laborator9::~Laborator9()
- {
- }
- void Laborator9::Init()
- {
- const string textureLoc = "Source/Laboratoare/Laborator9/Textures/";
- // Load textures
- {
- Texture2D* texture = new Texture2D();
- texture->Load2D((textureLoc + "santa_diffuse.png").c_str(), GL_REPEAT);
- mapTextures["santa"] = texture;
- }
- {
- Texture2D* texture = new Texture2D();
- texture->Load2D("Resources\Textures\black.png", GL_REPEAT);
- mapTextures["black"] = texture;
- }
- {
- Texture2D* texture = new Texture2D();
- texture->Load2D((textureLoc + "blue.jpg").c_str(), GL_REPEAT);
- mapTextures["blue"] = texture;
- }
- {
- Texture2D* texture = new Texture2D();
- texture->Load2D((textureLoc + "grey.jpg").c_str(), GL_REPEAT);
- mapTextures["grey"] = texture;
- }
- // Load meshes
- {
- Mesh* mesh = new Mesh("santa");
- mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "santa.obj");
- meshes[mesh->GetMeshID()] = mesh;
- }
- {
- Mesh* mesh = new Mesh("santa_black");
- mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "santa.obj");
- meshes[mesh->GetMeshID()] = mesh;
- }
- {
- Mesh* mesh = new Mesh("box");
- mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "box.obj");
- meshes[mesh->GetMeshID()] = mesh;
- }
- {
- Mesh* mesh = new Mesh("floor");
- mesh->LoadMesh(RESOURCE_PATH::MODELS + "Primitives", "plane50.obj");
- meshes[mesh->GetMeshID()] = mesh;
- }
- // Create a simple quad
- {
- vector<glm::vec3> vertices
- {
- glm::vec3(0.5f, 0.5f, 0.0f), // Top Right
- glm::vec3(0.5f, -0.5f, 0.0f), // Bottom Right
- glm::vec3(-0.5f, -0.5f, 0.0f), // Bottom Left
- glm::vec3(-0.5f, 0.5f, 0.0f), // Top Left
- };
- vector<glm::vec3> normals
- {
- glm::vec3(0, 1, 1),
- glm::vec3(1, 0, 1),
- glm::vec3(1, 0, 0),
- glm::vec3(0, 1, 0)
- };
- // TODO : Complete texture coordinates for the square
- vector<glm::vec2> textureCoords
- {
- glm::vec2(0.0f, 0.0f),
- glm::vec2(0.0f, 1.0f),
- glm::vec2(1.0f, 1.0f),
- glm::vec2(1.0f, 0.0f),
- };
- vector<unsigned short> indices =
- {
- 0, 1, 3,
- 1, 2, 3
- };
- Mesh* mesh = new Mesh("square");
- mesh->InitFromData(vertices, normals, textureCoords, indices);
- meshes[mesh->GetMeshID()] = mesh;
- }
- // Create a shader program for drawing face polygon with the color of the normal
- {
- Shader *shader = new Shader("ShaderLab9");
- shader->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader.glsl", GL_VERTEX_SHADER);
- shader->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader.glsl", GL_FRAGMENT_SHADER);
- shader->CreateAndLink();
- shaders[shader->GetName()] = shader;
- Shader *shader_toon = new Shader("ShaderLab9Toon");
- shader_toon->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader_Toon.glsl", GL_VERTEX_SHADER);
- shader_toon->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader_Toon.glsl", GL_FRAGMENT_SHADER);
- shader_toon->CreateAndLink();
- shaders[shader_toon->GetName()] = shader_toon;
- Shader *shader_stepped = new Shader("ShaderLab9Stepped");
- shader_stepped->AddShader("Source/Laboratoare/Laborator9/Shaders/VertexShader_Stepped.glsl", GL_VERTEX_SHADER);
- shader_stepped->AddShader("Source/Laboratoare/Laborator9/Shaders/FragmentShader_Stepped.glsl", GL_FRAGMENT_SHADER);
- shader_stepped->CreateAndLink();
- shaders[shader_stepped->GetName()] = shader_stepped;
- }
- }
- void Laborator9::FrameStart()
- {
- // clears the color buffer (using the previously set color) and depth buffer
- glClearColor(0, 0, 0, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glm::ivec2 resolution = window->GetResolution();
- // sets the screen area where to draw
- glViewport(0, 0, resolution.x, resolution.y);
- }
- void Laborator9::Update(float deltaTimeSeconds)
- {
- glEnable(GL_CULL_FACE);
- {
- glCullFace(GL_FRONT);
- glFrontFace(GL_CW);
- glm::mat4 modelMatrix = glm::mat4(1);
- modelMatrix = glm::translate(modelMatrix, glm::vec3(-1, 0, -3));
- modelMatrix = glm::scale(modelMatrix, glm::vec3(0.01, 0.01, 0.01));
- RenderSimpleMesh(meshes["santa"], shaders["ShaderLab9Stepped"], modelMatrix, mapTextures["santa"]);
- }
- {
- glCullFace(GL_FRONT);
- glFrontFace(GL_CCW);
- glm::mat4 modelMatrix = glm::mat4(1);
- modelMatrix = glm::translate(modelMatrix, glm::vec3(-1, 0, -3));
- modelMatrix = glm::scale(modelMatrix, glm::vec3(0.01, 0.01, 0.01));
- RenderSimpleMesh(meshes["santa_black"], shaders["ShaderLab9Toon"], modelMatrix, mapTextures["black"]);
- }
- glDisable(GL_CULL_FACE);
- {
- glm::mat4 modelMatrix = glm::mat4(1);
- modelMatrix = glm::translate(modelMatrix, glm::vec3(1, 1, -3));
- modelMatrix = glm::scale(modelMatrix, glm::vec3(200));
- RenderSimpleMesh(meshes["box"], shaders["ShaderLab9"], modelMatrix, mapTextures["blue"]);
- }
- {
- glm::mat4 modelMatrix = glm::mat4(1);
- modelMatrix = glm::translate(modelMatrix, glm::vec3(1, -0.1, -3));
- modelMatrix = glm::scale(modelMatrix, glm::vec3(2));
- RenderSimpleMesh(meshes["floor"], shaders["ShaderLab9"], modelMatrix, mapTextures["grey"]);
- }
- }
- void Laborator9::FrameEnd()
- {
- DrawCoordinatSystem();
- }
- void Laborator9::RenderSimpleMesh(Mesh *mesh, Shader *shader, const glm::mat4 & modelMatrix, Texture2D* texture1, Texture2D* texture2)
- {
- if (!mesh || !shader || !shader->GetProgramID())
- return;
- // render an object using the specified shader and the specified position
- glUseProgram(shader->program);
- // Bind model matrix
- GLint loc_model_matrix = glGetUniformLocation(shader->program, "Model");
- glUniformMatrix4fv(loc_model_matrix, 1, GL_FALSE, glm::value_ptr(modelMatrix));
- // Bind view matrix
- glm::mat4 viewMatrix = GetSceneCamera()->GetViewMatrix();
- int loc_view_matrix = glGetUniformLocation(shader->program, "View");
- glUniformMatrix4fv(loc_view_matrix, 1, GL_FALSE, glm::value_ptr(viewMatrix));
- // Bind projection matrix
- glm::mat4 projectionMatrix = GetSceneCamera()->GetProjectionMatrix();
- int loc_projection_matrix = glGetUniformLocation(shader->program, "Projection");
- glUniformMatrix4fv(loc_projection_matrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
- int loc_time = glGetUniformLocation(shader->program, "Time");
- if (mesh == meshes["santa_black"]) {
- glUniform1f(loc_time, Engine::GetElapsedTime());
- }
- else {
- glUniform1f(loc_time, 0);
- }
- if (texture1)
- {
- //TODO : activate texture location 0
- glActiveTexture(GL_TEXTURE0);
- //TODO : Bind the texture1 ID
- glBindTexture(GL_TEXTURE_2D, texture1->GetTextureID());
- //TODO : Send texture uniform value
- glUniform1i(glGetUniformLocation(shader->program, "texture_1"), 0);
- }
- if (texture2)
- {
- //TODO : activate texture location 0
- glActiveTexture(GL_TEXTURE1);
- //TODO : Bind the texture1 ID
- glBindTexture(GL_TEXTURE_2D, texture2->GetTextureID());
- //TODO : Send texture uniform value
- glUniform1i(glGetUniformLocation(shader->program, "texture_2"), 1);
- }
- // Draw the object
- glBindVertexArray(mesh->GetBuffers()->VAO);
- glDrawElements(mesh->GetDrawMode(), static_cast<int>(mesh->indices.size()), GL_UNSIGNED_SHORT, 0);
- }
- Texture2D* Laborator9::CreateRandomTexture(unsigned int width, unsigned int height)
- {
- GLuint textureID = 0;
- unsigned int channels = 3;
- unsigned int size = width * height * channels;
- unsigned char* data = new unsigned char[size];
- // TODO: generate random texture data
- for (int i = 0; i < size; ++i)
- data[i] = rand() % 255;
- // Generate and bind the new texture ID
- glGenTextures(1, &textureID);
- glActiveTexture(GL_TEXTURE2);
- glBindTexture(GL_TEXTURE_2D, textureID);
- // TODO: Set the texture parameters (MIN_FILTER, MAG_FILTER and WRAPPING MODE) using glTexParameteri
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4);
- glPixelStorei(GL_PACK_ALIGNMENT, 1);
- CheckOpenGLError();
- // TODO: Use glTextImage2D to set the texture data
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
- // TODO: Generate texture mip-maps
- glGenerateMipmap(GL_TEXTURE_2D);
- CheckOpenGLError();
- // Save the texture into a wrapper Texture2D class for using easier later during rendering phase
- Texture2D* texture = new Texture2D();
- texture->Init(textureID, width, height, channels);
- SAFE_FREE_ARRAY(data);
- return texture;
- }
- // Documentation for the input functions can be found in: "/Source/Core/Window/InputController.h" or
- // https://github.com/UPB-Graphics/Framework-EGC/blob/master/Source/Core/Window/InputController.h
- void Laborator9::OnInputUpdate(float deltaTime, int mods)
- {
- float speed = 2;
- if (!window->MouseHold(GLFW_MOUSE_BUTTON_RIGHT))
- {
- glm::vec3 up = glm::vec3(0, 1, 0);
- glm::vec3 right = GetSceneCamera()->transform->GetLocalOXVector();
- glm::vec3 forward = GetSceneCamera()->transform->GetLocalOZVector();
- forward = glm::normalize(glm::vec3(forward.x, 0, forward.z));
- }
- }
- void Laborator9::OnKeyPress(int key, int mods)
- {
- // add key press event
- }
- void Laborator9::OnKeyRelease(int key, int mods)
- {
- // add key release event
- }
- void Laborator9::OnMouseMove(int mouseX, int mouseY, int deltaX, int deltaY)
- {
- // add mouse move event
- }
- void Laborator9::OnMouseBtnPress(int mouseX, int mouseY, int button, int mods)
- {
- // add mouse button press event
- }
- void Laborator9::OnMouseBtnRelease(int mouseX, int mouseY, int button, int mods)
- {
- // add mouse button release event
- }
- void Laborator9::OnMouseScroll(int mouseX, int mouseY, int offsetX, int offsetY)
- {
- }
- void Laborator9::OnWindowResize(int width, int height)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement