From cf060dbfed190a1004c8aecccf87843bb8d92be2 Mon Sep 17 00:00:00 2001 From: Jacob Pine Date: Tue, 4 Apr 2023 19:18:11 -0400 Subject: [PATCH] setting up raytrace --- Controller.cpp | 1 + View.cpp | 71 +++++++++++++++++++++++--------------- sgraph/RaycastRenderer.hpp | 23 +++++++++--- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/Controller.cpp b/Controller.cpp index d3d07a4..14fa33c 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -67,6 +67,7 @@ void Controller::run() } deltaTime = view.display(scenegraph, cameras, cameras[activeCameraIndex]); + if (view.useRaycast) break; } view.closeWindow(); exit(EXIT_SUCCESS); diff --git a/View.cpp b/View.cpp index 2686c40..a7410ea 100644 --- a/View.cpp +++ b/View.cpp @@ -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); @@ -42,36 +42,36 @@ 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(glfwGetWindowUserPointer(window))->onkey(key,scancode,action,mods); - }); + }); glfwSetWindowSizeCallback(window, - [](GLFWwindow* window, int width,int height) - { + [](GLFWwindow* window, int width,int height) + { reinterpret_cast(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(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(glfwGetWindowUserPointer(window))->mouseButton(button, action, mods); - }); + }); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); @@ -79,7 +79,7 @@ void View::init(Callbacks *callbacks, Model& model) // 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 @@ -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) { @@ -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 cameraMesh = model.getCameraMesh(); cameraObj = new util::ObjectInstance("camera"); cameraObj->initPolygonMesh(shaderLocations, shaderVarsToVertexAttribs, cameraMesh); @@ -187,6 +185,16 @@ void View::initShaderVariables() { float View::display(sgraph::IScenegraph *scenegraph, vector& 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); @@ -255,6 +263,8 @@ float View::display(sgraph::IScenegraph *scenegraph, vector& cameras, C glfwSwapBuffers(window); glfwPollEvents(); + + frames++; double currenttime = glfwGetTime(); deltaTime = currenttime - time; @@ -264,6 +274,8 @@ float View::display(sgraph::IScenegraph *scenegraph, vector& cameras, C } void View::resize() { + if (useRaycast) return; + int window_width,window_height; glfwGetFramebufferSize(window,&window_width,&window_height); @@ -275,6 +287,7 @@ void View::resize() { } bool View::shouldWindowClose() { + if (useRaycast) return false; return glfwWindowShouldClose(window); } @@ -291,6 +304,10 @@ void View::closeWindow() { cameraObj->cleanup(); delete cameraObj; + if (useRaycast) { + delete raycastRenderer; + } + glfwDestroyWindow(window); glfwTerminate(); diff --git a/sgraph/RaycastRenderer.hpp b/sgraph/RaycastRenderer.hpp index 3d6ee9a..d40b192 100644 --- a/sgraph/RaycastRenderer.hpp +++ b/sgraph/RaycastRenderer.hpp @@ -16,6 +16,8 @@ #include #include #include "../Ray3D.hpp" +#include +#include "../HitRecord.hpp" using namespace std; @@ -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& mv,map& os) - : modelview(mv) - , objects(os) { + RaycastRenderer(stack& mv, map& os, string outfileLoc) + : modelview(mv), + objects(os), + outfileLoc(outfileLoc) { for (map::iterator it=objects.begin();it!=objects.end();it++) { cout << "Mesh with name: "<< it->first << endl; } @@ -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); + } /** @@ -122,6 +130,10 @@ namespace sgraph { return out; } + + void raytrace(int width, int height, stack& mv) { + + } private: stack& modelview; @@ -129,7 +141,10 @@ namespace sgraph { // TODO: map textures; unordered_map modelviewMap; unordered_map normalmatrixMap; + unordered_map objTypeMap; + string outfileLoc; + vector > rayHits; }; }