Skip to content

Commit

Permalink
Merge branch 'Billboards'
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Mateu Tudela committed Mar 22, 2018
2 parents 7707f06 + e6675a3 commit 4b62630
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 3 deletions.
21 changes: 21 additions & 0 deletions LCSEngine/Billboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Billboard.h"

Billboard::Billboard(float3 position, float width, float height) : position(position), width(width), height(height) {}

Billboard::~Billboard() {}

/*Vertexs will contain all 4 vertexs of the quad in this order: Up Left, Up Right, Down Left, Down Right*/
void Billboard::ComputeQuad(const Frustum* frustum, std::vector<float3>& vertexs)
{
float3 right = (position - frustum->pos).Cross(float3(0.f,1.f,0.f)).Normalized();

float3 h = (float3(0.f, 1.f, 0.f) * height);
float3 w = (right * width);

vertexs.clear();
vertexs.reserve(4);
vertexs.push_back(position + (float3(0.f, 1.f, 0.f) * height) - (right * width) / 2.f);
vertexs.push_back(position + (float3(0.f, 1.f, 0.f) * height) + (right * width) / 2.f);
vertexs.push_back(position + (right * width) / 2.f);
vertexs.push_back(position - (right * width) / 2.f);
}
21 changes: 21 additions & 0 deletions LCSEngine/Billboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __BILLBOARD_H__
#define __BILLBOARD_H__

#include "MathGeoLib/src/Geometry/Frustum.h"
#include "MathGeoLib/src/Math/float3.h"

class Billboard
{
public:
Billboard(float3 position, float width, float height);
~Billboard();

void ComputeQuad(const Frustum* frustum, std::vector<float3>& vertexs);

public:
float3 position = float3::zero;
float width = 0.f;
float height = 0.f;
};

#endif //__BILLBOARD_H__
141 changes: 141 additions & 0 deletions LCSEngine/BillboardGridComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "Globals.h"
#include "Application.h"
#include "GameObject.h"
#include "TransformComponent.h"
#include "CameraComponent.h"
#include "BillboardGridComponent.h"
#include "Billboard.h"
#include "ModuleCamera.h"
#include "ModuleSceneMain.h"
#include "Imgui/imgui.h"
#include <glew.h>

/**/
#include "ModuleTextures.h"
#include "AssetTexture.h"
/**/

BillboardGridComponent::BillboardGridComponent(GameObject* gameObject, bool isEnable, bool isUnique) : Component(gameObject, isEnable, isUnique)
{
typeComponent = BILLBOARDGRID;

glGenBuffers(1, (GLuint*) &(idVertVBO));
glGenBuffers(1, (GLuint*) &(idIdxVAO));
glGenBuffers(1, (GLuint*) &(idTexCoords));
glGenBuffers(1, (GLuint*) &(idColors));
billboards.reserve(n*m);

float3 gameObjectPos = ((TransformComponent*)gameObject->getComponent(TRANSFORM))->position;

for (unsigned int i = 0; i < n; ++i)
{
for (unsigned int j = 0; j < m; ++j)
{
float difX = -(quadSpaceX / 2.f) + float(rand() / float(RAND_MAX / quadSpaceX));
float difY = -(quadSpaceY / 2.f) + float(rand() / float(RAND_MAX / quadSpaceY));
float3 position = gameObjectPos + (float3(1.f, 0.f, 0.f) * ((quadSpaceX * j) + difX)) + float3(0.f, 0.f, 1.f) * ((quadSpaceY * i) + difY);
Billboard* billboard = new Billboard(position, minW + float(rand() / float(RAND_MAX / (maxW-minW))), minH + float(rand() / float(RAND_MAX / (maxH - minH))));
billboards.push_back(billboard);
}
}

/*TODO: set texture on GUI*/
map<std::string, AssetTexture*>::iterator itNewTexture = App->textures->textures.find("Assets/Images/billboardgrass.png");
if (itNewTexture != App->textures->textures.end())
{
texID = (*itNewTexture).second->ID;
(*itNewTexture).second->numberUsages++;
hasTexture = true;
}
else if (App->textures->loadTexture("Assets/Images/billboardgrass.png"))
{
texID = App->textures->textures["Assets/Images/billboardgrass.png"]->ID;
hasTexture = true;
}
else
{
texID = 0;
hasTexture = false;
}
}

BillboardGridComponent::~BillboardGridComponent() {}

void BillboardGridComponent::calculateVertexs()
{
updateBillboards();

glBindBuffer(GL_ARRAY_BUFFER, idVertVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * verticesVBO.size() * 3, verticesVBO[0].ptr(), GL_DYNAMIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idIdxVAO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indicesVBO.size(), &indicesVBO[0], GL_DYNAMIC_DRAW);



if (texCoordsVBO.size() > 0)
{
glBindBuffer(GL_ARRAY_BUFFER, idTexCoords);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * texCoordsVBO.size() * 2, texCoordsVBO[0].ptr(), GL_DYNAMIC_DRAW);
}

colorsVBO.clear();
for (unsigned int i = 0; i < verticesVBO.size(); ++i)
{
colorsVBO.push_back(float3(1.f, 1.f, 1.f));
}

glBindBuffer(GL_ARRAY_BUFFER, idColors);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * colorsVBO.size() * 3, colorsVBO[0].ptr(), GL_DYNAMIC_DRAW);
}

void BillboardGridComponent::drawGUI()
{
if (ImGui::CollapsingHeader("Billboard Grid"))
{
ImGui::Checkbox("Active", &isEnable); ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Button, ImColor::HSV(0.6f, 0.6f, 0.6f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImColor::HSV(0.6f, 0.7f, 0.7f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImColor::HSV(0.6f, 0.8f, 0.8f));
if (ImGui::Button("Delete Component"))
{
App->sceneMain->garbageCollectorComponent.push_back(this);
}
ImGui::PopStyleColor(3);
}
}

void BillboardGridComponent::updateBillboards()
{
float3 gameObjectPos = ((TransformComponent*)gameObject->getComponent(TRANSFORM))->position;

verticesVBO.clear();
indicesVBO.clear();
texCoordsVBO.clear();

for (unsigned int i = 0; i < n; ++i)
{
for (unsigned int j = 0; j < m; ++j)
{
vector<float3> vertexs;
billboards[i*m + j]->ComputeQuad(&App->camera->currentCamera->frustum, vertexs);

indicesVBO.push_back(1 + verticesVBO.size());
indicesVBO.push_back(3 + verticesVBO.size());
indicesVBO.push_back(2 + verticesVBO.size());
indicesVBO.push_back(1 + verticesVBO.size());
indicesVBO.push_back(0 + verticesVBO.size());
indicesVBO.push_back(3 + verticesVBO.size());

verticesVBO.push_back(vertexs[0]);
verticesVBO.push_back(vertexs[1]);
verticesVBO.push_back(vertexs[2]);
verticesVBO.push_back(vertexs[3]);

texCoordsVBO.push_back(float2(0.f, 1.f));
texCoordsVBO.push_back(float2(1.f, 1.f));
texCoordsVBO.push_back(float2(1.f, 0.f));
texCoordsVBO.push_back(float2(0.f, 0.f));
}
}
}
47 changes: 47 additions & 0 deletions LCSEngine/BillboardGridComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef __BILLBOARDGRIDCOMPONENT_H__
#define __BILLBOARDGRIDCOMPONENT_H__

#include "Component.h"
#include "MathGeoLib/src/Math/float2.h"
#include "MathGeoLib/src/Math/float3.h"
#include "MathGeoLib/src/Math/float4x4.h"

typedef unsigned int GLuint;
class Billboard;

class BillboardGridComponent : public Component
{
public:
BillboardGridComponent(GameObject* gameObject, bool isEnable = true, bool isUnique = false);
~BillboardGridComponent();

void calculateVertexs();
void drawGUI() override;

public:
std::vector<Billboard*> billboards;
std::vector<float3> verticesVBO;
std::vector<float3> colorsVBO;
std::vector<float2> texCoordsVBO;
std::vector<unsigned int> indicesVBO;
GLuint idVertVBO = 0;
GLuint idIdxVAO = 0;
GLuint idTexCoords = 0;
GLuint idColors = 0;
GLuint texID = 0;
bool hasTexture = false;
int n = 5;
int m = 8;
float minW = 0.9f;
float maxW = 1.1f;
float minH = 0.8f;
float maxH = 1.2f;
float quadSpaceX = 0.5f;
float quadSpaceY = 0.5f;

private:
void updateBillboards();

};

#endif //__BILLBOARDGRIDCOMPONENT_H__
3 changes: 3 additions & 0 deletions LCSEngine/ComponentFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "GameObject.h"
#include "AudioListenerComponent.h"
#include "AudioSourceComponent.h"
#include "BillboardGridComponent.h"

ComponentFactory::~ComponentFactory() {}

Expand Down Expand Up @@ -48,6 +49,8 @@ Component* ComponentFactory::getComponent(TypeComponent typeComponent, GameObjec
case TypeComponent::AUDIOSOURCE:
return new AudioSourceComponent(parentObject);
break;
case TypeComponent::BILLBOARDGRID:
return new BillboardGridComponent(parentObject);
default:
return nullptr;
break;
Expand Down
24 changes: 24 additions & 0 deletions LCSEngine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "AnimationComponent.h"
#include "AudioListenerComponent.h"
#include "AudioSourceComponent.h"
#include "BillboardGridComponent.h"
#include "ComponentFactory.h"
#include "ModuleSceneMain.h"
#include "ModuleCamera.h"
Expand Down Expand Up @@ -383,6 +384,10 @@ void GameObject::drawComponentsElementsGui()
{
addComponent(factory->getComponent(AUDIOSOURCE, this));
}
else if (ImGui::MenuItem("Billboard Grid"))
{
addComponent(factory->getComponent(BILLBOARDGRID, this));
}
//Change all this and make same as components
else if (ImGui::MenuItem("UI Image"))
{
Expand Down Expand Up @@ -451,6 +456,22 @@ void GameObject::draw()
case ANIMATION:
((AnimationComponent*)(*it))->drawHierarchy();
break;
case BILLBOARDGRID:
{
renderData data;
data.id = id;
data.idVertVBO = ((BillboardGridComponent*)(*it))->idVertVBO;
data.sizeVertVBO = ((BillboardGridComponent*)(*it))->verticesVBO.size();
data.idIdxVAO = ((BillboardGridComponent*)(*it))->idIdxVAO;
data.sizeIdxVAO = ((BillboardGridComponent*)(*it))->indicesVBO.size();
data.textureID = ((BillboardGridComponent*)(*it))->texID;
data.colorID = ((BillboardGridComponent*)(*it))->idColors;
data.hasTexture = ((BillboardGridComponent*)(*it))->hasTexture;
data.textureCoordsID = ((BillboardGridComponent*)(*it))->idTexCoords;
data.mode = GL_TRIANGLES;
App->renderer->renderQueue.insert(std::pair<GLuint,renderData>(program,data));
}
break;
default:
break;
}
Expand Down Expand Up @@ -831,6 +852,9 @@ void GameObject::updateComponents()
case AUDIOSOURCE:
App->audio->updatePositionAudioSource(((AudioSourceComponent*)(*it))->idAudioGameObj, id);
break;
case BILLBOARDGRID:
((BillboardGridComponent*)(*it))->calculateVertexs();
break;
default:
break;
}
Expand Down
3 changes: 2 additions & 1 deletion LCSEngine/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum TypeComponent
CAMERA,
ANIMATION,
AUDIOLISTENER,
AUDIOSOURCE
AUDIOSOURCE,
BILLBOARDGRID
};

enum TypeElemeneGametUI
Expand Down
4 changes: 4 additions & 0 deletions LCSEngine/LCSEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
<ClInclude Include="Application.h" />
<ClInclude Include="AssetTexture.h" />
<ClInclude Include="AudioSourceComponent.h" />
<ClInclude Include="Billboard.h" />
<ClInclude Include="BillboardGridComponent.h" />
<ClInclude Include="CameraComponent.h" />
<ClInclude Include="Component.h" />
<ClInclude Include="ComponentFactory.h" />
Expand Down Expand Up @@ -263,6 +265,8 @@
<ClCompile Include="Application.cpp" />
<ClCompile Include="AssetTexture.cpp" />
<ClCompile Include="AudioSourceComponent.cpp" />
<ClCompile Include="Billboard.cpp" />
<ClCompile Include="BillboardGridComponent.cpp" />
<ClCompile Include="CameraComponent.cpp" />
<ClCompile Include="Component.cpp" />
<ClCompile Include="ComponentFactory.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions LCSEngine/LCSEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@
<ClInclude Include="SaveLoadManager.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="Billboard.h">
<Filter>Utils</Filter>
</ClInclude>
<ClInclude Include="BillboardGridComponent.h">
<Filter>Components</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Log.cpp">
Expand Down Expand Up @@ -620,6 +626,12 @@
<ClCompile Include="SaveLoadManager.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="Billboard.cpp">
<Filter>Utils</Filter>
</ClCompile>
<ClCompile Include="BillboardGridComponent.cpp">
<Filter>Components</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="MathGeoLib\src\Geometry\KDTree.inl">
Expand Down
2 changes: 1 addition & 1 deletion LCSEngine/ModuleRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bool ModuleRender::init()
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearDepth(1.0f);
glClearColor(0.f, 0.f, 0.f, 1.f);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
Expand All @@ -66,7 +67,6 @@ bool ModuleRender::init()
glEnable(GL_TEXTURE_2D);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_BLEND);

return ret;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion LCSEngine/ProjectDir/Assets/Shaders/fragshader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ void main()
{
if (useText)
{
color = vec4(texture(text,UV).rgb * col, texture(text,UV).a);
vec4 texture = texture(text,UV);
if (texture.a == 0.f)
{
discard;
}
color = vec4(texture.rgb * col, texture.a);
}
else
{
Expand Down

0 comments on commit 4b62630

Please sign in to comment.