It is currently Tue Apr 25, 2017 7:23 pm

All times are UTC - 5 hours




 Page 1 of 1 [ 5 posts ] 
Author Message
 Post subject: Shader VMK 30 - Shaders
PostPosted: Mon Aug 19, 2013 8:17 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
In this video we take a look at what the ShaderManager can do for us when working with shaders. Near the end of the VMK, I present the first challenge to the viewers to see if you can come up with a shader that performs the specific task that I demonstrate in the video.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 30 - Shaders
PostPosted: Wed Aug 21, 2013 10:02 am 

Joined: Tue Aug 14, 2012 10:41 am
Posts: 10
Location: ShangHai, China
Will there be answeres of problems in the next VMK?


Offline
 Profile  
 
 Post subject: Re: Shader VMK 30 - Shaders
PostPosted: Wed Aug 21, 2013 10:19 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
henrya2 wrote:
Will there be answeres of problems in the next VMK?

Would you like to see the solution that I came up with in the next VMK?

You could post your own solution in this forum thread to compare with other members to see how this problem can be solved in different ways.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 30 - Shaders
PostPosted: Wed Aug 21, 2013 12:53 pm 

Joined: Sat Aug 16, 2008 7:58 am
Posts: 447
I like the idea of the challenges. Only, my skill level with OpenGL shaders is very limited. I wouldn't know where to begin to try something like that. To see the solution would be great and after a while as we start working with the shaders I would then be able to try them on my own.


Offline
 Profile  
 
 Post subject: Re: Shader VMK 30 - Shaders
PostPosted: Thu Aug 29, 2013 7:53 pm 

Joined: Sun Sep 23, 2012 11:28 pm
Posts: 1
May not be perfect but here's what I did.

-Created a second set of shaders inputting currentTime and calculate a scale factor from this.
-Ensure the scale factor is positive.
-Render the object twice, with the first using the new shaders.
-Use the scale factor to modify the object size and color.
-Render the object again over top with the original color data.

Vertex shader #2:
#version 330

in vec3 inPosition;

out float exScale;

uniform mat4 MVP;
uniform float currentTime;

void main()
{
float scale = cos(currentTime) * 0.05;
if(scale < 0.0)
    scale *= -1.0;
vec3 newPosition = (1 + scale) * inPosition;
    gl_Position = MVP * vec4(newPosition, 1.0);
    exScale = scale;
}


Fragment #2:
#version 330
precision highp float;

in float exScale;

out vec4 outColor;

void main()
{
   vec3 temp = vec3(1.0,1.0,1.0) / (exScale*50);
   outColor = vec4(temp, 1.0);
}


Render code: (using my own shader manager under MinGW64, just cause I could :) )

void Game::render(float fTimeDifferenceInSeconds) {
   glm::mat4 m4Result = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -1.5f));
   m4Result = _m4Projection * m4Result;

   static float fTime = 0;
   fTime += fTimeDifferenceInSeconds;

   glBindVertexArray(_vao);

   glUseProgram(_SimpleShader2->getProgram());
   glUniformMatrix4fv(_SimpleShader2->getUniformIndex("MVP"), 1, GL_FALSE, &m4Result[0][0]);
   glUniform1f(_SimpleShader2->getUniformIndex("currentTime"), fTime);
   glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, NULL);

   glUseProgram(_SimpleShader->getProgram());
   glUniformMatrix4fv(_SimpleShader->getUniformIndex("MVP"), 1, GL_FALSE, &m4Result[0][0]);
   glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, NULL);

   glBindVertexArray(0);
}


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron