leocad/common/lc_viewsphere.cpp

405 lines
12 KiB
C++
Raw Normal View History

2018-10-29 01:59:01 +01:00
#include "lc_global.h"
#include "lc_viewsphere.h"
#include "view.h"
#include "lc_previewwidget.h"
2018-10-29 01:59:01 +01:00
#include "lc_context.h"
#include "lc_stringcache.h"
#include "lc_application.h"
#include "image.h"
#include "lc_texture.h"
lcTexture* lcViewSphere::mTexture;
lcVertexBuffer lcViewSphere::mVertexBuffer;
lcIndexBuffer lcViewSphere::mIndexBuffer;
const float lcViewSphere::mRadius = 1.0f;
2019-01-20 05:04:08 +01:00
const float lcViewSphere::mHighlightRadius = 0.35f;
const int lcViewSphere::mSubdivisions = 7;
2018-10-29 01:59:01 +01:00
lcViewSphere::lcViewSphere(View* View)
2020-12-05 17:45:29 +01:00
: mWidget(View), mPreview(nullptr), mView(View), mIsPreview(false)
{
2020-12-05 19:29:39 +01:00
UpdateSettings();
}
lcViewSphere::lcViewSphere(lcPreviewWidget* Preview)
2020-12-05 17:45:29 +01:00
: mWidget(Preview), mPreview(Preview), mView(nullptr), mIsPreview(true)
2018-10-29 01:59:01 +01:00
{
2020-12-05 19:29:39 +01:00
UpdateSettings();
}
void lcViewSphere::UpdateSettings()
{
const lcPreferences& Preferences = lcGetPreferences();
if (!mIsPreview)
{
mSize = Preferences.mViewSphereSize;
mEnabled = Preferences.mViewSphereEnabled;
mLocation = Preferences.mViewSphereLocation;
}
else
{
mSize = Preferences.mPreviewViewSphereSize;
mEnabled = Preferences.mPreviewViewSphereEnabled;
mLocation = Preferences.mPreviewViewSphereLocation;
}
2018-10-29 01:59:01 +01:00
}
lcMatrix44 lcViewSphere::GetViewMatrix() const
{
2020-12-05 17:45:29 +01:00
lcMatrix44 ViewMatrix = mWidget->GetCamera()->mWorldView;
2018-10-29 01:59:01 +01:00
ViewMatrix.SetTranslation(lcVector3(0, 0, 0));
return ViewMatrix;
}
lcMatrix44 lcViewSphere::GetProjectionMatrix() const
{
return lcMatrix44Ortho(-mRadius * 1.25f, mRadius * 1.25f, -mRadius * 1.25f, mRadius * 1.25f, -mRadius * 1.25f, mRadius * 1.25f);
2018-10-29 01:59:01 +01:00
}
void lcViewSphere::CreateResources(lcContext* Context)
2018-10-29 01:59:01 +01:00
{
const int ImageSize = 128;
mTexture = new lcTexture();
const QString ViewNames[6] =
{
QCoreApplication::translate("ViewName", "Left"), QCoreApplication::translate("ViewName", "Right"), QCoreApplication::translate("ViewName", "Back"),
QCoreApplication::translate("ViewName", "Front"), QCoreApplication::translate("ViewName", "Top"), QCoreApplication::translate("ViewName", "Bottom")
2018-10-29 01:59:01 +01:00
};
const QTransform ViewTransforms[6] =
{
QTransform(0, 1, 1, 0, 0, 0), QTransform(0, -1, -1, 0, ImageSize, ImageSize), QTransform(-1, 0, 0, 1, ImageSize, 0),
QTransform(1, 0, 0, -1, 0, ImageSize), QTransform(1, 0, 0, -1, 0, ImageSize), QTransform(-1, 0, 0, 1, ImageSize, 0)
};
QImage PainterImage(ImageSize, ImageSize, QImage::Format_ARGB32);
QPainter Painter;
QFont Font("Helvetica", 20);
std::vector<Image> Images;
for (int ViewIdx = 0; ViewIdx < 6; ViewIdx++)
{
Image TextureImage;
TextureImage.Allocate(ImageSize, ImageSize, LC_PIXEL_FORMAT_A8);
2018-10-29 01:59:01 +01:00
Painter.begin(&PainterImage);
Painter.fillRect(0, 0, PainterImage.width(), PainterImage.height(), QColor(0, 0, 0));
Painter.setBrush(QColor(255, 255, 255));
Painter.setPen(QColor(255, 255, 255));
Painter.setFont(Font);
Painter.setTransform(ViewTransforms[ViewIdx]);
Painter.drawText(0, 0, PainterImage.width(), PainterImage.height(), Qt::AlignCenter, ViewNames[ViewIdx]);
Painter.end();
for (int y = 0; y < ImageSize; y++)
{
unsigned char* Dest = TextureImage.mData + (ImageSize - y - 1) * TextureImage.mWidth;
2018-10-29 01:59:01 +01:00
for (int x = 0; x < ImageSize; x++)
*Dest++ = qRed(PainterImage.pixel(x, y));
2018-10-29 01:59:01 +01:00
}
Images.emplace_back(std::move(TextureImage));
}
mTexture->SetImage(std::move(Images), LC_TEXTURE_CUBEMAP | LC_TEXTURE_LINEAR);
lcVector3 Verts[(mSubdivisions + 1) * (mSubdivisions + 1) * 6];
GLushort Indices[mSubdivisions * mSubdivisions * 6 * 6];
lcMatrix44 Transforms[6] =
{
lcMatrix44(lcVector4(0.0f, 1.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(1.0f, 0.0f, 0.0f, 0.0f), lcVector4(1.0f, 0.0f, 0.0f, 1.0f)),
lcMatrix44(lcVector4(0.0f, -1.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(1.0f, 0.0f, 0.0f, 0.0f), lcVector4(-1.0f, 0.0f, 0.0f, 1.0f)),
lcMatrix44(lcVector4(-1.0f, 0.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(0.0f, 1.0f, 0.0f, 0.0f), lcVector4(0.0f, 1.0f, 0.0f, 1.0f)),
lcMatrix44(lcVector4(1.0f, 0.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(0.0f, 1.0f, 0.0f, 0.0f), lcVector4(0.0f, -1.0f, 0.0f, 1.0f)),
lcMatrix44(lcVector4(1.0f, 0.0f, 0.0f, 0.0f), lcVector4(0.0f, 1.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 1.0f)),
lcMatrix44(lcVector4(1.0f, 0.0f, 0.0f, 0.0f), lcVector4(0.0f, -1.0f, 0.0f, 0.0f), lcVector4(0.0f, 0.0f, 1.0f, 0.0f), lcVector4(0.0f, 0.0f, -1.0f, 1.0f)),
};
const float Step = 2.0f / mSubdivisions;
lcVector3* CurVert = Verts;
for (int FaceIdx = 0; FaceIdx < 6; FaceIdx++)
{
for (int y = 0; y <= mSubdivisions; y++)
{
for (int x = 0; x <= mSubdivisions; x++)
{
lcVector3 Vert = lcMul31(lcVector3(Step * x - 1.0f, Step * y - 1.0f, 0.0f), Transforms[FaceIdx]);
lcVector3 Vert2 = Vert * Vert;
2019-03-28 23:44:30 +01:00
*CurVert++ = lcVector3(Vert.x * sqrt(1.0 - 0.5 * (Vert2.y + Vert2.z) + Vert2.y * Vert2.z / 3.0),
Vert.y * sqrt(1.0 - 0.5 * (Vert2.z + Vert2.x) + Vert2.z * Vert2.x / 3.0),
Vert.z * sqrt(1.0 - 0.5 * (Vert2.x + Vert2.y) + Vert2.x * Vert2.y / 3.0)
) * mRadius;
}
}
}
GLushort* CurIndex = Indices;
for (int FaceIdx = 0; FaceIdx < 6; FaceIdx++)
{
const int FaceBase = FaceIdx * (mSubdivisions + 1) * (mSubdivisions + 1);
for (int y = 0; y < mSubdivisions; y++)
{
int RowBase = FaceBase + (mSubdivisions + 1) * y;
for (int x = 0; x < mSubdivisions; x++)
{
*CurIndex++ = RowBase + x;
*CurIndex++ = RowBase + x + 1;
*CurIndex++ = RowBase + x + (mSubdivisions + 1);
*CurIndex++ = RowBase + x + 1;
*CurIndex++ = RowBase + x + 1 + (mSubdivisions + 1);
*CurIndex++ = RowBase + x + (mSubdivisions + 1);
}
}
}
mVertexBuffer = Context->CreateVertexBuffer(sizeof(Verts), Verts);
mIndexBuffer = Context->CreateIndexBuffer(sizeof(Indices), Indices);
2018-10-29 01:59:01 +01:00
}
void lcViewSphere::DestroyResources(lcContext* Context)
2018-10-29 01:59:01 +01:00
{
delete mTexture;
mTexture = nullptr;
Context->DestroyVertexBuffer(mVertexBuffer);
Context->DestroyIndexBuffer(mIndexBuffer);
2018-10-29 01:59:01 +01:00
}
void lcViewSphere::Draw()
{
2020-12-05 19:29:39 +01:00
UpdateSettings();
2018-10-29 01:59:01 +01:00
2020-12-05 19:29:39 +01:00
if (!mSize || !mEnabled)
2018-10-29 01:59:01 +01:00
return;
2020-12-05 17:45:29 +01:00
lcContext* Context = mWidget->mContext;
2020-12-05 19:29:39 +01:00
const int Width = mWidget->mWidth;
const int Height = mWidget->mHeight;
const int ViewportSize = mSize;
const int Left = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::TopLeft) ? 0 : Width - ViewportSize;
const int Bottom = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::BottomRight) ? 0 : Height - ViewportSize;
2018-10-29 01:59:01 +01:00
Context->SetViewport(Left, Bottom, ViewportSize, ViewportSize);
glDepthFunc(GL_ALWAYS);
glEnable(GL_CULL_FACE);
Context->SetVertexBuffer(mVertexBuffer);
Context->SetVertexFormatPosition(3);
Context->SetIndexBuffer(mIndexBuffer);
2020-03-22 21:44:20 +01:00
Context->SetMaterial(lcMaterialType::UnlitColor);
Context->SetColor(lcVector4(0.0f, 0.0f, 0.0f, 1.0f));
float Scale = 1.005f + 2.0f / (float)ViewportSize;
Context->SetWorldMatrix(lcMatrix44Scale(lcVector3(Scale, Scale, Scale)));
Context->SetViewMatrix(GetViewMatrix());
Context->SetProjectionMatrix(GetProjectionMatrix());
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6 * 6, GL_UNSIGNED_SHORT, 0);
2020-03-22 21:44:20 +01:00
Context->SetMaterial(lcMaterialType::UnlitViewSphere);
2018-10-29 01:59:01 +01:00
Context->BindTextureCubeMap(mTexture->mTexture);
Context->SetWorldMatrix(lcMatrix44Identity());
Context->SetViewMatrix(GetViewMatrix());
Context->SetProjectionMatrix(GetProjectionMatrix());
2019-01-20 05:04:08 +01:00
lcVector4 HighlightPosition(0.0f, 0.0f, 0.0f, 0.0f);
2018-10-29 01:59:01 +01:00
if (mIntersectionFlags.any())
{
2019-01-13 02:43:23 +01:00
for (int AxisIdx = 0; AxisIdx < 3; AxisIdx++)
{
if (mIntersectionFlags.test(2 * AxisIdx))
2019-01-20 05:04:08 +01:00
HighlightPosition[AxisIdx] = 1.0f;
2019-01-13 02:43:23 +01:00
else if (mIntersectionFlags.test(2 * AxisIdx + 1))
2019-01-20 05:04:08 +01:00
HighlightPosition[AxisIdx] = -1.0f;
2019-01-13 02:43:23 +01:00
}
2019-01-20 05:04:08 +01:00
HighlightPosition = lcVector4(lcNormalize(lcVector3(HighlightPosition)), mHighlightRadius);
2018-10-29 01:59:01 +01:00
}
2020-12-05 19:29:39 +01:00
const lcPreferences& Preferences = lcGetPreferences();
2019-01-20 20:59:18 +01:00
const lcVector4 TextColor = lcVector4FromColor(Preferences.mViewSphereTextColor);
const lcVector4 BackgroundColor = lcVector4FromColor(Preferences.mViewSphereColor);
const lcVector4 HighlightColor = lcVector4FromColor(Preferences.mViewSphereHighlightColor);
2019-01-20 05:04:08 +01:00
Context->SetHighlightParams(HighlightPosition, TextColor, BackgroundColor, HighlightColor);
Context->DrawIndexedPrimitives(GL_TRIANGLES, mSubdivisions * mSubdivisions * 6 * 6, GL_UNSIGNED_SHORT, 0);
2018-10-29 01:59:01 +01:00
glDisable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
Context->SetViewport(0, 0, Width, Height);
}
bool lcViewSphere::OnLeftButtonDown()
{
2020-12-05 19:29:39 +01:00
if (!mSize || !mEnabled)
2018-10-29 01:59:01 +01:00
return false;
mIntersectionFlags = GetIntersectionFlags(mIntersection);
if (!mIntersectionFlags.any())
return false;
2020-12-05 17:45:29 +01:00
mMouseDownX = mWidget->GetMouseX();
mMouseDownY = mWidget->GetMouseY();
2018-10-29 01:59:01 +01:00
mMouseDown = true;
return true;
}
bool lcViewSphere::OnLeftButtonUp()
{
2020-12-05 19:29:39 +01:00
if (!mSize || !mEnabled)
2018-10-29 01:59:01 +01:00
return false;
if (!mMouseDown)
return false;
mMouseDown = false;
if (!mIntersectionFlags.any())
return false;
lcVector3 Position(0.0f, 0.0f, 0.0f);
for (int AxisIdx = 0; AxisIdx < 3; AxisIdx++)
{
if (mIntersectionFlags.test(AxisIdx * 2))
Position[AxisIdx] = 1250.0f;
else if (mIntersectionFlags.test(AxisIdx * 2 + 1))
Position[AxisIdx] = -1250.0f;
}
mIsPreview ? mPreview->SetViewpoint(Position) : mView->SetViewpoint(Position);
2018-10-29 01:59:01 +01:00
return true;
}
bool lcViewSphere::OnMouseMove()
{
2020-12-05 19:29:39 +01:00
if (!mSize || !mEnabled)
2018-10-29 01:59:01 +01:00
return false;
if (IsDragging())
{
mIntersectionFlags.reset();
mIsPreview ? mPreview->StartOrbitTracking() : mView->StartOrbitTracking();
2018-10-29 01:59:01 +01:00
return true;
}
2020-12-05 17:45:29 +01:00
if (mWidget->IsTracking())
2018-10-29 01:59:01 +01:00
return false;
std::bitset<6> IntersectionFlags = GetIntersectionFlags(mIntersection);
if (IntersectionFlags != mIntersectionFlags)
{
mIntersectionFlags = IntersectionFlags;
2020-12-05 17:45:29 +01:00
mWidget->Redraw();
2018-10-29 01:59:01 +01:00
}
return mIntersectionFlags.any();
}
bool lcViewSphere::IsDragging() const
{
2020-12-05 17:45:29 +01:00
int InputStateX = mWidget->GetMouseX();
int InputStateY = mWidget->GetMouseY();
return mMouseDown && (qAbs(mMouseDownX - InputStateX) > 3 || qAbs(mMouseDownY - InputStateY) > 3);
2018-10-29 01:59:01 +01:00
}
std::bitset<6> lcViewSphere::GetIntersectionFlags(lcVector3& Intersection) const
{
2020-12-05 19:29:39 +01:00
const int Width = mWidget->mWidth;
const int Height = mWidget->mHeight;
const int ViewportSize = mSize;
const int Left = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::TopLeft) ? 0 : Width - ViewportSize;
const int Bottom = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::BottomRight) ? 0 : Height - ViewportSize;
const int x = mWidget->GetMouseX() - Left;
const int y = mWidget->GetMouseY() - Bottom;
2018-10-29 01:59:01 +01:00
std::bitset<6> IntersectionFlags;
if (x < 0 || x > Width || y < 0 || y > Height)
return IntersectionFlags;
lcVector3 StartEnd[2] = { lcVector3(x, y, 0), lcVector3(x, y, 1) };
const int Viewport[4] = { 0, 0, ViewportSize, ViewportSize };
lcUnprojectPoints(StartEnd, 2, GetViewMatrix(), GetProjectionMatrix(), Viewport);
float Distance;
if (lcSphereRayMinIntersectDistance(lcVector3(0.0f, 0.0f, 0.0f), mRadius, StartEnd[0], StartEnd[1], &Distance))
2018-10-29 01:59:01 +01:00
{
Intersection = (StartEnd[0] + (StartEnd[1] - StartEnd[0]) * Distance) / mRadius;
2018-10-29 01:59:01 +01:00
2019-01-20 05:04:08 +01:00
auto CheckIntersection = [&]()
2018-10-29 01:59:01 +01:00
{
2019-01-20 05:04:08 +01:00
for (int Axis1Idx = 0; Axis1Idx < 6; Axis1Idx++)
{
2019-05-18 20:33:27 +02:00
lcVector3 Point1(0.0f, 0.0f, 0.0f);
2019-01-20 05:04:08 +01:00
2019-05-18 20:33:27 +02:00
Point1[Axis1Idx / 2] = Axis1Idx % 2 ? -1.0f : 1.0f;
2019-01-20 05:04:08 +01:00
2019-05-18 20:33:27 +02:00
if (lcLengthSquared(Point1 - Intersection) < mHighlightRadius * mHighlightRadius)
2019-01-20 05:04:08 +01:00
{
IntersectionFlags.set(Axis1Idx);
return;
}
for (int Axis2Idx = 0; Axis2Idx < 6; Axis2Idx++)
{
if (Axis1Idx / 2 == Axis2Idx / 2)
continue;
2019-05-18 20:33:27 +02:00
lcVector3 Point2(0.0f, 0.0f, 0.0f);
Point2[Axis1Idx / 2] = Axis1Idx % 2 ? -0.70710678118f : 0.70710678118f;
Point2[Axis2Idx / 2] = Axis2Idx % 2 ? -0.70710678118f : 0.70710678118f;
2019-01-20 05:04:08 +01:00
2019-05-18 20:33:27 +02:00
if (lcLengthSquared(Point2 - Intersection) < mHighlightRadius * mHighlightRadius)
2019-01-20 05:04:08 +01:00
{
IntersectionFlags.set(Axis1Idx);
IntersectionFlags.set(Axis2Idx);
return;
}
for (int Axis3Idx = 0; Axis3Idx < 6; Axis3Idx++)
{
if (Axis1Idx / 2 == Axis3Idx / 2 || Axis2Idx / 2 == Axis3Idx / 2)
continue;
2019-05-18 20:33:27 +02:00
lcVector3 Point3(0.0f, 0.0f, 0.0f);
Point3[Axis1Idx / 2] = Axis1Idx % 2 ? -0.57735026919f : 0.57735026919f;
Point3[Axis2Idx / 2] = Axis2Idx % 2 ? -0.57735026919f : 0.57735026919f;
Point3[Axis3Idx / 2] = Axis3Idx % 2 ? -0.57735026919f : 0.57735026919f;
2019-01-20 05:04:08 +01:00
2019-05-18 20:33:27 +02:00
if (lcLengthSquared(Point3 - Intersection) < mHighlightRadius * mHighlightRadius)
2019-01-20 05:04:08 +01:00
{
IntersectionFlags.set(Axis1Idx);
IntersectionFlags.set(Axis2Idx);
IntersectionFlags.set(Axis3Idx);
return;
}
}
}
}
};
CheckIntersection();
2018-10-29 01:59:01 +01:00
}
return IntersectionFlags;
}