Changed Picking colors from vec2 to ivec2. Picking now working for multiple scenes (Solution not perfect in the editor, yet)
This commit is contained in:
@@ -127,7 +127,7 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
|
||||
|
||||
auto widgetTransform = m_World->GetComponent(m_Widget, "Transform");
|
||||
glm::vec3 widgetOrientation = widgetTransform["Orientation"];
|
||||
glm::quat totalOrientation = m_Renderer->Camera()->Orientation() * glm::inverse(glm::quat(widgetOrientation));
|
||||
glm::quat totalOrientation = m_Camera->Orientation() * glm::inverse(glm::quat(widgetOrientation));
|
||||
|
||||
int width;
|
||||
int height;
|
||||
@@ -139,14 +139,14 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e)
|
||||
delta2,
|
||||
m_WidgetPickingDepth,
|
||||
res,
|
||||
m_Renderer->Camera()->ProjectionMatrix(),
|
||||
m_Camera->ProjectionMatrix(),
|
||||
glm::toMat4(glm::inverse(totalOrientation))
|
||||
);
|
||||
glm::vec3 origin = ScreenCoords::ToWorldPos(
|
||||
glm::vec2(res.Width / 2.f, res.Height / 2.f),
|
||||
m_WidgetPickingDepth,
|
||||
res,
|
||||
m_Renderer->Camera()->ProjectionMatrix(),
|
||||
m_Camera->ProjectionMatrix(),
|
||||
glm::toMat4(glm::inverse(totalOrientation))
|
||||
);
|
||||
deltaWorld = deltaWorld - origin;
|
||||
@@ -252,7 +252,7 @@ void EditorSystem::Picking()
|
||||
(entity == m_WidgetZ) || (entity == m_WidgetOrigin) || (entity == m_WidgetPlaneX || entity == m_WidgetPlaneY)
|
||||
);
|
||||
m_WidgetPickingDepth = result.Depth;
|
||||
|
||||
m_Camera = result.Camera;
|
||||
//auto widgetTransform = m_World->GetComponent(m_Widget, "Transform");
|
||||
//auto selectionTransform = m_World->GetComponent(m_Selection, "Transform");
|
||||
//widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"];
|
||||
@@ -263,6 +263,7 @@ void EditorSystem::Picking()
|
||||
}
|
||||
setWidgetMode(m_WidgetMode);
|
||||
m_Selection = entity;
|
||||
m_Camera = result.Camera;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,18 @@ void Camera::SetOrientation(glm::quat val)
|
||||
UpdateViewMatrix();
|
||||
}
|
||||
|
||||
|
||||
void Camera::SetProjectionMatrix(glm::mat4 val)
|
||||
{
|
||||
m_ProjectionMatrix = val;
|
||||
}
|
||||
|
||||
|
||||
void Camera::SetViewMatrix(glm::mat4 val)
|
||||
{
|
||||
m_ViewMatrix = val;
|
||||
}
|
||||
|
||||
//void Camera::Pitch(float val)
|
||||
//{
|
||||
// m_Pitch = val;
|
||||
|
||||
@@ -28,7 +28,7 @@ void DrawScenePass::Draw(RenderScene& scene)
|
||||
//glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
GLERROR("Renderer::Draw PickingPass");
|
||||
|
||||
DrawScenePassState state;
|
||||
DrawScenePassState state = DrawScenePassState();
|
||||
|
||||
for (auto &job : scene.ForwardJobs) {
|
||||
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
|
||||
|
||||
@@ -10,8 +10,8 @@ DrawScenePassState::DrawScenePassState()
|
||||
Enable(GL_CULL_FACE);
|
||||
Enable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
ClearColor(glm::vec4(255.f / 255, 163.f / 255, 176.f / 255, 0.f));
|
||||
Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
// ClearColor(glm::vec4(255.f / 255, 163.f / 255, 176.f / 255, 0.f));
|
||||
// Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
DrawScenePassState::~DrawScenePassState()
|
||||
|
||||
@@ -45,17 +45,15 @@ void PickingPass::InitializeShaderPrograms()
|
||||
|
||||
void PickingPass::Draw(RenderScene& scene)
|
||||
{
|
||||
m_PickingColorsToEntity.clear();
|
||||
PickingPassState* state = new PickingPassState(m_PickingBuffer.GetHandle());
|
||||
|
||||
int r = 0;
|
||||
int g = 0;
|
||||
|
||||
//TODO: Render: Add code for more jobs than modeljobs.
|
||||
|
||||
GLuint ShaderHandle = m_PickingProgram->GetHandle();
|
||||
m_PickingProgram->Bind();
|
||||
|
||||
std::map<EntityID, glm::vec2> entityColors;
|
||||
|
||||
|
||||
m_Camera = scene.Camera;
|
||||
|
||||
@@ -63,22 +61,28 @@ void PickingPass::Draw(RenderScene& scene)
|
||||
auto modelJob = std::dynamic_pointer_cast<ModelJob>(job);
|
||||
|
||||
if (modelJob) {
|
||||
int pickColor[2] = { r, g };
|
||||
auto color = entityColors.find(modelJob->Entity);
|
||||
if (color != entityColors.end()) {
|
||||
int pickColor[2] = { m_ColorCounter[0], m_ColorCounter[1] };
|
||||
|
||||
PickingInfo pickInfo;
|
||||
pickInfo.Entity = modelJob->Entity;
|
||||
pickInfo.World = modelJob->World;
|
||||
pickInfo.Camera = scene.Camera;
|
||||
|
||||
auto color = m_EntityColors.find(std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera));
|
||||
if (color != m_EntityColors.end()) {
|
||||
pickColor[0] = color->second[0];
|
||||
pickColor[1] = color->second[1];
|
||||
} else {
|
||||
entityColors[modelJob->Entity] = glm::vec2(pickColor[0], pickColor[1]);
|
||||
if (r + 10 > 255) {
|
||||
r = 0;
|
||||
g += 1;
|
||||
m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]);
|
||||
if (m_ColorCounter[0] > 255) {
|
||||
m_ColorCounter[0] = 0;
|
||||
m_ColorCounter[1]++;;
|
||||
} else {
|
||||
r += 1;
|
||||
m_ColorCounter[0]++;;
|
||||
}
|
||||
}
|
||||
m_PickingColorsToEntity[glm::vec2(pickColor[0], pickColor[1])] = modelJob->Entity;
|
||||
|
||||
m_PickingColorsToEntity[glm::ivec2(pickColor[0], pickColor[1])] = pickInfo;
|
||||
|
||||
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix));
|
||||
glUniformMatrix4fv(glGetUniformLocation(ShaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix()));
|
||||
@@ -101,6 +105,19 @@ void PickingPass::Draw(RenderScene& scene)
|
||||
|
||||
|
||||
|
||||
void PickingPass::ClearPicking()
|
||||
{
|
||||
m_PickingColorsToEntity.clear();
|
||||
m_EntityColors.clear();
|
||||
m_ColorCounter[0] = 0;
|
||||
m_ColorCounter[1] = 0;
|
||||
|
||||
m_PickingBuffer.Bind();
|
||||
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
m_PickingBuffer.Unbind();
|
||||
}
|
||||
|
||||
PickData PickingPass::Pick(glm::vec2 screenCoord)
|
||||
{
|
||||
int fbWidth;
|
||||
@@ -114,14 +131,19 @@ PickData PickingPass::Pick(glm::vec2 screenCoord)
|
||||
ScreenCoords::PixelData data = ScreenCoords::ToPixelData(screenCoord, &m_PickingBuffer, m_DepthBuffer);
|
||||
pickData.Depth = data.Depth;
|
||||
|
||||
auto it = m_PickingColorsToEntity.find(glm::vec2(data.Color[0], data.Color[1]));
|
||||
PickingInfo pickInfo;
|
||||
|
||||
auto it = m_PickingColorsToEntity.find(glm::ivec2(data.Color[0], data.Color[1]));
|
||||
if (it != m_PickingColorsToEntity.end()) {
|
||||
pickData.Entity = it->second;
|
||||
pickInfo = it->second;
|
||||
} else {
|
||||
pickData.Entity = EntityID_Invalid;
|
||||
}
|
||||
pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
|
||||
pickData.Position = ScreenCoords::ToWorldPos(screenCoord.x, screenCoord.y, data.Depth, resolution, pickInfo.Camera->ProjectionMatrix(), pickInfo.Camera->ViewMatrix());
|
||||
|
||||
|
||||
pickData.Entity = pickInfo.Entity;
|
||||
pickData.Camera = pickInfo.Camera;
|
||||
return pickData;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ PickingPassState::PickingPassState(GLuint frameBuffer)
|
||||
Enable(GL_CULL_FACE);
|
||||
|
||||
glm::vec4 clearColor = glm::vec4(0.f);
|
||||
ClearColor(clearColor);
|
||||
Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
//ClearColor(clearColor);
|
||||
//Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
PickingPassState::~PickingPassState()
|
||||
|
||||
@@ -93,7 +93,7 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& jobs, World
|
||||
glm::mat4 modelMatrix = Transform::ModelMatrix(modelComponent.EntityID, world);
|
||||
|
||||
for (auto texGroup : model->TextureGroups) {
|
||||
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent));
|
||||
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(model, m_Camera, modelMatrix, texGroup, modelComponent, world));
|
||||
jobs.push_back(modelJob);
|
||||
}
|
||||
}
|
||||
@@ -118,11 +118,25 @@ void RenderSystem::Update(World* world, double dt)
|
||||
|
||||
//Only supports opaque geometry atm
|
||||
m_RenderFrame->Clear();
|
||||
|
||||
|
||||
|
||||
RenderScene rs;
|
||||
rs.Camera = m_Camera;
|
||||
rs.Viewport = Rectangle(1280, 720);
|
||||
fillModels(rs.ForwardJobs, world);
|
||||
m_RenderFrame->Add(rs);
|
||||
|
||||
RenderScene rs2;
|
||||
rs2.Camera = new Camera((float)m_Renderer->Resolution().Width / m_Renderer->Resolution().Height, glm::radians(45.f), 0.01f, 5000.f);;
|
||||
rs2.Camera->SetProjectionMatrix(glm::mat4(1));
|
||||
rs2.Camera->SetViewMatrix(glm::mat4(1));
|
||||
|
||||
rs2.Viewport = Rectangle(1280, 720);
|
||||
fillModels(rs2.ForwardJobs, world);
|
||||
m_RenderFrame->Add(rs2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void RenderSystem::updateCamera(World* world, double dt)
|
||||
@@ -167,7 +181,7 @@ void RenderSystem::updateCamera(World* world, double dt)
|
||||
m_Camera->SetOrientation(orientation);
|
||||
|
||||
updateProjectionMatrix(cameraComponent);
|
||||
m_Camera->UpdateViewMatrix();
|
||||
|
||||
}
|
||||
} else {
|
||||
m_Camera = m_DefaultCamera;
|
||||
@@ -186,5 +200,7 @@ void RenderSystem::updateCamera(World* world, double dt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_Camera->UpdateViewMatrix();
|
||||
}
|
||||
|
||||
|
||||
@@ -91,11 +91,15 @@ void Renderer::Update(double dt)
|
||||
|
||||
void Renderer::Draw(RenderFrame& frame)
|
||||
{
|
||||
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 0.f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
m_PickingPass->ClearPicking();
|
||||
for (auto scene : frame.RenderScenes){
|
||||
m_Camera = scene->Camera; // remove renderer camera when Editor uses the render scene cameras.
|
||||
m_PickingPass->Draw(*scene);
|
||||
|
||||
glClearColor(255.f / 255, 163.f / 255, 176.f / 255, 1.f);
|
||||
|
||||
|
||||
m_DrawScenePass->Draw(*scene);
|
||||
GLERROR("Renderer::Draw m_DrawScenePass->Draw");
|
||||
|
||||
Reference in New Issue
Block a user