Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------FRAGMENTSHADER------------------
- #version 120
- uniform gl_LightSourceParameters gl_LightSource[gl_MaxLights];
- uniform sampler2D DiffuseTex;
- uniform vec4 SpecularColor;
- varying vec3 VSPosition;
- varying vec3 VSNormal;
- varying vec2 TexCoord;
- void main() {
- vec3 lightVector = gl_LightSource[0].position.xyz - VSPosition;
- vec3 normalizedLightVector = normalize(lightVector);
- //float distanceFalloff = length(lightVector);
- float diffuseBrightness = max(dot(normalizedLightVector, VSNormal), 0); /// distanceFalloff;
- vec3 diffuseLight = gl_LightSource[0].diffuse.xyz*diffuseBrightness;
- float specularity = dot(VSNormal, gl_LightSource[0].halfVector.xyz);
- specularity = pow(specularity, 64);///distanceFalloff;
- vec3 specularLight = gl_LightSource[0].specular.xyz*specularity;//vec3(specularity, 0.0, 0.0);
- vec3 shade = gl_LightSource[0].ambient.xyz +
- diffuseLight +
- specularLight;
- gl_FragColor = vec4(shade, 1.0)*texture2D(DiffuseTex , TexCoord.st) ;
- }
- --------------------TEXTURELOADER--------------------
- GLuint loadTex(std::string filename) {
- gli::texture2D Texture(gli::loadStorageDDS(filename));
- GLuint TextureName;
- glGenTextures(1, &TextureName);
- assert(!Texture.empty());
- glBindTexture(GL_TEXTURE_2D, TextureName);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, GLint(Texture.levels() - 1));
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
- glTexImage2D(GL_TEXTURE_2D,
- 0,
- GL_RGB8,
- GLsizei(Texture[0].dimensions().x),
- GLsizei(Texture[0].dimensions().y),
- 0,
- GL_BGRA,
- GL_UNSIGNED_BYTE,
- Texture[0].data());
- return TextureName;
- }
- void Texture::Load(const std::string &filename) {
- // Load only if we havent done so already
- if(m_texture_id_ == 0) {
- // skip
- // Insert texture creation here
- m_texture_id_ = loadTex(filename);
- // Construct mipmaps
- glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
- }
- }
- //I bind it before I render, but after I activate the shader.
- // Activate the shader
- m_shader_.Activate();
- // Bind the texture and bind it to the uniform in the shader
- glBindTexture(GL_TEXTURE_2D, m_texture_.GetTextureId());
- //This is how I compute the texCoord for the sphare.
- // Compute texture coords
- v.texcoord[0] = 1.0f - theta / (2.0f * pi);
- v.texcoord[1] = phi / pi;
- //The sphare renderer
- void TexturedSphere::render() {
- // Bind VAO and VBO's and call a drawing function
- glBindBuffer(GL_ARRAY_BUFFER, m_vertices_vbo_id_);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indices_vbo_id_);
- // Setup the strides, offsets etc.
- glVertexPointer(3, GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(0));
- glNormalPointer(GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(12));
- glTexCoordPointer(2, GL_FLOAT, sizeof(TexturedVertex), BUFFER_OFFSET(24));
- // Enable the above arrays
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- // TexturedVertex::BindVertexAttributes(0, 0, 0);
- glDrawElements(GL_TRIANGLES, m_index_count_, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
- // Unbind VBOs
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- // Disbale arrays
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- }
- --------------ROTATION----------------
- //I am using quaternions to calculate the rotated vertices.
- // This function computes the quaternion corresponding to the great circle rotation between two vectors
- glm::fquat SimpleViewer::getGreatCircleRotation(glm::vec3 a, glm::vec3 b) {
- glm::vec3 axis = glm::cross(a, b);
- float length = glm::length(axis);
- if(length > std::numeric_limits<float>::epsilon()) {
- axis = axis / length;
- float angle = glm::acos(glm::dot(a,b));
- float c = glm::cos(0.5*angle);
- float s = glm::sin(0.5*angle);
- return glm::fquat(c, s*axis[0], s*axis[1], s*axis[2]);
- } else {
- return glm::fquat(1.0f, 0.0f, 0.0f, 0.0f);
- }
- }
- // This function computes the quaternion corresponding to the rotation by a give angle around given axis
- glm::fquat SimpleViewer::getAxisAngleRotation(glm::vec3& axis, float angle) {
- float c = glm::cos(0.5f * angle);
- float s = glm::sin(0.5f * angle);
- return glm::fquat(c, s*axis[0], s*axis[1], s*axis[2]);
- }
Advertisement
Add Comment
Please, Sign In to add comment