diff --git a/Renderer.cpp b/Renderer.cpp index bcec60d..d31d596 100755 --- a/Renderer.cpp +++ b/Renderer.cpp @@ -383,6 +383,10 @@ namespace ZL { SetMatrix(); } + Matrix4f Renderer::GetProjectionModelViewMatrix() + { + return ProjectionModelViewMatrix; + } void Renderer::SetMatrix() { diff --git a/Renderer.h b/Renderer.h index e1cb639..405b2aa 100755 --- a/Renderer.h +++ b/Renderer.h @@ -104,6 +104,8 @@ namespace ZL { void PopMatrix(); + Matrix4f GetProjectionModelViewMatrix(); + void SetMatrix(); diff --git a/book001.txt b/book001.txt new file mode 100644 index 0000000..e2f03ec --- /dev/null +++ b/book001.txt @@ -0,0 +1,453 @@ +===Vertices: 32 +Vertex 0: +Vertex 1: +Vertex 2: +Vertex 3: +Vertex 4: +Vertex 5: +Vertex 6: +Vertex 7: +Vertex 8: +Vertex 9: +Vertex 10: +Vertex 11: +Vertex 12: +Vertex 13: +Vertex 14: +Vertex 15: +Vertex 16: +Vertex 17: +Vertex 18: +Vertex 19: +Vertex 20: +Vertex 21: +Vertex 22: +Vertex 23: +Vertex 24: +Vertex 25: +Vertex 26: +Vertex 27: +Vertex 28: +Vertex 29: +Vertex 30: +Vertex 31: +===UV Coordinates: +Face count: 64 +Face 0 +UV Count: 3 + UV + UV + UV +Face 1 +UV Count: 3 + UV + UV + UV +Face 2 +UV Count: 3 + UV + UV + UV +Face 3 +UV Count: 3 + UV + UV + UV +Face 4 +UV Count: 3 + UV + UV + UV +Face 5 +UV Count: 3 + UV + UV + UV +Face 6 +UV Count: 3 + UV + UV + UV +Face 7 +UV Count: 3 + UV + UV + UV +Face 8 +UV Count: 3 + UV + UV + UV +Face 9 +UV Count: 3 + UV + UV + UV +Face 10 +UV Count: 3 + UV + UV + UV +Face 11 +UV Count: 3 + UV + UV + UV +Face 12 +UV Count: 3 + UV + UV + UV +Face 13 +UV Count: 3 + UV + UV + UV +Face 14 +UV Count: 3 + UV + UV + UV +Face 15 +UV Count: 3 + UV + UV + UV +Face 16 +UV Count: 3 + UV + UV + UV +Face 17 +UV Count: 3 + UV + UV + UV +Face 18 +UV Count: 3 + UV + UV + UV +Face 19 +UV Count: 3 + UV + UV + UV +Face 20 +UV Count: 3 + UV + UV + UV +Face 21 +UV Count: 3 + UV + UV + UV +Face 22 +UV Count: 3 + UV + UV + UV +Face 23 +UV Count: 3 + UV + UV + UV +Face 24 +UV Count: 3 + UV + UV + UV +Face 25 +UV Count: 3 + UV + UV + UV +Face 26 +UV Count: 3 + UV + UV + UV +Face 27 +UV Count: 3 + UV + UV + UV +Face 28 +UV Count: 3 + UV + UV + UV +Face 29 +UV Count: 3 + UV + UV + UV +Face 30 +UV Count: 3 + UV + UV + UV +Face 31 +UV Count: 3 + UV + UV + UV +Face 32 +UV Count: 3 + UV + UV + UV +Face 33 +UV Count: 3 + UV + UV + UV +Face 34 +UV Count: 3 + UV + UV + UV +Face 35 +UV Count: 3 + UV + UV + UV +Face 36 +UV Count: 3 + UV + UV + UV +Face 37 +UV Count: 3 + UV + UV + UV +Face 38 +UV Count: 3 + UV + UV + UV +Face 39 +UV Count: 3 + UV + UV + UV +Face 40 +UV Count: 3 + UV + UV + UV +Face 41 +UV Count: 3 + UV + UV + UV +Face 42 +UV Count: 3 + UV + UV + UV +Face 43 +UV Count: 3 + UV + UV + UV +Face 44 +UV Count: 3 + UV + UV + UV +Face 45 +UV Count: 3 + UV + UV + UV +Face 46 +UV Count: 3 + UV + UV + UV +Face 47 +UV Count: 3 + UV + UV + UV +Face 48 +UV Count: 3 + UV + UV + UV +Face 49 +UV Count: 3 + UV + UV + UV +Face 50 +UV Count: 3 + UV + UV + UV +Face 51 +UV Count: 3 + UV + UV + UV +Face 52 +UV Count: 3 + UV + UV + UV +Face 53 +UV Count: 3 + UV + UV + UV +Face 54 +UV Count: 3 + UV + UV + UV +Face 55 +UV Count: 3 + UV + UV + UV +Face 56 +UV Count: 3 + UV + UV + UV +Face 57 +UV Count: 3 + UV + UV + UV +Face 58 +UV Count: 3 + UV + UV + UV +Face 59 +UV Count: 3 + UV + UV + UV +Face 60 +UV Count: 3 + UV + UV + UV +Face 61 +UV Count: 3 + UV + UV + UV +Face 62 +UV Count: 3 + UV + UV + UV +Face 63 +UV Count: 3 + UV + UV + UV +===Normals: +Vertex 0: Normal +Vertex 1: Normal +Vertex 2: Normal +Vertex 3: Normal +Vertex 4: Normal +Vertex 5: Normal +Vertex 6: Normal +Vertex 7: Normal +Vertex 8: Normal +Vertex 9: Normal +Vertex 10: Normal +Vertex 11: Normal +Vertex 12: Normal +Vertex 13: Normal +Vertex 14: Normal +Vertex 15: Normal +Vertex 16: Normal +Vertex 17: Normal +Vertex 18: Normal +Vertex 19: Normal +Vertex 20: Normal +Vertex 21: Normal +Vertex 22: Normal +Vertex 23: Normal +Vertex 24: Normal +Vertex 25: Normal +Vertex 26: Normal +Vertex 27: Normal +Vertex 28: Normal +Vertex 29: Normal +Vertex 30: Normal +Vertex 31: Normal +===Triangles: 64 +Triangle: [0, 1, 2] +Triangle: [3, 4, 1] +Triangle: [5, 6, 7] +Triangle: [8, 9, 6] +Triangle: [7, 9, 2] +Triangle: [0, 10, 5] +Triangle: [11, 12, 13] +Triangle: [4, 2, 1] +Triangle: [14, 7, 4] +Triangle: [15, 2, 9] +Triangle: [16, 13, 17] +Triangle: [5, 18, 8] +Triangle: [15, 19, 0] +Triangle: [8, 20, 15] +Triangle: [21, 22, 23] +Triangle: [18, 24, 20] +Triangle: [20, 21, 19] +Triangle: [10, 22, 18] +Triangle: [25, 26, 27] +Triangle: [21, 25, 19] +Triangle: [10, 26, 23] +Triangle: [23, 28, 21] +Triangle: [17, 0, 29] +Triangle: [13, 3, 17] +Triangle: [13, 5, 14] +Triangle: [12, 0, 5] +Triangle: [30, 19, 31] +Triangle: [16, 27, 11] +Triangle: [16, 19, 25] +Triangle: [11, 10, 30] +Triangle: [30, 29, 12] +Triangle: [29, 16, 17] +Triangle: [0, 3, 1] +Triangle: [3, 14, 4] +Triangle: [5, 8, 6] +Triangle: [8, 15, 9] +Triangle: [7, 6, 9] +Triangle: [0, 19, 10] +Triangle: [11, 30, 12] +Triangle: [4, 7, 2] +Triangle: [14, 5, 7] +Triangle: [15, 0, 2] +Triangle: [16, 11, 13] +Triangle: [5, 10, 18] +Triangle: [15, 20, 19] +Triangle: [8, 18, 20] +Triangle: [21, 24, 22] +Triangle: [18, 22, 24] +Triangle: [20, 24, 21] +Triangle: [10, 23, 22] +Triangle: [25, 28, 26] +Triangle: [21, 28, 25] +Triangle: [10, 27, 26] +Triangle: [23, 26, 28] +Triangle: [17, 3, 0] +Triangle: [13, 14, 3] +Triangle: [13, 12, 5] +Triangle: [12, 29, 0] +Triangle: [30, 10, 19] +Triangle: [16, 25, 27] +Triangle: [16, 31, 19] +Triangle: [11, 27, 10] +Triangle: [30, 31, 29] +Triangle: [29, 31, 16] diff --git a/book03.bmp b/book03.bmp new file mode 100644 index 0000000..dea14df Binary files /dev/null and b/book03.bmp differ diff --git a/main.cpp b/main.cpp index 2abc35f..d27fb28 100755 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,25 @@ namespace ZL { + void worldToScreenCoordinates(Vector3f objectPos, + Matrix4f projectionModelView, + int screenWidth, int screenHeight, + int& screenX, int& screenY) { + + + Vector4f inx = { objectPos.v[0], objectPos.v[1], objectPos.v[2], 1.0f}; + + Vector4f clipCoords = MultMatrixVector(projectionModelView, inx); + + // Перспективное деление + float ndcX = clipCoords.v[0] / clipCoords.v[3]; + float ndcY = clipCoords.v[1] / clipCoords.v[3]; + + // Преобразуем NDC в экранные координаты + screenX = (int)((ndcX + 1.0f) * 0.5f * screenWidth); + //screenY = (int)((1.0f - ndcY) * 0.5f * screenHeight); + screenY = (int)((1.0f + ndcY) * 0.5f * screenHeight); + } namespace Env { @@ -35,6 +54,7 @@ namespace ZL bool downPressed = false; Vector3f cameraShift = {0,0,0}; + Vector3f characterPos = { 0,0,0 }; /* Vector2f birdStartPos; @@ -47,11 +67,27 @@ namespace ZL }*/ } + struct ActiveObject + { + std::shared_ptr activeObjectTexturePtr; + VertexDataStruct activeObjectMesh; + VertexRenderStruct activeObjectMeshMutable; + + std::shared_ptr activeObjectScreenTexturePtr; + VertexDataStruct activeObjectScreenMesh; + VertexRenderStruct activeObjectScreenMeshMutable; + + + Vector3f objectPos; + bool highlighted = false; + }; + namespace GameObjects { std::shared_ptr testObjTexturePtr; std::shared_ptr roomTexturePtr; std::shared_ptr coneTexturePtr; + //std::shared_ptr activeObjectTexturePtr; VertexDataStruct colorCubeMesh; VertexRenderStruct colorCubeMeshMutable; @@ -68,6 +104,7 @@ namespace ZL VertexDataStruct coneMesh; VertexRenderStruct coneMeshMutable; + std::vector activeObjects; } static SDL_Window* window = NULL; @@ -129,98 +166,53 @@ namespace ZL renderer.TranslateMatrix(Env::cameraShift); + + //ActiveObject ao1; + + for (auto& ao : GameObjects::activeObjects) + { + renderer.PushMatrix(); + renderer.TranslateMatrix(ao.objectPos); + glBindTexture(GL_TEXTURE_2D, ao.activeObjectTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(ao.activeObjectMeshMutable); + renderer.PopMatrix(); + } + glBindTexture(GL_TEXTURE_2D, GameObjects::roomTexturePtr->getTexID()); renderer.DrawVertexRenderStruct(GameObjects::textMeshMutable); - - - - - - //renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 2.0)); - //renderer.RotateMatrix(QuatFromRotateAroundZ(-M_PI / 4.0)); - - /* - renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 3.0)); - - - for (int i = 0; i < GameObjects::testmd3.parts.size(); i++) - { - - for (int j = 0; j < GameObjects::testmd3.parts[i].renderMeshes.size(); j++) - { - - glBindTexture(GL_TEXTURE_2D, GameObjects::testmd3.parts[i].textures[0]->getTexID()); - renderer.DrawVertexRenderStruct(GameObjects::testmd3.parts[i].renderMeshes[j]); - } - } - */ - /* - GameObjects::testObjMeshMutable.AssignFrom(GameObjects::testObjMesh); - GameObjects::testObjMeshMutable.data.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundZ(gs.rotateTimer * M_PI / 3.0))); - GameObjects::testObjMeshMutable.RefreshVBO();*/ - - - //GameObjects::testmd3mutable[0].RefreshVBO(); - //GameObjects::testmd3mutable[1].RefreshVBO(); - - //glBindTexture(GL_TEXTURE_2D, GameObjects::testObjTexturePtr->getTexID()); - //renderer.DrawVertexRenderStruct(GameObjects::testObjMeshMutable); - - /* - glBindTexture(GL_TEXTURE_2D, GameObjects::md3TexturePtr->getTexID()); - renderer.DrawVertexRenderStruct(GameObjects::testmd3mutable[0]); - renderer.DrawVertexRenderStruct(GameObjects::testmd3mutable[1]); - */ - - - - //GameObjects::bxMutable.AssignFrom(GameObjects::bx.mesh); - //GameObjects::bxMutable.RefreshVBO(); - //renderer.DrawVertexRenderStruct(GameObjects::bxMutable); + auto latestProjectionModelView = renderer.GetProjectionModelViewMatrix(); renderer.PopMatrix(); + renderer.PopProjectionMatrix(); + glClear(GL_DEPTH_BUFFER_BIT); + renderer.PushProjectionMatrix(static_cast(Env::width), static_cast(Env::height)); + renderer.PushMatrix(); + + renderer.LoadIdentity(); + + for (auto& ao : GameObjects::activeObjects) + { + if (ao.highlighted) + { + int screenX, screenY; + worldToScreenCoordinates(ao.objectPos, latestProjectionModelView, Env::width, Env::height, screenX, screenY); + renderer.PushMatrix(); + renderer.TranslateMatrix(Vector3f{screenX + 0.f, screenY + 0.f, 0.0f}); + glBindTexture(GL_TEXTURE_2D, ao.activeObjectScreenTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(ao.activeObjectScreenMeshMutable); + renderer.PopMatrix(); + } + } + + renderer.PopMatrix(); renderer.PopProjectionMatrix(); renderer.DisableVertexAttribArray(vPositionName); renderer.DisableVertexAttribArray(vTexCoordName); renderer.shaderManager.PopShader(); - - - /* - renderer.shaderManager.PushShader(colorShaderName); - - //renderer.RenderUniform1i(textureUniformName, 0); - - renderer.EnableVertexAttribArray(vPositionName); - - renderer.EnableVertexAttribArray(vColorName); - - renderer.PushPerspectiveProjectionMatrix(1.0 / 6.0, static_cast(Env::width)/ static_cast(Env::height), 10, 10000); - renderer.PushMatrix(); - - renderer.LoadIdentity(); - - renderer.TranslateMatrix({ 0,0, -1000 }); - - - renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 3.0)); - - GameObjects::colorCubeMeshMutable.AssignFrom(GameObjects::colorCubeMesh); - GameObjects::colorCubeMeshMutable.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundZ(gs.rotateTimer * M_PI / 3.0))); - - renderer.DrawVertexRenderStruct(GameObjects::colorCubeMeshMutable); - - renderer.PopMatrix(); - - renderer.PopProjectionMatrix(); - - renderer.DisableVertexAttribArray(vColorName); - renderer.DisableVertexAttribArray(vPositionName); - - renderer.shaderManager.PopShader();*/ CheckGlError(); @@ -247,6 +239,29 @@ namespace ZL { Env::cameraShift.v[2] -= SPEED * ms; } + + Env::characterPos.v[0] = -Env::cameraShift.v[0]; + Env::characterPos.v[1] = -Env::cameraShift.v[1]; + Env::characterPos.v[2] = -Env::cameraShift.v[2]; + + for (auto& ao : GameObjects::activeObjects) + { + if (sqrtf( + (Env::characterPos.v[0] - ao.objectPos.v[0]) * (Env::characterPos.v[0] - ao.objectPos.v[0]) + + + (Env::characterPos.v[1] - ao.objectPos.v[1]) * (Env::characterPos.v[1] - ao.objectPos.v[1]) + + + (Env::characterPos.v[2] - ao.objectPos.v[2]) * (Env::characterPos.v[2] - ao.objectPos.v[2]) + ) < 50.f) + { + ao.highlighted = true; + } + else + { + ao.highlighted = false; + } + } + } void ProcessTickCount() @@ -308,8 +323,7 @@ namespace ZL GameObjects::roomTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")); GameObjects::coneTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); - - + GameObjects::colorCubeMesh = CreateCube3D(5.0); GameObjects::colorCubeMeshMutable.data = CreateCube3D(5.0); GameObjects::colorCubeMeshMutable.RefreshVBO(); @@ -331,6 +345,30 @@ namespace ZL GameObjects::coneMeshMutable.AssignFrom(GameObjects::coneMesh); GameObjects::coneMeshMutable.RefreshVBO(); + + ActiveObject ao1; + + + + ao1.activeObjectMesh = LoadFromTextFile("./book001.txt"); + ao1.activeObjectMesh.Scale(4); + + ao1.activeObjectMeshMutable.AssignFrom(ao1.activeObjectMesh); + ao1.activeObjectMeshMutable.RefreshVBO(); + + ao1.objectPos = Vector3f{50, 0, -300}; + ao1.activeObjectTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./book03.bmp")); + + ao1.activeObjectScreenTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./aoscreen01.bmp")); + + ao1.activeObjectScreenMesh = CreateRect2D({ 0.f, 0.f }, { 64.f, 64.f }, 0.5); + ao1.activeObjectScreenMeshMutable.AssignFrom(ao1.activeObjectScreenMesh); + ao1.activeObjectScreenMeshMutable.RefreshVBO(); + + GameObjects::activeObjects.push_back(ao1); + + + std::cout << "Hello test 4x" << std::endl; renderer.InitOpenGL();