Skip to content

Commit

Permalink
#123 Update viewpoint renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
duc.pham authored and duc.pham committed May 2, 2021
1 parent 46cc366 commit b4e621d
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 13 deletions.
26 changes: 18 additions & 8 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace Skylicht
CSpace(window, editor),
m_scene(NULL),
m_renderRP(NULL),
m_viewpointRP(NULL),
m_editorCamera(NULL),
m_gridPlane(NULL),
m_leftMouseDown(false),
Expand Down Expand Up @@ -73,6 +74,9 @@ namespace Skylicht
if (m_renderRP != NULL)
delete m_renderRP;

if (m_viewpointRP != NULL)
delete m_viewpointRP;

CSceneController* sceneController = CSceneController::getInstance();
sceneController->setScene(NULL);
sceneController->setSpaceScene(NULL);
Expand Down Expand Up @@ -131,11 +135,6 @@ namespace Skylicht
m_viewpointCamera = camObj->getComponent<CCamera>();
m_viewpointCamera->setAspect(1.0f);

// grid
CGameObject* gridPlane = m_viewpointZone->createEmptyObject();
gridPlane->setName(L"Grid3D");
gridPlane->addComponent<CGridPlane>();

// viewpoint
CGameObject* viewpoint = m_viewpointZone->createEmptyObject();
viewpoint->setName(L"Viewpoint");
Expand Down Expand Up @@ -165,6 +164,17 @@ namespace Skylicht
// resize
m_renderRP->resize((int)w, (int)h);
}

if (m_viewpointRP == NULL)
{
// rendering pipe line
m_viewpointRP = new CForwardRP(false);
m_viewpointRP->initRender((int)w, (int)h);
}
else
{
m_viewpointRP->resize((int)w, (int)h);
}
}
}

Expand All @@ -189,7 +199,7 @@ namespace Skylicht
core::vector3df up(matData[4], matData[5], matData[6]);
up.normalize();

float distance = 2.0f;
float distance = 2.5f;
core::vector3df pos = -dir * distance;

m_viewpointCamera->setPosition(pos);
Expand Down Expand Up @@ -221,7 +231,7 @@ namespace Skylicht
// setup viewpoint viewport
int paddingTop = 10;
int paddingLeft = 10;
int viewpointSize = 80;
int viewpointSize = 125;
viewport.UpperLeftCorner.set((int)(position.X + base->width()) - viewpointSize - paddingLeft, (int)position.Y + paddingTop);
viewport.LowerRightCorner = viewport.UpperLeftCorner + core::vector2di(viewpointSize, viewpointSize);

Expand All @@ -231,7 +241,7 @@ namespace Skylicht
m_scene->setVisibleAllZone(false);
m_viewpointZone->setVisible(true);

m_renderRP->render(NULL, m_viewpointCamera, m_scene->getEntityManager(), viewport);
m_viewpointRP->render(NULL, m_viewpointCamera, m_scene->getEntityManager(), viewport);

// disable viewpoint
m_scene->setVisibleAllZone(true);
Expand Down
1 change: 1 addition & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Skylicht
CScene* m_scene;
CCamera* m_editorCamera;
IRenderPipeline* m_renderRP;
IRenderPipeline* m_viewpointRP;

CGameObject* m_gridPlane;

Expand Down
102 changes: 102 additions & 0 deletions Projects/Editor/Source/EditorComponents/Viewpoint/CViewpointData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,121 @@ This file is part of the "Skylicht Engine".

#include "pch.h"
#include "CViewpointData.h"
#include "Material/Shader/CShaderManager.h"

namespace Skylicht
{
namespace Editor
{
CViewpointData::CViewpointData()
{
Buffer = new CMeshBuffer<video::S3DVertex>(getVideoDriver()->getVertexDescriptor(EVT_STANDARD));
Buffer->getMaterial().MaterialType = CShaderManager::getInstance()->getShaderIDByName("VertexColor");
Buffer->getMaterial().BackfaceCulling = false;
Buffer->setHardwareMappingHint(EHM_STREAM);

const float p = 1.0f;

Position[0].set(p, 0.0f, 0.0f);
Position[1].set(-p, 0.0f, 0.0f);

Position[2].set(0.0f, p, 0.0f);
Position[3].set(0.0f, -p, 0.0f);

Position[4].set(0.0f, 0.0f, p);
Position[5].set(0.0f, 0.0f, -p);

Buffer->getVertexBuffer()->set_used(6 * 4);
Buffer->getIndexBuffer()->set_used(6 * 2 * 3);

u16* idx = (u16*)Buffer->getIndexBuffer()->getIndices();
for (int i = 0; i < 6; i++)
{
idx[i * 6 + 0] = i * 4;
idx[i * 6 + 1] = i * 4 + 1;
idx[i * 6 + 2] = i * 4 + 2;

idx[i * 6 + 3] = i * 4;
idx[i * 6 + 4] = i * 4 + 2;
idx[i * 6 + 5] = i * 4 + 3;
}

SColor blue = SColor(255, 65, 105, 140);
SColor red = SColor(255, 155, 65, 80);
SColor green = SColor(255, 105, 140, 45);
core::vector3df zero(0.0f, 0.0f, 0.0f);

float s = 0.7f;
addLineVertexBatch(zero, Position[0] * s, red);
addLineVertexBatch(zero, Position[2] * s, blue);
addLineVertexBatch(zero, Position[4] * s, green);
}

CViewpointData::~CViewpointData()
{

}

void CViewpointData::updateBillboard(const core::vector3df& look, const core::vector3df& up)
{
video::S3DVertex* vtx = (video::S3DVertex*)Buffer->getVertexBuffer()->getVertices();

core::vector3df upQuad = up;
core::vector3df sideQuad = upQuad.crossProduct(look);

upQuad.normalize();
sideQuad.normalize();

upQuad *= 0.25f;
sideQuad *= 0.25f;

SColor color(255, 255, 255, 255);
core::vector2df uvScale(1.0f, 1.0f);
core::vector2df uvOffset(0.0f, 0.0f);

for (int i = 0; i < 6; i++)
{
int offset = i * 4;
int offsetVertex = offset;

float x = Position[i].X;
float y = Position[i].Y;
float z = Position[i].Z;

// top left vertex
vtx[offset].Pos.set(
x - sideQuad.X + upQuad.X,
y - sideQuad.Y + upQuad.Y,
z - sideQuad.Z + upQuad.Z);
vtx[offset].Color = color;
vtx[offset].TCoords = core::vector2df(0.0f, 0.0f) * uvScale + uvOffset;

// top right vertex
vtx[++offset].Pos.set(
x + sideQuad.X + upQuad.X,
y + sideQuad.Y + upQuad.Y,
z + sideQuad.Z + upQuad.Z);
vtx[offset].Color = color;
vtx[offset].TCoords = core::vector2df(1.0f, 0.0f) * uvScale + uvOffset;

// bottom right vertex
vtx[++offset].Pos.set(
x + sideQuad.X - upQuad.X,
y + sideQuad.Y - upQuad.Y,
z + sideQuad.Z - upQuad.Z);
vtx[offset].Color = color;
vtx[offset].TCoords = core::vector2df(1.0f, 1.0f) * uvScale + uvOffset;

// bottom left vertex
vtx[++offset].Pos.set(
x - sideQuad.X - upQuad.X,
y - sideQuad.Y - upQuad.Y,
z - sideQuad.Z - upQuad.Z);
vtx[offset].Color = color;
vtx[offset].TCoords = core::vector2df(0.0f, 1.0f) * uvScale + uvOffset;
}

Buffer->setDirty(EBT_VERTEX);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ This file is part of the "Skylicht Engine".
#pragma once

#include "Entity/IEntityData.h"
#include "LineDraw/CLineDrawData.h"

namespace Skylicht
{
namespace Editor
{
class CViewpointData : public IEntityData
class CViewpointData : public CLineDrawData
{
public:
IMeshBuffer* Buffer;

core::vector3df Position[6];

public:
CViewpointData();

virtual ~CViewpointData();

void updateBillboard(const core::vector3df& look, const core::vector3df& up);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,34 @@ namespace Skylicht
CViewpointData** viewpoints = m_viewpoints.pointer();
CWorldTransformData** transforms = m_transforms.pointer();

irr::core::matrix4 invModelView;
{
irr::core::matrix4 modelView(driver->getTransform(video::ETS_VIEW));
modelView.getInversePrimitive(invModelView); // wont work for odd modelview matrices (but should happen in very special cases)
}

core::vector3df look(invModelView[8], invModelView[9], invModelView[10]);
core::vector3df up(invModelView[4], invModelView[5], invModelView[6]);

look.normalize();
up.normalize();

for (u32 i = 0, n = m_viewpoints.size(); i < n; i++)
{
driver->setTransform(video::ETS_WORLD, transforms[i]->World);

// IMeshBuffer* buffer = viewpoints[i]->Buffer;
// driver->setMaterial(buffer->getMaterial());
// driver->drawMeshBuffer(buffer);
IMeshBuffer* line = viewpoints[i]->LineBuffer;
driver->setMaterial(line->getMaterial());
driver->drawMeshBuffer(line);
}

for (u32 i = 0, n = m_viewpoints.size(); i < n; i++)
{
driver->setTransform(video::ETS_WORLD, transforms[i]->World);
viewpoints[i]->updateBillboard(look, up);

IMeshBuffer* buffer = viewpoints[i]->Buffer;
driver->setMaterial(buffer->getMaterial());
driver->drawMeshBuffer(buffer);
}
}
}
Expand Down

0 comments on commit b4e621d

Please sign in to comment.