Add texture support

This commit is contained in:
4VRDriver
2020-09-02 18:37:53 +02:00
parent f8868be702
commit b8715fba4d
11 changed files with 133 additions and 14 deletions

View File

@@ -19,6 +19,7 @@ add_executable(Fall-Fever
EventHandler.cpp EventHandler.cpp
ShaderProgram.cpp ShaderProgram.cpp
VertexBuffer.cpp VertexBuffer.cpp
Texture.cpp
) )
target_link_libraries( target_link_libraries(

View File

@@ -10,6 +10,7 @@
#endif #endif
#include "Controller.h" #include "Controller.h"
#include "Texture.h"
Controller::Controller() { Controller::Controller() {
if(!glfwInit()) exit(-1); if(!glfwInit()) exit(-1);
@@ -40,13 +41,23 @@ void Controller::run() {
shaderProgram.bind(); shaderProgram.bind();
Vertex vertices[] = { Vertex vertices[] = {
Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, Vertex{-0.5f, -0.5f, 0.0f,
Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f}, 0.0f, 0.0f,
Vertex{0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f} 1.0f, 0.0f, 0.0f, 1.0f},
Vertex{0.5f, -0.5f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,0.0f, 1.0f},
Vertex{-0.5f, 0.5f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f},
Vertex{0.5f, 0.5f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f}
}; };
uint32_t indices[] = { uint32_t indices[] = {
0, 1, 2 0, 1, 2,
1, 2, 3
}; };
uint32_t numVertices = sizeof(vertices) / sizeof(Vertex); uint32_t numVertices = sizeof(vertices) / sizeof(Vertex);
@@ -54,6 +65,8 @@ void Controller::run() {
VertexBuffer vertexBuffer(vertices, indices, numVertices, numIndices); VertexBuffer vertexBuffer(vertices, indices, numVertices, numIndices);
Texture tex1("res/tex.png", shaderProgram.getShaderProgramId());
// This is the game loop // This is the game loop
while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) { while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
// Timing // Timing
@@ -74,7 +87,9 @@ void Controller::run() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
vertexBuffer.bind(); vertexBuffer.bind();
tex1.bind(0);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
tex1.unbind();
vertexBuffer.unbind(); vertexBuffer.unbind();
glfwSwapBuffers(gameWindow->getGLFWwindow()); glfwSwapBuffers(gameWindow->getGLFWwindow());

View File

@@ -3,8 +3,8 @@
#include "ShaderProgram.h" #include "ShaderProgram.h"
ShaderProgram::ShaderProgram(const char* vertexShaderFilename, const char* fragmentShaderFilename) { ShaderProgram::ShaderProgram(const char* vertexShaderPath, const char* fragmentShaderPath) {
shaderProgramId = createShader(vertexShaderFilename, fragmentShaderFilename); shaderProgramId = createShader(vertexShaderPath, fragmentShaderPath);
} }
ShaderProgram::~ShaderProgram() { ShaderProgram::~ShaderProgram() {
@@ -19,9 +19,9 @@ void ShaderProgram::unbind() {
glUseProgram(0); glUseProgram(0);
} }
GLuint ShaderProgram::createShader(const char* vertexShaderFilename, const char* framentShaderFilename) { GLuint ShaderProgram::createShader(const char* vertexShaderPath, const char* framentShaderPath) {
std::string vertexShaderSource = parse(vertexShaderFilename); std::string vertexShaderSource = parse(vertexShaderPath);
std::string fragmentShaderSource = parse(framentShaderFilename); std::string fragmentShaderSource = parse(framentShaderPath);
GLuint program = glCreateProgram(); GLuint program = glCreateProgram();
GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER); GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER);

View File

@@ -7,7 +7,7 @@ class ShaderProgram {
public: public:
ShaderProgram(const char* vertexShaderFilename, const char* framentShaderFilename); ShaderProgram(const char* vertexShaderPath, const char* framentShaderPath);
~ShaderProgram(); ~ShaderProgram();
void bind(); void bind();
@@ -18,11 +18,13 @@ public:
void setInt(const char *name, int value) const; void setInt(const char *name, int value) const;
void setFloat(const char *name, float value) const; void setFloat(const char *name, float value) const;
GLuint getShaderProgramId() { return shaderProgramId; }
private: private:
std::string parse(const char* filename); std::string parse(const char* filename);
GLuint compile(std::string shaderSource, GLenum type); GLuint compile(std::string shaderSource, GLenum type);
GLuint createShader(const char* vertexShaderFilename, const char* framentShaderFilename); GLuint createShader(const char* vertexShaderPath, const char* framentShaderPath);
GLuint shaderProgramId; GLuint shaderProgramId;

54
Texture.cpp Normal file
View File

@@ -0,0 +1,54 @@
#include "Texture.h"
#include <stb_image.h>
#include <iostream>
Texture::Texture(const char* texturePath, GLuint shaderProgramId) {
this->shaderProgramId = shaderProgramId;
stbi_set_flip_vertically_on_load(1);
stbi_uc *textureBuffer = stbi_load(texturePath, &textureWidth, &textureHeight, &bitsPerPixel, 4);
// Push texture to grahics card;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if(textureBuffer) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureBuffer);
//glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "[Warning] Texture " << texturePath << " not found!" << std::endl;
}
stbi_image_free(textureBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture() {
glDeleteTextures(1, &textureId);
}
void Texture::bind(uint8_t textureUnit) {
GLint uniformLoc = glGetUniformLocation(shaderProgramId, "u_texture");
glUniform1i(uniformLoc, textureUnit);
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textureId);
}
void Texture::unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}

26
Texture.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <cstdint>
#include <glad/glad.h>
class Texture {
public:
Texture(const char* texturePath, GLuint shaderProgramId);
~Texture();
void bind(uint8_t textureUnit);
void unbind();
private:
int32_t textureWidth;
int32_t textureHeight;
int32_t bitsPerPixel;
GLuint textureId;
GLuint shaderProgramId;
};

View File

@@ -16,10 +16,17 @@ VertexBuffer::VertexBuffer(void *vertexData, void *indexData, uint32_t numVertic
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), indexData, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), indexData, GL_STATIC_DRAW);
// Position
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x));
// UV Texture Mapping
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, r)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, u));
// Color
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, r));
// This will also unbind the vertex buffer // This will also unbind the vertex buffer
glBindVertexArray(0); glBindVertexArray(0);

View File

@@ -1,10 +1,16 @@
#pragma once #pragma once
struct Vertex { struct Vertex {
// Postition
float x; float x;
float y; float y;
float z; float z;
// UV Texture Mapping
float u;
float v;
// Color
float r; float r;
float g; float g;
float b; float b;

View File

@@ -3,7 +3,12 @@
layout(location = 0) out vec4 f_color; layout(location = 0) out vec4 f_color;
in vec4 v_color; in vec4 v_color;
in vec2 v_texCoord;
uniform sampler2D u_texture;
void main() { void main() {
f_color = v_color; //f_color = v_color;
vec4 texColor = texture(u_texture, v_texCoord);
f_color = texColor;
} }

View File

@@ -1,12 +1,15 @@
#version 330 core #version 330 core
layout(location = 0) in vec3 a_position; layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color; layout(location = 1) in vec2 a_texCoord;
layout(location = 2) in vec4 a_color;
out vec4 v_color; out vec4 v_color;
out vec2 v_texCoord;
void main() { void main() {
gl_Position = vec4(a_position, 1.0f); gl_Position = vec4(a_position, 1.0f);
//gl_Position = vec4(a_position.x, -a_position.y, a_position.z, 1.0f); //gl_Position = vec4(a_position.x, -a_position.y, a_position.z, 1.0f);
v_texCoord = a_texCoord;
v_color = a_color; v_color = a_color;
} }

BIN
res/tex.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB