mirror of
https://github.com/ApfelTeeSaft/vb01.git
synced 2026-08-26 19:33:45 +00:00
ray sample
This commit is contained in:
@@ -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<Ray::CollisionResult> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user