From d49d09a1e54a4b572c257da7ae7158b43ddc16f6 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Sun, 9 Jun 2024 21:41:54 +0300 Subject: [PATCH] light working --- include/Render/RenderMisc.h | 3 ++- src/ModelManager/ModelManager.cpp | 9 ++++++- src/Render/RenderMisc.cpp | 20 ++++++++++++++ src/SimpleLand/SimpleLand.cpp | 43 ++++++++++++++++++++++++++++--- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/include/Render/RenderMisc.h b/include/Render/RenderMisc.h index ac5eeb6..402672b 100644 --- a/include/Render/RenderMisc.h +++ b/include/Render/RenderMisc.h @@ -54,11 +54,12 @@ namespace SE { std::vector PositionData; std::vector TexCoordData; + std::vector NormalData; std::shared_ptr vao; std::shared_ptr positionVBO; std::shared_ptr texCoordVBO; - + std::shared_ptr normalVBO; void RefreshVBO(); }; diff --git a/src/ModelManager/ModelManager.cpp b/src/ModelManager/ModelManager.cpp index 1e85f68..df605cd 100644 --- a/src/ModelManager/ModelManager.cpp +++ b/src/ModelManager/ModelManager.cpp @@ -119,6 +119,7 @@ bool TLiteModel::LoadModel(const std::string& s) vertexDataStruct.PositionData = res.VertexArr; vertexDataStruct.TexCoordData = res.TexCoordArr; + vertexDataStruct.NormalData = res.NormalArr; vertexDataStruct.RefreshVBO(); /* @@ -237,12 +238,17 @@ void TLiteModel::DrawVBO() EnableVertexAttribArray("vPosition"); EnableVertexAttribArray("vTexCoord"); + EnableVertexAttribArray("vNormal"); + //Renderer->DrawTriangleList(TriangleList); DrawVertexDataStruct(vertexDataStruct); - DisableVertexAttribArray("vPosition"); + DisableVertexAttribArray("vNormal"); DisableVertexAttribArray("vTexCoord"); + DisableVertexAttribArray("vPosition"); + + RenderUniform1i(CONST_STRING_NORMALMAPEXISTS_UNIFORM, 1); @@ -267,6 +273,7 @@ void TLiteModel::RotateModel(const mat3& r) for (cardinal i = 0; i < TriangleCount * 3; i++) { vertexDataStruct.PositionData[i] = r * vertexDataStruct.PositionData[i]; + vertexDataStruct.NormalData[i] = r * vertexDataStruct.NormalData[i]; //TriangleList.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB][i] = r * TriangleList.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB][i]; //TriangleList.Data.Vec3CoordArr[CONST_STRING_NORMAL_ATTRIB][i] = r * TriangleList.Data.Vec3CoordArr[CONST_STRING_NORMAL_ATTRIB][i]; //TriangleList.Data.Vec3CoordArr[CONST_STRING_TANGENT_ATTRIB][i] = r * TriangleList.Data.Vec3CoordArr[CONST_STRING_TANGENT_ATTRIB][i]; diff --git a/src/Render/RenderMisc.cpp b/src/Render/RenderMisc.cpp index 6e3d84f..cfda087 100644 --- a/src/Render/RenderMisc.cpp +++ b/src/Render/RenderMisc.cpp @@ -66,17 +66,37 @@ namespace SE glBufferData(GL_ARRAY_BUFFER, TexCoordData.size() * 8, &TexCoordData[0], GL_STATIC_DRAW); + if (NormalData.size() > 0) + { + if (!normalVBO) + { + normalVBO = std::make_shared(); + } + + glBindBuffer(GL_ARRAY_BUFFER, normalVBO->getBuffer()); + + glBufferData(GL_ARRAY_BUFFER, NormalData.size() * 12, &NormalData[0], GL_STATIC_DRAW); + + } + } void DrawVertexDataStruct(const VertexDataStruct& vertexDataStruct) { static const std::string vTexCoord("vTexCoord"); static const std::string vPosition("vPosition"); + static const std::string vNormal("vNormal"); //Check if main thread, check if data is not empty... glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.texCoordVBO->getBuffer()); VertexAttribPointer2fv(vTexCoord, 0, NULL); + if (vertexDataStruct.normalVBO) + { + glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.normalVBO->getBuffer()); + VertexAttribPointer3fv(vNormal, 0, NULL); + } + glBindBuffer(GL_ARRAY_BUFFER, vertexDataStruct.positionVBO->getBuffer()); VertexAttribPointer3fv(vPosition, 0, NULL); diff --git a/src/SimpleLand/SimpleLand.cpp b/src/SimpleLand/SimpleLand.cpp index 7e08678..e3c0890 100644 --- a/src/SimpleLand/SimpleLand.cpp +++ b/src/SimpleLand/SimpleLand.cpp @@ -73,7 +73,7 @@ void TSimpleLandClass::CreateVBO() vertexDataStruct.PositionData.clear(); vertexDataStruct.TexCoordData.clear(); - + vertexDataStruct.NormalData.clear(); for (i = 0; i < Height - 1; i++) { for (j = 0; j < Width - 1; j++) @@ -98,6 +98,16 @@ void TSimpleLandClass::CreateVBO() vertexDataStruct.PositionData.push_back(VertexMatrix[k_up].pos); vertexDataStruct.PositionData.push_back(VertexMatrix[k_right].pos); + + vertexDataStruct.NormalData.push_back(VertexMatrix[k].norm); + vertexDataStruct.NormalData.push_back(VertexMatrix[k_right].norm); + vertexDataStruct.NormalData.push_back(VertexMatrix[k_up].norm); + + + vertexDataStruct.NormalData.push_back(VertexMatrix[k_right_up].norm); + vertexDataStruct.NormalData.push_back(VertexMatrix[k_up].norm); + vertexDataStruct.NormalData.push_back(VertexMatrix[k_right].norm); + vertexDataStruct.TexCoordData.push_back({ tx0, ty0 }); vertexDataStruct.TexCoordData.push_back({ tx1, ty0 }); vertexDataStruct.TexCoordData.push_back({ tx0, ty1 }); @@ -361,7 +371,7 @@ bool TSimpleLandClass::LoadFromFile(const std::string& filename) //Replace old matrices with new - TriangleMatrix.resize(2*(Width-1)*(Height-1)); + TriangleMatrix.resize(2*(Width)*(Height)); VertexMatrix = tempVertexMatrix; UpdateTriangleMatrix(); @@ -556,12 +566,15 @@ void TSimpleLandClass::DrawVBO() EnableVertexAttribArray("vPosition"); EnableVertexAttribArray("vTexCoord"); + EnableVertexAttribArray("vNormal"); //Renderer->DrawTriangleList(TriangleList); DrawVertexDataStruct(vertexDataStruct); DisableVertexAttribArray("vPosition"); DisableVertexAttribArray("vTexCoord"); + DisableVertexAttribArray("vNormal"); + /* @@ -876,6 +889,25 @@ void TSimpleLandClass::RecalcNormalVectors() { int i,j,ifrom,ito,jfrom,jto; vec3 v1,v2; + for (i = 1; i < Height-1; i++) + { + for (j = 1; j < Width-1; j++) + { + ifrom = i - 1; + ito = i + 1; + + jfrom = j - 1; + jto = j + 1; + + v1 = Normalize(VertexMatrix[j + ito * Width].pos - VertexMatrix[j + ifrom * Width].pos); + + v2 = Normalize(VertexMatrix[jto + i * Width].pos - VertexMatrix[jfrom + i * Width].pos); + + VertexMatrix[j+i*Width].norm = CrossProduct(v2, v1); + } + } + + /* for (i=0;i