leocad/common/lc_context.h

230 lines
4.9 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:
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:
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;
};
};
enum lcMaterialType
2016-10-05 23:28:52 +02:00
{
LC_MATERIAL_UNLIT_COLOR,
LC_MATERIAL_UNLIT_TEXTURE_MODULATE,
LC_MATERIAL_UNLIT_TEXTURE_DECAL,
LC_MATERIAL_UNLIT_VERTEX_COLOR,
LC_MATERIAL_FAKELIT_COLOR,
LC_MATERIAL_FAKELIT_TEXTURE_DECAL,
LC_NUM_MATERIALS
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;
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;
};
2016-10-05 23:28:52 +02:00
class lcContext
{
public:
lcContext();
~lcContext();
static void CreateResources();
static void DestroyResources();
void SetDefaultState();
2017-03-25 20:29:28 +01:00
void ClearResources();
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 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);
2016-10-05 23:28:52 +02:00
void SetColor(const lcVector4& Color)
{
mColor = Color;
mColorDirty = true;
}
void SetColor(float Red, float Green, float Blue, float Alpha);
void SetColorIndex(int ColorIndex);
void SetColorIndexTinted(int ColorIndex, lcInterfaceColor InterfaceColor);
void SetEdgeColorIndex(int ColorIndex);
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:
static void CreateShaderPrograms();
void FlushState();
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;
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;
bool mColorDirty;
bool mWorldMatrixDirty;
bool mViewMatrixDirty;
bool mProjectionMatrixDirty;
bool mViewProjectionMatrixDirty;
GLuint mFramebufferObject;
static lcProgram mPrograms[LC_NUM_MATERIALS];
2016-10-05 23:28:52 +02:00
Q_DECLARE_TR_FUNCTIONS(lcContext);
};