Fixed crash drawing models in the piece preview.

This commit is contained in:
leo 2015-01-02 11:58:14 +00:00
parent 6f3694fb89
commit 7546ff0b54
5 changed files with 27 additions and 18 deletions

View file

@ -2,10 +2,11 @@
#define _LC_CONTEXT_H_
#include "lc_array.h"
#include "lc_math.h"
struct lcScene
{
lcCamera* Camera;
lcMatrix44 ViewMatrix;
lcArray<lcRenderMesh> OpaqueMeshes;
lcArray<lcRenderMesh> TranslucentMeshes;
lcArray<lcObject*> InterfaceObjects;

View file

@ -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();

View file

@ -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];

View file

@ -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<lcRenderMesh>& OpaqueMeshes, lcArray<lcRenderMesh>& TranslucentMeshes);

View file

@ -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);
}