From 76c92327f976037f11521bbfcdaa88a214193877 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 25 Mar 2022 20:10:38 +0200 Subject: [PATCH] ray sample --- raySample.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 raySample.cpp diff --git a/raySample.cpp b/raySample.cpp new file mode 100644 index 0000000..f0a46fc --- /dev/null +++ b/raySample.cpp @@ -0,0 +1,81 @@ +#include "root.h" +#include "model.h" +#include "material.h" +#include "assetManager.h" +#include "lineRenderer.h" +#include "ray.h" + +using namespace std; +using namespace vb01; + +int main(){ + const string PATH = "../", TEX_PATH = PATH + "samples/textures/", MODEL_PATH = PATH + "samples/models/"; + + const int numTextures = 6; + string skyboxTextures[numTextures] = { + TEX_PATH + "top.jpg", + TEX_PATH + "bottom.jpg", + TEX_PATH + "left.jpg", + TEX_PATH + "right.jpg", + TEX_PATH + "front.jpg", + TEX_PATH + "back.jpg" + }; + + for(int i = 0; i < numTextures; i++) + AssetManager::getSingleton()->load(skyboxTextures[i]); + + /* + * Gets Root object to start the sample, + * also passes the base path for asset retrieval. + */ + Root *root = Root::getSingleton(); + root->start(800, 600, PATH, "Ray sample"); + root->createSkybox(skyboxTextures); + + Camera *cam = root->getCamera(); + cam->setPosition(Vector3(1, 1, 1) * 10); + cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); + + Node *rootNode = root->getRootNode(); + + string modelPath = MODEL_PATH + "sphere.xml"; + AssetManager::getSingleton()->load(modelPath); + Model *sphModel = new Model(modelPath); + Material *mat = new Material(PATH + "texture"); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4::VEC_L); + sphModel->setMaterial(mat); + rootNode->attachChild(sphModel); + sphModel->setPosition(Vector3(-4, 0, 0)); + + LineRenderer *lineRenderer = LineRenderer::getSingleton(); + float endYPos = -4; + Vector3 rayStart = Vector3(4, 0, 0); + lineRenderer->addLine(rayStart, Vector3(-4, endYPos, 0), Vector3::VEC_IJK); + float shiftSpeed = -.1; + + while(true){ + if(endYPos < -4) + shiftSpeed = .1; + else if(endYPos > 4) + shiftSpeed = -.1; + + endYPos += shiftSpeed; + Vector3 rayEnd = Vector3(-4, endYPos, 0); + lineRenderer->changeLineField(0, rayEnd, LineRenderer::END); + + vector results; + Ray::retrieveCollisions(rayStart, rayEnd - rayStart, sphModel, results); + Ray::sortResults(results); + + mat->setVec4Uniform("diffuseColor", Vector4::VEC_L); + + if(!results.empty()) + mat->setVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); + + root->update(); + } + + return 0; +}