emacs magic befell me and we are now done
This commit is contained in:
@@ -3,13 +3,14 @@
|
||||
in layout(location = 0) vec3 normal_in;
|
||||
in layout(location = 1) vec2 textureCoordinates;
|
||||
in layout(location = 2) vec3 worldPosition;
|
||||
in layout(location = 3) mat3 TBN;
|
||||
|
||||
struct LightSource {
|
||||
vec3 position;
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
uniform LightSource lights[3];
|
||||
uniform LightSource lights[1];
|
||||
uniform vec3 cameraPosition;
|
||||
uniform vec3 ballPosition;
|
||||
|
||||
@@ -48,13 +49,19 @@ void main()
|
||||
vec3 normal = bool(normalMapped) ? 2*vec3(texture(normalSampler, textureCoordinates))-1 : normal_in;
|
||||
|
||||
vec3 N = normalize(normal);
|
||||
if (bool(normalMapped))
|
||||
N = TBN * N;
|
||||
vec3 V = normalize(cameraPosition - worldPosition);
|
||||
|
||||
if (bool(normalMapped))
|
||||
V = TBN * V;
|
||||
|
||||
// use textures for normal mapped geometry
|
||||
vec3 result = ambientStrength * (bool(normalMapped) ? vec3(texture(diffuseSampler, textureCoordinates)) : baseColor);
|
||||
|
||||
for (int i = 0; i < 1; i++) {
|
||||
vec3 toLight = lights[i].position - worldPosition;
|
||||
if (bool(normalMapped))
|
||||
toLight = TBN * toLight;
|
||||
vec3 L = normalize(toLight);
|
||||
float dist = length(toLight);
|
||||
|
||||
@@ -87,4 +94,6 @@ void main()
|
||||
}
|
||||
|
||||
fragColor = vec4(result + dither(textureCoordinates), 1.0);
|
||||
// DEBUG: use to see if tbn is correct. it is.
|
||||
//fragColor = vec4(TBN * (texture(normalSampler, textureCoordinates).xyz * 2 - 1), 1.0);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
in layout(location = 0) vec3 position;
|
||||
in layout(location = 1) vec3 normal_in;
|
||||
in layout(location = 2) vec2 textureCoordinates_in;
|
||||
in layout(location = 6) vec3 tangents;
|
||||
in layout(location = 7) vec3 bitangents;
|
||||
|
||||
uniform layout(location = 3) mat4 MVP;
|
||||
uniform layout(location = 4) mat4 modelMatrix;
|
||||
@@ -11,9 +13,15 @@ uniform layout(location = 5) mat3 normalMatrix;
|
||||
out layout(location = 0) vec3 normal_out;
|
||||
out layout(location = 1) vec2 textureCoordinates_out;
|
||||
out layout(location = 2) vec3 worldSpaceVertices;
|
||||
out layout(location = 3) mat3 TBN;
|
||||
|
||||
void main()
|
||||
{
|
||||
TBN = (mat3(
|
||||
mat3(modelMatrix) * normalize(tangents),
|
||||
mat3(modelMatrix) * normalize(bitangents),
|
||||
mat3(modelMatrix) * normalize(normal_in)
|
||||
));
|
||||
worldSpaceVertices = vec3(modelMatrix * vec4(position, 1.0));
|
||||
normal_out = normalize(normalMatrix * normal_in);
|
||||
gl_Position = MVP * vec4(position, 1.0f);
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "glm/gtx/string_cast.hpp"
|
||||
|
||||
#include <utilities/shader.hpp>
|
||||
#include <utilities/timeutils.h>
|
||||
#include <utilities/mesh.h>
|
||||
@@ -125,6 +127,10 @@ void initGame(GLFWwindow* window, CommandLineOptions gameOptions) {
|
||||
Mesh boxMesh = cube(boxDimensions, glm::vec2(90), true, true); // inverted for interior view
|
||||
Mesh ballMesh = generateSphere(1.0, 40, 40);
|
||||
|
||||
for (auto v : boxMesh.bitangents) {
|
||||
std::cout << glm::to_string(v) << std::endl;
|
||||
}
|
||||
|
||||
unsigned int padVAO = generateBuffer(padMesh);
|
||||
unsigned int boxVAO = generateBuffer(boxMesh);
|
||||
unsigned int ballVAO = generateBuffer(ballMesh);
|
||||
@@ -362,6 +368,7 @@ void renderNode(SceneNode* node) {
|
||||
glUniform3fv(shader->getUniformFromName("cameraPosition"), 1, glm::value_ptr(cameraPosition));
|
||||
glUniform3fv(shader->getUniformFromName("ballPosition"), 1, glm::value_ptr(ballPosition));
|
||||
|
||||
|
||||
for (size_t i = 0; i < lights.size(); i++) {
|
||||
std::string base = "lights[" + std::to_string(i) + "]";
|
||||
glUniform3fv(shader->getUniformFromName((base + ".position").c_str()), 1, glm::value_ptr(lights[i].position));
|
||||
|
||||
@@ -14,6 +14,43 @@ unsigned int generateAttribute(int id, int elementsPerEntry, std::vector<T> data
|
||||
return bufferID;
|
||||
}
|
||||
|
||||
void computeTangentBasis(
|
||||
// inputs
|
||||
std::vector<glm::vec3> &vertices,
|
||||
std::vector<glm::vec2> &uvs,
|
||||
std::vector<glm::vec3> &normals,
|
||||
// outputs
|
||||
std::vector<glm::vec3> &tangents,
|
||||
std::vector<glm::vec3> &bitangents
|
||||
){
|
||||
for ( int i=0; i<vertices.size(); i+=3){
|
||||
glm::vec3 &v0 = vertices[i+0];
|
||||
glm::vec3 &v1 = vertices[i+1];
|
||||
glm::vec3 &v2 = vertices[i+2];
|
||||
|
||||
glm::vec2 &uv0 = uvs[i+0];
|
||||
glm::vec2 &uv1 = uvs[i+1];
|
||||
glm::vec2 &uv2 = uvs[i+2];
|
||||
|
||||
glm::vec3 deltaPos1 = v1-v0;
|
||||
glm::vec3 deltaPos2 = v2-v0;
|
||||
|
||||
glm::vec2 deltaUV1 = uv1-uv0;
|
||||
glm::vec2 deltaUV2 = uv2-uv0;
|
||||
float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
||||
glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y)*r;
|
||||
glm::vec3 bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x)*r;
|
||||
|
||||
tangents.push_back(tangent);
|
||||
tangents.push_back(tangent);
|
||||
tangents.push_back(tangent);
|
||||
|
||||
bitangents.push_back(bitangent);
|
||||
bitangents.push_back(bitangent);
|
||||
bitangents.push_back(bitangent);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int generateBuffer(Mesh &mesh) {
|
||||
unsigned int vaoID;
|
||||
glGenVertexArrays(1, &vaoID);
|
||||
@@ -26,6 +63,12 @@ unsigned int generateBuffer(Mesh &mesh) {
|
||||
if (mesh.textureCoordinates.size() > 0) {
|
||||
generateAttribute(2, 2, mesh.textureCoordinates, false);
|
||||
}
|
||||
if (mesh.tangents.size() > 0) {
|
||||
generateAttribute(6, 3, mesh.tangents, false);
|
||||
}
|
||||
if (mesh.bitangents.size() > 0) {
|
||||
generateAttribute(7, 3, mesh.bitangents, false);
|
||||
}
|
||||
|
||||
unsigned int indexBufferID;
|
||||
glGenBuffers(1, &indexBufferID);
|
||||
|
||||
@@ -2,4 +2,13 @@
|
||||
|
||||
#include "mesh.h"
|
||||
|
||||
unsigned int generateBuffer(Mesh &mesh);
|
||||
unsigned int generateBuffer(Mesh &mesh);
|
||||
void computeTangentBasis(
|
||||
// inputs
|
||||
std::vector<glm::vec3> &vertices,
|
||||
std::vector<glm::vec2> &uvs,
|
||||
std::vector<glm::vec3> &normals,
|
||||
// outputs
|
||||
std::vector<glm::vec3> &tangents,
|
||||
std::vector<glm::vec3> &bitangents
|
||||
);
|
||||
|
||||
@@ -7,6 +7,8 @@ struct Mesh {
|
||||
std::vector<glm::vec3> vertices;
|
||||
std::vector<glm::vec3> normals;
|
||||
std::vector<glm::vec2> textureCoordinates;
|
||||
|
||||
std::vector<glm::vec3> tangents;
|
||||
std::vector<glm::vec3> bitangents;
|
||||
|
||||
std::vector<unsigned int> indices;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <iostream>
|
||||
#include "shapes.h"
|
||||
#include "glutils.h"
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include "glm/gtx/string_cast.hpp"
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265359f
|
||||
@@ -87,6 +91,17 @@ Mesh cube(glm::vec3 scale, glm::vec2 textureScale, bool tilingTextures, bool inv
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<glm::vec3> tangents;
|
||||
std::vector<glm::vec3> bitangents;
|
||||
computeTangentBasis(m.vertices, m.textureCoordinates, m.normals, tangents, bitangents);
|
||||
m.tangents = tangents;
|
||||
// printf("%d\n", tangents.at(0).x);
|
||||
printf("tangents:\n");
|
||||
for (auto v : m.tangents) {
|
||||
std::cout << glm::to_string(v);
|
||||
}
|
||||
m.bitangents = bitangents;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@@ -203,5 +218,6 @@ Mesh generateSphere(float sphereRadius, int slices, int layers) {
|
||||
mesh.normals = normals;
|
||||
mesh.indices = indices;
|
||||
mesh.textureCoordinates = uvs;
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user