TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en...

60
Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual -- ETAPA GEOMETRÍA’12/13 TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL Curso 2013 / 14 Procesadores Gráficos y Aplicaciones en Tiempo Real Profesores: David Miraut y Óscar D. Robles © GMRV 2005-2014 – Febrero 2014

Transcript of TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en...

Page 1: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/141/60

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual -- ETAPA GEOMETRÍA’12/13

TEMA 2.3.1ETAPA DE GEOMETRÍA

EJEMPLOS CON OPENGL

Curso 2013 / 14

Procesadores Gráficos y Aplicaciones en Tiempo RealProfesores: David Miraut y Óscar D. Robles

© GMRV 2005-2014 – Febrero 2014

Page 2: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/142/60

Índice

• Shaders que dejan pasar los datos• Curva de Bézier• Vista alámbrica sólida• Visualización de normales• Explosión• Siluetas Ejemplos tomados de la

asignatura AdvancedTopics in Graphics delDr. Burton -- CSE4431(Univ. York)

Page 3: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/143/60

Pass-through Shader

• a simple geometry shader that does nothing but pass information from the vertex shaderthrough to the fragment shader– vertex shader performs per vertex lighting

#version 330#extension GL_EXT_geometry_shader4: enable

layout (triangles) in;layout (triangle_strip, max_vertices = 3) out;

in vec4 vColor[3];out vec4 gColor;

Page 4: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/144/60

Pass-through Shader

#version 330#extension GL_EXT_geometry_shader4: enable

layout (triangles) in;layout (triangle_strip, max_vertices = 3) out;

in vec4 vColor[3];out vec4 gColor;

input primitives are triangles → 3 per vertex colors as input

you need to enable this extension to use some of the built-in variables

output one color per vertex

Page 5: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/145/60

Pass-through Shader

void main(void){for (int i = 0; i < gl_VerticesIn; i++){gl_Position = gl_PositionIn[i];gColor = vColor[i];EmitVertex();

}EndPrimitive();

}

for each vertex in the primitive

set the position and color of thevertex and emit the vertex

Page 6: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/146/60

Bezier Curve Shader

•• a Bezier curve is a smooth curve defined using n points• the Bezier curve defined using 4 points has the

parametric form

• the curve starts at P0 heading towards the direction of P1 and finishes at P3 coming from the direction of P2– the curve usually does not pass through P1 and P2

10)1(3)1(3)1()( 3

32

21

20

3

≤≤+−+−+−=

tPtPttPttPttP

Bernstein polynomials

Page 7: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/147/60

Bezier Curve Shader

Page 8: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/148/60

Bezier Curve Shader

• the shader takes as input– 4 vertices as a line adjacency primitive– a uniform variable that defines the number of line

segments used to represent the Bezier curve

• it outputs the curve as a line strip with n line segments– parametric form makes this very easy to do

Page 9: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/149/60

Bezier Curve Shader

n = 4

n = 8

Page 10: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1410/60

Bezier Curve Shader

#version 330#extension GL_EXT_geometry_shader4: enable

uniform int uNum;

layout (lines_adjacency) in;layout (line_strip, max_vertices = 1024) out;

Page 11: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1411/60

Bezier Curve Shader

void main( ){float dt = 1. / float(uNum);float t = 0.;for( int i = 0; i <= uNum; i++ ){float omt = 1. - t;float omt2 = omt * omt;float omt3 = omt * omt2;float t2 = t * t;float t3 = t * t2;vec4 xyzw = omt3 * gl_PositionIn[0].xyzw +

3. * t * omt2 * gl_PositionIn[1].xyzw +3. * t2 * omt * gl_PositionIn[2].xyzw +t3 * gl_PositionIn[3].xyzw;

gl_Position = xyzw;EmitVertex( );t += dt;

}}

Page 12: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1412/60

Solid Wireframe• the standard method of rendering an object as

a solid wireframe is described in Chapter 6 of the “OpenGL Programming Guide” (red book)– render the object twice, first as a solid using a

small polygon offset then as an unlit wireframe– polygon offset needed to avoid

“z fighting”

“Solid Wireframe”, NVIDIA Whitepaper, Feb 2007

Page 13: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1413/60

Solid Wireframe

• a one-pass algorithm is possible using the geometry shader

• this geometry shader works in screen coordinates– computes the distance between a vertex and its

opposite edge on a triangle– outputs the three distances as a vector called the

edge-distance vector• the rasterizer interpolates the edge-distance

vectors to generate the correct distances (in pixels) between a fragment and each edge

Page 14: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1414/60

Solid Wireframe

Page 15: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1415/60

Solid Wireframe

edge-distance vector for A is (ha, 0, 0)

Page 16: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1416/60

Solid Wireframe

• the fragment shader takes as input an edge-distance vector– finds the minimum of the edge distances

• if the distance is less than the line width the shadermixes the fragment color with the line color

• done with a smoothstep to reduce aliasing

• fragment shader also computes lighting (but this could also be done in the vertex shader)

Page 17: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1417/60

Solid Wireframe

• vertex shader just performs the usual transformations

#version 330

in vec4 aVertex;in vec4 aNormal;

out vec4 vPosition; // in eye coordinatesout vec4 vNormal; // in eye coordinates

uniform mat4 uModelViewProjectionMatrix;uniform mat4 uModelViewMatrix;uniform mat4 uNormalMatrix;

void main(void){

vPosition = uModelViewMatrix * aVertex;vNormal = normalize(uNormalMatrix * aNormal);gl_Position = uModelViewProjectionMatrix * aVertex;

}

Page 18: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1418/60

Solid Wireframe

• geometry shader computes edge-distance vector

#version 330#extension GL_EXT_geometry_shader4: enable

layout( triangles ) in;layout( triangle_strip, max_vertices = 3 ) out;

out vec4 gNormal; // in eye coordinatesout vec4 gPosition; // in eye coordinates

noperspective out vec3 gEdgeDistance;

in vec4 vNormal[];in vec4 vPosition[];

// viewport dimensions; should be uniforms supplied by applicationconst int viewWidth = 512;const int viewHeight = 512;

noperspective instructs the rasterizerto perform bilinear interpolation withoutaccounting for perspective projection

Page 19: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1419/60

Solid Wireframe

void main(){// we need the window coordinates of the incoming vertexfloat sx = viewWidth / 2.;float sy = viewHeight / 2.;mat3 viewportMatrix = mat3(sx, 0., 0., 0., sy, 0., sx, sy, 1.);

// transform each vertex into viewport spacevec2 p0 = vec2(viewportMatrix *

vec3(gl_PositionIn[0].xy / gl_PositionIn[0].w, 1.));vec2 p1 = vec2(viewportMatrix *

vec3(gl_PositionIn[1].xy / gl_PositionIn[1].w, 1.));vec2 p2 = vec2(viewportMatrix *

vec3(gl_PositionIn[2].xy / gl_PositionIn[2].w, 1.));

Page 20: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1420/60

Solid Wireframe

// find the altitudes (ha, hb and hc)float a = length(p1 - p2);float b = length(p2 - p0);float c = length(p1 - p0);float alpha = acos( (b*b + c*c - a*a) / (2.0*b*c) );float beta = acos( (a*a + c*c - b*b) / (2.0*a*c) );float ha = abs( c * sin( beta ) );float hb = abs( c * sin( alpha ) );float hc = abs( b * sin( alpha ) );

Page 21: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1421/60

Solid Wireframe

// send the triangle along with the edge distancesgEdgeDistance = vec3( ha, 0, 0 );gNormal = vNormal[0];gPosition = vPosition[0];gl_Position = gl_PositionIn[0];EmitVertex();

gEdgeDistance = vec3( 0, hb, 0 );gNormal = vNormal[1];gPosition = vPosition[1];gl_Position = gl_PositionIn[1];EmitVertex();

Page 22: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1422/60

Solid Wireframe

gEdgeDistance = vec3( 0, 0, hc );gNormal = vNormal[2];gPosition = vPosition[2];gl_Position = gl_PositionIn[2];EmitVertex();

EndPrimitive();}

Page 23: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1423/60

Solid Wireframe

• fragment shader uses the interpolated depth vector to determine if the fragment should be shaded as a line

#version 330

in vec4 gNormal;in vec4 gPosition;noperspective in vec3 gEdgeDistance;

out vec4 fFragColor;

// constants; would normally be uniformsconst float lineWidth = 1;const vec3 lineColor = vec3(0., 0., 1.);

Page 24: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1424/60

Solid Wireframe

vec3 ADSLightModel(in vec3 myNormal, in vec3 myPosition) { // not shown }

void main(void){

vec3 color = ADSLightModel(normalize(gNormal.xyz), gPosition.xyz);

// distance to nearest edgefloat d = min(gEdgeDistance.x, gEdgeDistance.y);d = min(d, gEdgeDistance.z);

// mix line color and lighting colorfloat f = smoothstep(lineWidth - 1, lineWidth + 1, d);fFragColor.rgb = mix(lineColor, color, f);fFragColor.a = 1.;

}

Page 25: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1425/60

Solid Wireframe

Page 26: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1426/60

Solid Wireframe

• there is a potential problem when a triangle is visible and one or two of its vertices are behind the eye– situation is uncommon but might occur in some

applications– solution is to have the geometry shader provide the

edge directions to the fragment shader and allow the fragment shader to compute the edge distances

• see link below for details• http://developer.download.nvidia.com/whitepapers/2007/SD

K10/SolidWireframe.pdf

Page 27: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1427/60

Visualizing Normal Vectors

• textbook calls this a “hedgehog plot” but (I think) that this is a misuse of the term– a hedgehog plot is used to visualize vector fields

http://classes.soe.ucsc.edu/cmps161/Winter08/projects/asopenap/index.html

Page 28: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1428/60

Visualizing Normal Vectors

• idea is to render a line in the direction of the normal vector at each vertex

• at locations other than a vertex the normalsare interpolated

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 29: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1429/60

Visualizing Normal Vectors

• one way to achieve this effect– geometry shader takes as input triangles and outputs lines– object must be rendered twice

• once for the triangles (does not require the geometry shader)• once for the lines (requires the geometry shader)

• another way– geometry shader takes as input triangles and outputs

triangles• lines are drawn as tubes made up of triangles• the input triangle can be passed through as is

– object is rendered once

Page 30: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1430/60

Visualizing Normal Vectors

• look at a slightly simpler version of the problem– draw one normal vector per vertex and one normal

vector at the centroid of the triangle• vertex shader is strictly passthrough#version 330in vec4 aVertex;in vec3 aNormal;

out vec3 vNormal;

void main(void){gl_Position = aVertex;vNormal = aNormal;

}

Page 31: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1431/60

Visualizing Normal Vectors

• geometry shader accepts as input a triangle and outputs 4 line strips

• also needs to apply the modelview projection transformation

Page 32: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1432/60

Visualizing Normal Vectors

• geometry shader

#version 330#extension GL_EXT_geometry_shader4 : enable

layout (triangles) in;layout (line_strip, max_vertices = 8) out;

in vec3 vNormal[3];

uniform uModelViewProjectionMatrix;

uniform float uNormalLength; // length of generated lines

Page 33: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1433/60

Visualizing Normal Vectors

• geometry shader (cont)

void main(){// compute the lines for the vertex normalsfor (int i = 0; i < gl_VerticesIn; i++){gl_Position = uModelViewProjectionMatrix * gl_PositionIn[i];EmitVertex();gl_Position = uModelViewProjectionMatrix *

vec4(gl_PositionIn[i].xyz + vNormal[i] * uNormalLength, 1.0);EmitVertex();EndPrimitive();

}

Page 34: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1434/60

Visualizing Normal Vectors

• geometry shader (cont)// compute centroidvec4 cen = (gl_PositionIn[0] + gl_PositionIn[1] + gl_PositionIn[2]) / 3;

// compute normal vector to the trianglevec3 v01 = gl_PositionIn[1].xyz - gl_PositionIn[0].xyz;vec3 v02 = gl_PositionIn[2].xyz - gl_PositionIn[0].xyz;vec3 n = normalize(cross(v01, v02));

// compute line for the triangle normalgl_Position = uModelViewProjectionMatrix * cen;EmitVertex();gl_Position = uModelViewProjectionMatrix *

vec4(cen.xyz + n * uNormalLength, 1.0);EmitVertex();EndPrimitive();

}

Page 35: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1435/60

Visualizing Normal Vectors

Page 36: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1436/60

Visualizing Normal Vectors

• textbook version is more sophisticated– generates more normal vectors per triangle using

vertex normal interpolation– each normal vector is represented as a line strip of uLength segments

– there is a uDroop scalar that controls how much the line strip bends downwards

Page 37: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1437/60

Visualizing Normal Vectors

• vertex shader performs per vertex lighting• vertices are transformed into eye space (no perspective

projection to make interpolation easier)

• geometry shader computes line strips representing the normal vectors

• performs perspective projection• passes through the per vertex lighting

• fragment shader passes through the lighting from geometry shader

Page 38: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1438/60

Visualizing Normal Vectors

• geometry shader#version 330 compatibility#extension GL_EXT_geometry_shader4: enable

layout( triangles ) in;layout( line_strip, max_vertices=128 ) out;

uniform mat4 uProjectionMatrix;

uniform int uDetail;uniform float uDroop;uniform int uLength;uniform float uStep;

in vec3 vNormal[3];in vec4 vColor[3];out vec4 gColor;

Page 39: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1439/60

Visualizing Normal Vectors

int ILength;vec3 Norm[3];vec3 N0, N01, N02;vec4 V0, V01, V02;

void ProduceVertices( float s, float t ){vec4 v = V0 + s*V01 + t*V02;vec3 n = normalize( N0 + s*N01 + t*N02 );for( int i = 0; i <= uLength; i++ ){gl_Position = uProjectionMatrix * v;gColor = vColor[0];EmitVertex( );v.xyz += uStep * n;v.y -= uDroop * float(i*i);

}EndPrimitive();

}1

0,)()(),( 02010

≤+≥

−+−+=

tsts

VVtVVsVtsV

Page 40: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1440/60

Visualizing Normal Vectors

void main( ){V0 = gl_PositionIn[0];V01 = ( gl_PositionIn[1] - gl_PositionIn[0] );V02 = ( gl_PositionIn[2] - gl_PositionIn[0] );

Norm[0] = vNormal[0];Norm[1] = vNormal[1];Norm[2] = vNormal[2];if( dot( Norm[0], Norm[1] ) < 0. )Norm[1] = -Norm[1];

if( dot( Norm[0], Norm[2] ) < 0. )Norm[2] = -Norm[2];

N0 = normalize( Norm[0] );N01 = normalize( Norm[1] - Norm[0] );N02 = normalize( Norm[2] - Norm[0] );

Page 41: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1441/60

Visualizing Normal Vectors

int numLayers = 1 << uDetail;float dt = 1. / float( numLayers );float t = 1.;for( int it = 0; it <= numLayers; it++ ){float smax = 1. - t;int nums = it + 1;float ds = smax / float( nums - 1 );float s = 0.;for( int is = 0; is < nums; is++ ){ProduceVertices( s, t );s += ds;

}t -= dt;

}}

Page 42: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1442/60

Visualizing Normal Vectors

Page 43: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1443/60

Visualizing Normal VectorsuDetail = 0

Page 44: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1444/60

Visualizing Normal VectorsuDetail = 1

Page 45: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1445/60

Visualizing Normal VectorsuDetail = 2

Page 46: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1446/60

Visualizing Normal VectorsuDetail = 3

Page 47: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1447/60

Visualizing Normal VectorsuDetail = 4

number of vertices exceedsmax_vertices causing primitivesto be not output

Page 48: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1448/60

Visualizing Normal VectorsuDetail = 1uDroop = 0.02uLength = 6uStep = 0.1

Page 49: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1449/60

Explosion

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 50: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1450/60

Explosion

• vertex shader simply passes vertices through to geometry shader

• does not perform perspective projection because point physics are defined in object or world coordinates

• geometry shader computes points, applies physics model, and computes lighting intensity

• fragment shader computes fragment color

Page 51: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1451/60

Explosion

• geometry shader#version 150#extension GL_EXT_geometry_shader4: enable#extension GL_EXT_gpu_shader4: enable

layout( triangles ) in;layout( points, max_vertices=200 ) out;

uniform mat4 uProjectionMatrix;uniform int uLevel;uniform float uGravity;uniform float uTime;uniform float uVelScale;

out float gLightIntensity;

Page 52: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1452/60

Explosion

const vec3 LIGHTPOS = vec3( 0., 0., 10. );

vec3 V0, V01, V02;vec3 CG;vec3 Normal;

void ProduceVertex( float s, float t ){vec3 v = V0 + s*V01 + t*V02;gLightIntensity = abs( dot( normalize(LIGHTPOS - v), Normal ) );vec3 vel = uVelScale * ( v - CG );v = v + vel * uTime + 0.5 * vec3(0.,uGravity,0.) * uTime * uTime;gl_Position = uProjectionMatrix * vec4( v, 1. );EmitVertex( );

}

Page 53: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1453/60

Explosion

int numLayers = 1 << uLevel;float dt = 1. / float( numLayers );float t = 1.;for( int it = 0; it <= numLayers; it++ ){float smax = 1. - t;int nums = it + 1;float ds = smax / float( nums - 1 );float s = 0.;for( int is = 0; is < nums; is++ ){ProduceVertex( s, t );s += ds;

}t -= dt;

}}

Page 54: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1454/60

Explosions

Page 55: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1455/60

Silhouettes

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 56: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1456/60http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 57: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1457/60http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 58: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1458/60

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 59: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1459/60

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf

Page 60: TEMA 2.3.1 ETAPA DE GEOMETRÍA EJEMPLOS CON OPENGL · Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/14 1/60--ETAPA

Procesadores Gráficos -- Máster en Informática Gráfica, Juegos y Realidad Virtual – ETAPA GEOMETRÍA’13/1460/60

http://web.engr.oregonstate.edu/~mjb/cs519/Handouts/geometry_shaders.1pp.pdf