NoobsDeSroobs

Half the globe.

Oct 25th, 2014
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. -----------------FRAGMENTSHADER------------------
  2. #version 120
  3.  
  4. uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
  5.  
  6. uniform sampler2D DiffuseTex;
  7.  
  8. uniform vec4 SpecularColor;
  9.  
  10. varying vec3 VSPosition;
  11. varying vec3 VSNormal;
  12. varying vec2 TexCoord;
  13.  
  14. void main() {
  15.         vec3 lightVector = gl_LightSource[0].position.xyz - VSPosition;
  16.     vec3 normalizedLightVector = normalize(lightVector);
  17.     //float distanceFalloff = length(lightVector);
  18.     float diffuseBrightness = max(dot(normalizedLightVector, VSNormal), 0); /// distanceFalloff;
  19.      vec3 diffuseLight = gl_LightSource[0].diffuse.xyz*diffuseBrightness;
  20.  
  21.     float specularity = dot(VSNormal, gl_LightSource[0].halfVector.xyz);
  22.     specularity = pow(specularity, 64);///distanceFalloff;
  23.     vec3 specularLight = gl_LightSource[0].specular.xyz*specularity;//vec3(specularity, 0.0, 0.0);
  24.    
  25.         vec3 shade = gl_LightSource[0].ambient.xyz +
  26.         diffuseLight +
  27.         specularLight;
  28.  
  29.   gl_FragColor = vec4(shade, 1.0)*texture2D(DiffuseTex , TexCoord.st) ;
  30. }
  31.  
  32. --------------------TEXTURELOADER--------------------
  33.  
  34. GLuint loadTex(std::string filename) {
  35.     gli::texture2D Texture(gli::loadStorageDDS(filename));
  36.     GLuint TextureName;
  37.     glGenTextures(1, &TextureName);
  38.     assert(!Texture.empty());
  39.     glBindTexture(GL_TEXTURE_2D, TextureName);
  40.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  41.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GLint(Texture.levels() - 1));
  42.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
  43.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
  44.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
  45.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
  46.     glTexImage2D(GL_TEXTURE_2D,
  47.         0,
  48.         GL_RGB8,
  49.         GLsizei(Texture[0].dimensions().x),
  50.         GLsizei(Texture[0].dimensions().y),
  51.         0,
  52.         GL_BGRA,
  53.         GL_UNSIGNED_BYTE,
  54.         Texture[0].data());
  55.     return TextureName;
  56. }
  57.  
  58. void Texture::Load(const std::string &filename) {
  59.   // Load only if we havent done so already
  60.   if(m_texture_id_ == 0) {
  61.     // skip
  62.     // Insert texture creation here
  63.         m_texture_id_ = loadTex(filename);
  64.     // Construct mipmaps
  65.     glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
  66.   }
  67. }
  68.  
  69.  
  70. //I bind it before I render, but after I activate the shader.
  71.  // Activate the shader
  72.   m_shader_.Activate();
  73.   // Bind the texture and bind it to the uniform in the shader
  74.   glBindTexture(GL_TEXTURE_2D, m_texture_.GetTextureId());
  75.  
  76. //This is how I compute the texCoord for the sphare.
  77. // Compute texture coords
  78.       v.texcoord[0] = 1.0f - theta / (2.0f * pi);
  79.       v.texcoord[1] = phi / pi;
  80.  
  81. //The sphare renderer
  82. void TexturedSphere::render() {
  83.   // Bind VAO and VBO's and call a drawing function
  84.   glBindBuffer(GL_ARRAY_BUFFER, m_vertices_vbo_id_);
  85.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices_vbo_id_);
  86.  
  87.   // Setup the strides, offsets etc.
  88.   glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(0));
  89.   glNormalPointer(GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(12));
  90.   glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(24));
  91.  
  92.   // Enable the above arrays
  93.   glEnableClientState(GL_VERTEX_ARRAY);
  94.   glEnableClientState(GL_NORMAL_ARRAY);
  95.   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  96.  
  97.   // TexturedVertex::BindVertexAttributes(0, 0, 0);
  98.   glDrawElements(GL_TRIANGLES, m_index_count_, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
  99.  
  100.   // Unbind VBOs
  101.   glBindBuffer(GL_ARRAY_BUFFER, 0);
  102.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  103.  
  104.   // Disbale arrays
  105.   glDisableClientState(GL_VERTEX_ARRAY);
  106.   glDisableClientState(GL_NORMAL_ARRAY);
  107.   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  108. }
  109.  
  110. --------------ROTATION----------------
  111. //I am using quaternions to calculate the rotated vertices.
  112. // This function computes the quaternion corresponding to the great circle rotation between two vectors
  113. glm::fquat SimpleViewer::getGreatCircleRotation(glm::vec3 a, glm::vec3 b) {
  114.   glm::vec3 axis = glm::cross(a, b);
  115.   float length = glm::length(axis);
  116.   if(length > std::numeric_limits<float>::epsilon()) {
  117.     axis = axis / length;
  118.  
  119.     float angle = glm::acos(glm::dot(a,b));
  120.     float c = glm::cos(0.5*angle);
  121.     float s = glm::sin(0.5*angle);
  122.     return glm::fquat(c, s*axis[0], s*axis[1], s*axis[2]);
  123.   } else {
  124.     return glm::fquat(1.0f, 0.0f, 0.0f, 0.0f);
  125.   }
  126. }
  127.  
  128. // This function computes the quaternion corresponding to the rotation by a give angle around given axis
  129. glm::fquat SimpleViewer::getAxisAngleRotation(glm::vec3& axis, float angle) {
  130.   float c = glm::cos(0.5f * angle);
  131.   float s = glm::sin(0.5f * angle);
  132.   return glm::fquat(c, s*axis[0], s*axis[1], s*axis[2]);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment