Find the following for a point p(u, v) in terms of u and v.
Solutions:
2) pu and pv are given by:
3) Since d = a u + b v, we have
and
Evaluationg this, we obtain
1) From the given conditions, we have
p(u, v) = ( sin πv cos 2πu, sin πv sin 2πu, cos πv )
Solutions:
The vertex shader that morphs vertices is shown below. We use Vertex2 to pass
in the vertices of the second object and use the built in mix() function
to blend the two vertices.
Your application should provide the blending factor t and Vertex2. The
following is a sample code segment.
//morph.vert: morphing vertex
uniform float t; //value provided by application program
attribute vec4 Vertex2; //vertex of second object
void main(void)
{
//gl_vertex*(1-t) + Vertex2*t
gl_Position = gl_ModelViewProjectionMatrix * mix(gl_Vertex, Vertex2, t );
}
float tValue;
GLuint tParam; //parameters for sending to vertex shader
GLuint Vertex2Param;
.....
void init()
{
....
....
tParam = glGetUniformLocation ( programObject, "t" );
Vertex2Param = glGetAttribLocation ( programObject, "Vertex2" );
}
//arbitrarily create two figures for demo use
void makeFigures( float A[][3], float B[][3] )
{
A[0][0] = 0; A[0][1] = 0; A[1][0] = 3; A[1][1] = 3;
A[2][0] = 6; A[2][1] = 0; A[3][0] = 6; A[3][1] = -6;
A[4][0] = 4; A[4][1] = -6; A[5][0] = 4; A[5][1] = -4;
A[6][0] = 2; A[6][1] = -4; A[7][0] = 2; A[7][1] = -6;
A[8][0] = 0; A[8][1] = -6;
B[0][0] = 0; B[0][1] = 0; B[1][0] = 3; B[1][1] = 0;
B[2][0] = 6; B[2][1] = 0; B[3][0] = 6; B[3][1] = -2;
B[4][0] = 4; B[4][1] = -2; B[5][0] = 4; B[5][1] = -6;
B[6][0] = 2; B[6][1] = -6; B[7][0] = 2; B[7][1] = -2;
B[8][0] = 0; B[8][1] = -2;
}
void display(void)
{
....
....
int N = 9;
GLfloat A[N][3], B[N][3];
makeFigures( A, B );
glBegin ( GL_POLYGON );
for ( int i = 0; i < N; ++i ) {
glUniform1f ( tParam, tValue );
glVertexAttrib3fv ( Vertex2Param, &B[i][0] ); //send second object vertex toshader
glVertex2fv ( A[i] ); //vertex of first object
}
glEnd();
....
}
Solutions:
This is a simple physics problem. Suppose the ground is at y = 0 and initially
the ball is at height y = h0 with zero velocity and the gravitational acceleration is g.
The time required for the
ball to drop through distance h0 is given by
Combining these, we can have vertex shader like the following.
//bounce.vert
uniform float time; //value provided by application program
attribute vec3 vel; //value provided by application program
void main(void)
{
float s = 1000.0; //scale factor
float g = 10.0; //gravitational acceleration
float t;
float h, h0; //h0 = initial height
float t0;
float c = 0.9; //coefficient of restitution
t = time / s; //time in ms
h0 = gl_Vertex.y; //initial height of ball
vec3 norm = vec3 ( 0, 1, 0 );
vec3 vel1, vel2;
t0 = sqrt ( 2 * h0 / g ); //time to reach ground
vel1.x = vel.x;
vel1.y = vel.y - g * t0;
vel1.z = vel.z;
//initial height is gl_vertex.y
h = h0 - g/(2.0)*t*t; //height of ball at time t
while ( h <= 0 ){ //ball should always be above ground
vel2 = c * reflect ( vel1, norm ); //bouncing velocity reduced
t = t - t0;
h = vel2.y * t - g/(2.0) * t * t;
h0 = c * h0; //reduced bouncing height
t0 = 2 * sqrt ( 2 * h0 / g ); //time for one bounce
vel1.y = vel2.y - g * t0;
}
gl_Position = gl_ModelViewProjectionMatrix * vec4 ( 0, h, 0, 1 ); //object_pos;
}
Extra Credit ( 10 points ) Use blender to create a short animated movie ( > 15 seconds ) that shows a 3-D chess piece moving around a chess board. Besides the demo, turn in a copy of the steps you have used to create it.
Make a demo to the instructor before or after a class or during the office hours. Progams must be able to be compiled and run in the machines of the lab.