void deletePBO(GLuint *pbo) { glDeleteBuffers(1, pbo); SDK_CHECK_ERROR_GL(); *pbo = 0; }
int main(){ //Change this line to use your name! yourName = "Paul Gurniak"; // Needed in OSX to force use of OpenGL3.2 glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); init(); initCuda(); SDK_CHECK_ERROR_GL(); initVAO(); initTextures(); GLuint passthroughProgram; passthroughProgram = initShader("shaders/passthroughVS.glsl", "shaders/passthroughFS.glsl"); glUseProgram(passthroughProgram); glActiveTexture(GL_TEXTURE0); // send into GLFW main loop while(1){ display(); if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS || !glfwGetWindowParam( GLFW_OPENED )){ exit(0); } } glfwTerminate(); return 0; }
void Renderer::initPBO() { // initialize the PBO for transferring data from CUDA to openGL uint num_texels = image_width * image_height; uint size_tex_data = sizeof(GLubyte) * num_texels * 4; void *data = malloc(size_tex_data); // test init buffer for (int i=0; i<size_tex_data; i+=4) { uchar *datam = (uchar*)data; datam[i+0] = 0; datam[i+1] = 0; datam[i+2] = 255.0 * i / (float)size_tex_data; datam[i+3] = 255; } // create buffer object glGenBuffers(1, &pbo); glBindBuffer(GL_ARRAY_BUFFER, pbo); glBufferData(GL_ARRAY_BUFFER, size_tex_data, data, GL_DYNAMIC_DRAW); free(data); glBindBuffer(GL_ARRAY_BUFFER, 0); // register this buffer object with CUDA checkCudaErrors(cudaGLRegisterBufferObject(pbo)); SDK_CHECK_ERROR_GL(); // create the texture that we use to visualize the ray-tracing result glActiveTexture(GL_TEXTURE0 + RENDER_TEXTURE); glGenTextures(1, &result_texture); glBindTexture(GL_TEXTURE_2D, result_texture); // set basic parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // buffer data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); SDK_CHECK_ERROR_GL(); // unbind glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0 + UNUSED_TEXTURE); }
void Renderer::render(const Camera& camera, float time) { // calc cam vars glm::vec3 A,B,C; { // camera ray C = glm::normalize(camera.getLookAt()-camera.getPosition()); // calc A (screen x) // calc B (screen y) then scale down relative to aspect // fov is for screen x axis A = glm::normalize(glm::cross(C,camera.getUp())); B = 1.0f/camera.getAspect()*glm::normalize(glm::cross(A,C)); // scale by FOV float tanFOV = tan(glm::radians(camera.getFOV())); A *= tanFOV; B *= tanFOV; } // cuda call unsigned int* out_data; checkCudaErrors(cudaGLMapBufferObject((void**)&out_data, pbo)); if (mode == RAYTRACE) { raytrace1(out_data, image_width, image_height, time, camera.getPosition(), A, B, C, scene_d, sceneSize); } else if (mode == PATHTRACE) { ++filmIters; pathtrace(out_data, image_width, image_height, time, camera.getPosition(), A, B, C, camera.m_lensRadius, camera.m_focalDist, scene_d, sceneSize, rand_d, rays_d, col_d, idx_d, film_d, filmIters); } checkCudaErrors(cudaGLUnmapBufferObject(pbo)); // download texture from destination PBO glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); glActiveTexture(GL_TEXTURE0 + RENDER_TEXTURE); glBindTexture(GL_TEXTURE_2D, result_texture); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glActiveTexture(GL_TEXTURE0 + UNUSED_TEXTURE); SDK_CHECK_ERROR_GL(); fullScreenQuad.display(); }
void CreateVBO(GLuint *vbo, struct cudaGraphicsResource **resource, unsigned int vbo_res_flags) { assert(vbo); // create buffer object glGenBuffers(1, vbo); glBindBuffer(GL_ARRAY_BUFFER, *vbo); // initialize buffer object unsigned int size = MeshWidth * MeshHeight * 4 * sizeof(float); glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // register this buffer object with CUDA checkCudaErrors(cudaGraphicsGLRegisterBuffer(resource, *vbo, vbo_res_flags), exit(0)); SDK_CHECK_ERROR_GL(); }
//////////////////////////////////////////////////////////////////////////////// //! Initialize GL //////////////////////////////////////////////////////////////////////////////// bool initGL(int *argc, char **argv) { glutInit(argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(world_width, world_height); glutCreateWindow("MPI Cuda GL Interop (VBO)"); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMotionFunc(motion); glutTimerFunc(REFRESH_DELAY, timerEvent,0); // initialize necessary OpenGL extensions glewInit(); if (! glewIsSupported("GL_VERSION_2_0 ")) { fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing."); fflush(stderr); return false; } // default initialization glClearColor(0.0, 0.0, 0.0, 1.0); glDisable(GL_DEPTH_TEST); // viewport glViewport(0, 0, world_width, world_height); // projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0); gluPerspective(60.0, (GLfloat)world_width / (GLfloat) world_height, 0.1, 1024.0); //HPV se cambio de 10 a 1024 el ultimo campo ZFar glTranslated(-256, -256, -700); //HPV Se cambia la longitud de alcance de vision con ZFar y tmb el sistema de coordenadas SDK_CHECK_ERROR_GL(); return true; }
//////////////////////////////////////////////////////////////////////////////// //! Create PBO //////////////////////////////////////////////////////////////////////////////// void createPBO(GLuint *pbo, struct cudaGraphicsResource **pbo_resource) { // set up vertex data parameter num_texels = image_width * image_height; num_values = num_texels * 4; size_tex_data = sizeof(GLubyte) * num_values; void *data = malloc(size_tex_data); // create buffer object glGenBuffers(1, pbo); glBindBuffer(GL_ARRAY_BUFFER, *pbo); glBufferData(GL_ARRAY_BUFFER, size_tex_data, data, GL_DYNAMIC_DRAW); free(data); glBindBuffer(GL_ARRAY_BUFFER, 0); // register this buffer object with CUDA checkCudaErrors(cudaGraphicsGLRegisterBuffer(pbo_resource, *pbo, cudaGraphicsMapFlagsNone)); SDK_CHECK_ERROR_GL(); }