Commit c1208357 authored by Dario Seyb's avatar Dario Seyb

GGJ2016 changes

parent e81e2b02
......@@ -2,18 +2,21 @@
#if LAST_SHADER == VERTEX
in vec3 vNormal;
in vec3 vTangent;
in vec2 vTexCoord;
in vec3 vPosition;
#endif
#if LAST_SHADER == TESSELATION
in vec3 teNormal;
in vec3 teTangent;
in vec2 teTexCoord;
in vec3 tePosition;
#endif
#if LAST_SHADER == GEOMETRY
in vec3 gNormal;
in vec3 gTangent;
in vec2 gTexCoord;
in vec3 gPosition;
in vec3 gPatchDistance;
......
......@@ -7,10 +7,12 @@ uniform float uFar;
uniform float uTime;
in vec3 aNormal;
in vec3 aTangent;
in vec3 aPosition;
in vec2 aTexCoord;
out vec3 vNormal;
out vec3 vTangent;
out vec2 vTexCoord;
out vec3 vPosition;
......@@ -23,6 +25,7 @@ void main()
vNormal = normal();
vTexCoord = texCoord();
vPosition = position();
vTangent = aTangent;
gl_Position = uViewProjectionMatrix * vec4(vPosition, 1);
......
......@@ -19,21 +19,16 @@ vec4 emissive() {
vec3 normal() {
if(false && uHasNormalMap) {
vec3 tangentNormal = unpackNormal(texture(uNormalMap, vTexCoord));
vec3 Q1 = dFdx(vPosition);
vec3 Q2 = dFdy(vPosition);
vec2 st1 = dFdx(vTexCoord);
vec2 st2 = dFdy(vTexCoord);
vec3 tangentNormal = unpackNormalTex(texture(uNormalMap, vTexCoord));
vec3 T = normalize(Q1*st2.t - Q2*st1.t);
vec3 B = normalize(-Q1*st2.s + Q2*st1.s);
vec3 T = normalize(vTangent);
vec3 B = normalize(cross(vNormal, vTangent));
// the transpose of texture-to-eye space matrix
mat3 TBN = mat3(T, B, vNormal);
// transform the normal to eye space
return tangentNormal*TBN;
return TBN * tangentNormal;
}
return vNormal;
}
......
#define M_PI 3.1415926535897932384626433832795
vec3 unpackNormalTex(vec4 tex) {
return normalize(tex.xyz * 2.0 - vec3(1.0));
}
vec3 unpackNormal(vec4 normalMotion) {
return normalize(vec3(normalMotion.xy, sqrt(1.0 - normalMotion.x*normalMotion.x - normalMotion.y*normalMotion.y)));
}
\ No newline at end of file
......@@ -34,7 +34,7 @@ SharedGeometryData loadGeometryData(const std::string& _filename);
//! Loads from a Wavefront OBJ file. If _computeNormals and the mesh had no normals stored,
//! face normals are computed from the geometry
SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _computeNormals = true);
SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _computeNormals = true, bool _computeTangents = true);
//! Loads data from an attribute ATB file. If no _attributeName is specified, it will be guessed
//! from the filename, e.g. /foo/bar/VertexColor.atb --> aVertexColor
......
......@@ -144,7 +144,7 @@ namespace OpenGL{
// library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _computeNormals)
SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _computeNormals, bool _computeTangents)
{
char *currentLocale;
currentLocale = setlocale( LC_NUMERIC, NULL ); // store current locale
......@@ -163,9 +163,11 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
GLenum primitiveType = GL_INVALID_ENUM;
bool hasTexCoords = false;
bool hasNormals = false;
bool hasTangents = false;
int positionDimension = 4;
int texCoordDimension = -1;
int normalDimension = 3;
int tangentDimension = 3;
std::vector<glm::vec4> positionData;
std::vector<glm::vec3> texCoordData;
......@@ -346,10 +348,21 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
_computeNormals = false;
}
if (!hasTangents && _computeTangents) {
// perform own per-face normal creation only if the model had no own normals!
if (primitiveType != GL_TRIANGLES || !hasNormals || !hasTexCoords) {
warning() << "computing OBJ tangents is only supported for models with faces" << std::endl;
_computeTangents = false;
} else {
debug() << "computing tangents" << std::endl;
hasTangents = true;
}
}
// all data are read from the file. construct an ArrayBuffer from the data
data = SharedGeometryData(new GeometryData());
size_t abDataElements = (positionDimension + hasTexCoords * texCoordDimension + hasNormals * normalDimension) * indices.size();
size_t abDataElements = (positionDimension + hasTexCoords * texCoordDimension + hasNormals * normalDimension + hasTangents * tangentDimension) * indices.size();
GLfloat* abData = new GLfloat[abDataElements];
size_t pos = 0;
......@@ -393,6 +406,33 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
abData[pos++] = normal.z;
}
}
if (_computeTangents && hasNormals && hasTexCoords) {
size_t triangleIndex = i / 3;
glm::vec3 v0 = (glm::vec3)positionData[indices[3 * triangleIndex + 0].position];
glm::vec3 v1 = (glm::vec3)positionData[indices[3 * triangleIndex + 1].position];
glm::vec3 v2 = (glm::vec3)positionData[indices[3 * triangleIndex + 2].position];
glm::vec3 uv0 = (glm::vec3)texCoordData[indices[3 * triangleIndex + 0].texCoord];
glm::vec3 uv1 = (glm::vec3)texCoordData[indices[3 * triangleIndex + 1].texCoord];
glm::vec3 uv2 = (glm::vec3)texCoordData[indices[3 * triangleIndex + 2].texCoord];
// Edges of the triangle : postion delta
glm::vec3 deltaPos1 = v1 - v0;
glm::vec3 deltaPos2 = v2 - v0;
// UV delta
glm::vec3 deltaUV1 = uv1 - uv0;
glm::vec3 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;
abData[pos++] = tangent.x;
abData[pos++] = tangent.y;
abData[pos++] = tangent.z;
}
}
size_t strideSize = 0;
......@@ -417,6 +457,13 @@ SharedGeometryData loadGeometryDataFromOBJ(const std::string& _filename, bool _c
data->mAttributes.push_back(attrNormal);
}
if (hasTangents) {
ArrayBuffer::Attribute attrTangent = { "aTangent", GL_FLOAT, tangentDimension, (GLuint)strideSize, GL_FALSE, 0, GL_FALSE };
strideSize += tangentDimension * sizeof(GLfloat);
data->mAttributes.push_back(attrTangent);
}
data->setStrideSize( (GLsizei) strideSize);
data->setSize( (GLsizei) abDataElements * sizeof(GLfloat));
data->setData((GLubyte*)abData);
......
......@@ -114,7 +114,7 @@ bool WindowSystem::createWindow() {
}
// Use Vsync
if (SDL_GL_SetSwapInterval(1) < 0) {
if (SDL_GL_SetSwapInterval(0) < 0) {
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
}
......
......@@ -82,8 +82,9 @@ bool AtmosphereTestScene::startup() {
m_renderer->setRenderPassActive("Minimap"_sh, false);
m_renderer->addRenderPass(consoleCamera, "Console"_sh, ScreenSpaceSize::HALF);
m_renderer->setRenderPassActive("Cockpit"_sh, true);
m_renderer->setRenderPassActive("Console"_sh, true);
m_console.m_events = m_events;
m_console.m_renderer = m_renderer;
m_console.m_sceneGraph = m_sceneGraph;
......
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