예제 #1
0
void marching_cubes_update() {

  int size[3] = {width, height, depth};
  
  /* Update volumes */
  
  kernel_memory_gl_aquire(metaball_positions);
  kernel_memory_gl_aquire(vertex_positions_buffer);
  kernel_memory_gl_aquire(vertex_normals_buffer);
  
  kernel_set_argument(write_metaballs, 1, sizeof(cl_int3), &size);
  kernel_set_argument(write_metaballs, 2, sizeof(kernel_memory), &metaball_positions);
  kernel_set_argument(write_metaballs, 3, sizeof(cl_int), &num_metaballs);
  kernel_run(write_metaballs, width * height * depth);
  
  /* End */
  
  int zero = 0;
  kernel_memory_write(vertex_index, sizeof(int), &zero);
  
  const int num_workers = (width-1) * (height-1) * (depth-1);
  
  kernel_set_argument(construct_surface, 0, sizeof(kernel_memory), &volume);
  kernel_set_argument(construct_surface, 1, sizeof(cl_int3), &size);
  kernel_set_argument(construct_surface, 2, sizeof(kernel_memory), &vertex_positions_buffer);
  kernel_set_argument(construct_surface, 3, sizeof(kernel_memory), &vertex_index);
  kernel_run(construct_surface, num_workers);
  
  kernel_memory_read(vertex_index, sizeof(cl_int), &num_verts);
  
  /* Generate Normals */
  
  if (num_verts > 0) {
    kernel_set_argument(generate_normals_smooth, 0, sizeof(kernel_memory), &vertex_positions_buffer);
    kernel_set_argument(generate_normals_smooth, 1, sizeof(kernel_memory), &vertex_normals_buffer);
    kernel_set_argument(generate_normals_smooth, 2, sizeof(kernel_memory), &metaball_positions);
    kernel_set_argument(generate_normals_smooth, 3, sizeof(cl_int), &num_metaballs);
    kernel_run(generate_normals_smooth, num_verts);
  }
  
  /*
  kernel_set_argument(generate_normals, 0, sizeof(kernel_memory), &vertex_positions_buffer);
  kernel_set_argument(generate_normals, 1, sizeof(kernel_memory), &vertex_normals_buffer);
  kernel_run(generate_normals, num_verts/3);
  */
  
  kernel_memory_gl_release(vertex_positions_buffer);
  kernel_memory_gl_release(vertex_normals_buffer);
  kernel_memory_gl_release(metaball_positions);
  
  kernel_run_finish();
  
}
예제 #2
0
void marching_cubes_render(bool wireframe, camera* c, light* l) {
  
  const int full_size = width * height * depth;
  
  kernel_memory_gl_aquire(point_color_buffer);
  kernel_run(write_point_color_back, full_size);
  kernel_memory_gl_release(point_color_buffer);
  
  /* Before we do the metaballs lets just do a rendering based upon volume data */
  
  /*
  
  glPointSize(1.0f);
  
  glBindBuffer(GL_ARRAY_BUFFER, point_positions);
  glVertexPointer(4, GL_FLOAT, 0, (void*)0);
  glEnableClientState(GL_VERTEX_ARRAY);
  
  glBindBuffer(GL_ARRAY_BUFFER, point_colors);
  glColorPointer(4, GL_FLOAT, 0, (void*)0);
  glEnableClientState(GL_COLOR_ARRAY);
  
  glDrawArrays(GL_POINTS, 0, full_size);
  
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);
  glPointSize(1.0f);
  
  */
  
  /* Then Draw Triangles */
  if (wireframe) {
    glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  }
  
  
  material* metaballs_mat = asset_get_load(P("./shaders/metaballs.mat"));
  
  shader_program* metaballs = material_get_entry(metaballs_mat, 0)->program;
  GLuint NORMALS = glGetAttribLocation(*metaballs, "normals");
  
  glUseProgram(*metaballs);
  
  GLint light_position_u = glGetUniformLocation(*metaballs, "light_position");
  glUniform3f(light_position_u, l->position.x, l->position.y, l->position.z);
  
  GLint camera_position_u = glGetUniformLocation(*metaballs, "camera_position");
  glUniform3f(camera_position_u, c->position.x, c->position.y, c->position.z);
  
  mat4 lviewm = light_view_matrix(l);
  mat4 lprojm = light_proj_matrix(l);
  
  mat4_to_array(lviewm, lview_matrix);
  mat4_to_array(lprojm, lproj_matrix);
  
  GLint lproj_matrix_u = glGetUniformLocation(*metaballs, "light_proj");
  glUniformMatrix4fv(lproj_matrix_u, 1, 0, lproj_matrix);
  
  GLint lview_matrix_u = glGetUniformLocation(*metaballs, "light_view");
  glUniformMatrix4fv(lview_matrix_u, 1, 0, lview_matrix);
  
  
  texture* env_map = asset_get_load(P("./resources/metaballs_env.dds"));
  glActiveTexture(GL_TEXTURE0 + 0 );
  glBindTexture(GL_TEXTURE_2D, *env_map);
  glEnable(GL_TEXTURE_2D);
  glUniform1i(glGetUniformLocation(*metaballs, "env_map"), 0);
  
  texture* shadow_map = shadow_mapper_depth_texture();
  glActiveTexture(GL_TEXTURE0 + 1);
  glBindTexture(GL_TEXTURE_2D, *shadow_map);
  glEnable(GL_TEXTURE_2D);
  glUniform1i(glGetUniformLocation(*metaballs, "shadow_map"), 1);
  
  glBindBuffer(GL_ARRAY_BUFFER, vertex_positions);
  glVertexPointer(4, GL_FLOAT, 0, (void*)0);
  glEnableClientState(GL_VERTEX_ARRAY);
  
  glBindBuffer(GL_ARRAY_BUFFER, vertex_normals);
  glVertexAttribPointer(NORMALS, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
  glEnableVertexAttribArray(NORMALS);
  
    glDrawArrays(GL_TRIANGLES, 0, num_verts);
  
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableVertexAttribArray(NORMALS);
  
  glActiveTexture(GL_TEXTURE0 + 1 );
  glDisable(GL_TEXTURE_2D);
  
  glActiveTexture(GL_TEXTURE0 + 0 );
  glDisable(GL_TEXTURE_2D);
  
  glUseProgram(0);
  
  if (wireframe) {
    glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  }
  
}
예제 #3
0
void particles_update(float timestep) {
  
  int random = rand();
  
#ifndef CPU_ONLY
  
  #ifndef OPEN_GL_CPU
    kernel_memory_gl_aquire(k_particle_positions);
    kernel_memory_gl_aquire(k_particle_velocities);
    kernel_memory_gl_aquire(k_particle_lifetimes);
    kernel_memory_gl_aquire(k_particle_randoms);
  #endif
    
      kernel_set_argument(k_update, 6, sizeof(cl_float), &timestep);
      kernel_set_argument(k_update, 7, sizeof(cl_int), &reset);
      kernel_set_argument(k_update, 8, sizeof(cl_int), &random);
      kernel_run(k_update, particle_count);
      
      reset = 0;
    
  #ifndef OPEN_GL_CPU
    kernel_memory_gl_release(k_particle_positions);
    kernel_memory_gl_release(k_particle_velocities);
    kernel_memory_gl_release(k_particle_lifetimes);
    kernel_memory_gl_release(k_particle_randoms);
  #endif
    
    kernel_run_finish();
  
#else
  
  for(int i = 0; i < particle_count; i++) {
  
    particle_lifetimes[i] = particle_lifetimes[i] + timestep;
    
    if ((particle_lifetimes[i] > 60.0) || ( v4_length(particle_velocities[i]) < 0.5 )) {
      
      particle_lifetimes[i] = 0.0;
      
      particle_positions[i] = v4(32,15,32,1);
      
      int random_index = (random + i) % particle_count;
      float rx = particle_randoms[random_index].x;
      float ry = particle_randoms[random_index].y;
      float rz = particle_randoms[random_index].z;
      particle_velocities[i] = v4_mul(v4(rx, ry, rz, 0), 5);
    
    } else {
    
      /* Update positions and velocity */
      particle_positions[i].x = particle_positions[i].x + (particle_velocities[i].x * timestep);
      particle_positions[i].y = particle_positions[i].y + (particle_velocities[i].y * timestep);
      particle_positions[i].z = particle_positions[i].z + (particle_velocities[i].z * timestep);
      
      particle_velocities[i].y = particle_velocities[i].y - (9.81 * timestep);
      
      /* Bounce on floors */
      if (particle_positions[i].y < 15.0) {
        particle_velocities[i].x = particle_velocities[i].x * 0.75;
        particle_velocities[i].y = particle_velocities[i].y * 0.75;
        particle_velocities[i].z = particle_velocities[i].z * 0.75;
        particle_velocities[i].y = -particle_velocities[i].y;
      }
    }
  }

#endif
  
}