diff --git a/Controller.cpp b/Controller.cpp index 613bbd5..bb2ae4c 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include @@ -14,6 +14,7 @@ #include "Controller.h" #include "Texture.h" +#include "Mesh.h" Controller::Controller() { if(!glfwInit()) exit(-1); @@ -44,40 +45,48 @@ void Controller::run() { ShaderProgram shaderProgram("res/shaders/basic.vs", "res/shaders/basic.fs"); shaderProgram.bind(); - Vertex vertices[] = { + std::vector vertices { Vertex{ glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)}, Vertex{ glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)}, Vertex{ glm::vec3(-0.5f, 0.5f, 0.0f), glm::vec2(0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)}, Vertex{ glm::vec3(0.5f, 0.5f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)} }; - uint32_t indices[] = { + std::vector indices { 0, 1, 2, 1, 2, 3 }; - uint32_t numVertices = sizeof(vertices) / sizeof(Vertex); - uint32_t numIndices = sizeof(indices) / sizeof(indices[0]); + std::vector textures { + new Texture("res/tex2.png", texture_diffuse), + new Texture("res/tex1.png", texture_diffuse) + }; - VertexBuffer vertexBuffer(vertices, indices, numVertices, numIndices); - - Texture tex1("res/tex.png", shaderProgram.getShaderProgramId()); + Mesh mesh1(vertices, indices, textures); glm::mat4 model = glm::mat4(1.0f); @@ -98,7 +107,8 @@ void Controller::run() { // Update game // ... - model = glm::rotate(model, (float)(this->deltaTime*0.0005), glm::vec3(0.0f, 1.0f, 0.0f)); + shaderProgram.setUniform("mix_val", (float) (sin(glfwGetTime())*sin(glfwGetTime()))); + model = glm::rotate(model, (float)(this->deltaTime*0.005), glm::vec3(0.0f, 1.0f, 0.0f)); //camera->lookAtTarget(glm::vec3(0.0f, 0.0f, 0.0f)); camera->lookForward(); @@ -109,18 +119,14 @@ void Controller::run() { // Render and buffer swap glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - vertexBuffer.bind(); - tex1.bind(0); for(int i=0;i<20;i++) { - glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); - model = glm::translate(model, glm::vec3(0.0f, 0.0f, 1.0f)); + mesh1.draw(&shaderProgram); + model = glm::translate(model, glm::vec3(0.0f, 0.0f, -1.0f)); camera->updateVPM(); glm::mat4 modelViewProj = camera->getViewProj() * model; shaderProgram.setUniform("u_modelViewProj", modelViewProj); } - model = glm::translate(model, glm::vec3(0.0f, 0.0f, -20.0f)); - tex1.unbind(); - vertexBuffer.unbind(); + model = glm::translate(model, glm::vec3(0.0f, 0.0f, 20.0f)); glfwSwapBuffers(gameWindow->getGLFWwindow()); @@ -135,6 +141,8 @@ void Controller::run() { camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY()); } + delete textures[0]; + } void Controller::limit_framerate() { diff --git a/Fall-Fever b/Fall-Fever new file mode 120000 index 0000000..4f45bfc --- /dev/null +++ b/Fall-Fever @@ -0,0 +1 @@ +/home/derek/Projekte/Fall-Fever/build/Fall-Fever \ No newline at end of file diff --git a/Mesh.cpp b/Mesh.cpp index 49582af..c28a777 100644 --- a/Mesh.cpp +++ b/Mesh.cpp @@ -1 +1,38 @@ #include "Mesh.h" + +Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) + : vertices(vertices), + indices(indices), + textures(textures), + vertexBuffer(static_cast(vertices.data()), static_cast(indices.data()), vertices.size(), indices.size()) { + + +} + +void Mesh::draw(ShaderProgram *shaderProgram) { + + uint8_t typeNumberCount[TEXTURE_TYPE_NUM_ITEMS] {0}; + + // Bind all textures in order to its texture unit + for(auto it = textures.begin(); it != textures.end(); it++) { + int i = it - textures.begin(); + + uint8_t currentTextureType = (*it)->getTextureType(); + + (*it)->bind(i, shaderProgram, typeNumberCount[currentTextureType]); + + typeNumberCount[currentTextureType] += 1; + } + + // Draw elements + vertexBuffer.bind(); + glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); + vertexBuffer.unbind(); + + // Unbind all textures + for(auto it = textures.begin(); it != textures.end(); it++) { + (**it).unbind(); + } + + +} diff --git a/Mesh.h b/Mesh.h index 6f70f09..c3506c4 100644 --- a/Mesh.h +++ b/Mesh.h @@ -1 +1,30 @@ #pragma once + +#include + +#include "ShaderProgram.h" +#include "VertexBuffer.h" +#include "Texture.h" +#include "defines.h" + +class Mesh { + +public: + + Mesh(std::vector vertices, std::vector indices, std::vector textures); + ~Mesh() = default; + + void draw(ShaderProgram *shaderProgram); + +private: + + std::vector vertices; + std::vector indices; + std::vector textures; + + VertexBuffer vertexBuffer; + //ShaderProgram *shaderProgram; + + void setupMesh(); + +}; \ No newline at end of file diff --git a/Texture.cpp b/Texture.cpp index 2c3f82d..f2288f0 100644 --- a/Texture.cpp +++ b/Texture.cpp @@ -3,8 +3,10 @@ #include #include -Texture::Texture(const char* texturePath, GLuint shaderProgramId) { - this->shaderProgramId = shaderProgramId; +Texture::Texture(const char* texturePath, uint8_t textureType) { + + this->texturePath = texturePath; + this->textureType = textureType; stbi_set_flip_vertically_on_load(1); stbi_uc *textureBuffer = stbi_load(texturePath, &textureWidth, &textureHeight, &bitsPerPixel, 4); @@ -42,9 +44,27 @@ Texture::~Texture() { glDeleteTextures(1, &textureId); } -void Texture::bind(uint8_t textureUnit) { - GLint uniformLoc = glGetUniformLocation(shaderProgramId, "u_texture"); - glUniform1i(uniformLoc, textureUnit); +void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t textureTypeNum) { + std::string uniformName = "u_texture_"; + + switch(textureType) { + + case texture_diffuse: + uniformName += "diffuse" + std::to_string(textureTypeNum); + break; + case texture_specular: + uniformName += "specular" + std::to_string(textureTypeNum); + break; + case texture_height: + uniformName += "height" + std::to_string(textureTypeNum); + break; + case texture_normal: + uniformName += "normal" + std::to_string(textureTypeNum); + break; + } + + shaderProgram->setUniform(uniformName.c_str(), textureTypeNum); + glActiveTexture(GL_TEXTURE0 + textureUnit); glBindTexture(GL_TEXTURE_2D, textureId); } diff --git a/Texture.h b/Texture.h index 0917bc5..1df0f93 100644 --- a/Texture.h +++ b/Texture.h @@ -1,29 +1,35 @@ #pragma once +#include "ShaderProgram.h" + #include #include #include +enum textureType{texture_diffuse, texture_specular, texture_normal, texture_height, TEXTURE_TYPE_NUM_ITEMS}; + class Texture { public: - Texture(const char* texturePath, GLuint shaderProgramId); + Texture(const char* texturePath, uint8_t textureType); ~Texture(); - void bind(uint8_t textureUnit); + void bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t textureTypeNum); void unbind(); + uint8_t getTextureType() { return textureType; } + private: + std::string texturePath; + int32_t textureWidth; int32_t textureHeight; int32_t bitsPerPixel; GLuint textureId; - std::string textureType; - - GLuint shaderProgramId; + uint8_t textureType; }; \ No newline at end of file diff --git a/VertexBuffer.cpp b/VertexBuffer.cpp index 07441f1..1b9318d 100644 --- a/VertexBuffer.cpp +++ b/VertexBuffer.cpp @@ -24,11 +24,15 @@ VertexBuffer::VertexBuffer(void *vertexData, void *indexData, uint32_t numVertic glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, textureCoords)); - // Color + // Normal vectors glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, color)); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, normalVec)); + + // Color + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, color)); - // This will also unbind the vertex buffer + // This will also unbind the vertex buffer and index buffer glBindVertexArray(0); } diff --git a/Window.cpp b/Window.cpp index 107f330..2472863 100644 --- a/Window.cpp +++ b/Window.cpp @@ -28,7 +28,7 @@ Window::Window() { glEnable(GL_DEPTH_TEST); // Disable mouse cursor - setGrabbedCursor(1); + setGrabbedCursor(0); #ifdef _DEBUG std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; diff --git a/defines.h b/defines.h index dbb0d5f..13660e9 100644 --- a/defines.h +++ b/defines.h @@ -12,7 +12,13 @@ struct Vertex { glm::vec2 textureCoords; // Normal vector - glm::vec3 normal; + glm::vec3 normalVec; + + // Tangent vector + glm::vec3 tangentVec; + + // Bittangent vector + glm::vec3 bitangentVec; // Color glm::vec4 color; diff --git a/res/shaders/basic.fs b/res/shaders/basic.fs index a116e09..8ae3cfa 100644 --- a/res/shaders/basic.fs +++ b/res/shaders/basic.fs @@ -5,10 +5,14 @@ layout(location = 0) out vec4 f_color; in vec4 v_color; in vec2 v_texCoord; -uniform sampler2D u_texture; +uniform sampler2D u_texture_diffuse0; +uniform sampler2D u_texture_diffuse1; + +uniform float mix_val; void main() { //f_color = v_color; - vec4 texColor = texture(u_texture, v_texCoord); - f_color = texColor; + vec4 texColor1 = texture(u_texture_diffuse0, v_texCoord); + vec4 texColor2 = texture(u_texture_diffuse1, v_texCoord); + f_color = mix(texColor1, texColor2, mix_val); } diff --git a/res/tex.png b/res/tex1.png similarity index 100% rename from res/tex.png rename to res/tex1.png diff --git a/res/tex2.png b/res/tex2.png new file mode 100644 index 0000000..2a91a6c Binary files /dev/null and b/res/tex2.png differ