Skip to content

Pull request for Rasterizer project. #14

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

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
158afd8
Simple Vertex Shader and primitive Assembly.
rohith10 Oct 26, 2013
b9cb1bf
Slightly more efficient primitive assembly (IBO access reduced).
rohith10 Oct 26, 2013
5349013
Started rasterization kernel.
rohith10 Oct 26, 2013
aa47a06
Converted everything to screen space.
rohith10 Oct 27, 2013
4bb8216
Code for rasterization complete.
rohith10 Oct 27, 2013
7ca0499
Weird parenthesis missing error.
rohith10 Oct 27, 2013
3795d1a
Changes made to make the damn thing work!
rohith10 Oct 29, 2013
4b21d19
Reordered vbo, nbo, ibo.
rohith10 Oct 29, 2013
ad336c4
Debug code added.
rohith10 Oct 29, 2013
8a32c84
Fucking ridiculous. convertToScreenSpace is messing up colours (autom…
rohith10 Oct 29, 2013
2f3082b
What's ridiculous is my not feeding stuff properly. From now onwards,…
rohith10 Oct 29, 2013
cc192b2
Benchmarking and stuff.
rohith10 Oct 30, 2013
d507553
Stream Compaction for Back face culling.
rohith10 Oct 30, 2013
133a381
Alternate rasterization - one thread for every primitive.
rohith10 Oct 30, 2013
4e4416d
Update README.md
rohith10 Oct 30, 2013
ad5b4cf
Update README.md
rohith10 Oct 30, 2013
5f16c4f
Background support for camera functions.
rohith10 Oct 31, 2013
b6f1d91
Passing normals.
rohith10 Oct 31, 2013
15a390f
Support for passing in custom matrices.
rohith10 Oct 31, 2013
763aeab
Made compilable.
rohith10 Oct 31, 2013
4380d05
Merge branch 'master' of https://github.com/rohith10/RasterMaster
rohith10 Oct 31, 2013
e278ccf
Now with custom view, world and projection matrices.
rohith10 Oct 31, 2013
2f5fbaa
Camera transform in progress.
rohith10 Oct 31, 2013
3262e16
Fragment shader implemented.
rohith10 Oct 31, 2013
d48a77d
Normals were not being loaded correctly; fixed. Fragment shader done.
rohith10 Oct 31, 2013
b1486d3
Support for interactive camera.
rohith10 Oct 31, 2013
8fca379
Error prone interactive camera.
rohith10 Nov 1, 2013
7280f65
Glitchy interactive camera.
rohith10 Nov 1, 2013
062a522
Submission commit.
rohith10 Nov 1, 2013
ed89700
Actual submission commit.
rohith10 Nov 1, 2013
ad5e6d1
Update README.md
rohith10 Nov 1, 2013
83d7278
Screenshot added.
rohith10 Nov 1, 2013
cc7c3fe
Working Camera Transform.
rohith10 Nov 2, 2013
c7d5f7b
Dead code removal.
rohith10 Nov 2, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,135 changes: 825 additions & 310 deletions PROJ4_WIN/src/rasterizeKernels.cu.deps

Large diffs are not rendered by default.

254 changes: 80 additions & 174 deletions README.md

Large diffs are not rendered by default.

15,325 changes: 15,325 additions & 0 deletions objs/cow_smooth2.obj

Large diffs are not rendered by default.

Binary file added renders/OnePsychedelicCow.png
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/WithOutlines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 31 additions & 15 deletions src/ObjCore/obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ 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];
NBOvec.push_back(normals[facenormal[0]][0]); NBOvec.push_back(normals[facenormal[0]][1]); NBOvec.push_back(normals[facenormal[0]][2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(normals[facenormal[i-1]][0]); NBOvec.push_back(normals[facenormal[i-1]][1]); NBOvec.push_back(normals[facenormal[i-1]][2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(normals[facenormal[i]][0]); NBOvec.push_back(normals[facenormal[i]][1]); NBOvec.push_back(normals[facenormal[i]][2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(normals[facenormal[0]][0]); NBOvec.push_back(normals[facenormal[0]][1]); NBOvec.push_back(normals[facenormal[0]][2]); NBOvec.push_back(0.0f);
NBOvec.push_back(normals[facenormal[i-1]][0]); NBOvec.push_back(normals[facenormal[i-1]][1]); NBOvec.push_back(normals[facenormal[i-1]][2]); NBOvec.push_back(0.0f);
NBOvec.push_back(normals[facenormal[i]][0]); NBOvec.push_back(normals[facenormal[i]][1]); NBOvec.push_back(normals[facenormal[i]][2]); NBOvec.push_back(0.0f);
}else{

glm::vec3 a = glm::vec3(p1[0], p1[1], p1[2]) - glm::vec3(p0[0], p0[1], p0[2]);
glm::vec3 b = glm::vec3(p2[0], p2[1], p2[2]) - glm::vec3(p0[0], p0[1], p0[2]);
glm::vec3 n = glm::normalize(glm::cross(a,b));
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); //NBOvec.push_back(0.0f);
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); NBOvec.push_back(0.0f);
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); NBOvec.push_back(0.0f);
NBOvec.push_back(n[0]); NBOvec.push_back(n[1]); NBOvec.push_back(n[2]); NBOvec.push_back(0.0f);
}

IBOvec.push_back(index+0); IBOvec.push_back(index+1); IBOvec.push_back(index+2);
Expand All @@ -88,14 +88,30 @@ void obj::buildVBOs(){
vbosize = (int)VBOvec.size();
nbosize = (int)NBOvec.size();
ibosize = (int)IBOvec.size();
for(int i=0; i<VBOvec.size(); i++){
vbo[i] = VBOvec[i];

int vertStep = vbosize / 4;
int normStep = nbosize / 4;
int indxStep = ibosize / 3;

// Reorder vbo so that it is more amenable to coalesced global memory accesses.
for(int i=0; i<vertStep; i++){
vbo[i] = VBOvec[i*4];
vbo[i+vertStep] = VBOvec[(i*4)+1];
vbo[i+(2*vertStep)] = VBOvec[(i*4)+2];
vbo[i+(3*vertStep)] = VBOvec[(i*4)+3];
}
for(int i=0; i<NBOvec.size(); i++){
nbo[i] = NBOvec[i];
// Reorder nbo so that it is more amenable to coalesced global memory accesses.
for(int i=0; i<normStep; i++){
nbo[i] = NBOvec[i*4];
nbo[i+normStep] = NBOvec[(i*4)+1];
nbo[i+(2*normStep)] = NBOvec[(i*4)+2];
nbo[i+(3*normStep)] = NBOvec[(i*4)+3];
}
for(int i=0; i<IBOvec.size(); i++){
ibo[i] = IBOvec[i];
// Reorder ibo so that it is more amenable to coalesced global memory accesses.
for(int i=0; i<indxStep; i++){
ibo[i] = IBOvec[i*3];
ibo[i+indxStep] = IBOvec[(i*3)+1];
ibo[i+(2*indxStep)] = IBOvec[(i*3)+2];
}
setColor(glm::vec3(.4,.4,.4));
}
Expand Down
121 changes: 114 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@
// Written by Yining Karl Li, Copyright (c) 2012 University of Pennsylvania

#include "main.h"
#include "glm/gtc/matrix_transform.hpp"

bool first = true;
int oldX = 0, oldY = 0, dx = 0, dy = 0;
bool leftMButtonDown = false;

float camRadius = 5.0f;
float scrollSpeed = 0.25f;
bool outline = false, camControl = false;

cbuffer constantBuffer;
glm::mat4 cameraTransform;
glm::vec3 currentLookAt = glm::vec3 (0,0,1);
float u = 0.5;
float v = 0;
//-------------------------------
//-------------MAIN--------------
//-------------------------------
Expand All @@ -21,18 +35,42 @@ int main(int argc, char** argv){
mesh->buildVBOs();
delete loader;
loadedScene = true;
}
else if (strcmp(header.c_str(), "radius")==0)
{
camRadius = strtod (data.c_str(), NULL);
}
else if (strcmp(header.c_str(), "scrollspeed")==0)
{
scrollSpeed = strtod (data.c_str(), NULL);
}
else if (strcmp(header.c_str(), "outline")==0)
{
if (strcmp(data.c_str(), "true")==0)
outline = true;
}
else if (strcmp(header.c_str(), "cameraControl")==0)
{
if (strcmp(data.c_str(), "true")==0)
camControl = true;
}
}

if(!loadedScene){
cout << "Usage: mesh=[obj file]" << endl;
std::cin.get ();
return 0;
}

frame = 0;
seconds = time (NULL);
fpstracker = 0;

constantBuffer.model = glm::rotate (glm::mat4 (1.0f), 180.0f, glm::vec3 (1,0,0));
constantBuffer.projection = glm::perspective (60.0f, (float)(width/height), 0.1f, 100.0f);
constantBuffer.modelIT = glm::transpose(glm::inverse (constantBuffer.model));
constantBuffer.lightPos = glm::vec4 (0, 10, -10, 1);

// Launch CUDA/GL
#ifdef __APPLE__
// Needed in OSX to force use of OpenGL3.2
Expand Down Expand Up @@ -71,6 +109,8 @@ int main(int argc, char** argv){
#else
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc (onButtonPress);
glutMotionFunc (onMouseMove);

glutMainLoop();
#endif
Expand All @@ -82,25 +122,60 @@ int main(int argc, char** argv){
//---------RUNTIME STUFF---------
//-------------------------------

void runCuda(){
void runCuda(bool &isFirstTime){
// Map OpenGL buffer object for writing from CUDA on a single GPU
// No data is moved (Win & Linux). When mapped to CUDA, OpenGL should not use this buffer
dptr=NULL;

vbo = mesh->getVBO();
vbosize = mesh->getVBOsize();

float newcbo[] = {0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 0.0};
float newcbo[] = {0.35, 0.35, 0.35,
0.4, 0.4, 0.4,
0.3, 0.3, 0.3};
cbo = newcbo;
cbosize = 9;

ibo = mesh->getIBO();
ibosize = mesh->getIBOsize();

nbo = mesh->getNBO ();
nbosize = mesh->getNBOsize ();

// Change u and v with respect to change in Y and X respectively.
u = u + (float)dy / (float)(height/scrollSpeed);
v = v - (float)dx / (float)(width/scrollSpeed);

// Make sure u and v stay within [0,1]
if (u < 0)
u = - u;
else if (u > 1)
u = 1 - (u - 1);

if (v < 0)
v = 1 + v;
else if (v > 1)
v = v - 1;

glm::vec4 camOrigin = glm::vec4 (camRadius*sin (PI*u)*sin (2.0f*PI*v),
camRadius*cos (PI*u),
-camRadius*sin (PI*u)*cos (2.0f*PI*v), 1.0);

glm::vec4 center = glm::vec4 (0,0,0,0);
glm::vec4 up = glm::vec4 (0,1,0,0);

if (camControl)
constantBuffer.view = glm::lookAt (glm::vec3 (camOrigin.x, camOrigin.y, camOrigin.z),
glm::vec3 (center.x, center.y, center.z),
glm::vec3 (up.x, up.y, up.z));
else
constantBuffer.view = glm::lookAt (glm::vec3 (0.0f, 0.0f, -1.0f),
glm::vec3 (0.0f,0.0f,1.0f),
glm::vec3 (0.0f,1.0f,0.0f));

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,
nbo, nbosize, isFirstTime, constantBuffer);
cudaGLUnmapBufferObject(pbo);

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

void display(){
runCuda();
runCuda(first);
time_t seconds2 = time (NULL);

if(seconds2-seconds >= 1){
Expand Down Expand Up @@ -183,9 +258,41 @@ void runCuda(){
case(27):
shut_down(1);
break;
case 'o':
case 'O':
outline = !outline;
break;
}
}

void onButtonPress (int button, int state, int x, int y)
{
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
oldX = x; oldY = y;
leftMButtonDown = true;
}
else
{
leftMButtonDown = false;
dx = 0;
dy = 0;
}
}

void onMouseMove (int x, int y)
{
if (leftMButtonDown)
{
dx = -(x - oldX);
dy = -(y - oldY);
}
else
{
dx = 0;
dy = 0;
}
}
#endif

//-------------------------------
Expand Down Expand Up @@ -256,7 +363,7 @@ void initCuda(){
// Clean up on program exit
atexit(cleanupCuda);

runCuda();
runCuda(first);
}

void initTextures(){
Expand Down
6 changes: 5 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ float* cbo;
int cbosize;
int* ibo;
int ibosize;
float *nbo;
int nbosize;

//-------------------------------
//----------CUDA STUFF-----------
Expand All @@ -76,13 +78,15 @@ int main(int argc, char** argv);
//---------RUNTIME STUFF---------
//-------------------------------

void runCuda();
void runCuda(bool &isFirstTime);

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

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