diff --git a/common/lc_context.h b/common/lc_context.h index f3a552f7..4f3c38c0 100644 --- a/common/lc_context.h +++ b/common/lc_context.h @@ -2,10 +2,11 @@ #define _LC_CONTEXT_H_ #include "lc_array.h" +#include "lc_math.h" struct lcScene { - lcCamera* Camera; + lcMatrix44 ViewMatrix; lcArray OpaqueMeshes; lcArray TranslucentMeshes; lcArray InterfaceObjects; diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 5744bc9a..6a193114 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -860,7 +860,7 @@ void lcModel::Paste() void lcModel::GetScene(lcScene& Scene, lcCamera* ViewCamera, bool DrawInterface) const { - Scene.Camera = ViewCamera; + Scene.ViewMatrix = ViewCamera->mWorldView; Scene.OpaqueMeshes.RemoveAll(); Scene.OpaqueMeshes.AllocGrow(mPieces.GetSize()); Scene.TranslucentMeshes.RemoveAll(); diff --git a/common/pieceinf.cpp b/common/pieceinf.cpp index 248b3d40..9761af8e 100644 --- a/common/pieceinf.cpp +++ b/common/pieceinf.cpp @@ -185,7 +185,7 @@ bool PieceInfo::BoxTest(const lcMatrix44& WorldMatrix, const lcVector4 WorldPlan } // Zoom extents for the preview window and print catalog -void PieceInfo::ZoomExtents(float Fov, float Aspect, float* EyePos) const +void PieceInfo::ZoomExtents(const lcMatrix44& ProjectionMatrix, lcMatrix44& ViewMatrix, float* EyePos) const { lcVector3 Points[8] = { @@ -208,14 +208,9 @@ void PieceInfo::ZoomExtents(float Fov, float Aspect, float* EyePos) const Position = lcVector3(-250.0f, -250.0f, 75.0f); Position += Center; - lcMatrix44 Projection = lcMatrix44Perspective(30.0f, Aspect, 1.0f, 2500.0f); lcMatrix44 ModelView = lcMatrix44LookAt(Position, Center, lcVector3(0, 0, 1)); - Position = lcZoomExtents(Position, ModelView, Projection, Points, 8); - - glMatrixMode(GL_PROJECTION); - glLoadMatrixf(Projection); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(lcMatrix44LookAt(Position, Center, lcVector3(0, 0, 1))); + Position = lcZoomExtents(Position, ModelView, ProjectionMatrix, Points, 8); + ViewMatrix = lcMatrix44LookAt(Position, Center, lcVector3(0, 0, 1)); if (EyePos) { @@ -253,7 +248,7 @@ void PieceInfo::AddRenderMeshes(lcScene& Scene, const lcMatrix44& WorldMatrix, i if ((mFlags & LC_PIECE_HAS_TRANSLUCENT) || ((mFlags & LC_PIECE_HAS_DEFAULT) && Translucent)) { - lcVector3 Pos = lcMul31(WorldMatrix[3], Scene.Camera->mWorldView); + lcVector3 Pos = lcMul31(WorldMatrix[3], Scene.ViewMatrix); RenderMesh.Distance = Pos[2]; diff --git a/common/pieceinf.h b/common/pieceinf.h index 91e78272..c95fa994 100644 --- a/common/pieceinf.h +++ b/common/pieceinf.h @@ -91,7 +91,7 @@ public: } // Operations - void ZoomExtents(float Fov, float Aspect, float* EyePos = NULL) const; + void ZoomExtents(const lcMatrix44& ProjectionMatrix, lcMatrix44& ViewMatrix, float* EyePos = NULL) const; void RenderPiece(int nColor); void AddRenderMeshes(lcScene& Scene, const lcMatrix44& WorldMatrix, int ColorIndex, bool Focused, bool Selected); void AddRenderMeshes(const lcMatrix44& ViewMatrix, const lcMatrix44& WorldMatrix, int ColorIndex, bool Focused, bool Selected, lcArray& OpaqueMeshes, lcArray& TranslucentMeshes); diff --git a/common/preview.cpp b/common/preview.cpp index 16798c1f..a7431691 100644 --- a/common/preview.cpp +++ b/common/preview.cpp @@ -43,10 +43,13 @@ void PiecePreview::OnDraw() Eye = lcMul30(Eye, lcMatrix44RotationX(-m_RotateX * LC_DTOR)); Eye = lcMul30(Eye, lcMatrix44RotationZ(-m_RotateZ * LC_DTOR)); + lcMatrix44 ProjectionMatrix = lcMatrix44Perspective(30.0f, aspect, 1.0f, 2500.0f); + lcMatrix44 ViewMatrix; + if (m_AutoZoom) { Eye = Eye * 100.0f; - m_PieceInfo->ZoomExtents(30.0f, aspect, Eye); + m_PieceInfo->ZoomExtents(ProjectionMatrix, ViewMatrix, Eye); // Update the new camera distance. lcVector3 d = Eye - m_PieceInfo->GetCenter(); @@ -54,13 +57,23 @@ void PiecePreview::OnDraw() } else { - glMatrixMode(GL_PROJECTION); - glLoadMatrixf(lcMatrix44Perspective(30.0f, aspect, 1.0f, 2500.0f)); - glMatrixMode(GL_MODELVIEW); - glLoadMatrixf(lcMatrix44LookAt(Eye * m_Distance, m_PieceInfo->GetCenter(), lcVector3(0, 0, 1))); + ViewMatrix = lcMatrix44LookAt(Eye * m_Distance, m_PieceInfo->GetCenter(), lcVector3(0, 0, 1)); } - m_PieceInfo->RenderPiece(gMainWindow->mColorIndex); + mContext->SetProjectionMatrix(ProjectionMatrix); + + lcScene Scene; + Scene.ViewMatrix = ViewMatrix; + + m_PieceInfo->AddRenderMeshes(Scene, lcMatrix44Identity(), gMainWindow->mColorIndex, false, false); + + Scene.OpaqueMeshes.Sort(lcOpaqueRenderMeshCompare); + Scene.TranslucentMeshes.Sort(lcTranslucentRenderMeshCompare); + + mContext->DrawOpaqueMeshes(ViewMatrix, Scene.OpaqueMeshes); + mContext->DrawTranslucentMeshes(ViewMatrix, Scene.TranslucentMeshes); + + mContext->UnbindMesh(); // context remove glDisableClientState(GL_VERTEX_ARRAY); }