Answered step by step
Verified Expert Solution
Question
1 Approved Answer
7 week 5, Excerciese 1/1: Splines in a geometry shader O A geometry shader is an optional shader stage between the vertex and the
7 week 5, Excerciese 1/1: Splines in a geometry shader O A geometry shader is an optional shader stage between the vertex and the fragment shader. The geometry shader takes a geometric primitive, consisting of a number of vertices, as input. The shader can then transform the input into a new primitive, consisting of dif- ferent vertices. A good example for using a geometry shader is the interpolation of curves, like Bzier curves. In this exercise you implement a GLSL effect that constructs a cubic Bzier curve when given four control points - the vertices of a quad. After this, you write a vertex shader to animate a cube moving along a "path" constructed using splines. See video and figure 3. You are given a skeleton project that displays a quad. In the geom- etry shader, vertices are generated using GLSL function EmitVertex(), see function genVertex(). First, implement the functions evalBezier(float t) and evalBezier Tan( float t) to return a point on the cubic curve6 at parameter t and its tangent, respectively. Next, make the shader emit vertices along the curve. The amount of segments per curve is defined by the variable uNum. The end results should look similar to the curve in the middle image of figure 3. In the next step we give the curve some some thickness. Generate7 a triangle strip8 of thickness 9 d, instead of a line strip. Implement the vertex shader of shader pass Anim to move the cube along the curve. (1) Activate the Anim shader pass by clicking eye icon next to it. (2) Implement 20 the evalBezier and evalBezierTan func- tions. (3) Implement the translation and scaling transformation functions, T(vec3 t) and S(vec3 s). (4) Construct matrix mr to move the cube according to variablet along the curve, making it face the tangent direction of the center curve. Figure 3: Some control points (vertices of a quad) being trans- formed into a curve and a sur- face, using geometry shaders. Figure 4: Construction of a triangle strip. Each generated vertex (blue) forms a triangle with the last two vertices. The four control points are available as gl_in[i].gl Position with i = 0,1,2,3. 7 In the layout of the geometry shader use triangle-strip instead of line-strip. The construction of the triangle strip is as shown in figure 4- "Hint: Use tangent function to con- struct normal vector n in point p assuming that the curve lies in the z = 0 plane. Generate points p + dn of the triangle strip centered around the curve. *Reuse code from the geometry shader, but modify to use the control points P0, P1, P2, P3 as parameters. Simple (VS) File Code Simple (GS) Simple (PS) #version 330 3 layout (location = 0) in vec3 pos; 4 layout (location = 1) in vec3 normal; 5 6 gout VertexData { vec4 color; vec4 pos; [} OutData; 11 @void main() { 12 OutData.color = vec4(sign (pos.xy), 1.0, 1.0); OutData.pos - vec4 (pos, 1.0); gl_Position = vec4 (pos, 1); X Preview Simple (VS) Simple (GS) File Code #version 330 corel 2 3 layout( lines_adjacency ) in; 4 layout(line_strip, max_vertices=200) out; 5 //layout(triangle_strip, max_vertices=200) out; 6 7pin VertexData{ 8 9 10 } VertexIn[4]; 11 12 13 gout VertexData { 14 vec4 color; 15 ) VertexOut; 16 17 uniform mat4 matP; 18 uniform mat4 matV; 19 uniform mat4 matGeo; 20 21 //uniform int uNum; 22 const int uNum = 10; 23 const float d = 0.07; 24 25 vec4 evalBezier(in float t) 26 ( 27 28 29 } 30 31 vec4 evalBezier Tan (in float t) 32 Q{ 33 34 35 [} 36 vec4 color; vec4 pos; 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 37 //first transforms a vertex, then emits it 38 void genVertex(in vec4 v, in mat4 matMVP) 39 { 40 41 42 L} 43 62 63 64 //TODO return vec4(0); 44 void main() 45 { 46 65 > } Simple (PS) //TODO return vec4(0); gl_Position = matMVP v; EmitVertex(); VertexOut.color=vec4(0.2); mat4 matMVP = matP * matV * matGeo; //TODO //generate the vertices on the control points VertexOut.color = VertexIn[0].color; genVertex(gl_in[0].gl_Position, matMVP); VertexOut.color = VertexIn[1]. color; genVertex(gl_in[1].gl_Position, matMVP); VertexOut.color = VertexIn[2].color; genVertex (gl_in[2].gl_Position, matMVP); VertexOut.color = Vertex In[3].color; genVertex (gl_in[3].gl_Position, matMVP); EndPrimitive(); Simple (VS) File Code Simple (GS) 1 #version 330 2 3 gin VertexData { 4 vec4 color; 5 [} VertexIn; 6 7 8 9 avoid main() { 10 11 L} out vec4 outColor; Simple (PS) out Color = VertexIn.color;
Step by Step Solution
★★★★★
3.43 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
According to the given information the data is collected Objective To create a code for the modification of the geometry shader To modify your geometr...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started