make rendering code more reusable, update testbed
This commit is contained in:
parent
e2a9084df1
commit
0fbd543d96
@ -29,7 +29,12 @@
|
||||
|
||||
Camera* g_camera = nullptr;
|
||||
Draw* g_draw = nullptr;
|
||||
u32 g_drawFlags = 0;
|
||||
|
||||
bool g_glDrawPoints;
|
||||
bool g_glDrawLines;
|
||||
bool g_glDrawTriangles;
|
||||
b3Mat44 g_glViewMatrix;
|
||||
b3Mat44 g_glProjectionMatrix;
|
||||
|
||||
Camera::Camera()
|
||||
{
|
||||
@ -76,7 +81,7 @@ b3Transform Camera::BuildWorldTransform() const
|
||||
b3Mat44 Camera::BuildWorldMatrix() const
|
||||
{
|
||||
b3Transform xf = BuildWorldTransform();
|
||||
return MakeMat44(xf);
|
||||
return b3TransformMat44(xf);
|
||||
}
|
||||
|
||||
b3Transform Camera::BuildViewTransform() const
|
||||
@ -90,7 +95,7 @@ b3Transform Camera::BuildViewTransform() const
|
||||
b3Mat44 Camera::BuildViewMatrix() const
|
||||
{
|
||||
b3Transform xf = BuildViewTransform();
|
||||
return MakeMat44(xf);
|
||||
return b3TransformMat44(xf);
|
||||
}
|
||||
|
||||
b3Vec2 Camera::ConvertWorldToScreen(const b3Vec3& pw3) const
|
||||
@ -142,6 +147,12 @@ b3Ray3 Camera::ConvertScreenToWorld(const b3Vec2& ps) const
|
||||
|
||||
Draw::Draw()
|
||||
{
|
||||
g_glViewMatrix.SetZero();
|
||||
g_glProjectionMatrix.SetZero();
|
||||
g_glDrawPoints = true;
|
||||
g_glDrawLines = true;
|
||||
g_glDrawTriangles = true;
|
||||
|
||||
m_points = new DrawPoints();
|
||||
m_lines = new DrawLines();
|
||||
m_triangles = new DrawTriangles();
|
||||
@ -158,6 +169,31 @@ Draw::~Draw()
|
||||
delete m_solid;
|
||||
}
|
||||
|
||||
void Draw::SetViewMatrix(const b3Mat44& m)
|
||||
{
|
||||
g_glViewMatrix = m;
|
||||
}
|
||||
|
||||
void Draw::SetProjectionMatrix(const b3Mat44& m)
|
||||
{
|
||||
g_glProjectionMatrix = m;
|
||||
}
|
||||
|
||||
void Draw::EnableDrawPoints(bool flag)
|
||||
{
|
||||
g_glDrawPoints = flag;
|
||||
}
|
||||
|
||||
void Draw::EnableDrawLines(bool flag)
|
||||
{
|
||||
g_glDrawLines = flag;
|
||||
}
|
||||
|
||||
void Draw::EnableDrawTriangles(bool flag)
|
||||
{
|
||||
g_glDrawTriangles = flag;
|
||||
}
|
||||
|
||||
void Draw::DrawPoint(const b3Vec3& p, float32 size, const b3Color& color)
|
||||
{
|
||||
m_points->Vertex(p, size, color);
|
||||
|
@ -51,29 +51,22 @@ public:
|
||||
float32 m_zFar;
|
||||
};
|
||||
|
||||
inline b3Mat44 MakeMat44(const b3Transform& T)
|
||||
{
|
||||
return b3Mat44(
|
||||
b3Vec4(T.rotation.x.x, T.rotation.x.y, T.rotation.x.z, 0.0f),
|
||||
b3Vec4(T.rotation.y.x, T.rotation.y.y, T.rotation.y.z, 0.0f),
|
||||
b3Vec4(T.rotation.z.x, T.rotation.z.y, T.rotation.z.z, 0.0f),
|
||||
b3Vec4(T.position.x, T.position.y, T.position.z, 1.0f));
|
||||
}
|
||||
|
||||
//
|
||||
enum DrawFlags
|
||||
{
|
||||
e_pointsFlag = 0x0001,
|
||||
e_linesFlag = 0x0002,
|
||||
e_trianglesFlag = 0x0004
|
||||
};
|
||||
|
||||
class Draw : public b3Draw
|
||||
{
|
||||
public:
|
||||
Draw();
|
||||
~Draw();
|
||||
|
||||
void SetViewMatrix(const b3Mat44& m);
|
||||
|
||||
void SetProjectionMatrix(const b3Mat44& m);
|
||||
|
||||
void EnableDrawPoints(bool flag);
|
||||
|
||||
void EnableDrawLines(bool flag);
|
||||
|
||||
void EnableDrawTriangles(bool flag);
|
||||
|
||||
void DrawPoint(const b3Vec3& p, float32 size, const b3Color& color);
|
||||
|
||||
void DrawSegment(const b3Vec3& p1, const b3Vec3& p2, const b3Color& color);
|
||||
@ -139,6 +132,5 @@ private:
|
||||
|
||||
extern Camera* g_camera;
|
||||
extern Draw* g_draw;
|
||||
extern u32 g_drawFlags;
|
||||
|
||||
#endif
|
@ -19,15 +19,24 @@
|
||||
#ifndef DRAW_GL2_H
|
||||
#define DRAW_GL2_H
|
||||
|
||||
#include <testbed/framework/draw.h>
|
||||
|
||||
#include <glad_2/glad.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <bounce/common/math/transform.h>
|
||||
#include <bounce/common/math/mat44.h>
|
||||
#include <bounce/common/draw.h>
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
|
||||
|
||||
extern bool g_glDrawPoints;
|
||||
extern bool g_glDrawLines;
|
||||
extern bool g_glDrawTriangles;
|
||||
|
||||
extern b3Mat44 g_glViewMatrix;
|
||||
extern b3Mat44 g_glProjectionMatrix;
|
||||
|
||||
static void AssertGL()
|
||||
{
|
||||
GLenum errorCode = glGetError();
|
||||
@ -189,7 +198,7 @@ struct DrawPoints
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_pointsFlag) == 0)
|
||||
if (!g_glDrawPoints)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -197,8 +206,8 @@ struct DrawPoints
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -324,7 +333,7 @@ struct DrawLines
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_linesFlag) == 0)
|
||||
if (!g_glDrawLines)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -332,8 +341,8 @@ struct DrawLines
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
|
||||
@ -460,7 +469,7 @@ struct DrawTriangles
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -468,8 +477,8 @@ struct DrawTriangles
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -646,19 +655,19 @@ struct DrawWire
|
||||
|
||||
void DrawSphere(float32 radius, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_linesFlag) == 0)
|
||||
if (!g_glDrawLines)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = radius * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -940,20 +949,20 @@ struct DrawSolid
|
||||
|
||||
void DrawCylinder(float32 radius, float32 height, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = height * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniform4fv(m_colorUniform, 1, &c.r);
|
||||
@ -983,20 +992,20 @@ struct DrawSolid
|
||||
|
||||
void DrawSphere(float32 radius, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = radius * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniform4fv(m_colorUniform, 1, &c.r);
|
||||
|
@ -19,15 +19,24 @@
|
||||
#ifndef DRAW_GL4_H
|
||||
#define DRAW_GL4_H
|
||||
|
||||
#include <testbed/framework/draw.h>
|
||||
|
||||
#include <glad_4/glad.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <bounce/common/math/transform.h>
|
||||
#include <bounce/common/math/mat44.h>
|
||||
#include <bounce/common/draw.h>
|
||||
|
||||
#define BUFFER_OFFSET(i) ((char*)NULL + (i))
|
||||
|
||||
extern bool g_glDrawPoints;
|
||||
extern bool g_glDrawLines;
|
||||
extern bool g_glDrawTriangles;
|
||||
|
||||
extern b3Mat44 g_glViewMatrix;
|
||||
extern b3Mat44 g_glProjectionMatrix;
|
||||
|
||||
static void AssertGL()
|
||||
{
|
||||
GLenum errorCode = glGetError();
|
||||
@ -203,7 +212,7 @@ struct DrawPoints
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_pointsFlag) == 0)
|
||||
if (!g_glDrawPoints)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -211,8 +220,8 @@ struct DrawPoints
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -340,7 +349,7 @@ struct DrawLines
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_linesFlag) == 0)
|
||||
if (!g_glDrawLines)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -348,8 +357,8 @@ struct DrawLines
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
|
||||
@ -484,7 +493,7 @@ struct DrawTriangles
|
||||
return;
|
||||
}
|
||||
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
m_count = 0;
|
||||
return;
|
||||
@ -492,8 +501,8 @@ struct DrawTriangles
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m2 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m1 = g_glViewMatrix;
|
||||
b3Mat44 m2 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -663,19 +672,19 @@ struct DrawWire
|
||||
|
||||
void DrawSphere(float32 radius, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_linesFlag) == 0)
|
||||
if (!g_glDrawLines)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = radius * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniformMatrix4fv(m_projectionUniform, 1, GL_FALSE, &m.x.x);
|
||||
@ -956,20 +965,20 @@ struct DrawSolid
|
||||
|
||||
void DrawCylinder(float32 radius, float32 height, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = height * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniform4fv(m_colorUniform, 1, &c.r);
|
||||
@ -995,20 +1004,20 @@ struct DrawSolid
|
||||
|
||||
void DrawSphere(float32 radius, const b3Color& c, const b3Transform& xf)
|
||||
{
|
||||
if ((g_drawFlags & DrawFlags::e_trianglesFlag) == 0)
|
||||
if (!g_glDrawTriangles)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
glUseProgram(m_programId);
|
||||
|
||||
b3Mat44 m1 = MakeMat44(xf);
|
||||
b3Mat44 m1 = b3TransformMat44(xf);
|
||||
m1.x = radius * m1.x;
|
||||
m1.y = radius * m1.y;
|
||||
m1.z = radius * m1.z;
|
||||
|
||||
b3Mat44 m2 = g_camera->BuildViewMatrix();
|
||||
b3Mat44 m3 = g_camera->BuildProjectionMatrix();
|
||||
b3Mat44 m2 = g_glViewMatrix;
|
||||
b3Mat44 m3 = g_glProjectionMatrix;
|
||||
b3Mat44 m = m3 * m2 * m1;
|
||||
|
||||
glUniform4fv(m_colorUniform, 1, &c.r);
|
||||
|
@ -97,10 +97,11 @@ void Model::Command_Move_Cursor(const b3Vec2& ps)
|
||||
|
||||
void Model::Update()
|
||||
{
|
||||
g_drawFlags = 0;
|
||||
g_drawFlags += g_settings->drawPoints * DrawFlags::e_pointsFlag;
|
||||
g_drawFlags += g_settings->drawLines * DrawFlags::e_linesFlag;
|
||||
g_drawFlags += g_settings->drawTriangles * DrawFlags::e_trianglesFlag;
|
||||
m_draw.EnableDrawPoints(g_settings->drawPoints);
|
||||
m_draw.EnableDrawLines(g_settings->drawLines);
|
||||
m_draw.EnableDrawTriangles(g_settings->drawTriangles);
|
||||
m_draw.SetViewMatrix(g_camera->BuildViewMatrix());
|
||||
m_draw.SetProjectionMatrix(g_camera->BuildProjectionMatrix());
|
||||
|
||||
glViewport(0, 0, GLsizei(m_camera.m_width), GLsizei(m_camera.m_height));
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
Loading…
x
Reference in New Issue
Block a user