Browse Source

i think normal mapping works

master
alistair 3 years ago
parent
commit
7d823a3512
  1. 89
      src/main.cpp
  2. 24
      src/mesh.cpp
  3. 1
      src/mesh.h
  4. 15
      src/shaders/instanced.vert
  5. 7
      src/shaders/normaldip.frag
  6. 28
      src/shaders/normaldip.geom
  7. 16
      src/shaders/test.frag
  8. 26
      src/shaders/test.vert

89
src/main.cpp

@ -154,6 +154,7 @@ class SDLGLGLWindow { @@ -154,6 +154,7 @@ class SDLGLGLWindow {
glm::mat4 view = glm::mat4(1.0f);
private:
Shader *shader;
Shader *modelShader;
Shader *texShader;
Shader *skyboxShader;
Model *bp_model;
@ -270,6 +271,7 @@ class SDLGLGLWindow { @@ -270,6 +271,7 @@ class SDLGLGLWindow {
bool success = true;
//Generate program
shader = new Shader("src/shaders/instanced.vert", "src/shaders/test.frag");
modelShader = new Shader("src/shaders/instanced.vert", "src/shaders/test.frag");
texShader = new Shader("src/shaders/screentexture.vert", "src/shaders/screentexture.frag");
skyboxShader = new Shader("src/shaders/sky.vert", "src/shaders/sky.frag");
glCheckError();
@ -277,10 +279,11 @@ class SDLGLGLWindow { @@ -277,10 +279,11 @@ class SDLGLGLWindow {
//Link program
stbi_set_flip_vertically_on_load(true);
modelShader->use();
bp_model = new Model("src/assets/objects/backpack/backpack.obj");
glCheckError();
shader->use();
// model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
@ -377,7 +380,8 @@ class SDLGLGLWindow { @@ -377,7 +380,8 @@ class SDLGLGLWindow {
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
glGenTextures(2, texture);
unsigned char *data = stbi_load("src/assets/container.jpg", &width, &height, &nrChannels, 0);
unsigned char *data = stbi_load("src/assets/Tiles01-1k/Tiles01 diffuse 1k.jpg", &width, &height, &nrChannels, 0);
unsigned char *datanorm = stbi_load("src/assets/Tiles01-1k/Tiles01 normal 1k.jpg", &width, &height, &nrChannels, 0);
if (data) {
glBindTexture(GL_TEXTURE_2D, texture[0]);
@ -394,46 +398,46 @@ class SDLGLGLWindow { @@ -394,46 +398,46 @@ class SDLGLGLWindow {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glCheckError();
} else {
throw std::exception () ;
}
/*
data = stbi_load("src/assets/grass.png", &width, &height, &nrChannels, 0);
if (data)
{
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
/* load normal */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, datanorm);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(datanorm);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
} else {
throw std::exception () ;
}
*/
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glCheckError();
/*
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);
*/
shader->use();
shader->setInt("material.texture_diffuse1", 0);
glActiveTexture(GL_TEXTURE0 + 1);
shader->setInt("material.texture_normal1", 1);
glCheckError();
// shader->setInt("material.specularmap", 1);
shader->setBool("lighting_emit", false);
float cubeVertices[] = {
// vertices normals textures
// vertices normals textures tangent
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f,
@ -613,6 +617,25 @@ class SDLGLGLWindow { @@ -613,6 +617,25 @@ class SDLGLGLWindow {
glCheckError();
modelShader->setInt("num_lights", 1);
modelShader->setInt("lights[0].type", 0);
modelShader->setFloat("lights[0].angle_cutoff", glm::cos(glm::radians(12.5f)));
modelShader->setFloat("lights[0].outer_angle_cutoff", glm::cos(glm::radians(17.5f)));
modelShader->setVec3("lights[0].direction", glm::vec3(0,-1,0));
modelShader->setVec3("lights[0].position", camera_pos);
modelShader->setVec3("lights[0].ambient", glm::vec3(0.1));
modelShader->setVec3("lights[0].diffuse", glm::vec3(0.8,0.7,1.0) * glm::vec3(0.5));
modelShader->setVec3("lights[0].specular", glm::vec3(1.0));
modelShader->setBool("lights[0].attenuate", false);
shader->setInt("num_lights", 3);
shader->setInt("lights[0].type", 0);
shader->setFloat("lights[0].angle_cutoff", glm::cos(glm::radians(12.5f)));
shader->setFloat("lights[0].outer_angle_cutoff", glm::cos(glm::radians(17.5f)));
@ -625,7 +648,6 @@ class SDLGLGLWindow { @@ -625,7 +648,6 @@ class SDLGLGLWindow {
shader->setVec3("lights[0].specular", glm::vec3(1.0));
shader->setBool("lights[0].attenuate", false);
// shader->setVec3("lights[0].attenuation", glm::vec3(1.0, 0.09, 0.032));
shader->setInt("num_lights", 3);
shader->setInt("lights[1].type", 1);
shader->setVec3("lights[1].position", lightPositions[0]);
@ -651,6 +673,11 @@ class SDLGLGLWindow { @@ -651,6 +673,11 @@ class SDLGLGLWindow {
shader->setFloat("material.shininess", 32);
glCheckError();
modelShader->setVec3("material.specular", glm::vec3(0.7));
modelShader->setVec3("material.diffuse", glm::vec3(1.0, 0.7, 0.8));
modelShader->setVec3("material.ambient", glm::vec3(0.2));
modelShader->setFloat("material.shininess", 32);
/***** Light Attenuation
*
* Constant x, Linear y, Quadratic z
@ -699,14 +726,20 @@ class SDLGLGLWindow { @@ -699,14 +726,20 @@ class SDLGLGLWindow {
glCheckError();
model = glm::mat4(1.0);
loc = shader->get_uniform("model");
model = glm::rotate(model, glm::radians( 20.0f + 160.0f *(float)SDL_GetTicks() / 1000.0f), glm::vec3(0.6, 0.3, 1.0));
glCheckError();
loc = modelShader->get_uniform("model");
glCheckError();
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(model));
glCheckError();
loc = shader->get_uniform("view");
loc = modelShader->get_uniform("view");
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(view));
glCheckError();
loc = modelShader->get_uniform("projection");
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(projection));
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap);
bp_model->draw(*shader);
@ -714,8 +747,14 @@ class SDLGLGLWindow { @@ -714,8 +747,14 @@ class SDLGLGLWindow {
//*** Draw random cubes ****/
shader->use();
glBindVertexArray(gVAO);
glActiveTexture(GL_TEXTURE0); // activate proper texture unit before binding
glBindTexture(GL_TEXTURE_2D, texture[0]);
shader->setInt("material.texture_diffuse1", 0);
glActiveTexture(GL_TEXTURE0 + 1); // activate proper texture unit before binding
glBindTexture(GL_TEXTURE_2D, texture[1]);
shader->setInt("material.texture_normal1", 1);
glCheckError();
glDrawArraysInstanced(GL_TRIANGLES, 0,36,1000);
glCheckError();

24
src/mesh.cpp

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
#include "mesh.h"
#include "stb_image.h"
#include "glerror.h"
#include <assimp/postprocess.h>
Mesh::Mesh(std::vector<struct vertex> vertices, std::vector<unsigned int> indices,
@ -22,7 +23,7 @@ void Mesh::setupMesh() @@ -22,7 +23,7 @@ void Mesh::setupMesh()
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData( GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex), &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int),
@ -37,6 +38,9 @@ void Mesh::setupMesh() @@ -37,6 +38,9 @@ void Mesh::setupMesh()
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)offsetof(vertex, texture_coords));
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)offsetof(vertex, tangent));
glCheckError();
glBindVertexArray(0);
@ -46,6 +50,7 @@ void Mesh::draw(Shader &shader) { @@ -46,6 +50,7 @@ void Mesh::draw(Shader &shader) {
glCheckError();
unsigned int specularNr = 1;
unsigned int diffuseNr = 1;
unsigned int normalNr= 1;
for(unsigned int i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i); // activate proper texture unit before binding
@ -53,11 +58,15 @@ void Mesh::draw(Shader &shader) { @@ -53,11 +58,15 @@ void Mesh::draw(Shader &shader) {
// retrieve texture number (the N in diffuse_textureN)
std::string number;
std::string name = textures[i].type;
if(name == "texture_diffuse")
if(name == "texture_diffuse") {
number = std::to_string(diffuseNr++);
else if(name == "texture_specular")
} else if(name == "texture_specular") {
number = std::to_string(specularNr++);
} else if(name == "texture_normal") {
number = std::to_string(normalNr++);
}
shader.setInt(("material." + name + number).c_str(), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
glCheckError();
}
@ -86,7 +95,7 @@ void Model::draw(Shader &shader) @@ -86,7 +95,7 @@ void Model::draw(Shader &shader)
}
void Model::load_model(std::string path) {
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
@ -124,10 +133,12 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene) @@ -124,10 +133,12 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
for (unsigned int i = 0 ; i < mesh->mNumVertices; i++) {
glm::vec3 pos = glm::vec3(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
glm::vec3 norm = glm::vec3(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
glm::vec3 tangent = glm::vec3(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z);
vertex v;
v.position = pos;
v.normal = norm;
v.tangent = tangent;
if(mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
{
@ -140,7 +151,6 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene) @@ -140,7 +151,6 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
}
vertices.push_back(v);
}
for(unsigned int i = 0; i < mesh->mNumFaces; i++)
@ -159,6 +169,10 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene) @@ -159,6 +169,10 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
std::vector<texture> specularMaps = load_material_textures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
std::vector<texture> normalMaps = load_material_textures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
}
glCheckError();

1
src/mesh.h

@ -17,6 +17,7 @@ struct vertex { @@ -17,6 +17,7 @@ struct vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texture_coords;
glm::vec3 tangent;
};
struct texture {

15
src/shaders/instanced.vert

@ -4,8 +4,8 @@ layout (location = 0) in vec3 LVertexPos2D; @@ -4,8 +4,8 @@ layout (location = 0) in vec3 LVertexPos2D;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec3 offset;
layout (location = 4) in vec3 aTangent;
out vec3 theTexCoord;
uniform mat4 model;
uniform mat4 view;
@ -13,6 +13,8 @@ uniform mat4 projection; @@ -13,6 +13,8 @@ uniform mat4 projection;
out vec3 Normal;
out vec3 FragPos;
out vec3 theTexCoord;
out mat3 TBN;
void main()
{
@ -23,4 +25,15 @@ void main() @@ -23,4 +25,15 @@ void main()
theTexCoord = vec3(aTexCoords, 1.0);
Normal = mat3(transpose(inverse(model))) * aNormal;
FragPos = vec3((model) * pos);
// calculating TBN
vec3 T = normalize(vec3(model * vec4(aTangent, 0.0)));
vec3 N = normalize(vec3(model * vec4(aNormal, 0.0)));
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N,T);
TBN = mat3(T, B, N);
}

7
src/shaders/normaldip.frag

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}

28
src/shaders/normaldip.geom

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
#version 330 core
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;
in vec3 Normal[];
in vec3 FragPos[];
const float MAGNITUDE = 0.4;
uniform mat4 projection;
void GenerateLine(int index)
{
gl_Position = projection * FragPos[index];
EmitVertex();
gl_Position = projection * (FragPos[index] +
(Normal[index], 0.0) * MAGNITUDE);
EmitVertex();
EndPrimitive();
}
void main()
{
GenerateLine(0); // first vertex normal
GenerateLine(1); // second vertex normal
GenerateLine(2); // third vertex normal
}

16
src/shaders/test.frag

@ -2,9 +2,10 @@ @@ -2,9 +2,10 @@
out vec4 LFragment;
in vec3 theTexCoord;
in vec3 Normal;
in vec3 FragPos;
in mat3 TBN;
in vec3 theTexCoord;
uniform vec3 cameraPosition;
uniform samplerCube skybox;
@ -19,6 +20,7 @@ struct Material { @@ -19,6 +20,7 @@ struct Material {
sampler2D texture_specular1;
sampler2D texture_specular2;
sampler2D texture_specular3;
sampler2D texture_normal1;
vec3 ambient;
vec3 diffuse;
vec3 specular;
@ -51,11 +53,11 @@ uniform int num_lights; @@ -51,11 +53,11 @@ uniform int num_lights;
uniform Material material;
vec4
reflection()
reflection(vec3 norm)
{
vec3 reflectiveness = material.specular * material.shininess / 50;
vec3 I = normalize(FragPos - cameraPosition);
vec3 R = reflect(I, normalize(Normal));
vec3 R = reflect(I, norm);
return texture(skybox, R) * (material.shininess / 90);
}
@ -107,13 +109,18 @@ void @@ -107,13 +109,18 @@ void
main()
{
vec3 endResult = vec3(0);
// normal
vec3 norm;
norm = texture(material.texture_normal1, vec2(theTexCoord)).rgb;
norm = norm * 2.0 - 1.0;
norm = normalize(TBN * norm);
for (int i = 0; i < num_lights; i++) {
vec3 ambient = lights[i].ambient * material.ambient;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = vec3(0);
if (lights[i].type == POINT_LIGHT || lights[i].type == SPOT_LIGHT) {
lightDir = normalize(lights[i].position - FragPos);
@ -145,6 +152,7 @@ main() @@ -145,6 +152,7 @@ main()
// gamma correction
LFragment = parta;
LFragment.rgb = pow(parta.rgb, vec3(1.0/gamma));
// LFragment = vec4(norm, 1);
}

26
src/shaders/test.vert

@ -1,10 +1,9 @@ @@ -1,10 +1,9 @@
#version 330 core
layout (location = 0) in vec3 LVertexPos2D;
layout (location = 0) in vec3 aVertexPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec3 theTexCoord;
layout (location = 4) in vec3 aTangent;
uniform mat4 model;
uniform mat4 view;
@ -13,18 +12,31 @@ uniform bool skyboxON; @@ -13,18 +12,31 @@ uniform bool skyboxON;
out vec3 Normal;
out vec3 FragPos;
out vec3 theTexCoord;
out mat3 TBN;
void main()
{
gl_Position = projection * view * model * vec4( LVertexPos2D, 1.0);
gl_Position = projection * view * model * vec4( aVertexPos, 1.0);
if (skyboxON) {
theTexCoord = LVertexPos2D;
FragPos = LVertexPos2D; // vec3(model * vec4(LVertexPos2D, 1.0));
theTexCoord = aVertexPos;
FragPos = aVertexPos; // vec3(model * vec4(aVertexPos, 1.0));
return;
}
// calculating TBN
vec3 T = normalize(vec3(model * vec4(aTangent, 0.0)));
vec3 N = normalize(vec3(model * vec4(aNormal, 0.0)));
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N,T);
TBN = mat3(T, B, N);
// calculating normal
theTexCoord = vec3(aTexCoords, 1.0);
Normal = mat3(transpose(inverse(model))) * aNormal;
FragPos = vec3(model * vec4(LVertexPos2D, 1.0));
FragPos = vec3(model * vec4(aVertexPos, 1.0));
}

Loading…
Cancel
Save