Commit 444e7f1e authored by Sadman Kazi's avatar Sadman Kazi 🎸

Added material support

parent 0119931f
Pipeline #160 failed with stage
in 60 minutes and 28 seconds
......@@ -128,7 +128,7 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
......@@ -195,4 +195,4 @@ FakesAssemblies/
*.opt
# vim/vi temp files
*.swp
*.sw*
This diff is collapsed.
......@@ -6,8 +6,14 @@ out vec4 fragColor;
uniform sampler2D u_diffuse;
uniform vec3 u_ambientLight;
uniform vec3 u_color;
void main () {
vec4 totalLight = vec4(u_ambientLight, 1);
fragColor = texture2D(u_diffuse, texCoord0.xy) * clamp(dot(-vec3(1,1,1), normal0), 0.2, 1.0);
vec4 textureColor = texture2D(u_diffuse, texCoord0.xy) * clamp(dot(-vec3(1,1,1), normal0), 0.2, 1.0);
if (textureColor == vec4(0))
fragColor = vec4(u_color, 1);
else
fragColor = textureColor * vec4(u_color, 1);
}
......@@ -11,8 +11,18 @@ BasicShader::BasicShader() :
Shader(BASIC_SHADER_FILE) {
addUniform("u_transform");
addUniform("u_color");
}
void BasicShader::updateUniforms(const glm::mat4& proj_matrix) {
void BasicShader::updateUniforms(const glm::mat4& proj_matrix,
const Material* material) {
if (material->getTexture()) {
material->bindTexture();
} else {
Texture::unbindAll();
}
setUniformMat4("u_transform", proj_matrix);
setUniformVec3("u_color", *material->getColor());
}
......@@ -6,7 +6,8 @@
class BasicShader : public Shader {
public:
static BasicShader* getInstance();
virtual void updateUniforms(const glm::mat4& proj_matrix);
virtual void updateUniforms(const glm::mat4& proj_matrix,
const Material* material);
private:
BasicShader();
......
......@@ -35,8 +35,9 @@ Game::Game()
//m_mesh = new Mesh(data, sizeof(data)/sizeof(data[0]), indices, sizeof(indices)/sizeof(indices[0]));
m_mesh = new Mesh("../res/sample_level.obj");
m_texture = new Texture("../res/gray.jpg");
//m_mesh = new Mesh("../res/sample_level.obj");
m_mesh = new Mesh("../res/monkey.obj");
m_material = new Material(new Texture("../res/gray.jpg"), new glm::vec3(0, 1, 1));
m_transform = new Transform();
m_camera = new Camera(glm::vec3(0.0f, 0.0f, -2.0f), 70.0f, (float)WIDTH/HEIGHT, 1.0f, 100.0f);
......@@ -53,6 +54,7 @@ Game::~Game()
delete m_mesh;
delete m_transform;
delete m_camera;
delete m_material;
}
void Game::input() {
......@@ -61,7 +63,6 @@ void Game::input() {
void Game::render() {
m_shader->bind();
m_texture->bind(0);
m_mesh->draw();
/*if (Input::getKeyDown(SDLK_UP)) {
std::cout << "You have pressed up!" << std::endl;
......@@ -89,7 +90,7 @@ void Game::update() {
//m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
m_shader->updateUniforms(m_transform->getProjectedModel(m_camera));
m_shader->updateUniforms(m_transform->getProjectedModel(m_camera), m_material);
}
void Game::loadMesh(const std::string& model_path) {
......
......@@ -16,9 +16,9 @@
#define GAME_H_
#include "mesh.h"
#include "texture.h"
#include "shader.h"
#include "transform.h"
#include "material.h"
#include "camera.h"
#define WIDTH 1280
......@@ -42,8 +42,8 @@ private:
Mesh* m_mesh;
Shader* m_shader;
Transform* m_transform;
Material* m_material;
Camera* m_camera;
Texture* m_texture;
float m_counter;
};
......
#include "material.h"
Material::Material(Texture* texture, glm::vec3* color) :
m_texture(texture),
m_color(color) {}
const Texture* Material::getTexture() const {
return m_texture;
}
void Material::bindTexture(int index) const {
m_texture->bind(index);
}
const glm::vec3* Material::getColor() const {
return m_color;
}
void Material::setColor(const glm::vec3& color) {
if (m_color != nullptr) delete m_color;
m_color = new glm::vec3(color);
}
Material::~Material() {
delete m_texture;
delete m_color;
}
#ifndef MATERIAL_H_
#define MATERIAL_H_
#include "texture.h"
#include <glm/glm.hpp>
class Material {
public:
Material(Texture*, glm::vec3*);
const Texture* getTexture() const;
void bindTexture(int index = 0) const;
const glm::vec3* getColor() const;
void setColor(const glm::vec3& color);
virtual ~Material();
private:
Texture* m_texture;
glm::vec3* m_color;
};
#endif // MATERIAL_H_
......@@ -6,9 +6,9 @@
#include <unordered_map>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "material.h"
class Shader
{
class Shader {
public:
Shader(const std::string& fileName);
......@@ -20,7 +20,7 @@ public:
void setUniformF(const std::string& name, float value);
void setUniformVec3(const std::string& name, const glm::vec3& value);
void setUniformMat4(const std::string& name, const glm::mat4& value);
virtual void updateUniforms(const glm::mat4&) = 0;
virtual void updateUniforms(const glm::mat4&, const Material*) = 0;
virtual ~Shader();
private:
......
......@@ -5,7 +5,7 @@
Texture::Texture(const std::string& fileName) {
int width, height, numComponents; //returned by the stb loader
unsigned char* imageData = stbi_load(fileName.c_str(), &width, &height, &numComponents, 4);
if (imageData == NULL)
......@@ -33,9 +33,13 @@ Texture::~Texture() {
glDeleteTextures(1, &m_texture); //delete the texture
}
void Texture::bind(unsigned int unit) {
void Texture::bind(unsigned int unit) const {
assert(unit >= 0 && unit <= 31);
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
void Texture::unbindAll() {
glBindTexture(GL_TEXTURE_2D, 0);
}
......@@ -7,7 +7,8 @@
class Texture {
public:
Texture(const std::string& fileName);
void bind(unsigned int unit);
void bind(unsigned int unit) const;
static void unbindAll();
virtual ~Texture();
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment