Advertisement
Guest User

random noise texture opengl

a guest
Apr 30th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include "graphics_framework.h"
  2. #include <glm/glm.hpp>
  3. #include <glm/gtx/rotate_vector.hpp>
  4. using namespace std;
  5. using namespace graphics_framework;
  6. using namespace glm;
  7.  
  8. target_camera cam;
  9.  
  10. GLuint noise_texture;
  11. const int noise_width = 512;
  12. const int noise_height = noise_width;
  13. GLfloat noise_data[noise_height * noise_width];
  14.  
  15. mesh example_sphere;
  16. effect sphere_eff;
  17.  
  18. bool load_content() {
  19.   example_sphere = mesh(geometry_builder::create_sphere());
  20.   sphere_eff.add_shader("tuisku/shader.vert", GL_VERTEX_SHADER);
  21.   sphere_eff.add_shader("tuisku/shader.frag", GL_FRAGMENT_SHADER);
  22.   sphere_eff.build();
  23.   cam.set_position(vec3(5.0f, 5.0f, 5.0f));
  24.   cam.set_target(vec3(0.0f, 0.0f, 0.0f));
  25.   auto aspect = static_cast<float>(renderer::get_screen_width()) / static_cast<float>(renderer::get_screen_height());
  26.   cam.set_projection(1.0472f, aspect, 2.414f, 1000.0f);
  27.  
  28.   for (auto& f : noise_data)
  29.   {
  30.     //Please don't do random numbers like this, but it works
  31.     f = (float)rand() / (float)RAND_MAX;
  32.   }
  33.   //Creat the texture
  34.   glGenTextures(1, &noise_texture);
  35.   //Bind it
  36.   glBindTexture(GL_TEXTURE_2D, noise_texture);
  37.   //Send the Data
  38.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, noise_width, noise_height, 0, GL_RED, GL_FLOAT, noise_data);
  39.   //Set the parameters
  40.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  41.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  42.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  43.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  44.  
  45.   return true;
  46. }
  47.  
  48. bool update(float delta_time) {
  49.   example_sphere.get_transform().rotate(vec3(0, delta_time, 0));
  50.   cam.update(delta_time);
  51.   return true;
  52. }
  53.  
  54. bool render() {
  55.   renderer::bind(sphere_eff);
  56.  
  57.   auto V = cam.get_view();
  58.   auto P = cam.get_projection();
  59.   auto MVP_sphere = P * V * example_sphere.get_transform().get_transform_matrix();
  60.   glUniformMatrix4fv(sphere_eff.get_uniform_location("MVP"), 1, GL_FALSE, value_ptr(MVP_sphere));
  61.  
  62.   //------------------------------------------
  63.   // Set active texture unit (unit 0)
  64.   glActiveTexture(GL_TEXTURE0);
  65.   // Bind texture to that unit (unit 0)
  66.   glBindTexture(GL_TEXTURE_2D, noise_texture);
  67.   // Set tex uniform to that unit (unit 0)
  68.   glUniform1i(sphere_eff.get_uniform_location("tex"), 0);
  69.   //------------------------------------------
  70.   //^that is the equivilent of doing renderer::bind(noise_texture, 0), but with pure opengl.
  71.  
  72.   renderer::render(example_sphere);
  73.  
  74.   return true;
  75. }
  76.  
  77. int main() {
  78.  
  79.   app application("", renderer::windowed);
  80.   application.set_load_content(load_content);
  81.   application.set_update(update);
  82.   application.set_render(render);
  83.   application.run();
  84.   return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement