简体   繁体   中英

shrink and grow an object using cos and time in vertex shader

so here is my problem. I have to make a rabbit grow and shrink using a time variable and a cos() I managed to pass the time variable to my vertex shader but I have absolutely no idea how to make the rabbit grow or shrink. I try some formula that I find on the internet but none works I will show the code where I pass my variable time to the vertex shader as well as my vertex shader.

sorry english is not my first language

my animate function :

void MaterialTP2::animate(Node* o, const float elapsedTime) {
Camera * test = Scene::getInstance()->camera(); 
glProgramUniformMatrix4fv(m_ProgramPipeline->getId(), l_View, 1, GL_FALSE, glm::value_ptr(test->getViewMatrix()));
glProgramUniformMatrix4fv(m_ProgramPipeline->getId(), l_Model, 1, GL_FALSE, glm::value_ptr(o->frame()->getModelMatrix()));
glProgramUniformMatrix4fv(m_ProgramPipeline->getId(), l_Proj, 1, GL_FALSE, glm::value_ptr(test->getProjectionMatrix()));
glProgramUniform1f(m_ProgramPipeline->getId(), glGetUniformLocation(vp->getId(), "time"), elapsedTime);
}

here is my vertex shader :

#version 460

#define PI 3.1415926538
uniform mat4 Model;
uniform mat4 View;
uniform mat4 Proj;
uniform float time;

out gl_PerVertex {
    vec4 gl_Position;
    float gl_PointSize;
    float gl_ClipDistance[];
};

layout (location = 0) in vec3 Position;
layout(location = 2) in vec3 normal;
varying vec3 out_color;

void main()
{
float scaleFactor = 0.2 * cos((2 * PI) / time * 100 );
gl_Position = Proj * View * Model  * vec4(Position  ,1.0) ;
out_color = 0.5 * normal  + vec3(0.5, 0.5, 0.5);

} 

the rabbit

You have to scale the vertex coordinate:

float scaleFactor = 0.2 * (cos((2 * PI) / time * 100) + 2.0);
gl_Position = Proj * View * Model * vec4(Position * scaleFactor, 1.0);

However, I suggest to scale the model transformation matrix on the CPU:

glm::mat4 modelMatrix = glm::scale(o->frame()->getModelMatrix(), glm::vec3(scaleFactor));
glProgramUniformMatrix4fv(m_ProgramPipeline->getId(),
    l_Model, 1, GL_FALSE, glm::value_ptr(modelMatrix));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM