Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIS 565 Project 4 #9

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
332 changes: 159 additions & 173 deletions README.md

Large diffs are not rendered by default.

104,501 changes: 104,501 additions & 0 deletions objs/bunny.obj

Large diffs are not rendered by default.

150,000 changes: 150,000 additions & 0 deletions objs/dragon.obj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions objs/tri.obj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
v 0 0 0
v 0.1 0 0
v 0 0.1 0
v 1 0 0
v 0 1 0

vn 0 0 1

Expand Down
Binary file added renders/bunny_color_diffuse.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/bunny_color_flat.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/bunny_white.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/dragon_normal.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/dragon_white.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/ObjCore/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ obj::~obj(){
}

void obj::buildVBOs(){
recenter();
//recenter();
vector<float> VBOvec;
vector<float> NBOvec;
vector<int> IBOvec;
Expand All @@ -56,9 +56,9 @@ void obj::buildVBOs(){
for(int i=2; i<face.size(); i++){
glm::vec4 p1 = points[face[i-1]];
glm::vec4 p2 = points[face[i]];
VBOvec.push_back(p0[0]) ; VBOvec.push_back(p0[1]); VBOvec.push_back(p0[2]); //VBOvec.push_back(1.0f);
VBOvec.push_back(p1[0]); VBOvec.push_back(p1[1]); VBOvec.push_back(p1[2]); //VBOvec.push_back(1.0f);
VBOvec.push_back(p2[0]); VBOvec.push_back(p2[1]); VBOvec.push_back(p2[2]); //VBOvec.push_back(1.0f);
VBOvec.push_back(p0[0]) ; VBOvec.push_back(p0[1]); VBOvec.push_back(p0[2]); VBOvec.push_back(1.0f);
VBOvec.push_back(p1[0]); VBOvec.push_back(p1[1]); VBOvec.push_back(p1[2]); VBOvec.push_back(1.0f);
VBOvec.push_back(p2[0]); VBOvec.push_back(p2[1]); VBOvec.push_back(p2[2]); VBOvec.push_back(1.0f);

if(genNormals==false){
vector<int> facenormal = facenormals[k];
Expand Down
112 changes: 0 additions & 112 deletions src/glm/gtx/matrix_operation.hpp
Original file line number Diff line number Diff line change
@@ -1,112 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref gtx_matrix_operation
/// @file glm/gtx/matrix_operation.hpp
/// @date 2009-08-29 / 2011-06-07
/// @author Christophe Riccio
///
/// @see core (dependence)
///
/// @defgroup gtx_matrix_operation GLM_GTX_matrix_operation: Extended matrix operations
/// @ingroup gtx
///
/// @brief Build diagonal matrices from vectors.
///
/// <glm/gtx/matrix_operation.hpp> need to be included to use these functionalities.
///////////////////////////////////////////////////////////////////////////////////

#ifndef GLM_GTX_matrix_operation
#define GLM_GTX_matrix_operation GLM_VERSION

// Dependency:
#include "../glm.hpp"

#if(defined(GLM_MESSAGES) && !defined(glm_ext))
# pragma message("GLM: GLM_GTX_matrix_operation extension included")
#endif

namespace glm
{
/// @addtogroup gtx_matrix_operation
/// @{

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat2x2<valType> diagonal2x2(
detail::tvec2<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat2x3<valType> diagonal2x3(
detail::tvec2<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat2x4<valType> diagonal2x4(
detail::tvec2<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat3x2<valType> diagonal3x2(
detail::tvec2<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat3x3<valType> diagonal3x3(
detail::tvec3<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat3x4<valType> diagonal3x4(
detail::tvec3<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat4x2<valType> diagonal4x2(
detail::tvec2<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat4x3<valType> diagonal4x3(
detail::tvec3<valType> const & v);

//! Build a diagonal matrix.
//! From GLM_GTX_matrix_operation extension.
template <typename valType>
detail::tmat4x4<valType> diagonal4x4(
detail::tvec4<valType> const & v);

/// @}
}//namespace glm

#include "matrix_operation.inl"

#endif//GLM_GTX_matrix_operation
101 changes: 97 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ int main(int argc, char** argv){

glfwTerminate();
#else
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0,0,1));
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(mouseMove);

glutMainLoop();
#endif
Expand Down Expand Up @@ -99,8 +102,15 @@ void runCuda(){
ibo = mesh->getIBO();
ibosize = mesh->getIBOsize();

glm::mat4 view = glm::lookAt(eye, center, up);
proj = glm::perspective(fov, (float)width/height, zNear, zFar);
float angle = glm::radians(5.0f);
//model = glm::rotate(model, angle, glm::vec3(0,1,0));

MV = view * model;

cudaGLMapBufferObject((void**)&dptr, pbo);
cudaRasterizeCore(dptr, glm::vec2(width, height), frame, vbo, vbosize, cbo, cbosize, ibo, ibosize);
cudaRasterizeCore(dptr, glm::vec2(width, height), frame, vbo, vbosize, cbo, cbosize, ibo, ibosize, MV, proj, view * model_switch * light);
cudaGLUnmapBufferObject(pbo);

vbo = NULL;
Expand Down Expand Up @@ -148,7 +158,24 @@ void runCuda(){
#else

void display(){
cudaEvent_t start, stop;
float time_cuda;

// Keep track of time
cudaEventCreate(&start);
cudaEventCreate(&stop);

cudaEventRecord( start, 0 );

runCuda();

cudaEventRecord( stop, 0 );
cudaEventSynchronize( stop );

cudaEventElapsedTime( &time_cuda, start, stop );
cudaEventDestroy( start );
cudaEventDestroy( stop );

time_t seconds2 = time (NULL);

if(seconds2-seconds >= 1){
Expand All @@ -158,9 +185,10 @@ void runCuda(){
seconds = seconds2;

}

string title = "CIS565 Rasterizer | "+ utilityCore::convertIntToString((int)fps) + "FPS";
glutSetWindowTitle(title.c_str());
char info[1024];
sprintf(info, "CIS565 Rasterizer | %i FPS | Total : %3.3f ms", (int)fps, time_cuda);
string title(info);
glutSetWindowTitle(title.c_str());

glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
glBindTexture(GL_TEXTURE_2D, displayImage);
Expand All @@ -176,16 +204,81 @@ void runCuda(){
glutSwapBuffers();
}

glm::vec3 raycast(float x, float y){
glm::vec3 view = center - eye;
glm::vec3 A = glm::cross(view, up);
glm::vec3 B = glm::cross(A, view);
glm::vec3 M = eye + view;

float phi = glm::radians(fov);
float theta = glm::atan(tan(phi)*(float)width/height);
float C = glm::length(view);

glm::vec3 V = glm::normalize(B) * (C * tan(phi));
glm::vec3 H = glm::normalize(A) * (C * tan(theta));

float sx = (float) x / (width - 1.0f);
float sy = (float) y / (height - 1.0f);

return (M + (2 * sx - 1.0f)*H + (1.0f - 2 * sy)*V - eye);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case(27):
shut_down(1);
break;
case 'i':
fov -= .5f;
break;
case 'o':
fov += .5f;
break;
}
}

void mouse(int button, int state, int x, int y){
switch (button){
case(GLUT_LEFT_BUTTON):
if(state == GLUT_DOWN){
mouseClickPos = glm::vec2(x,y);
buttonID = button;
}
break;
case(GLUT_MIDDLE_BUTTON):
if(state == GLUT_DOWN){
mouseClickPos = glm::vec2(x,y);
buttonID = button;
}
break;
}
}

void mouseMove(int x, int y){
switch(buttonID){
case(GLUT_LEFT_BUTTON):
rightDir = glm::normalize(glm::cross(center - eye, up));
eye = glm::rotate(eye, glm::length(center - eye) * glm::radians((x - mouseClickPos.x)), up);
eye = glm::rotate(eye, glm::length(center - eye) * glm::radians((y - mouseClickPos.y)), rightDir);
up = glm::normalize(glm::cross(rightDir, glm::normalize(center - eye)));
mouseClickPos = glm::vec2(x,y);
break;
case(GLUT_MIDDLE_BUTTON):
glm::vec3 click_pos = raycast(mouseClickPos.x, mouseClickPos.y);
glm::vec3 curr_pos = raycast(x,y);
glm::vec3 diff = curr_pos - click_pos;

rightDir = glm::normalize(glm::cross(glm::normalize(center - eye), up));

eye = eye + (glm::length(diff) * glm::dot(diff, rightDir)) * rightDir + (glm::length(diff) * glm::dot(diff, up)) * up;
center = center + (glm::length(diff) * glm::dot(diff, rightDir)) * rightDir + (glm::length(diff) * glm::dot(diff, up)) * up;
mouseClickPos = glm::vec2(x,y);
break;
}
}

#endif

//-------------------------------
Expand Down
28 changes: 28 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <time.h>
#include "glslUtility.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/rotate_vector.hpp"s
#include "rasterizeKernels.h"
#include "utilities.h"
#include "ObjCore/objloader.h"
Expand Down Expand Up @@ -60,6 +62,25 @@ int cbosize;
int* ibo;
int ibosize;

//-------------------------------
//---------CAMERA STUFF----------
//-------------------------------

glm::vec3 eye = glm::vec3(0,0,5);
glm::vec3 up = glm::vec3(0,1,0);
glm::vec3 center = glm::vec3(0,0,0);

float fov = 45.0f;
float zNear = 0.1f;
float zFar = 100.0f;

glm::mat4 model = glm::mat4(glm::vec4(-1,0,0,0), glm::vec4(0,-1,0,0), glm::vec4(0,0,1,0), glm::vec4(0,0,0,1));
glm::mat4 model_switch = glm::mat4(glm::vec4(-1,0,0,0), glm::vec4(0,-1,0,0), glm::vec4(0,0,1,0), glm::vec4(0,0,0,1));
glm::mat4 MV = glm::mat4(1.0f);
glm::mat4 proj = glm::mat4(1.0f);

glm::vec4 light = glm::normalize(glm::vec4(5,5,7,1));

//-------------------------------
//----------CUDA STUFF-----------
//-------------------------------
Expand All @@ -78,11 +99,18 @@ int main(int argc, char** argv);

void runCuda();

int buttonID = -1;

glm::vec2 mouseClickPos;
glm::vec3 rightDir;

#ifdef __APPLE__
void display();
#else
void display();
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void mouseMove(int x, int y);
#endif

//-------------------------------
Expand Down
Loading