leocad/common/lc_context.h

289 lines
6.5 KiB
C
Raw Normal View History

#pragma once
2016-10-05 14:28:52 -07:00
#include "lc_math.h"
#include "lc_colors.h"
#include "lc_mesh.h"
class lcVertexBuffer
{
public:
2020-03-22 13:44:20 -07:00
constexpr lcVertexBuffer()
: Pointer(nullptr)
2016-10-05 14:28:52 -07:00
{
}
bool IsValid() const
{
return Pointer != nullptr;
2016-10-05 14:28:52 -07:00
}
union
{
GLuint Object;
void* Pointer;
};
};
class lcIndexBuffer
{
public:
2020-03-22 13:44:20 -07:00
constexpr lcIndexBuffer()
: Pointer(nullptr)
2016-10-05 14:28:52 -07:00
{
}
bool IsValid() const
{
return Pointer != nullptr;
2016-10-05 14:28:52 -07:00
}
union
{
GLuint Object;
void* Pointer;
};
};
2020-03-22 13:44:20 -07:00
enum class lcMaterialType
2016-10-05 14:28:52 -07:00
{
2020-03-22 13:44:20 -07:00
UnlitColor,
2021-03-06 18:54:24 -08:00
UnlitColorConditional,
2020-03-22 13:44:20 -07:00
UnlitTextureModulate,
UnlitTextureDecal,
UnlitVertexColor,
UnlitViewSphere,
FakeLitColor,
FakeLitTextureDecal,
Count
2016-10-05 14:28:52 -07:00
};
2021-04-10 15:22:41 -07:00
enum class lcProgramAttrib
2016-10-05 14:28:52 -07:00
{
2021-04-10 15:22:41 -07:00
Position,
Normal,
TexCoord,
Color,
ControlPoint1 = 0,
ControlPoint2,
ControlPoint3,
ControlPoint4,
Count
2016-10-05 14:28:52 -07:00
};
struct lcProgram
{
GLuint Object;
2017-03-11 16:47:21 -08:00
GLint WorldViewProjectionMatrixLocation;
GLint WorldMatrixLocation;
GLint MaterialColorLocation;
GLint LightPositionLocation;
GLint EyePositionLocation;
2019-01-12 17:43:23 -08:00
GLint HighlightParamsLocation;
2016-10-05 14:28:52 -07:00
};
2020-03-22 13:44:20 -07:00
enum class lcPolygonOffset
{
2020-03-22 13:44:20 -07:00
None,
Opaque,
Translucent
};
2021-01-12 14:29:56 -08:00
enum class lcDepthFunction
{
LessEqual,
Always
};
2021-04-10 15:22:41 -07:00
struct lcVertexAttribState
{
GLint Size = 0;
GLenum Type = 0;
GLboolean Normalized = 0;
bool Enabled = 0;
GLsizei Stride = 0;
const void* Pointer = nullptr;
GLuint VertexBufferObject = 0;
2021-04-10 15:22:41 -07:00
};
2020-12-27 13:05:55 -08:00
class lcContext : protected QOpenGLFunctions
2016-10-05 14:28:52 -07:00
{
public:
lcContext();
~lcContext();
2020-05-03 15:39:39 -07:00
lcContext(const lcContext&) = delete;
2021-11-14 18:34:24 -08:00
lcContext(lcContext&&) = delete;
2020-05-03 15:39:39 -07:00
lcContext& operator=(const lcContext&) = delete;
2021-11-14 18:34:24 -08:00
lcContext& operator=(lcContext&&) = delete;
2020-05-03 15:39:39 -07:00
static bool InitializeRenderer();
static void ShutdownRenderer();
static lcContext* GetGlobalOffscreenContext();
2020-12-27 13:05:55 -08:00
void CreateResources();
void DestroyResources();
2016-10-05 14:28:52 -07:00
void SetDefaultState();
2017-03-25 12:29:28 -07:00
void ClearResources();
2016-10-05 14:28:52 -07:00
void MakeCurrent();
2021-01-08 11:25:24 -08:00
void SetGLContext(QOpenGLContext* GLContext, QOpenGLWidget* Widget);
void SetOffscreenContext();
2020-12-27 13:05:55 -08:00
bool IsOffscreen() const
{
2024-10-09 12:01:49 -07:00
return mWidget == nullptr;
}
2020-12-29 15:32:11 -08:00
void ClearColorAndDepth(const lcVector4& ClearColor);
void ClearDepth();
2016-10-05 14:28:52 -07:00
void SetWorldMatrix(const lcMatrix44& WorldMatrix)
{
mWorldMatrix = WorldMatrix;
mWorldMatrixDirty = true;
}
void SetViewMatrix(const lcMatrix44& ViewMatrix)
{
mViewMatrix = ViewMatrix;
mViewMatrixDirty = true;
mViewProjectionMatrixDirty = true;
}
void SetProjectionMatrix(const lcMatrix44& ProjectionMatrix)
{
mProjectionMatrix = ProjectionMatrix;
mProjectionMatrixDirty = true;
mViewProjectionMatrixDirty = true;
}
2017-04-01 16:53:54 -07:00
const lcMatrix44& GetProjectionMatrix()
{
return mProjectionMatrix;
}
void SetMaterial(lcMaterialType MaterialType);
2016-10-05 14:28:52 -07:00
void SetViewport(int x, int y, int Width, int Height);
void SetPolygonOffset(lcPolygonOffset PolygonOffset);
2019-11-09 18:11:25 -08:00
void SetDepthWrite(bool Enable);
2021-01-12 14:29:56 -08:00
void SetDepthFunction(lcDepthFunction DepthFunction);
2021-11-22 16:01:45 -08:00
void EnableDepthTest(bool Enable);
2021-11-22 16:18:09 -08:00
void EnableColorWrite(bool Enable);
void EnableColorBlend(bool Enable);
2021-01-12 14:29:56 -08:00
void EnableCullFace(bool Enable);
2016-10-05 14:28:52 -07:00
void SetLineWidth(float LineWidth);
2021-11-21 12:16:19 -08:00
void BindTexture2D(const lcTexture* Texture);
void BindTextureCubeMap(const lcTexture* Texture);
void ClearTexture2D();
void ClearTextureCubeMap();
2021-11-21 18:25:17 -08:00
void UploadTexture(lcTexture* Texture);
2016-10-05 14:28:52 -07:00
void SetColor(const lcVector4& Color)
{
mColor = Color;
mColorDirty = true;
}
2019-01-19 20:04:08 -08:00
void SetHighlightParams(const lcVector4& HighlightPosition, const lcVector4& TextColor, const lcVector4& BackgroundColor, const lcVector4& HighlightColor)
2018-10-28 17:59:01 -07:00
{
2019-01-19 20:04:08 -08:00
mHighlightParams[0] = HighlightPosition;
mHighlightParams[1] = TextColor;
mHighlightParams[2] = BackgroundColor;
mHighlightParams[3] = HighlightColor;
2019-01-12 17:43:23 -08:00
mHighlightParamsDirty = true;
2018-10-28 17:59:01 -07:00
}
2016-10-05 14:28:52 -07:00
void SetColor(float Red, float Green, float Blue, float Alpha);
void SetColorIndex(int ColorIndex);
2021-11-24 15:20:08 -08:00
void SetColorIndexTinted(int ColorIndex, const lcVector4& Tint, float Weight);
2020-04-25 12:16:37 -07:00
void SetColorIndexTinted(int ColorIndex, const lcVector4& Tint);
2016-10-05 14:28:52 -07:00
void SetEdgeColorIndex(int ColorIndex);
2020-04-25 12:16:37 -07:00
void SetEdgeColorIndexTinted(int ColorIndex, const lcVector4& Tint);
2016-10-05 14:28:52 -07:00
lcVertexBuffer CreateVertexBuffer(int Size, const void* Data);
void DestroyVertexBuffer(lcVertexBuffer& VertexBuffer);
lcIndexBuffer CreateIndexBuffer(int Size, const void* Data);
void DestroyIndexBuffer(lcIndexBuffer& IndexBuffer);
void ClearVertexBuffer();
void SetVertexBuffer(lcVertexBuffer VertexBuffer);
void SetVertexBufferPointer(const void* VertexBuffer);
void ClearIndexBuffer();
void SetIndexBuffer(lcIndexBuffer IndexBuffer);
void SetIndexBufferPointer(const void* IndexBuffer);
2017-03-24 09:34:53 -07:00
void SetVertexFormat(int BufferOffset, int PositionSize, int NormalSize, int TexCoordSize, int ColorSize, bool EnableNormals);
void SetVertexFormatPosition(int PositionSize);
2021-03-06 18:54:24 -08:00
void SetVertexFormatConditional(int BufferOffset);
2016-10-05 14:28:52 -07:00
void DrawPrimitives(GLenum Mode, GLint First, GLsizei Count);
void DrawIndexedPrimitives(GLenum Mode, GLsizei Count, GLenum Type, int Offset);
2017-04-01 16:53:54 -07:00
void BindMesh(const lcMesh* Mesh);
2016-10-05 14:28:52 -07:00
protected:
static bool CreateOffscreenContext();
static void DestroyOffscreenContext();
2020-12-27 13:05:55 -08:00
void CreateShaderPrograms();
2016-10-05 14:28:52 -07:00
void FlushState();
2021-04-10 15:22:41 -07:00
void SetVertexAttribPointer(lcProgramAttrib Attrib, GLint Size, GLenum Type, GLboolean Normalized, GLsizei Stride, const void* Pointer);
void EnableVertexAttrib(lcProgramAttrib Attrib);
void DisableVertexAttrib(lcProgramAttrib Attrib);
QOpenGLWidget* mWidget = nullptr;
QOpenGLContext* mContext = nullptr;
2020-12-27 13:05:55 -08:00
2016-10-05 14:28:52 -07:00
GLuint mVertexBufferObject;
GLuint mIndexBufferObject;
2021-04-10 15:22:41 -07:00
const char* mVertexBufferPointer;
const char* mIndexBufferPointer;
const char* mVertexBufferOffset;
2016-10-05 14:28:52 -07:00
lcMaterialType mMaterialType;
2017-02-18 11:12:35 -08:00
bool mNormalEnabled;
2016-10-05 14:28:52 -07:00
bool mTexCoordEnabled;
bool mColorEnabled;
2021-04-10 15:22:41 -07:00
lcVertexAttribState mVertexAttribState[static_cast<int>(lcProgramAttrib::Count)];
2017-12-26 10:19:20 -08:00
GLuint mTexture2D;
2018-10-28 17:59:01 -07:00
GLuint mTextureCubeMap;
lcPolygonOffset mPolygonOffset;
2019-11-09 18:11:25 -08:00
bool mDepthWrite;
2021-01-12 14:29:56 -08:00
lcDepthFunction mDepthFunction;
2021-11-22 16:01:45 -08:00
bool mDepthTest;
2021-11-22 16:18:09 -08:00
bool mColorWrite;
bool mColorBlend;
2021-01-12 14:29:56 -08:00
bool mCullFace;
2016-10-05 14:28:52 -07:00
float mLineWidth;
int mMatrixMode;
2017-03-25 12:29:28 -07:00
bool mTextureEnabled;
2016-10-05 14:28:52 -07:00
lcVector4 mColor;
lcMatrix44 mWorldMatrix;
lcMatrix44 mViewMatrix;
lcMatrix44 mProjectionMatrix;
lcMatrix44 mViewProjectionMatrix;
2021-01-12 13:48:04 -08:00
lcVector4 mHighlightParams[4];
2016-10-05 14:28:52 -07:00
bool mColorDirty;
bool mWorldMatrixDirty;
bool mViewMatrixDirty;
bool mProjectionMatrixDirty;
bool mViewProjectionMatrixDirty;
2019-01-12 17:43:23 -08:00
bool mHighlightParamsDirty;
2016-10-05 14:28:52 -07:00
static std::unique_ptr<QOpenGLContext> mOffscreenContext;
static std::unique_ptr<QOffscreenSurface> mOffscreenSurface;
static std::unique_ptr<lcContext> mGlobalOffscreenContext;
2020-03-22 13:44:20 -07:00
static lcProgram mPrograms[static_cast<int>(lcMaterialType::Count)];
2016-10-05 14:28:52 -07:00
Q_DECLARE_TR_FUNCTIONS(lcContext);
};