Skip to content

Commit

Permalink
started raytrace renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinedev committed Apr 4, 2023
1 parent 9df8190 commit faf2587
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ using namespace std;
Controller::Controller(istream& scenegraphFile, Model& m,View& v): model(m), view(v) {
initScenegraph(scenegraphFile);

view.useRaycast = true;

globalCamera = new StationaryCamera(glm::vec3(0.0, 0.0, 20.0));
globalCamera->LookAt(glm::vec3(0.0,0.0,0.0));
cameras.push_back(globalCamera);
Expand Down
3 changes: 3 additions & 0 deletions View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "PolygonMesh.h"
#include "TextureImage.h"
#include "sgraph/LightAccumulator.h"
#include "sgraph/RaycastRenderer.hpp"
#include <cstdio>
#include <cstdlib>
#include <iostream>
Expand Down Expand Up @@ -96,6 +97,8 @@ void View::init(Callbacks *callbacks, Model& model)
deltaTime = 0;

renderer = new sgraph::GLScenegraphRenderer(modelview,objects,textureIds,shaderLocations);
raycastRenderer = new sgraph::RaycastRenderer(modelview,objects);
model.getScenegraph()->getRoot()->accept(raycastRenderer);

}

Expand Down
5 changes: 5 additions & 0 deletions View.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __VIEW_H__

#include "../include/Light.h"
#include "sgraph/RaycastRenderer.hpp"
#ifndef GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_NONE
#endif
Expand Down Expand Up @@ -41,6 +42,9 @@ class View
bool shouldWindowClose();
void closeWindow();

bool useRaycast = false;


private:
void initObjects(Model& model);
vector<glm::vec4> getLightPositions(const glm::mat4& transformation);
Expand All @@ -56,6 +60,7 @@ class View
glm::mat4 projection;
stack<glm::mat4> modelview;
sgraph::SGNodeVisitor *renderer;
sgraph::RaycastRenderer *raycastRenderer;
int frames;
double time;
double deltaTime;
Expand Down
122 changes: 122 additions & 0 deletions sgraph/RaycastRenderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#ifndef _RAYCASTRENDERER_H_
#define _RAYCASTRENDERER_H_

#include "SGNodeVisitor.h"
#include "GroupNode.h"
#include "LeafNode.h"
#include "TransformNode.h"
#include "RotateTransform.h"
#include "ScaleTransform.h"
#include "TranslateTransform.h"
#include <OpenGL/OpenGL.h>
#include <ShaderProgram.h>
#include <ShaderLocationsVault.h>
#include "ObjectInstance.h"
#include <stack>
#include <iostream>
#include <unordered_map>

using namespace std;

namespace sgraph {
/**
* This visitor implements drawing the scene graph using OpenGL
*
*/
class RaycastRenderer: public SGNodeVisitor {
public:
/**
* @brief Construct a new RaycastRenderer object
*
* @param mv a reference to modelview stack that will be used while rendering
* @param os the map of ObjectInstance objects
* @param shaderLocations the shader locations for the program used to render
*/
RaycastRenderer(stack<glm::mat4>& mv,map<string,util::ObjectInstance *>& os)
: modelview(mv)
, objects(os) {
for (map<string,util::ObjectInstance *>::iterator it=objects.begin();it!=objects.end();it++) {
cout << "Mesh with name: "<< it->first << endl;
}
}

/**
* @brief Recur to the children for drawing
*
* @param groupNode
*/
void visitGroupNode(GroupNode *groupNode) {
for (int i=0;i<groupNode->getChildren().size();i=i+1) {
groupNode->getChildren()[i]->accept(this);
}
}

/**
* @brief Draw the instance for the leaf, after passing the
* modelview and color to the shader
*
* @param leafNode
*/
void visitLeafNode(LeafNode *leafNode) {

util::Material mat = leafNode->getMaterial();

//send modelview matrix to GPU
glm::mat4 normalmatrix = glm::inverse(glm::transpose((modelview.top())));

string name = leafNode->getInstanceOf();
modelviewMap.emplace(name, modelview.top());
normalmatrixMap.emplace(name, normalmatrix);
}

/**
* @brief Multiply the transform to the modelview and recur to child
*
* @param transformNode
*/
void visitTransformNode(TransformNode * transformNode) {
modelview.push(modelview.top());
modelview.top() = modelview.top() * transformNode->getTransform();
if (transformNode->getChildren().size()>0) {
transformNode->getChildren()[0]->accept(this);
}
modelview.pop();
}

/**
* @brief For this visitor, only the transformation matrix is required.
* Thus there is nothing special to be done for each type of transformation.
* We delegate to visitTransformNode above
*
* @param scaleNode
*/
void visitScaleTransform(ScaleTransform *scaleNode) {
visitTransformNode(scaleNode);
}

/**
* @brief For this visitor, only the transformation matrix is required.
* Thus there is nothing special to be done for each type of transformation.
* We delegate to visitTransformNode above
*
* @param translateNode
*/
void visitTranslateTransform(TranslateTransform *translateNode) {
visitTransformNode(translateNode);
}

void visitRotateTransform(RotateTransform *rotateNode) {
visitTransformNode(rotateNode);
}

private:
stack<glm::mat4>& modelview;
map<string,util::ObjectInstance *> objects;
// TODO: map<string,util::TextureImage*> textures;
unordered_map<string,glm::mat4> modelviewMap;
unordered_map<string,glm::mat4> normalmatrixMap;

};
}

#endif

0 comments on commit faf2587

Please sign in to comment.