Add mesh class and update to proper texture handling

This commit is contained in:
4VRDriver
2020-09-04 16:28:00 +02:00
parent eab5c4e5e0
commit 473ba514ed
12 changed files with 149 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <glad/glad.h>
#include <glm/glm.hpp>
@@ -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<Vertex> 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<uint32_t> indices {
0, 1, 2,
1, 2, 3
};
uint32_t numVertices = sizeof(vertices) / sizeof(Vertex);
uint32_t numIndices = sizeof(indices) / sizeof(indices[0]);
std::vector<Texture*> 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() {

1
Fall-Fever Symbolic link
View File

@@ -0,0 +1 @@
/home/derek/Projekte/Fall-Fever/build/Fall-Fever

View File

@@ -1 +1,38 @@
#include "Mesh.h"
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, std::vector<Texture*> textures)
: vertices(vertices),
indices(indices),
textures(textures),
vertexBuffer(static_cast<void*>(vertices.data()), static_cast<void*>(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();
}
}

29
Mesh.h
View File

@@ -1 +1,30 @@
#pragma once
#include <vector>
#include "ShaderProgram.h"
#include "VertexBuffer.h"
#include "Texture.h"
#include "defines.h"
class Mesh {
public:
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture*> textures);
~Mesh() = default;
void draw(ShaderProgram *shaderProgram);
private:
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
std::vector<Texture*> textures;
VertexBuffer vertexBuffer;
//ShaderProgram *shaderProgram;
void setupMesh();
};

View File

@@ -3,8 +3,10 @@
#include <stb/stb_image.h>
#include <iostream>
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);
}

View File

@@ -1,29 +1,35 @@
#pragma once
#include "ShaderProgram.h"
#include <cstdint>
#include <glad/glad.h>
#include <string>
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;
};

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}

View File

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 176 KiB

BIN
res/tex2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 693 KiB