void GLCanvas::drawVBOs(const glm::mat4 &ProjectionMatrix,const glm::mat4 &ViewMatrix,const glm::mat4 &ModelMatrix){
  HandleGLError("enter GlCanvas::drawVBOs()");
  if (!args->intersect_backfacing) {
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
  } else {    
    glDisable(GL_CULL_FACE);
  }
  // prepare data to send to the shaders
  glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
  glm::vec3 lightPos = glm::vec3(4,4,4);
  glUniform3f(GLCanvas::LightID, lightPos.x, lightPos.y, lightPos.z);
  glUniformMatrix4fv(GLCanvas::MatrixID, 1, GL_FALSE, &MVP[0][0]);
  glUniformMatrix4fv(GLCanvas::ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
  glUniformMatrix4fv(GLCanvas::ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
  glUniform1i(GLCanvas::wireframeID, args->wireframe);
  glUniform1i(GLCanvas::colormodeID, 0);
  glUniform1i(GLCanvas::mytexture, /*GL_TEXTURE*/0);
  radiosity->drawVBOs();
  photon_mapping->drawVBOs();
  RayTree::drawVBOs();
  if (args->intersect_backfacing) {
    glDisable(GL_CULL_FACE);
  }
  raytracer->drawVBOs();
  if (args->intersect_backfacing) {
    glEnable(GL_CULL_FACE);
  }
  HandleGLError("leaving GlCanvas::drawVBOs()");
}
Beispiel #2
0
void BoundingBox::setupVBOs() {
  HandleGLError("bounding box setup VBOs enter");
  float thickness = 0.001*glm::length(maximum-minimum);

  glm::vec3& A = minimum;
  glm::vec3& B = maximum;
  glm::vec4 black(0,0,0,1);

  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,A.y,A.z),glm::vec3(A.x,A.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,A.y,B.z),glm::vec3(A.x,B.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,B.y,B.z),glm::vec3(A.x,B.y,A.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,B.y,A.z),glm::vec3(A.x,A.y,A.z),black,black,thickness,thickness);

  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(B.x,A.y,A.z),glm::vec3(B.x,A.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(B.x,A.y,B.z),glm::vec3(B.x,B.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(B.x,B.y,B.z),glm::vec3(B.x,B.y,A.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(B.x,B.y,A.z),glm::vec3(B.x,A.y,A.z),black,black,thickness,thickness);
  
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,A.y,A.z),glm::vec3(B.x,A.y,A.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,A.y,B.z),glm::vec3(B.x,A.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,B.y,B.z),glm::vec3(B.x,B.y,B.z),black,black,thickness,thickness);
  addEdgeGeometry(bb_verts,bb_tri_indices,glm::vec3(A.x,B.y,A.z),glm::vec3(B.x,B.y,A.z),black,black,thickness,thickness);

  glBindBuffer(GL_ARRAY_BUFFER,bb_verts_VBO); 
  glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*bb_verts.size(),&bb_verts[0],GL_STATIC_DRAW); 
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bb_tri_indices_VBO); 
  glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(VBOIndexedTri)*bb_tri_indices.size(),&bb_tri_indices[0],GL_STATIC_DRAW);

  HandleGLError("bounding box setup VBOs finished");
}
void GLCanvas::initialize(ArgParser *_args, Mesh *_mesh, 
				RayTracer *_raytracer, Radiosity *_radiosity, PhotonMapping *_photon_mapping) {

	args = _args;
	mesh = _mesh;
	raytracer = _raytracer;
	radiosity = _radiosity;
	photon_mapping = _photon_mapping;

	// setup glut stuff
	glutInitWindowSize(args->width, args->height);
	glutInitWindowPosition(100,100);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
	glutCreateWindow("OpenGL Viewer");
	HandleGLError("in glcanvas initialize");

#ifdef _WIN32
	GLenum err = glewInit();
	if (err != GLEW_OK) {
			fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
			exit(1);
	}
#endif
	// basic rendering 
	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
	glShadeModel(GL_SMOOTH);
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
	GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
	glCullFace(GL_BACK);
	glDisable(GL_CULL_FACE);

	// Initialize callback functions
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutIdleFunc(idle);

	HandleGLError("finished glcanvas initialize");

	RayTree::initializeVBOs();
	if (radiosity) radiosity->initializeVBOs();
	if (photon_mapping) photon_mapping->initializeVBOs();

	RayTree::setupVBOs();
	if (radiosity) radiosity->setupVBOs();
	if (photon_mapping) photon_mapping->setupVBOs();

	HandleGLError("finished glcanvas initialize");

	// Enter the main rendering loop
	glutMainLoop();
}
void PhotonMapping::initializeVBOs() {
  HandleGLError("enter photonmapping initializevbos()");
  glGenBuffers(1, &photon_direction_verts_VBO);
  glGenBuffers(1, &photon_direction_indices_VBO);
  glGenBuffers(1, &kdtree_verts_VBO);
  glGenBuffers(1, &kdtree_edge_indices_VBO);
  HandleGLError("leave photonmapping initializevbos()");
}
void GLCanvas::setupVBOs(){
  HandleGLError("enter GLCanvas::setupVBOs()");
  bbox.Set(*mesh->getBoundingBox());
  bbox.setupVBOs();
  radiosity->setupVBOs();
  photon_mapping->setupVBOs();
  HandleGLError("leaving GLCanvas::setupVBOs()");
}
Beispiel #6
0
void GLCanvas::InitShaders() {
  std::cout << "initialize shaders" << std::endl;
  CheckForGLSL();
  program = glCreateProgramObjectARB();
  vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
  fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
  HandleGLError("Failed to create program or shader objects");
  glAttachObjectARB(program,vertex_shader);
  glAttachObjectARB(program,fragment_shader);
  HandleGLError("Failed to attach shaders to program");
}
//Draws the buffers
void BoundingBox::drawVBOs(){
	HandleGLError("draw VBOs a ");

	glBindBuffer(GL_ARRAY_BUFFER,bb_verts_VBO);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3,GL_FLOAT,sizeof(VBOPos),BUFFER_OFFSET(0));
	glLineWidth(1);
	glColor3f(0,0,0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bb_edge_indices_VBO);
	HandleGLError("draw VBOS b ");
	glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT,0);
	HandleGLError("draw VBOs c ");
	glDisableClientState(GL_VERTEX_ARRAY);
}
void GLCanvas::display(void) {
	glDrawBuffer(GL_BACK);

	Vec3f bg = mesh->background_color;
	// Clear the display buffer, set it to the background color
	glClearColor(bg.r(),bg.g(),bg.b(),0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Set the camera parameters
	mesh->camera->glInit(args->width, args->height);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	mesh->camera->glPlaceCamera();
	InitLight(); // light will be a headlamp!

	if (args->intersect_backfacing)
		glDisable(GL_CULL_FACE);
	else
		glEnable(GL_CULL_FACE);

	glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);
	
	//	glCallList(display_list_index);
	HandleGLError(); 

	radiosity->drawVBOs();
	photon_mapping->drawVBOs();
	RayTree::drawVBOs();
	 
	// Swap the back buffer with the front buffer to display
	// the scene
	glutSwapBuffers();
}
void PhotonMapping::drawVBOs() {
  HandleGLError("enter photonmapping drawvbos()");

  glUniform1i(GLCanvas::colormodeID, 1);
  if (args->render_photons && photon_direction_verts.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,photon_direction_verts_VBO); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,photon_direction_indices_VBO); 
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)sizeof(glm::vec3) );
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2));
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)));
    glDrawElements(GL_TRIANGLES,
                   photon_direction_indices.size()*3,
                   GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(3);
  }

  if (args->render_kdtree && kdtree_edge_indices.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,kdtree_verts_VBO); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,kdtree_edge_indices_VBO); 
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)sizeof(glm::vec3) );
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2));
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)));
    glDrawElements(GL_TRIANGLES,
                   kdtree_edge_indices.size()*3,
                   GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(3);
  }

  HandleGLError("leave photonmapping drawvbos()");
}
Beispiel #10
0
void CompileProgram(GLcharARB* sourcecode, GLhandleARB *shader) {
  GLint	logLength;
  GLint	compiled;
  glShaderSourceARB(*shader,1,(const GLcharARB **)&sourcecode,0);
  HandleGLError("Failed glShaderSourceARB");
  glCompileShaderARB(*shader);
  HandleGLError("Failed glCompileShaderARB");
  glGetObjectParameterivARB(*shader,GL_OBJECT_COMPILE_STATUS_ARB,&compiled);
  glGetObjectParameterivARB(*shader,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength);
  if (logLength) {
    GLcharARB *log = (GLcharARB *)malloc(logLength+128);
    glGetInfoLogARB(*shader, logLength, &logLength, log);
    printf("Compile log: \n");
    free (log);
  }
  if (!compiled) {
    std::cout << "shader could not compile" << std::endl;
    exit(0);
  }
}
Beispiel #11
0
GLuint GLCanvas::LoadCompileLinkShaders() {
  std::cout << "load, compile, & link shaders" << std::endl;
  std::string v_string = ReadFile("shadows.vs");
  char *v = (char*) v_string.c_str();
  std::string f_string = ReadFile("shadows.fs");
  char *f = (char*) f_string.c_str();
  CompileProgram(v, &vertex_shader);
  CompileProgram(f, &fragment_shader);
  LinkProgram(program);
  HandleGLError("Failed to compile or link shaders");
  return glGetUniformLocationARB(program,"ShadowMap");
}
Beispiel #12
0
inline GLint GetGLUniformLocation(GLProgram program, const char *name)
{
    glGetError();
    GLint uniform = glGetUniformLocation(program.id, name);
    GLenum error = glGetError();
    if (error) {
        log::ErrorLog("Error getting uniform %s from program %d\n", name,
                      program.id);
        HandleGLError(error);
    }

    return uniform;
}
void GLCanvas::initializeVBOs(){
  HandleGLError("enter initilizeVBOs()");
  glGenVertexArrays(1, &render_VAO);
  glBindVertexArray(render_VAO);

  GLCanvas::MatrixID = glGetUniformLocation(GLCanvas::programID, "MVP");
  GLCanvas::LightID = glGetUniformLocation(GLCanvas::programID, "LightPosition_worldspace");
  GLCanvas::ViewMatrixID = glGetUniformLocation(GLCanvas::programID, "V");
  GLCanvas::ModelMatrixID = glGetUniformLocation(GLCanvas::programID, "M");
  GLCanvas::wireframeID = glGetUniformLocation(GLCanvas::programID, "wireframe");
  GLCanvas::colormodeID = glGetUniformLocation(GLCanvas::programID, "colormode");
  // FIXME: texture still buggy
  GLCanvas::mytexture = glGetUniformLocation(GLCanvas::programID, "mytexture");

  bbox.initializeVBOs();
  RayTree::initializeVBOs();
  radiosity->initializeVBOs();
  raytracer->initializeVBOs();
  photon_mapping->initializeVBOs();

  HandleGLError("leaving initilizeVBOs()");
}
Beispiel #14
0
void BoundingBox::drawVBOs() {
  HandleGLError("draw VBOs a ");
  glBindBuffer(GL_ARRAY_BUFFER, bb_verts_VBO);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)sizeof(glm::vec3) );
  glEnableVertexAttribArray(2);
  glVertexAttribPointer(2, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2));
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bb_tri_indices_VBO);
  glDrawElements(GL_TRIANGLES,
                 bb_tri_indices.size()*3,
                 GL_UNSIGNED_INT, 0);
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  glDisableVertexAttribArray(2);
}
Beispiel #15
0
void LinkProgram(GLhandleARB program) {
  GLint	logLength;
  GLint linked;
  glLinkProgramARB(program);
  HandleGLError("Failed glLinkProgramARB");
  glGetObjectParameterivARB(program,GL_OBJECT_LINK_STATUS_ARB,&linked);
  glGetObjectParameterivARB(program,GL_OBJECT_INFO_LOG_LENGTH_ARB,&logLength);
  if (logLength) {
    GLint charsWritten;
    GLcharARB *log = (GLcharARB*)malloc(logLength+128);
    glGetInfoLogARB(program, logLength, &charsWritten, log);
    printf("Link GetInfoLogARB:\n%s\n",log);
    free (log);
  }
  if (!linked)  {
    std::cout << "shader did not link" << std::endl;
    exit(0);
  }
}
//binds data to VBO buffers
void BoundingBox::setupVBOs(){
	HandleGLError("setup VBOs a ");
	VBOPos bb_verts[8];
	VBOIndexedEdge bb_edges[12];

	bb_verts[0] = VBOPos(Vec(minimum.x(),minimum.y(),minimum.z()));
	bb_verts[1] = VBOPos(Vec(minimum.x(),minimum.y(),maximum.z()));
	bb_verts[2] = VBOPos(Vec(minimum.x(),maximum.y(),minimum.z()));
	bb_verts[3] = VBOPos(Vec(minimum.x(),maximum.y(),maximum.z()));
	bb_verts[4] = VBOPos(Vec(maximum.x(),minimum.y(),minimum.z()));
	bb_verts[5] = VBOPos(Vec(maximum.x(),minimum.y(),maximum.z()));
	bb_verts[6] = VBOPos(Vec(maximum.x(),maximum.y(),minimum.z()));
	bb_verts[7] = VBOPos(Vec(maximum.x(),maximum.y(),maximum.z()));

	bb_edges[ 0] = VBOIndexedEdge(0,1);
	bb_edges[ 1] = VBOIndexedEdge(1,3);
	bb_edges[ 2] = VBOIndexedEdge(3,2);
	bb_edges[ 3] = VBOIndexedEdge(2,0);
	bb_edges[ 4] = VBOIndexedEdge(0,4);
	bb_edges[ 5] = VBOIndexedEdge(1,5);
	bb_edges[ 6] = VBOIndexedEdge(2,6);
	bb_edges[ 7] = VBOIndexedEdge(3,7);
	bb_edges[ 8] = VBOIndexedEdge(4,5);
	bb_edges[ 9] = VBOIndexedEdge(5,7);
	bb_edges[10] = VBOIndexedEdge(7,6);
	bb_edges[11] = VBOIndexedEdge(6,4);

	HandleGLError("setup VBOs b ");
	glBindBuffer(GL_ARRAY_BUFFER,bb_verts_VBO);
	HandleGLError("setup VBOs c ");
	glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*8,bb_verts,GL_STATIC_DRAW);
	HandleGLError("setup VBOs d ");
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bb_edge_indices_VBO);
	HandleGLError("setup VBOs e ");
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(VBOIndexedEdge)*12,bb_edges,GL_STATIC_DRAW);
	HandleGLError("setup VBOs bf ");
}
Beispiel #17
0
void Mesh::drawVBOs() {
  // scale it so it fits in the window
  Vec3f center; bbox.getCenter(center);
  float s = 1/bbox.maxDim();
  glScalef(s,s,s);
  glTranslatef(-center.x(),-center.y(),-center.z());

  // setup the light
  Vec3f light_position = LightPosition();
  GLfloat position[4] = { float(light_position.x()),float(light_position.y()),float(light_position.z()),1 };
  glLightfv(GL_LIGHT1, GL_POSITION, position);

  // --------------------------
  // Render Mesh 
  InsertColor(mesh_color);
  glEnable(GL_TEXTURE_2D);
  if (args->glsl_enabled) {
    glUseProgramObjectARB(GLCanvas::program);
    glActiveTexture(GL_TEXTURE0);

    // Height Map
    glActiveTexture(GL_TEXTURE0);
    GLint mapLoc = glGetUniformLocationARB(GLCanvas::program, "terrainMap");
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glUniform1iARB(mapLoc, 0);

    // Normal Map
    glActiveTexture(GL_TEXTURE1);
    GLint normLoc = glGetUniformLocationARB(GLCanvas::program, "normalMap");
    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glUniform1iARB(normLoc, 1);

    // Stone Texture
    glActiveTexture(GL_TEXTURE2);
    GLint stoneLoc = glGetUniformLocationARB(GLCanvas::program, "texStone");
    glBindTexture(GL_TEXTURE_2D, texture[2]);
    glUniform1iARB(stoneLoc, 2);

    // Grass Texture
    glActiveTexture(GL_TEXTURE3);
    GLint grassLoc = glGetUniformLocationARB(GLCanvas::program, "texGrass");
    glBindTexture(GL_TEXTURE_2D, texture[3]);
    glUniform1iARB(grassLoc, 3);

    // Snow Texture
    glActiveTexture(GL_TEXTURE4);
    GLint snowLoc = glGetUniformLocationARB(GLCanvas::program, "texSnow");
    glBindTexture(GL_TEXTURE_2D, texture[4]);
    glUniform1iARB(snowLoc, 4);

    glActiveTexture(GL_TEXTURE0);
  }
  else {
    glBindTexture(GL_TEXTURE_2D, texture[0]);
  }
  DrawMesh();
  if (args->glsl_enabled) {
    glUseProgramObjectARB(0);
  }
  glDisable(GL_TEXTURE_2D);

  DrawPlateVisualization();
  
  // -------------------------
  // Render Light (for debugging)
  glColor3f(1,1,0);
  DrawLight();
    
  HandleGLError(); 
}
void PhotonMapping::setupVBOs() {
  HandleGLError("enter photonmapping setupvbos()");

  photon_direction_verts.clear();
  photon_direction_indices.clear();
  kdtree_verts.clear();
  kdtree_edge_indices.clear();

  // initialize the data
  BoundingBox *bb = mesh->getBoundingBox();
  float max_dim = bb->maxDim();

  if (kdtree == NULL) return;
  std::vector<const KDTree*> todo;  
  todo.push_back(kdtree);
  while (!todo.empty()) {
    const KDTree *node = todo.back();
    todo.pop_back(); 
    if (node->isLeaf()) {

      // initialize photon direction vbo
      const std::vector<Photon> &photons = node->getPhotons();
      int num_photons = photons.size();
      for (int i = 0; i < num_photons; i++) {
	const Photon &p = photons[i];
	glm::vec3 energy = p.getEnergy()*float(args->num_photons_to_shoot);
        glm::vec4 color(energy.x,energy.y,energy.z,1);
	const glm::vec3 &position = p.getPosition();
	glm::vec3 other = position - p.getDirectionFrom()*0.02f*max_dim;
        addEdgeGeometry(photon_direction_verts,photon_direction_indices,
                        position,other,color,color,max_dim*0.0005f,0);
      }

      // initialize kdtree vbo
      float thickness = 0.001*max_dim;
      glm::vec3 A = node->getMin();
      glm::vec3 B = node->getMax();
      glm::vec4 black(1,0,0,1);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,A.y,A.z),glm::vec3(A.x,A.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,A.y,B.z),glm::vec3(A.x,B.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,B.y,B.z),glm::vec3(A.x,B.y,A.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,B.y,A.z),glm::vec3(A.x,A.y,A.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(B.x,A.y,A.z),glm::vec3(B.x,A.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(B.x,A.y,B.z),glm::vec3(B.x,B.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(B.x,B.y,B.z),glm::vec3(B.x,B.y,A.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(B.x,B.y,A.z),glm::vec3(B.x,A.y,A.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,A.y,A.z),glm::vec3(B.x,A.y,A.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,A.y,B.z),glm::vec3(B.x,A.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,B.y,B.z),glm::vec3(B.x,B.y,B.z),black,black,thickness,thickness);
      addEdgeGeometry(kdtree_verts,kdtree_edge_indices,glm::vec3(A.x,B.y,A.z),glm::vec3(B.x,B.y,A.z),black,black,thickness,thickness);

    } else {
      todo.push_back(node->getChild1());
      todo.push_back(node->getChild2());
    } 
  }



  // copy the data to each VBO
  if (photon_direction_verts.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,photon_direction_verts_VBO); 
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(VBOPosNormalColor) * photon_direction_verts.size(),
                 &photon_direction_verts[0],
                 GL_STATIC_DRAW); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,photon_direction_indices_VBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 sizeof(VBOIndexedTri) * photon_direction_indices.size(),
                 &photon_direction_indices[0], GL_STATIC_DRAW);
  }
  if (kdtree_verts.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,kdtree_verts_VBO); 
    glBufferData(GL_ARRAY_BUFFER,
                 sizeof(VBOPosNormalColor) * kdtree_verts.size(),
                 &kdtree_verts[0],
                 GL_STATIC_DRAW); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,kdtree_edge_indices_VBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 sizeof(VBOIndexedTri) * kdtree_edge_indices.size(),
                 &kdtree_edge_indices[0], GL_STATIC_DRAW);
  }

  HandleGLError("leave photonmapping setupvbos()");
}
Beispiel #19
0
void Mesh::drawVBOs() {
  // scale it so it fits in the window
  Vec3f center; bbox.getCenter(center);
  float s = 1/bbox.maxDim();
  glScalef(s,s,s);
  glTranslatef(-center.x(),-center.y(),-center.z());

  // setup the light
  Vec3f light_position = LightPosition();
  GLfloat position[4] = { float(light_position.x()),float(light_position.y()),float(light_position.z()),1 };
  glLightfv(GL_LIGHT1, GL_POSITION, position);
  
  if (args->glsl_enabled) {
  
  
    glEnable(GL_DEPTH_TEST);
    glClearColor(0,0,0,1.0f);
    glEnable(GL_CULL_FACE);
    //glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    
    //BEGIN SHADOW FUN/////////////===================================
    
    //First step: Render from the light POV to a FBO, story depth values only
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboId);  //Rendering offscreen
    
    //Using the fixed pipeline to render to the depthbuffer
    glUseProgramObjectARB(0);
      
    
    // In the case we render the shadowmap to a higher resolution, the viewport must be modified accordingly.
    glViewport(0,0,args->width * SHADOW_MAP_RATIO,args->height* SHADOW_MAP_RATIO);
    
    // Clear previous frame values
    glClear(GL_DEPTH_BUFFER_BIT);
    
    //Disable color rendering, we only want to write to the Z-Buffer
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    
    
    
    //LIGHT POSITION==========================================================================================
    setupMatrices(float(light_position.x()),float(light_position.y()),float(light_position.z()),
            10,10,1);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,args->width/args->height,1.0f,1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((float)light_position.x(),(float)light_position.y(),(float)light_position.z(),0,0,0,0,1,0);
            
    
    // Culling switching, rendering only backface, this is done to avoid self-shadowing
    glCullFace(GL_FRONT);
     
    
    //===========Draw the Things===================================================================================
    InsertColor(floor_color);
    DrawFloor();
    //startTranslate(0,0,0);
    //glutSolidCube(1);
    
    InsertColor(mesh_color);
    glUseProgramObjectARB(GLCanvas::program);
    glColor3b(GLbyte(240-127), GLbyte(184-127), GLbyte(0-127));
    DrawMesh(table, board_tri_verts_VBO);
    DrawPieces(editBoard());

    //endTranslate();
    
    glUseProgramObjectARB(0);
    
    //=============================================================================================================
    
    //Save modelview/projection matrice into texture7, also add a biais
    setTextureMatrix();
    
    
    // Now rendering from the camera POV, using the FBO to generate shadows
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
    
    glViewport(0,0,args->width,args->height);
    
    //Enabling color write (previously disabled for light POV z-buffer rendering)
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    
    // Clear previous frame values
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      
    //Using the shadow shader
    glUseProgramObjectARB(GLCanvas::program);

    glUniform1iARB(shadowMapUniform,7);
    glActiveTextureARB(GL_TEXTURE7);
    glBindTexture(GL_TEXTURE_2D,depthTextureId);
    
    Vec3f cam_position = camera->camera_position;
    Vec3f interest = camera->point_of_interest;
    
    //CAMERA MATRIX=======================================================================================================
    //setupMatrices(cam_pos[0],cam_pos[1],cam_pos[2],interest[0],interest[1],interest[2]);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,args->width/args->height,1.0f,1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((float)cam_position.x(),(float)cam_position.y(),(float)cam_position.z(),(float)interest.x(),(float)interest.y(),(float)interest.z(),0,1,0);
    
    glCullFace(GL_BACK);
    //============================Draw the things
    InsertColor(floor_color);
    DrawFloor();
    //startTranslate(0,0,0);
    //glutSolidCube(1);
    
    InsertColor(mesh_color);
    glUseProgramObjectARB(GLCanvas::program);
    glColor3b(GLbyte(240-127), GLbyte(184-127), GLbyte(0-127));
    DrawMesh(table, board_tri_verts_VBO);
    DrawPieces(editBoard());

    //endTranslate();
    
    glUseProgramObjectARB(0);
    
    //============================All the things
    
    // DEBUG only. this piece of code draw the depth buffer onscreen
    //*
    glUseProgramObjectARB(0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-args->width/2,args->width/2,-args->height/2,args->height/2,1,20);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor4f(1,1,1,1);
    glActiveTextureARB(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,depthTextureId);
    glEnable(GL_TEXTURE_2D);
    glTranslated(0,0,-1);
    glBegin(GL_QUADS);
    glTexCoord2d(0,0);glVertex3f(0,0,0);
    glTexCoord2d(1,0);glVertex3f(args->width/2,0,0);
    glTexCoord2d(1,1);glVertex3f(args->width/2,args->width/2,0);
    glTexCoord2d(0,1);glVertex3f(0,args->height/2,0);
     
    glEnd();
    glDisable(GL_TEXTURE_2D);
    //*/
    
    //glutSwapBuffers();

    glDisable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDepthMask(1);
    
	  //END SHADOW FUN//////////////////////////////////////////////////
  }
  else
  {
    InsertColor(floor_color);
    DrawFloor();
    
    InsertColor(mesh_color);
    glColor3b(GLbyte(240-127), GLbyte(184-127), GLbyte(0-127));

    DrawMesh(table, board_tri_verts_VBO);
    if(args->board_control)
    {
      DrawControlMap();
    }
    DrawPieces(editBoard());
  }
  

  // -------------------------
  // ADDITIONAL VISUALIZATIONS (for debugging)
  glColor3f(1,1,0);
  //DrawLight();
  if (args->bounding_box) {
    glColor3f(0,0,0);
    bbox.drawVBOs();
  }
    
  HandleGLError(); 
}
Beispiel #20
0
void Fluid::setupVBOs() {
  HandleGLError("in setup fluid VBOs");

  fluid_particles.clear();
  fluid_velocity_vis.clear();  
  fluid_face_velocity_vis.clear();
  fluid_pressure_vis.clear();
  fluid_cell_type_vis.clear();

  // =====================================================================================
  // setup the particles
  // =====================================================================================
  for (int x = 0; x < nx; x++) {
    for (int y = 0; y < ny; y++) {
      for (int z = 0; z < nz; z++) {
	Cell *cell = getCell(x,y,z);
	std::vector<FluidParticle*> &particles = cell->getParticles();
	for (unsigned int iter = 0; iter < particles.size(); iter++) {
	  FluidParticle *p = particles[iter];
	  Vec3f v = p->getPosition();
	  fluid_particles.push_back(VBOPos(v));
	}
      }
    }
  }

  // =====================================================================================
  // visualize the velocity
  // =====================================================================================
  if (args->dense_velocity == 0) {
    // one velocity vector per cell, at the centroid
    for (int i = 0; i < nx; i++) {
      for (int j = 0; j < ny; j++) {
	for (int k = 0; k < nz; k++) {
	  Vec3f cell_center((i+0.5)*dx,(j+0.5)*dy,(k+0.5)*dz);
	  Vec3f direction(get_u_avg(i,j,k),get_v_avg(i,j,k),get_w_avg(i,j,k));
	  Vec3f pt2 = cell_center+100*args->timestep*direction;
	  fluid_velocity_vis.push_back(VBOPosColor(cell_center,Vec3f(1,0,0)));
	  fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1)));
	}
      }
    }
  } else if (args->dense_velocity == 1) {
    double z = nz*dz / 2.0;
    for (double x = 0; x <= (nx+0.01)*dx; x+=0.25*dx) {
      for (double y = 0; y <= (ny+0.01)*dy; y+=0.25*dy) {
	Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z));
	Vec3f pt1(x,y,z);
	Vec3f pt2 = pt1 + 100*args->timestep*vel;
	fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0)));
	fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1)));
      } 
    }
  } else if (args->dense_velocity == 2) {
    double y = ny*dy / 2.0;
    for (double x = 0; x <= (nx+0.01)*dx; x+=0.25*dx) {
      for (double z = 0; z <= (nz+0.01)*dz; z+=0.25*dz) {
	Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z));
	Vec3f pt1(x,y,z);
	Vec3f pt2 = pt1 + 100*args->timestep*vel;
	fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0)));
	fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1)));
      }
    } 
  } else if (args->dense_velocity == 3) {
    double x = nx*dx / 2.0;
    for (double y = 0; y <= (ny+0.01)*dy; y+=0.25*dy) {
      for (double z = 0; z <= (nz+0.01)*dz; z+=0.25*dz) {
	Vec3f vel = getInterpolatedVelocity(Vec3f(x,y,z));
	Vec3f pt1(x,y,z);
	Vec3f pt2 = pt1 + 100*args->timestep*vel;
	fluid_velocity_vis.push_back(VBOPosColor(pt1,Vec3f(1,0,0)));
	fluid_velocity_vis.push_back(VBOPosColor(pt2,Vec3f(1,1,1)));
      }
    } 
  }

  // =====================================================================================
  // visualize the face velocity
  // render stubby triangles to visualize the u, v, and w velocities between cell faces
  // =====================================================================================
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      for (int k = 0; k < nz; k++) {
	double dt = args->timestep;
	double u = get_u_plus(i,j,k)*100*dt;
	double v = get_v_plus(i,j,k)*100*dt;
	double w = get_w_plus(i,j,k)*100*dt;
	double x = i*dx;
	double y = j*dy;
	double z = k*dz;
	if (u < -10*dt) {
	  Vec3f pts[5] = { Vec3f(x+dx+u,y+0.5*dy,z+0.5*dz),
			   Vec3f(x+dx,y+0.55*dy,z+0.55*dz),
			   Vec3f(x+dx,y+0.55*dy,z+0.45*dz),
			   Vec3f(x+dx,y+0.45*dy,z+0.45*dz),
			   Vec3f(x+dx,y+0.45*dy,z+0.55*dz) };
	  setupConeVBO(pts,Vec3f(1,0,0),fluid_face_velocity_vis);	  
	} else if (u > 10*dt) {
	  Vec3f pts[5] = { Vec3f(x+dx+u,y+0.5*dy,z+0.5*dz),
			   Vec3f(x+dx,y+0.45*dy,z+0.45*dz),
			   Vec3f(x+dx,y+0.55*dy,z+0.45*dz),
			   Vec3f(x+dx,y+0.55*dy,z+0.55*dz),
			   Vec3f(x+dx,y+0.45*dy,z+0.55*dz) };
	  setupConeVBO(pts,Vec3f(1,0,0),fluid_face_velocity_vis);	  
	}
	if (v < -10*dt) {
	  Vec3f pts[5] = { Vec3f(x+0.5*dx,y+dy+v,z+0.5*dz),
			   Vec3f(x+0.45*dx,y+dy,z+0.45*dz),
			   Vec3f(x+0.55*dx,y+dy,z+0.45*dz),
			   Vec3f(x+0.55*dx,y+dy,z+0.55*dz),
			   Vec3f(x+0.45*dx,y+dy,z+0.55*dz) };
	  setupConeVBO(pts,Vec3f(0,1,0),fluid_face_velocity_vis);	  
	} else if (v > 10*dt) {
	  Vec3f pts[5] = { Vec3f(x+0.5*dx,y+dy+v,z+0.5*dz),
			   Vec3f(x+0.55*dx,y+dy,z+0.55*dz),
			   Vec3f(x+0.55*dx,y+dy,z+0.45*dz),
			   Vec3f(x+0.45*dx,y+dy,z+0.45*dz),
			   Vec3f(x+0.45*dx,y+dy,z+0.55*dz) };
	  setupConeVBO(pts,Vec3f(0,1,0),fluid_face_velocity_vis);	  
	}
	if (w < -10*dt) {
	  Vec3f pts[5] = { Vec3f(x+0.5*dx,y+0.5*dy,z+dz+w),
			   Vec3f(x+0.55*dx,y+0.55*dy,z+dz),
			   Vec3f(x+0.55*dx,y+0.45*dy,z+dz),
			   Vec3f(x+0.45*dx,y+0.45*dy,z+dz),
			   Vec3f(x+0.45*dx,y+0.55*dy,z+dz) };
	  setupConeVBO(pts,Vec3f(0,0,1),fluid_face_velocity_vis);	  
	} else if (w > 10*dt) {
	  Vec3f pts[5] = { Vec3f(x+0.5*dx,y+0.5*dy,z+dz+w),
			   Vec3f(x+0.45*dx,y+0.45*dy,z+dz),
			   Vec3f(x+0.55*dx,y+0.45*dy,z+dz),
			   Vec3f(x+0.55*dx,y+0.55*dy,z+dz),
			   Vec3f(x+0.45*dx,y+0.55*dy,z+dz) };
	  setupConeVBO(pts,Vec3f(0,0,1),fluid_face_velocity_vis);	  
	}
      }
    }
  }

  // =====================================================================================
  // visualize the cell pressure
  // =====================================================================================
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      for (int k = 0; k < nz; k++) {
	Vec3f pts[8] = { Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.1)*dz),
			 Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.9)*dz),
			 Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.1)*dz),
			 Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.9)*dz),
			 Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.1)*dz),
			 Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.9)*dz),
			 Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.1)*dz),
			 Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.9)*dz) };
          double p = getCell(i,j,k)->getPressure();
          p *= 0.1;
          if (p > 1) p = 1;
          if (p < -1) p = -1;
          assert(p >= -1 && p <= 1);
          Vec3f color;
	  if (p < 0) {
            color = Vec3f(1+p,1+p,1);
          } else {
            color = Vec3f(1,1-p,1-p);
          }
	  setupCubeVBO(pts,color,fluid_pressure_vis);
      }
    }
  }

  // =====================================================================================
  // render the MAC cells (FULL, SURFACE, or EMPTY)
  // =====================================================================================
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      for (int k = 0; k < nz; k++) {
	Vec3f pts[8] = { Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.1)*dz),
			 Vec3f((i+0.1)*dx,(j+0.1)*dy,(k+0.9)*dz),
			 Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.1)*dz),
			 Vec3f((i+0.1)*dx,(j+0.9)*dy,(k+0.9)*dz),
			 Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.1)*dz),
			 Vec3f((i+0.9)*dx,(j+0.1)*dy,(k+0.9)*dz),
			 Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.1)*dz),
			 Vec3f((i+0.9)*dx,(j+0.9)*dy,(k+0.9)*dz) };
	Cell *cell = getCell(i,j,k);
	Vec3f color;
	if (cell->getStatus() == CELL_FULL) {
	  color = Vec3f(1,0,0);
	} else if (cell->getStatus() == CELL_SURFACE) {
	  color=Vec3f(0,0,1);
	} else {
	  continue;
	}
	setupCubeVBO(pts,color,fluid_cell_type_vis);
      }
    }
  }

  // cleanup old buffer data (if any)
  cleanupVBOs();

  // copy the data to each VBO
  glBindBuffer(GL_ARRAY_BUFFER,fluid_particles_VBO); 
  glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPos)*fluid_particles.size(),&fluid_particles[0],GL_STATIC_DRAW); 
  if (fluid_velocity_vis.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,fluid_velocity_vis_VBO); 
    glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosColor)*fluid_velocity_vis.size(),&fluid_velocity_vis[0],GL_STATIC_DRAW); 
  }
  if (fluid_face_velocity_vis.size() > 0) {
    glBindBuffer(GL_ARRAY_BUFFER,fluid_face_velocity_vis_VBO); 
    glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_face_velocity_vis.size(),&fluid_face_velocity_vis[0],GL_STATIC_DRAW); 
  }
  glBindBuffer(GL_ARRAY_BUFFER,fluid_pressure_vis_VBO); 
  glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_pressure_vis.size(),&fluid_pressure_vis[0],GL_STATIC_DRAW); 
  glBindBuffer(GL_ARRAY_BUFFER,fluid_cell_type_vis_VBO); 
  glBufferData(GL_ARRAY_BUFFER,sizeof(VBOPosNormalColor)*fluid_cell_type_vis.size(),&fluid_cell_type_vis[0],GL_STATIC_DRAW); 

  HandleGLError("leaving setup fluid");

  // =====================================================================================
  // setup a marching cubes representation of the surface
  // =====================================================================================
  for (int i = 0; i <= nx; i++) {
    for (int j = 0; j <= ny; j++) {
      for (int k = 0; k <= nz; k++) {
	marchingCubes->set(i,j,k,interpolateIsovalue(Vec3f((i-0.5),(j-0.5),(k-0.5))));
      } 
    }
  }
  marchingCubes->setupVBOs();
}
Beispiel #21
0
void Radiosity::drawVBOs() {

  // =====================
  // DRAW ALL THE POLYGONS

  assert ((int)mesh_tri_indices.size() + (int)mesh_textured_tri_indices.size() == num_faces*4);

  // render with Phong lighting?
  if (args->render_mode == RENDER_MATERIALS) {
    // yes
    glUniform1i(GLCanvas::colormodeID, 1);
  } else {
    // no
    glUniform1i(GLCanvas::colormodeID, 0);
  }

  // render untextured faces
  glBindBuffer(GL_ARRAY_BUFFER,mesh_tri_verts_VBO); 
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh_tri_indices_VBO); 
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)sizeof(glm::vec3) );
  glEnableVertexAttribArray(2);
  glVertexAttribPointer(2, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2));
  glEnableVertexAttribArray(3);
  glVertexAttribPointer(3, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)));
  glEnableVertexAttribArray(4);
  glVertexAttribPointer(4, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)*2));
  glDrawElements(GL_TRIANGLES, mesh_tri_indices.size()*3,GL_UNSIGNED_INT, 0);
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  glDisableVertexAttribArray(2);
  glDisableVertexAttribArray(3);
  glDisableVertexAttribArray(4);


  // render faces with textures
  if (mesh_textured_tri_indices.size() > 0) {

    // FIXME: there is something buggy with textures still
    //glUniform1i(GLCanvas::colormodeID, 2);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, GLCanvas::textureID);
    GLCanvas::mytexture = glGetUniformLocation(GLCanvas::programID, "mytexture");
    glUniform1i(GLCanvas::mytexture, /*GL_TEXTURE*/0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh_textured_tri_indices_VBO); 
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor),(void*)sizeof(glm::vec3) );
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2));
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)));
    glEnableVertexAttribArray(4);
    glVertexAttribPointer(4, 3, GL_FLOAT,GL_FALSE,sizeof(VBOPosNormalColor), (void*)(sizeof(glm::vec3)*2 + sizeof(glm::vec4)*2));
    glDrawElements(GL_TRIANGLES, mesh_textured_tri_indices.size()*3, GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);
    glDisableVertexAttribArray(3);
    glDisableVertexAttribArray(4);

    //glUniform1i(GLCanvas::colormodeID, 1);
  }

  HandleGLError(); 
}
Beispiel #22
0
void Radiosity::setupVBOs() {
  HandleGLError("enter radiosity setupVBOs()");
  mesh_tri_verts.clear();
  mesh_tri_indices.clear();
  mesh_textured_tri_indices.clear();

  // initialize the data in each vector
  int num_faces = mesh->numFaces();
  assert (num_faces > 0);
  for (int i = 0; i < num_faces; i++) {
    Face *f = mesh->getFace(i);
    Edge *e = f->getEdge();
    glm::vec3 normal = f->computeNormal();

    double avg_s = 0;
    double avg_t = 0;
    glm::vec3 avg_color(0,0,0);

    int start = mesh_tri_verts.size();

    // wireframe is normally black, except when it's the special
    // patch, then the wireframe is red
    glm::vec4 wireframe_color(0,0,0,0.5);
    if (args->render_mode == RENDER_FORM_FACTORS && i == max_undistributed_patch) {
      wireframe_color = glm::vec4(1,0,0,1);
    }

    // add the 4 corner vertices
    for (int j = 0; j < 4; j++) {
      glm::vec3 pos = ((*f)[j])->get();
      double s = (*f)[j]->get_s();
      double t = (*f)[j]->get_t();
      glm::vec3 color = setupHelperForColor(f,i,j);
      color = glm::vec3(linear_to_srgb(color.r),
                        linear_to_srgb(color.g),
                        linear_to_srgb(color.b));
      avg_color += 0.25f * color;
      mesh_tri_verts.push_back(VBOPosNormalColor(pos,normal,
                                                 glm::vec4(color.r,color.g,color.b,1.0),
                                                 wireframe_color,
                                                 s,t));
      avg_s += 0.25 * s;
      avg_t += 0.25 * t;
      e = e->getNext();
    }

    // the centroid (for wireframe rendering)
    glm::vec3 centroid = f->computeCentroid();
    mesh_tri_verts.push_back(VBOPosNormalColor(centroid,normal,
                                               glm::vec4(avg_color.r,avg_color.g,avg_color.b,1),
                                               glm::vec4(1,1,1,1),
                                               avg_s,avg_t));

    if (f->getMaterial()->hasTextureMap()) {
      mesh_textured_tri_indices.push_back(VBOIndexedTri(start+0,start+1,start+4));
      mesh_textured_tri_indices.push_back(VBOIndexedTri(start+1,start+2,start+4));
      mesh_textured_tri_indices.push_back(VBOIndexedTri(start+2,start+3,start+4));
      mesh_textured_tri_indices.push_back(VBOIndexedTri(start+3,start+0,start+4));
    } else {
      mesh_tri_indices.push_back(VBOIndexedTri(start+0,start+1,start+4));
      mesh_tri_indices.push_back(VBOIndexedTri(start+1,start+2,start+4));
      mesh_tri_indices.push_back(VBOIndexedTri(start+2,start+3,start+4));
      mesh_tri_indices.push_back(VBOIndexedTri(start+3,start+0,start+4));
    }
  }
  assert ((int)mesh_tri_verts.size() == num_faces*5);
  assert ((int)mesh_tri_indices.size() + (int)mesh_textured_tri_indices.size() == num_faces*4);
  
  // copy the data to each VBO
  glBindBuffer(GL_ARRAY_BUFFER,mesh_tri_verts_VBO); 
  glBufferData(GL_ARRAY_BUFFER,
	       sizeof(VBOPosNormalColor) * num_faces * 5,
	       &mesh_tri_verts[0],
	       GL_STATIC_DRAW); 
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh_tri_indices_VBO); 
  glBufferData(GL_ELEMENT_ARRAY_BUFFER,
	       sizeof(VBOIndexedTri) * mesh_tri_indices.size(),
	       &mesh_tri_indices[0], GL_STATIC_DRAW);
  if (mesh_textured_tri_indices.size() > 0) {
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,mesh_textured_tri_indices_VBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 sizeof(VBOIndexedTri) * mesh_textured_tri_indices.size(),
                 &mesh_textured_tri_indices[0], GL_STATIC_DRAW);
    
  }

  HandleGLError("radiosity setupVBOs() just before texture");

  // WARNING: this naive VBO implementation only allows a single texture
  // FIXME: something still buggy about textures
  int num_textured_materials = 0;
  for (unsigned int mat = 0; mat < mesh->materials.size(); mat++) {
    Material *m = mesh->materials[mat];
    if (m->hasTextureMap()) {
      // FIXME: old gl...
      //glBindTexture(GL_TEXTURE_2D,m->getTextureID());
      num_textured_materials++;
    }
  }
  assert (num_textured_materials <= 1);

  HandleGLError("leave radiosity setupVBOs()");
}
Beispiel #23
0
void Radiosity::Paint(ArgParser *args) {
    
    // this offset prevents "z-fighting" bewteen the edges and faces
    // the edges will always win.
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(1.1,4.0);
    
    if (args->render_mode == RENDER_MATERIALS) {
        // draw the faces with OpenGL lighting, just to understand the geometry
        // (the GL light has nothing to do with the surfaces that emit light!)
        for ( int i = 0; i < num_faces; i++) {
            Face *f = mesh->getFace(i);
            Material *m = f->getMaterial();
            
            Vec3f a = (*f)[0]->get();
            Vec3f b = (*f)[1]->get();
            Vec3f c = (*f)[2]->get();
            Vec3f d = (*f)[3]->get();
            Vec3f normal = f->computeNormal();
            glNormal3f(normal.x(),normal.y(),normal.z());
            
            if (!m->hasTextureMap()) {
                Vec3f color = m->getDiffuseColor();
                insertColor(color);
                glBegin (GL_QUADS);
                glVertex3f(a.x(),a.y(),a.z());
                glVertex3f(b.x(),b.y(),b.z());
                glVertex3f(c.x(),c.y(),c.z());
                glVertex3f(d.x(),d.y(),d.z());
                glEnd();
            } else {
                glEnable(GL_TEXTURE_2D);
                glColor3f(1,1,1);
                glBindTexture(GL_TEXTURE_2D,m->getTextureID());
                glBegin (GL_QUADS);
                glTexCoord2d((*f)[0]->get_s(),(*f)[0]->get_t()); 
                glVertex3f(a.x(),a.y(),a.z());
                glTexCoord2d((*f)[1]->get_s(),(*f)[1]->get_t()); 
                glVertex3f(b.x(),b.y(),b.z());
                glTexCoord2d((*f)[2]->get_s(),(*f)[2]->get_t()); 
                glVertex3f(c.x(),c.y(),c.z());
                glTexCoord2d((*f)[3]->get_s(),(*f)[3]->get_t()); 
                glVertex3f(d.x(),d.y(),d.z());
                glEnd();
                glDisable(GL_TEXTURE_2D);	
            }
        }
    } else if (args->render_mode == RENDER_RADIANCE && args->interpolate == true) {
        // interpolate the radiance values with neighboring faces having the same normal
        glDisable(GL_LIGHTING);
        glBegin (GL_QUADS);
        for ( int i = 0; i < num_faces; i++) {
            Face *f = mesh->getFace(i);
            Vec3f a = (*f)[0]->get();
            Vec3f b = (*f)[1]->get();
            Vec3f c = (*f)[2]->get();
            Vec3f d = (*f)[3]->get();
            insertInterpolatedColor(i,f,(*f)[0]);
            glVertex3f(a.x(),a.y(),a.z());
            insertInterpolatedColor(i,f,(*f)[1]);
            glVertex3f(b.x(),b.y(),b.z());
            insertInterpolatedColor(i,f,(*f)[2]);
            glVertex3f(c.x(),c.y(),c.z());
            insertInterpolatedColor(i,f,(*f)[3]);
            glVertex3f(d.x(),d.y(),d.z());
        }
        glEnd();
        glEnable(GL_LIGHTING);
    } else {
        // for all other visualizations, just render the patch in a uniform color
        glDisable(GL_LIGHTING);
        glBegin (GL_QUADS);
        for ( int i = 0; i < num_faces; i++) {
            Face *f = mesh->getFace(i);
            Vec3f color = whichVisualization(args->render_mode,f,i);
            insertColor(color);
            Vec3f a = (*f)[0]->get();
            Vec3f b = (*f)[1]->get();
            Vec3f c = (*f)[2]->get();
            Vec3f d = (*f)[3]->get();
            glVertex3f(a.x(),a.y(),a.z());
            glVertex3f(b.x(),b.y(),b.z());
            glVertex3f(c.x(),c.y(),c.z());
            glVertex3f(d.x(),d.y(),d.z());
        }
        glEnd();
        glEnable(GL_LIGHTING);
    }
    
    if (args->render_mode == RENDER_FORM_FACTORS) {
        // render the form factors of the patch with the most undistributed light
        glDisable(GL_LIGHTING);
        glColor3f(1,0,0);
        Face *t = mesh->getFace(max_undistributed_patch);
        glLineWidth(3);
        glBegin(GL_LINES);
        Vec3f a = (*t)[0]->get();
        Vec3f b = (*t)[1]->get();
        Vec3f c = (*t)[2]->get();
        Vec3f d = (*t)[3]->get();
        glVertex3f(a.x(),a.y(),a.z());
        glVertex3f(b.x(),b.y(),b.z());
        glVertex3f(b.x(),b.y(),b.z());
        glVertex3f(c.x(),c.y(),c.z());    
        glVertex3f(c.x(),c.y(),c.z());    
        glVertex3f(d.x(),d.y(),d.z());    
        glVertex3f(d.x(),d.y(),d.z());    
        glVertex3f(a.x(),a.y(),a.z());
        glEnd();
        glEnable(GL_LIGHTING);
    }
    
    glDisable(GL_POLYGON_OFFSET_FILL); 
    HandleGLError(); 
    
    if (args->wireframe) {
        mesh->PaintWireframe(); 
    }
}
void GLCanvas::initialize(ArgParser *_args) {

  args = _args;

  Load();

  glfwSetErrorCallback(error_callback);

  // Initialize GLFW
  if( !glfwInit() ) {
    std::cerr << "ERROR: Failed to initialize GLFW" << std::endl;
    exit(1);
  }
  
  // We will ask it to specifically open an OpenGL 3.2 context
  glfwWindowHint(GLFW_SAMPLES, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  // Create a GLFW window
  window = glfwCreateWindow(args->width,args->height, "OpenGL viewer", NULL, NULL);
  if (!window) {
    std::cerr << "ERROR: Failed to open GLFW window" << std::endl;
    glfwTerminate();
    exit(1);
  }
  glfwMakeContextCurrent(window);
  HandleGLError("in glcanvas first");

  // Initialize GLEW
  glewExperimental = true; // Needed for core profile
  if (glewInit() != GLEW_OK) {
    std::cerr << "ERROR: Failed to initialize GLEW" << std::endl;
    glfwTerminate();
    exit(1);
  }

  // there seems to be a "GL_INVALID_ENUM" error in glewInit that is a
  // know issue, but can safely be ignored
  HandleGLError("after glewInit()",true);

  std::cout << "-------------------------------------------------------" << std::endl;
  std::cout << "OpenGL Version: " << (char*)glGetString(GL_VERSION) << '\n';
  std::cout << "-------------------------------------------------------" << std::endl;

  // Initialize callback functions
  glfwSetCursorPosCallback(GLCanvas::window,GLCanvas::mousemotionCB);
  glfwSetMouseButtonCallback(GLCanvas::window,GLCanvas::mousebuttonCB);
  glfwSetKeyCallback(GLCanvas::window,GLCanvas::keyboardCB);

  programID = LoadShaders( args->path+"/render.vertexshader",
                           args->path+"/render.fragmentshader" );

  GLCanvas::initializeVBOs();
  GLCanvas::setupVBOs();
  camera->glPlaceCamera(); 

  HandleGLError("finished glcanvas initialize");
}