Last active
June 16, 2017 14:05
-
-
Save floooh/b2ed7a9e40054c1866d7963738516f42 to your computer and use it in GitHub Desktop.
A complete wireframe debug renderer for Oryol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// Wireframe.cc | |
//------------------------------------------------------------------------------ | |
#include "Pre.h" | |
#include "Wireframe.h" | |
#include "Gfx/Gfx.h" | |
#include "shaders.h" | |
namespace Oryol { | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Setup(const GfxSetup& gfxSetup) { | |
Gfx::PushResourceLabel(); | |
auto meshSetup = MeshSetup::Empty(MaxNumVertices, Usage::Stream); | |
meshSetup.Layout = { | |
{ VertexAttr::Position, VertexFormat::Float3 }, | |
{ VertexAttr::Color0, VertexFormat::Float4 } | |
}; | |
this->drawState.Mesh[0] = Gfx::CreateResource(meshSetup); | |
Id shd = Gfx::CreateResource(WireframeShader::Setup()); | |
auto pipSetup = PipelineSetup::FromLayoutAndShader(meshSetup.Layout, shd); | |
pipSetup.RasterizerState.SampleCount = gfxSetup.SampleCount; | |
pipSetup.BlendState.ColorFormat = gfxSetup.ColorFormat; | |
pipSetup.BlendState.DepthFormat = gfxSetup.DepthFormat; | |
pipSetup.PrimType = PrimitiveType::Lines; | |
this->drawState.Pipeline = Gfx::CreateResource(pipSetup); | |
this->label = Gfx::PopResourceLabel(); | |
} | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Discard() { | |
Gfx::DestroyResources(this->label); | |
} | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Line(const glm::vec4& p0, const glm::vec4& p1) { | |
if ((this->vertices.Size() + 2) < MaxNumVertices) { | |
this->vertices.Add(this->Model * p0, this->Color); | |
this->vertices.Add(this->Model * p1, this->Color); | |
} | |
} | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Line(const glm::vec3& p0, const glm::vec3& p1) { | |
this->Line(glm::vec4(p0, 1.0f), glm::vec4(p1, 1.0f)); | |
} | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Rect(const glm::vec3& p0, const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3) { | |
this->Line(p0, p1); this->Line(p1, p2); this->Line(p2, p3); this->Line(p3, p0); | |
} | |
//------------------------------------------------------------------------------ | |
void | |
Wireframe::Render() { | |
if (!this->vertices.Empty()) { | |
Gfx::UpdateVertices(this->drawState.Mesh[0], this->vertices.begin(), this->vertices.Size()*sizeof(Vertex)); | |
Gfx::ApplyDrawState(this->drawState); | |
WireframeShader::vsParams vsParams; | |
vsParams.viewProj = this->ViewProj; | |
Gfx::ApplyUniformBlock(vsParams); | |
Gfx::Draw({ 0, this->vertices.Size() }); | |
/** oops, don't forget to reset the vertices array :) **/ | |
this->vertices.Reset(); | |
} | |
} | |
} // namespace Oryol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
//------------------------------------------------------------------------------ | |
/** | |
@class Wireframe | |
@brief simple wireframe debug renderer | |
*/ | |
#include "Gfx/Gfx.h" | |
#include "Core/Containers/InlineArray.h" | |
#include <glm/mat4x4.hpp> | |
#include <glm/vec3.hpp> | |
#include <glm/vec4.hpp> | |
namespace Oryol { | |
struct Wireframe { | |
/// set this to the current ViewProj matrix | |
glm::mat4 ViewProj; | |
/// set this to the current model matrix | |
glm::mat4 Model; | |
/// set this to the current line color | |
glm::vec4 Color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); | |
/// setup the debug renderer | |
void Setup(const GfxSetup& setup); | |
/// discard the debug renderer | |
void Discard(); | |
/// call once before Gfx::CommitFrame to render the debug scene | |
void Render(); | |
/// add a wireframe line (vec3) | |
void Line(const glm::vec3& p0, const glm::vec3& p1); | |
/// add a wireframe line (vec4, w must be 1) | |
void Line(const glm::vec4& p0, const glm::vec4& p1); | |
/// add a rectangle (vec3) | |
void Rect(const glm::vec3& p0, const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3); | |
DrawState drawState; | |
ResourceLabel label; | |
struct Vertex { | |
float x, y, z, r, g, b, a; | |
Vertex() { }; | |
Vertex(const glm::vec4& p, const glm::vec4& c): | |
x(p.x), y(p.y), z(p.z), r(c.x), g(c.y), b(c.z), a(c.w) { }; | |
}; | |
static const int MaxNumVertices = 4096; | |
InlineArray<Vertex, MaxNumVertices> vertices; | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@vs wireframeVS | |
uniform vsParams { | |
mat4 viewProj; | |
}; | |
in vec4 position; | |
in vec4 color0; | |
out vec4 color; | |
void main() { | |
gl_Position = viewProj * position; | |
color = color0; | |
} | |
@end | |
@fs wireframeFS | |
in vec4 color; | |
out vec4 fragColor; | |
void main() { | |
fragColor = color; | |
} | |
@end | |
@program WireframeShader wireframeVS wireframeFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment