示例#1
0
GLuint Water::createTexture(std::string filename) {
 
  int w, h, n;
  unsigned char* pix;
 
  if(!rx_load_png(rx_to_data_path(filename), &pix, w, h, n)) {
    printf("Error: cannot find: %s\n", filename.c_str());
    return 0;
  }
 
  GLuint tex;
  GLenum format = GL_RGB;
 
  if(n == 4) {
    format = GL_RGBA;
  }
 
  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, format, GL_UNSIGNED_BYTE, pix);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
  delete[] pix;
  pix = NULL;
  return tex;
}
示例#2
0
  int Image::load(std::string filepath) {

    /* validate */
    if (0 == filepath.size()) { 
      RX_ERROR("Error: empty filepath.\n");
      return -1;
    }

    if (false == rx_file_exists(filepath)) { 
      RX_ERROR("File doesn't exist.\n");
      return -2;
    }

    std::string ext = rx_get_file_ext(filepath);
    if (ext == "jpg") {
      type = IMAGE_JPEG;
    }
    else if(ext == "png") {
      type = IMAGE_PNG;
    }
    else {
      RX_ERROR("Unknown extension: %s\n", ext.c_str());
      return -3;
    }

    if (type == IMAGE_PNG) {
      if (!rx_load_png(filepath, &pixels, width, height, channels, &capacity)) {
        RX_ERROR("Cannot load png: %s", filepath.c_str());
        return -4;
      }
    }
    else if(type == IMAGE_JPEG) {
      if (!rx_load_jpg(filepath, &pixels, width, height, channels, &capacity)) {
        RX_ERROR("Cannot load jpg: %s", filepath.c_str());
        return -5;
      }
    }
    else {
      RX_ERROR("Invald image type (shouldn't happen).");
      return -6;
    }

    RX_VERBOSE("Loaded: %s, allocated: %d bytes", filepath.c_str(), capacity);
    return 0;
  }
示例#3
0
bool Image::load(std::string filename, bool datapath) {
  if(pixels) {
    deallocate();
  }
  
  std::string ext = rx_get_file_ext(filename);

  if(ext == "png") {
    return rx_load_png(*this, filename, datapath);
  }
  else if(ext == "jpg") {
    return rx_load_jpg(*this, filename, datapath);
  }
  else if(ext == "tga") {
    return rx_load_tga(*this, filename, datapath);
  }
  else {
    RX_ERROR(ERR_IMG_UNSUPPORTED_EXT);
    return false;
  }

  return false;
}
示例#4
0
bool Water::setup(int w, int h) {
  assert(w && h);

  win_w = w;
  win_h = h;

  // create shader
  vert = rx_create_shader(GL_VERTEX_SHADER, WATER_VS);
  frag = rx_create_shader(GL_FRAGMENT_SHADER, WATER_FS);
  prog = rx_create_program(vert, frag);
  glBindAttribLocation(prog, 0, "a_tex");
  glLinkProgram(prog);
  rx_print_shader_link_info(prog);
  glUseProgram(prog);
  glUniform1i(glGetUniformLocation(prog, "u_tex_pos"),            0);  // VS
  glUniform1i(glGetUniformLocation(prog, "u_tex_norm"),           1);  // VS
  glUniform1i(glGetUniformLocation(prog, "u_tex_texcoord"),       2);  // VS
  //  glUniform1i(glGetUniformLocation(prog, "u_tex_tang"),       3);  // VS
  glUniform1i(glGetUniformLocation(prog, "u_tex_gradient"),       3);  // VS
  glUniform1i(glGetUniformLocation(prog, "u_noise_tex"),          4);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_norm_tex"),           5);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_flow_tex"),           6);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_diffuse_tex"),        7);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_foam_tex"),           8);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_color_tex"),          9);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_extra_flow_tex"),     10);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_foam_delta_tex"),     11);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_foam_colors"),        12);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_foam_ramp"),          13);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_vortex_tex"),         14);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_sand_tex"),           15);  // FS
  glUniform1i(glGetUniformLocation(prog, "u_depth_ramp_tex"),           16);  // FS

  //GLint texture_units = 0;
  //glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &texture_units);
  //printf("Max units: %d\n", texture_units);

  glUniformMatrix4fv(glGetUniformLocation(prog, "u_pm"), 1, GL_FALSE, height_field.pm.ptr());
  glUniformMatrix4fv(glGetUniformLocation(prog, "u_vm"), 1, GL_FALSE, height_field.vm.ptr());

  // load textures
  normals_tex = createTexture("images/water_normals.png");
  flow_tex = createTexture("images/water_flow.png");
  noise_tex = createTexture("images/water_noise.png");
  diffuse_tex = createTexture("images/water_diffuse.png");
  foam_tex = createTexture("images/water_foam.png");
  force_tex0 = createTexture("images/force.png");
  foam_colors_tex = createTexture("images/foam_densities.png");
  foam_ramp_tex = createTexture("images/foam_ramps.png");
  vortex_tex = createTexture("images/vortex.png");
  sand_tex = createTexture("images/sand.png");
  depth_ramp_tex = createTexture("images/depth_ramp.png");
  
  glBindTexture(GL_TEXTURE_2D, foam_colors_tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

  glBindTexture(GL_TEXTURE_2D, foam_ramp_tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

  glBindTexture(GL_TEXTURE_2D, depth_ramp_tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


  unsigned char* img_pix = NULL;
  int img_w, img_h,img_channels = 0;  

  // load diffuse color ramp
  if(!rx_load_png(rx_to_data_path("images/water_color.png"), &img_pix, img_w, img_h, img_channels)) {
    printf("Error: cannot load the water_color.png image.\n");
    return false;
  }

  glGenTextures(1, &color_tex);
  glBindTexture(GL_TEXTURE_1D, color_tex);
  glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, img_w, 0, GL_RGB, GL_UNSIGNED_BYTE, img_pix);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  delete[] img_pix;
  printf("water.color_tex: %d\n", color_tex);
  
  glBindTexture(GL_TEXTURE_2D, flow_tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


  // FBO we used for rendering extra diffuse colors to the water
  glGenFramebuffers(1, &fbo); 
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);

  glGenTextures(1, &extra_flow_tex);
  glBindTexture(GL_TEXTURE_2D, extra_flow_tex);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, win_w, win_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  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);

  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, extra_flow_tex, 0);

  if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    printf("Cannot create the framebuffer for the water diffuse capture.\n");
    return false;
  }
  glBindFramebuffer(GL_FRAMEBUFFER, 0);

  return true;
}