leocad/common/lc_context.h

285 lines
6.4 KiB
C
Raw Normal View History

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