Properly drawing VertexArrayMesh plane... yay

simple_math_single_header
Martin Felis 2018-02-28 13:37:33 +01:00
parent 76985c878f
commit c86f535571
2 changed files with 105 additions and 4 deletions

View File

@ -52,6 +52,7 @@ static const GLfloat g_coordinate_system_vertex_buffer_data[] = {
VertexArray gVertexArray; VertexArray gVertexArray;
VertexArrayMesh gVertexArrayMesh; VertexArrayMesh gVertexArrayMesh;
VertexArrayMesh gXZPlaneMesh;
// //
// Module // Module
@ -171,7 +172,7 @@ void Camera::DrawGui() {
ImGui::Checkbox("Orthographic", &mIsOrthographic); ImGui::Checkbox("Orthographic", &mIsOrthographic);
ImGui::SliderFloat("Fov", &mFov, 5, 160); ImGui::SliderFloat("Fov", &mFov, 5, 160);
ImGui::SliderFloat("Near", &mNear, -10, 10); ImGui::SliderFloat("Near", &mNear, -10, 10);
ImGui::SliderFloat("Far", &mFar, -10, 10); ImGui::SliderFloat("Far", &mFar, -20, 20);
if (ImGui::Button("Reset")) { if (ImGui::Button("Reset")) {
*this = Camera(); *this = Camera();
} }
@ -200,6 +201,38 @@ void Renderer::Initialize(int width, int height) {
gVertexArrayMesh.SetData(vertex_data, 6); gVertexArrayMesh.SetData(vertex_data, 6);
// Plane
const int plane_grid_size = 20;
gXZPlaneMesh.Initialize(gVertexArray, (plane_grid_size + 1) * 4);
std::vector<VertexArray::VertexData> plane_data((plane_grid_size + 1) * 4);
for (int i = 0, n = plane_grid_size + 1; i < n; ++i) {
// lines along the x axis
plane_data[2 * i] = VertexArray::VertexData(
- plane_grid_size * 0.5f, 0.0f, -plane_grid_size * 0.5f + 1.0f * i, 1.0f,
0.0f, 1.0f, 0.0f,
- plane_grid_size * 0.5f, -1.0f * i,
255, 255, 255, 255);
plane_data[2 * i + 1] = VertexArray::VertexData(
plane_grid_size * 0.5f, 0.0f, -plane_grid_size * 0.5 + 1.0f * i, 1.0f,
0.0f, 1.0f, 0.0f,
plane_grid_size * 0.5f, -1.0f * i,
255, 255, 255, 255);
// lines along the z axis
plane_data[n * 2 + 2 * i] = VertexArray::VertexData(
-plane_grid_size * 0.5f + 1.0f * i, 0.0f, - plane_grid_size * 0.5f, 1.0f,
0.0f, 1.0f, 0.0f,
-1.0f * i, - plane_grid_size * 0.5f,
255, 255, 255, 255);
plane_data[n * 2 + 2 * i + 1] = VertexArray::VertexData(
-plane_grid_size * 0.5f + 1.0f * i, 0.0f, plane_grid_size * 0.5f, 1.0f,
0.0f, 1.0f, 0.0f,
-1.0f * i, plane_grid_size * 0.5f,
255, 255, 255, 255);
}
gXZPlaneMesh.SetData(plane_data.data(), plane_data.size());
// Mesh // Mesh
glGenVertexArrays(1, &mMesh.mVertexArrayId); glGenVertexArrays(1, &mMesh.mVertexArrayId);
glBindVertexArray(mMesh.mVertexArrayId); glBindVertexArray(mMesh.mVertexArrayId);
@ -258,6 +291,7 @@ void Renderer::Initialize(int width, int height) {
muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj");
muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear");
muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar"); muRenderQuadDepthFar = mRenderQuadProgramDepth.GetUniformLocation("uFar");
} }
void Renderer::Shutdown() { void Renderer::Shutdown() {
@ -278,6 +312,10 @@ void Renderer::RenderGl() {
mCamera.UpdateMatrices(); mCamera.UpdateMatrices();
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_MULTISAMPLE);
Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f); Matrix44f model_matrix = TranslateMat44(0.0f, 0.0f, 0.0f);
Matrix44f model_view_projection = Matrix44f model_view_projection =
model_matrix model_matrix
@ -345,12 +383,22 @@ void Renderer::RenderGl() {
* mCamera.mProjectionMatrix; * mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data()); glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data()); glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data());
gVertexArray.Bind();
gVertexArrayMesh.Draw(GL_LINES);
// Plane
model_view_projection =
TranslateMat44(0.0f, 0.0f, 0.0f)
* mCamera.mViewMatrix
* mCamera.mProjectionMatrix;
glUniformMatrix4fv(muDefaultModelViewProjection, 1, GL_FALSE, model_view_projection.data());
glUniform4fv(muDefaultColor, 1, Vector4f(1.0f, 0.0f, 0.0f, 1.0f).data());
glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord"); glBindAttribLocation(mDefaultProgram.mProgramId, 0, "inCoord");
glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal"); glBindAttribLocation(mDefaultProgram.mProgramId, 1, "inNormal");
glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV"); glBindAttribLocation(mDefaultProgram.mProgramId, 2, "inUV");
glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor"); glBindAttribLocation(mDefaultProgram.mProgramId, 3, "inColor");
gVertexArray.Bind(); gVertexArray.Bind();
gVertexArrayMesh.Draw(GL_LINES); gXZPlaneMesh.Draw(GL_LINES);
if (mSettings->DrawDepth) { if (mSettings->DrawDepth) {
mRenderTarget.RenderToLinearizedDepth(true); mRenderTarget.RenderToLinearizedDepth(true);

View File

@ -214,8 +214,8 @@ struct VertexArray {
float nx; float nx;
float ny; float ny;
float nz; float nz;
float u; float s;
float v; float t;
GLubyte r; GLubyte r;
GLubyte g; GLubyte g;
GLubyte b; GLubyte b;
@ -228,6 +228,59 @@ struct VertexArray {
GLubyte mColor[4]; GLubyte mColor[4];
}; };
}; };
VertexData() :
x(0.0f),
y(0.0f),
z(0.0f),
w(0.0f),
nx(0.0f),
ny(0.0f),
nz(0.0f),
s(0.0f),
t(0.0f),
r(255),
g(255),
b(255),
a(255) {}
VertexData(
float x,
float y,
float z,
float w,
float nx,
float ny,
float nz,
float s,
float t,
GLubyte r,
GLubyte g,
GLubyte b,
GLubyte a
) :
x(x), y(y), z(z), w(w),
nx(nx), ny(ny), nz(nz),
s(s), t(t),
r(r), g(g), b(b), a(a) {}
VertexData& operator= (const VertexData& data) {
x = data.x;
y = data.y;
z = data.z;
w = data.w;
nx = data.nx;
ny = data.ny;
nz = data.nz;
r = data.r;
g = data.g;
b = data.b;
a = data.a;
return *this;
}
}; };
~VertexArray(); ~VertexArray();