-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
} |
This file contains 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
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__ |
This file contains 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
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)); | ||
} | ||
} | ||
} |
This file contains 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
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__ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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