Specularmap support,

(fix bug in texture binding!)
This commit is contained in:
4VRDriver
2020-09-06 20:37:59 +02:00
parent bd2df7e928
commit 8b1fe545c1
7 changed files with 27 additions and 55 deletions

View File

@@ -3,16 +3,13 @@
layout(location = 0) out vec4 f_color;
struct Material {
sampler2D u_texture_diffuse0;
sampler2D u_texture_diffuse1;
sampler2D u_texture_specular0;
sampler2D u_texture_specular1;
vec3 ambient;
vec3 diffuse;
vec3 specular;
sampler2D texture_diffuse0;
sampler2D texture_diffuse1;
sampler2D texture_specular0;
sampler2D texture_specular1;
float shininess;
};
uniform Material material;
uniform Material u_material;
struct Light {
vec3 position;
@@ -21,42 +18,37 @@ struct Light {
vec3 diffuse;
vec3 specular;
};
uniform Light light;
uniform Light u_light;
in vec3 v_normal;
in vec4 v_color;
in vec2 v_texCoord;
in vec3 v_fragmentPosition;
uniform mat3 u_normalMatrix;
uniform vec3 u_viewPosition;
void main() {
float specularStrength = 0.5;
vec3 ambient = light.ambient * material.ambient;
// Diffuse lighting
vec3 diffuseColor0 = vec3(texture(u_material.texture_diffuse0, v_texCoord));
vec3 normal = normalize(u_normalMatrix * v_normal);
vec3 lightDir = normalize(light.position - v_fragmentPosition);
vec3 lightDir = normalize(u_light.position - v_fragmentPosition);
float diff = max(dot(normal, lightDir), 0.0f);
vec3 diffuse = light.diffuse * (diff * material.diffuse);
vec3 diffuse = u_light.diffuse * diff * diffuseColor0;
// Ambient lighting
vec3 ambient = u_light.ambient * diffuseColor0;
// Specular lighting
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * (spec * material.specular);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_material.shininess);
vec3 specular = u_light.specular * spec * vec3(texture(u_material.texture_specular0, v_texCoord));
//f_color = v_color;
//vec4 texColor1 = texture(material.u_texture_diffuse0, v_texCoord);
//vec4 texColor2 = texture(material.u_texture_diffuse1, v_texCoord);
//vec4 texColor1 = texture(u_material.u_texture_diffuse0, v_texCoord);
//vec4 texColor2 = texture(u_material.u_texture_diffuse1, v_texCoord);
//f_color = texColor1;
f_color = vec4((ambient + diffuse + specular), 1.0f);
}

View File

@@ -3,11 +3,9 @@
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_normal;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in vec4 a_color;
out vec3 v_normal;
out vec2 v_texCoord;
out vec4 v_color;
out vec3 v_fragmentPosition;
@@ -21,5 +19,4 @@ void main() {
v_normal = a_normal;
v_texCoord = a_texCoord;
v_color = a_color;
}

BIN
res/textures/container.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 457 KiB

View File

@@ -50,16 +50,13 @@ void Controller::run() {
std::vector<Entity> scene;
//Model model_backpack("res/models/backpack.obj");
Model model_container("res/models/container.obj");
Model model_cube("res/models/cube.obj");
Model model_sphere("res/models/sphere.obj");
//Entity backpack1(&model_cube, &shaderProgram);
Entity cube(&model_cube, &shaderProgram);
Entity sphere(&model_sphere, &shaderProgram);
Entity cube(&model_container, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
sphere.translate(glm::vec3(5.0f, 0.0f, 2.5f));
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
lightSource.scale(0.2f);
@@ -68,20 +65,16 @@ void Controller::run() {
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f);
shaderProgram.bind();
shaderProgram.setUniform("light.position", lightSource.getPosition());
shaderProgram.setUniform("light.ambient", ambientColor);
shaderProgram.setUniform("light.diffuse", diffuseColor);
shaderProgram.setUniform("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
shaderProgram.setUniform("u_light.position", lightSource.getPosition());
shaderProgram.setUniform("u_light.ambient", ambientColor);
shaderProgram.setUniform("u_light.diffuse", diffuseColor);
shaderProgram.setUniform("u_light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
shaderProgram.setUniform("material.ambient", glm::vec3(1.0f, 0.5f, 0.31f));
shaderProgram.setUniform("material.diffuse", glm::vec3(1.0f, 0.5f, 0.31f));
shaderProgram.setUniform("material.specular", glm::vec3(0.5f, 0.5f, 0.5f));
shaderProgram.setUniform("material.shininess", 32.0f);
shaderProgram.setUniform("u_material.shininess", 32.0f);
shaderProgram.unbind();
scene.push_back(lightSource);
scene.push_back(cube);
scene.push_back(sphere);
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));
@@ -110,8 +103,6 @@ void Controller::run() {
for(auto it = scene.begin(); it != scene.end(); it++) {
it->draw(camera->getViewProj(), camera->getPosition());
}
//backpack1.draw(camera->getViewProj());
//cube.draw(camera->getViewProj());
glfwSwapBuffers(gameWindow->getGLFWwindow());

View File

@@ -44,7 +44,7 @@ Texture::~Texture() {
}
void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t textureTypeNum) {
std::string uniformName = "u_texture_";
std::string uniformName = "texture_";
switch(textureType) {
@@ -62,11 +62,10 @@ void Texture::bind(uint8_t textureUnit, ShaderProgram* shaderProgram, uint8_t te
break;
}
// Add material. as we store textures in a struct
uniformName = "material." + uniformName;
shaderProgram->setUniform(uniformName.c_str(), textureTypeNum);
// Add u_material as we store textures in a struct
uniformName = "u_material." + uniformName;
shaderProgram->setUniform(uniformName.c_str(), textureUnit);
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textureId);
}

View File

@@ -27,10 +27,6 @@ VertexBuffer::VertexBuffer(void *vertexData, void *indexData, uint32_t numVertic
// UV Texture Mapping
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, textureCoords));
// Color
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, color));
// This will also unbind the vertex buffer and index buffer
glBindVertexArray(0);

View File

@@ -19,7 +19,4 @@ struct Vertex {
// Bittangent vector
glm::vec3 bitangentVec;
// Color
glm::vec4 color;
};