leocad/common/lc_context.h

279 lines
6.2 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,
UnlitTextureModulate,
UnlitTextureDecal,
UnlitVertexColor,
UnlitViewSphere,
FakeLitColor,
FakeLitTextureDecal,
Count
2016-10-05 23:28:52 +02:00
};
enum lcProgramAttrib
{
2017-02-18 20:12:35 +01:00
LC_ATTRIB_POSITION,
LC_ATTRIB_NORMAL,
LC_ATTRIB_TEXCOORD,
2016-10-05 23:28:52 +02:00
LC_ATTRIB_COLOR
};
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
};
class lcFramebuffer
{
public:
lcFramebuffer()
{
}
lcFramebuffer(int Width, int Height)
: mWidth(Width), mHeight(Height)
{
}
bool IsValid() const
{
return mObject != 0;
}
GLuint mObject = 0;
GLuint mColorTexture = 0;
GLuint mDepthRenderbuffer = 0;
int mWidth = 0;
int mHeight = 0;
};
2020-03-22 21:44:20 +01:00
enum class lcPolygonOffset
{
2020-03-22 21:44:20 +01:00
None,
Opaque,
Translucent
};
2020-12-27 22:05:55 +01:00
#ifdef LC_USE_QOPENGLWIDGET
class lcContext : protected QOpenGLFunctions
#else
2016-10-05 23:28:52 +02:00
class lcContext
2020-12-27 22:05:55 +01:00
#endif
2016-10-05 23:28:52 +02:00
{
public:
lcContext();
~lcContext();
2020-05-04 00:39:39 +02:00
lcContext(const lcContext&) = delete;
lcContext& operator=(const lcContext&) = delete;
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
2020-12-27 22:05:55 +01:00
#ifdef LC_USE_QOPENGLWIDGET
void SetGLContext(QOpenGLContext* GLContext)
{
initializeOpenGLFunctions();
mGLContext = GLContext;
}
#endif
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);
2016-10-05 23:28:52 +02:00
void SetLineWidth(float LineWidth);
2017-04-27 07:24:54 +02:00
void SetSmoothShading(bool Smooth);
2017-12-26 19:19:20 +01:00
void BindTexture2D(GLuint Texture);
void BindTexture2DMS(GLuint Texture);
2018-10-29 01:59:01 +01:00
void BindTextureCubeMap(GLuint Texture);
void UnbindTexture2D(GLuint Texture);
void UnbindTextureCubeMap(GLuint 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);
void SetColorIndexTinted(int ColorIndex, lcInterfaceColor InterfaceColor, 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
void SetInterfaceColor(lcInterfaceColor InterfaceColor);
void ClearFramebuffer();
lcFramebuffer CreateFramebuffer(int Width, int Height, bool Depth, bool Multisample);
void DestroyFramebuffer(lcFramebuffer& Framebuffer);
void BindFramebuffer(GLuint FramebufferObject);
void BindFramebuffer(const lcFramebuffer& Framebuffer)
{
BindFramebuffer(Framebuffer.mObject);
}
std::pair<lcFramebuffer, lcFramebuffer> CreateRenderFramebuffer(int Width, int Height);
void DestroyRenderFramebuffer(std::pair<lcFramebuffer, lcFramebuffer>& RenderFramebuffer);
QImage GetRenderFramebufferImage(const std::pair<lcFramebuffer, lcFramebuffer>& RenderFramebuffer);
void GetRenderFramebufferImage(const std::pair<lcFramebuffer, lcFramebuffer>& RenderFramebuffer, quint8* Buffer);
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);
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:
2020-12-27 22:05:55 +01:00
void CreateShaderPrograms();
2016-10-05 23:28:52 +02:00
void FlushState();
2020-12-27 22:05:55 +01:00
#ifdef LC_USE_QOPENGLWIDGET
QOpenGLContext* mGLContext = nullptr;
#endif
2016-10-05 23:28:52 +02:00
GLuint mVertexBufferObject;
GLuint mIndexBufferObject;
char* mVertexBufferPointer;
char* mIndexBufferPointer;
char* mVertexBufferOffset;
lcMaterialType mMaterialType;
2017-02-18 20:12:35 +01:00
bool mNormalEnabled;
2016-10-05 23:28:52 +02:00
bool mTexCoordEnabled;
bool mColorEnabled;
2017-12-26 19:19:20 +01:00
GLuint mTexture2D;
GLuint mTexture2DMS;
2018-10-29 01:59:01 +01:00
GLuint mTextureCubeMap;
lcPolygonOffset mPolygonOffset;
2019-11-10 03:11:25 +01:00
bool mDepthWrite;
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;
lcVector4 mHighlightParams[5];
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
GLuint mFramebufferObject;
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);
};