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

Submission: Shuai Shao (Shrek) #3

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
460 changes: 117 additions & 343 deletions README.md

Large diffs are not rendered by default.

Binary file added img/0.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 img/1.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 img/antialiasing_contrast.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 img/antialiasing_test.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 img/car_normal.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 img/car_shade.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 img/car_vertex.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 img/car_wireframe.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 img/chart.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 img/cow_normal_smooth.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 img/cow_normal_unsmooth.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 img/cow_shade_smooth.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 img/cow_vertex.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 img/cow_wireframe.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 img/cow_wireframe_backfaceculling.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 img/earth.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 img/oldcar.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 img/scissor_test.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 img/sponza.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 img/tex_bilinear_filter.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 img/tex_nearest_neighbor.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 img/youtube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9,979 changes: 9,979 additions & 0 deletions objs/bunny.obj

Large diffs are not rendered by default.

2,089 changes: 2,089 additions & 0 deletions objs/teapot.obj

Large diffs are not rendered by default.

166 changes: 164 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
*/

#include "main.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform.hpp"
#include "GLFW/glfw3.h"


//-------------------------------
//-------------MAIN--------------
//-------------------------------




int main(int argc, char **argv) {
if (argc != 2) {
cout << "Usage: [obj file]" << endl;
Expand All @@ -28,19 +35,24 @@ int main(int argc, char **argv) {
frame = 0;
seconds = time(NULL);
fpstracker = 0;

// Launch CUDA/GL
if (init(mesh)) {
// GLFW main loop
mainLoop();
}

delete mesh;
return 0;
}

void mainLoop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();

//MY
setupCamera();

runCuda();

time_t seconds2 = time (NULL);
Expand Down Expand Up @@ -68,28 +80,85 @@ void mainLoop() {
glfwTerminate();
}




//-------------------------------
//---------RUNTIME STUFF---------
//-------------------------------

//translate x,y, angle, scale for keyboard operations
float scale = 0.15f;
//float scale = 0.05f;
float x_trans = 0.0f, y_trans = 0.0f, z_trans = -10.0f;
float x_angle = 0.0f, y_angle = 0.0f;


glm::mat4 M_model;
glm::mat4 M_view;
glm::mat4 M_perspective;

glm::mat4 inv_trans_M_view; //for normal transformation


//lights



void runCuda() {
// 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;

//int xpos, ypos;
//glfwGetMousePos(&xpos, &ypos);

cudaGLMapBufferObject((void **)&dptr, pbo);

vertexShader(M_perspective * M_view * M_model, M_view*M_model, inv_trans_M_view);

rasterize(dptr);



cudaGLUnmapBufferObject(pbo);

frame++;
fpstracker++;

}

//MY
void setupCamera()
{

//model-view
//M_model = glm::mat4(1.0f);
//M_model = glm::translate(M_model, glm::vec3(0, 0, z_trans));

//x_angle, y_angle : radium
M_model = glm::translate(glm::vec3(x_trans, y_trans, z_trans))
* glm::rotate(x_angle, glm::vec3(1.0f, 0.0f, 0.0f))
* glm::rotate(y_angle, glm::vec3(0.0f, 1.0f, 0.0f));

M_view = glm::mat4(1.0f);

//projection
//left, right, bottom, top, near, far
M_perspective = glm::frustum<float>(-scale * ((float)width) / ((float)height),
scale * ((float)width / (float)height),
-scale, scale, 1.0, 1000.0);

inv_trans_M_view = glm::transpose(glm::inverse(M_view * M_model));
}

//-------------------------------
//----------SETUP STUFF----------
//-------------------------------




bool init(obj *mesh) {
glfwSetErrorCallback(errorCallback);

Expand All @@ -107,6 +176,14 @@ bool init(obj *mesh) {
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);

//MY Mouse Control
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetCursorPosCallback(window, mouseMotionCallback);


glfwSetScrollCallback(window,mouseWheelCallback);
/////

// Set up GL context
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
Expand All @@ -124,9 +201,20 @@ bool init(obj *mesh) {
0.0, 0.0, 1.0,
1.0, 0.0, 0.0
};

rasterizeSetBuffers(mesh->getBufIdxsize(), mesh->getBufIdx(),
mesh->getBufPossize() / 3,
mesh->getBufPos(), mesh->getBufNor(), mesh->getBufCol());
mesh->getFaces()->size(),
mesh->getBufPos(), mesh->getBufNor(), mesh->getBufCol()
, (mesh->getTextureCoords())->size() > 0, mesh->getBufTex());


initTextureData(mesh->diffuse_tex.size() > 0, mesh->diffuse_width, mesh->diffuse_height, mesh->diffuse_tex.data(),
mesh->specular_tex.size() > 0, mesh->specular_width, mesh->specular_height, mesh->specular_tex.data(),
mesh->ambient_color,mesh->diffuse_color,mesh->specular_color,mesh->specular_exponent);




GLuint passthroughProgram;
passthroughProgram = initShader();
Expand Down Expand Up @@ -273,4 +361,78 @@ void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_W && action == GLFW_PRESS)
{
changeGeomMode();
}
else if (key == GLFW_KEY_S && action == GLFW_PRESS)
{

changeShaderMode( ) ;
}
else if (key == GLFW_KEY_B && action == GLFW_PRESS)
{

changeBackFaceCulling();
}




}

enum ControlState {NONE=0,ROTATE,TRANSLATE};
ControlState mouseState = NONE;
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
if (action == GLFW_PRESS)
{
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
mouseState = ROTATE;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
mouseState = TRANSLATE;
}

}
else if (action == GLFW_RELEASE)
{
mouseState = NONE;
}
//printf("%d\n", mouseState);
}

double lastx = (double)width / 2;
double lasty = (double)height / 2;
void mouseMotionCallback(GLFWwindow* window, double xpos, double ypos)
{
const double s_r = 0.01;
const double s_t = 0.01;

double diffx = xpos - lastx;
double diffy = ypos - lasty;
lastx = xpos;
lasty = ypos;

if (mouseState == ROTATE)
{
//rotate
x_angle += (float)s_r * diffy;
y_angle += (float)s_r * diffx;
}
else if (mouseState == TRANSLATE)
{
//translate
x_trans += (float)(s_t * diffx);
y_trans += (float)(-s_t * diffy);
}
}

void mouseWheelCallback(GLFWwindow* window, double xoffset, double yoffset)
{
const double s_s = 0.01;

scale += (float)(-s_s * yoffset);
}
4 changes: 4 additions & 0 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ int main(int argc, char **argv);
//-------------------------------

void runCuda();
void setupCamera();

#ifdef __APPLE__
void display();
Expand Down Expand Up @@ -93,3 +94,6 @@ void deleteTexture(GLuint *tex);
void mainLoop();
void errorCallback(int error, const char *description);
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void mouseMotionCallback(GLFWwindow* window, double xpos, double ypos);
void mouseWheelCallback(GLFWwindow* window, double xoffset, double yoffset);
Loading