From 043246fbe64ea9eaea67c3cb087aaacccd785896 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 5 Mar 2017 22:14:50 -0800 Subject: [PATCH] Changed normal format for compatibility. --- common/lc_context.cpp | 47 +++++++++++++++++++++++++++++++++++++------ common/lc_context.h | 1 + common/lc_math.h | 25 ++++++++++++++--------- 3 files changed, 57 insertions(+), 16 deletions(-) diff --git a/common/lc_context.cpp b/common/lc_context.cpp index 36c13d83..346b85cb 100644 --- a/common/lc_context.cpp +++ b/common/lc_context.cpp @@ -274,9 +274,14 @@ void lcContext::CreateShaderPrograms() LC_PIXEL_INPUT "vec3 PixelNormal;\n" LC_PIXEL_OUTPUT "uniform mediump vec4 Color;\n" + "uniform mediump vec3 LightDirection[2];\n" "void main()\n" "{\n" - " gl_FragColor = vec4(PixelNormal, 1.0);\n" + " vec3 Normal = normalize(PixelNormal);\n" + " float Diffuse = abs(dot(Normal, LightDirection[0])) * 0.5;\n" + " Diffuse += abs(dot(Normal, LightDirection[1])) * 0.5;\n" + " Diffuse = clamp(Diffuse, 0.0, 1.0);\n" + " gl_FragColor = vec4(Diffuse, Diffuse, Diffuse, 1.0);\n" "}\n", // LC_MATERIAL_TEXTURE LC_SHADER_VERSION @@ -411,6 +416,7 @@ void lcContext::CreateShaderPrograms() mPrograms[LightingMode][MaterialType].Object = Program; mPrograms[LightingMode][MaterialType].MatrixLocation = glGetUniformLocation(Program, "WorldViewProjectionMatrix"); mPrograms[LightingMode][MaterialType].ColorLocation = glGetUniformLocation(Program, "Color"); + mPrograms[LightingMode][MaterialType].LightDirectionLocation = glGetUniformLocation(Program, "LightDirection"); } } } @@ -462,7 +468,7 @@ void lcContext::SetDefaultState() glDisableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, NULL); - glNormalPointer(GL_INT_2_10_10_10_REV, 0, NULL); + glNormalPointer(GL_BYTE, 0, NULL); glTexCoordPointer(2, GL_FLOAT, 0, NULL); glColorPointer(4, GL_FLOAT, 0, NULL); } @@ -879,7 +885,7 @@ void lcContext::SetVertexFormat(int BufferOffset, int PositionSize, int NormalSi { if (gSupportsShaderObjects) { - glVertexAttribPointer(LC_ATTRIB_NORMAL, 4, GL_INT_2_10_10_10_REV, true, VertexSize, VertexBufferPointer + Offset); + glVertexAttribPointer(LC_ATTRIB_NORMAL, 4, GL_BYTE, true, VertexSize, VertexBufferPointer + Offset); if (!mNormalEnabled) { @@ -889,7 +895,7 @@ void lcContext::SetVertexFormat(int BufferOffset, int PositionSize, int NormalSi } else { - glNormalPointer(GL_INT_2_10_10_10_REV, VertexSize, VertexBufferPointer + Offset); + glNormalPointer(GL_BYTE, VertexSize, VertexBufferPointer + Offset); if (!mNormalEnabled) { @@ -1088,7 +1094,7 @@ void lcContext::UnbindMesh() else { glVertexPointer(3, GL_FLOAT, 0, NULL); - glNormalPointer(GL_INT_2_10_10_10_REV, 0, NULL); + glNormalPointer(GL_BYTE, 0, NULL); glTexCoordPointer(2, GL_FLOAT, 0, NULL); } @@ -1111,6 +1117,16 @@ void lcContext::FlushState() mViewProjectionMatrixDirty = false; } + if (mWorldMatrixDirty && Program.LightDirectionLocation != -1) + { + lcVector3 LightDirection[2] = + { + lcMul30(lcVector3(1.0f, 0.0f, 0.0f), lcMatrix44AffineInverse(mWorldMatrix)), + lcMul30(lcVector3(0.0f, 1.0f, 0.0f), lcMatrix44AffineInverse(mWorldMatrix)) + }; + glUniform3fv(Program.LightDirectionLocation, 2, LightDirection[0]); + } + glUniformMatrix4fv(Program.MatrixLocation, 1, false, lcMul(mWorldMatrix, mViewProjectionMatrix)); mWorldMatrixDirty = false; mViewMatrixDirty = false; @@ -1205,7 +1221,7 @@ void lcContext::DrawMeshSection(lcMesh* Mesh, lcMeshSection* Section) } } - bool DrawConditional = false; + const bool DrawConditional = false; if (Section->PrimitiveType != LC_MESH_CONDITIONAL_LINES) { @@ -1322,6 +1338,25 @@ void lcContext::DrawOpaqueMeshes(const lcArray& OpaqueMeshes) DrawMeshSection(Mesh, Section); } + + const bool DrawNormals = false; + + if (DrawNormals) + { + lcVertex* VertexBuffer = (lcVertex*)Mesh->mVertexData; + lcVector3* Vertices = (lcVector3*)malloc(Mesh->mNumVertices * 2 * sizeof(lcVector3)); + + for (int VertexIdx = 0; VertexIdx < Mesh->mNumVertices; VertexIdx++) + { + Vertices[VertexIdx * 2] = VertexBuffer[VertexIdx].Position; + Vertices[VertexIdx * 2 + 1] = VertexBuffer[VertexIdx].Position + lcUnpackNormal(VertexBuffer[VertexIdx].Normal); + } + + SetVertexBufferPointer(Vertices); + SetVertexFormat(0, 3, 0, 0, 0); + DrawPrimitives(GL_LINES, 0, Mesh->mNumVertices * 2); + free(Vertices); + } } } diff --git a/common/lc_context.h b/common/lc_context.h index 73d5c5be..7e42f25a 100644 --- a/common/lc_context.h +++ b/common/lc_context.h @@ -89,6 +89,7 @@ struct lcProgram GLuint Object; GLint MatrixLocation; GLint ColorLocation; + GLint LightDirectionLocation; }; enum lcTextureMode diff --git a/common/lc_math.h b/common/lc_math.h index 522fb0b4..dee2b1df 100644 --- a/common/lc_math.h +++ b/common/lc_math.h @@ -534,21 +534,26 @@ inline lcVector4& operator/=(lcVector4& a, float b) inline qint32 lcPackNormal(const lcVector3& Normal) { - qint32 Packed = 0; + quint32 Packed = 0; - Packed |= ((int)(fabsf(Normal.x) * 511.0f)) << 0; - if (Normal.x < 0.0f) - Packed |= 1 << 9; - Packed |= ((int)(fabsf(Normal.y) * 511.0f)) << 10; - if (Normal.y < 0.0f) - Packed |= 1 << 19; - Packed |= ((int)(fabsf(Normal.z) * 511.0f)) << 20; - if (Normal.z < 0.0f) - Packed |= 1 << 29; + Packed |= (((qint8)(Normal.x * 127.0f)) & 0xff) << 0; + Packed |= (((qint8)(Normal.y * 127.0f)) & 0xff) << 8; + Packed |= (((qint8)(Normal.z * 127.0f)) & 0xff) << 16; return Packed; } +inline lcVector3 lcUnpackNormal(qint32 Packed) +{ + lcVector3 Normal; + + Normal.x = (float)(qint8)((Packed >> 0) & 0xff) / 127.0f; + Normal.y = (float)(qint8)((Packed >> 8) & 0xff) / 127.0f; + Normal.z = (float)(qint8)((Packed >> 16) & 0xff) / 127.0f; + + return Normal; +} + inline lcVector3 lcVector3LDrawToLeoCAD(const lcVector3& Vector) { return lcVector3(Vector[0], Vector[2], -Vector[1]);