Skip to content

Commit

Permalink
setting up raytrace
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinedev committed Apr 5, 2023
1 parent 983d5f5 commit cf060db
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
1 change: 1 addition & 0 deletions Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void Controller::run()
}

deltaTime = view.display(scenegraph, cameras, cameras[activeCameraIndex]);
if (view.useRaycast) break;
}
view.closeWindow();
exit(EXIT_SUCCESS);
Expand Down
71 changes: 44 additions & 27 deletions View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ View::~View(){
void View::init(Callbacks *callbacks, Model& model)
{
if (!glfwInit())
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
Expand All @@ -42,44 +42,44 @@ void View::init(Callbacks *callbacks, Model& model)
window = glfwCreateWindow(800, 800, "Hello GLFW: Per-vertex coloring", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowUserPointer(window, (void *)callbacks);
glfwSetWindowUserPointer(window, (void *)callbacks);

//using C++ functions as callbacks to a C-style library
glfwSetKeyCallback(window,
[](GLFWwindow* window, int key, int scancode, int action, int mods)
{
[](GLFWwindow* window, int key, int scancode, int action, int mods)
{
reinterpret_cast<Callbacks*>(glfwGetWindowUserPointer(window))->onkey(key,scancode,action,mods);
});
});

glfwSetWindowSizeCallback(window,
[](GLFWwindow* window, int width,int height)
{
[](GLFWwindow* window, int width,int height)
{
reinterpret_cast<Callbacks*>(glfwGetWindowUserPointer(window))->reshape(width,height);
});
});

glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursorPosCallback(window,
[](GLFWwindow* window, double xpos, double ypos)
{
[](GLFWwindow* window, double xpos, double ypos)
{
reinterpret_cast<Callbacks*>(glfwGetWindowUserPointer(window))->mousePosition(xpos,ypos);
});
});

glfwSetMouseButtonCallback(window,
[](GLFWwindow* window, int button, int action, int mods)
{
[](GLFWwindow* window, int button, int action, int mods)
{
reinterpret_cast<Callbacks*>(glfwGetWindowUserPointer(window))->mouseButton(button, action, mods);
});
});

glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glfwSwapInterval(1);

// create the shader program
program.createProgram(string("shaders/phong-multiple.vert.glsl"),
string("shaders/phong-spot.frag.glsl"));
string("shaders/phong-spot.frag.glsl"));
// assuming it got created, get all the shader variables that it uses
// so we can initialize them at some point
// enable the shader program
Expand All @@ -96,13 +96,11 @@ void View::init(Callbacks *callbacks, Model& model)
time = glfwGetTime();
deltaTime = 0;

renderer = new sgraph::GLScenegraphRenderer(modelview,objects,textureIds,shaderLocations);
raycastRenderer = new sgraph::RaycastRenderer(modelview,objects);
if (useRaycast)
raycastRenderer = new sgraph::RaycastRenderer(modelview,objects,"render.ppm");
else
renderer = new sgraph::GLScenegraphRenderer(modelview,objects,textureIds,shaderLocations);

modelview.push(glm::mat4(1.0));
model.getScenegraph()->getRoot()->accept(raycastRenderer);
modelview.pop();

}

void View::initLights(Model& model) {
Expand Down Expand Up @@ -153,13 +151,13 @@ void View::initObjects(Model& model) {

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //if the s-coordinate goes outside (0,1), repeat it
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //if the t-coordinate goes outside (0,1), repeat it
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureObject->getWidth(),textureObject->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,textureObject->getImage());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureObject->getWidth(),textureObject->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,textureObject->getImage());
glGenerateMipmap(GL_TEXTURE_2D);
textureIds[texturePair.first] = textureId;
}

util::PolygonMesh<VertexAttrib> cameraMesh = model.getCameraMesh();
cameraObj = new util::ObjectInstance("camera");
cameraObj->initPolygonMesh(shaderLocations, shaderVarsToVertexAttribs, cameraMesh);
Expand Down Expand Up @@ -187,6 +185,16 @@ void View::initShaderVariables() {

float View::display(sgraph::IScenegraph *scenegraph, vector<Camera*>& cameras, Camera* activeCamera) {

if (useRaycast) {
modelview.push(glm::mat4(1.0));
modelview.top() *= activeCamera->GetViewMatrix();
scenegraph->getRoot()->accept(raycastRenderer);
raycastRenderer->raytrace(100, 100, modelview);
modelview.pop();

return 0;
}

program.enable();
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down Expand Up @@ -255,6 +263,8 @@ float View::display(sgraph::IScenegraph *scenegraph, vector<Camera*>& cameras, C

glfwSwapBuffers(window);
glfwPollEvents();


frames++;
double currenttime = glfwGetTime();
deltaTime = currenttime - time;
Expand All @@ -264,6 +274,8 @@ float View::display(sgraph::IScenegraph *scenegraph, vector<Camera*>& cameras, C
}

void View::resize() {
if (useRaycast) return;

int window_width,window_height;
glfwGetFramebufferSize(window,&window_width,&window_height);

Expand All @@ -275,6 +287,7 @@ void View::resize() {
}

bool View::shouldWindowClose() {
if (useRaycast) return false;
return glfwWindowShouldClose(window);
}

Expand All @@ -291,6 +304,10 @@ void View::closeWindow() {
cameraObj->cleanup();
delete cameraObj;

if (useRaycast) {
delete raycastRenderer;
}

glfwDestroyWindow(window);

glfwTerminate();
Expand Down
23 changes: 19 additions & 4 deletions sgraph/RaycastRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <iostream>
#include <unordered_map>
#include "../Ray3D.hpp"
#include <vector>
#include "../HitRecord.hpp"

using namespace std;

Expand All @@ -33,9 +35,10 @@ namespace sgraph {
* @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) {
RaycastRenderer(stack<glm::mat4>& mv, map<string,util::ObjectInstance *>& os, string outfileLoc)
: modelview(mv),
objects(os),
outfileLoc(outfileLoc) {
for (map<string,util::ObjectInstance *>::iterator it=objects.begin();it!=objects.end();it++) {
cout << "Mesh with name: "<< it->first << endl;
}
Expand Down Expand Up @@ -65,9 +68,14 @@ namespace sgraph {
//send modelview matrix to GPU
glm::mat4 normalmatrix = glm::inverse(glm::transpose((modelview.top())));

string name = leafNode->getInstanceOf();
string modelType = leafNode->getInstanceOf();
if (modelType != "box" && modelType != "sphere") return;

string name = leafNode->getName();
objTypeMap.emplace(name, modelType);
modelviewMap.emplace(name, modelview.top());
normalmatrixMap.emplace(name, normalmatrix);

}

/**
Expand Down Expand Up @@ -122,14 +130,21 @@ namespace sgraph {

return out;
}

void raytrace(int width, int height, stack<glm::mat4>& mv) {

}

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;
unordered_map<string,string> objTypeMap;

string outfileLoc;
vector<vector<HitRecord> > rayHits;
};
}

Expand Down

0 comments on commit cf060db

Please sign in to comment.