week 4 lecture 1: opengl 3.x & 4.x. 2 objectives changes in opengl 3.x 4.x changes in glsl...

23
Week 4 Lecture 1: OpenGL 3.x & 4.x

Upload: bryanna-pease

Post on 31-Mar-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

Week 4 Lecture 1: OpenGL 3.x & 4.x

Page 2: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

2

Objectives

•Changes in OpenGL 3.x 4.x•Changes in GLSL 1.3/4/5 4.x

Page 3: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

3

Fixed Function / Immediate Mode

• OpenGL 3.x deprecated stuff includes:­ immediate mode (Begin/End etc)­ Vertex specification­ Vertex Arrays­ Matrices­ Texture Coordinates­ Lighting­ All fixed-function options­ pixel formats with colour palettes

Page 4: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

4

I Need My Immediate Mode! Why have they done this?

• Shaders!• Shader performance is so much better that

immediate mode is no longer competative.• Direct X

• Direct X was outperforming OpenGL since Direct X 10

• Hardware• New Graphics cards were reducing fixed

functionality support in order to focus on gpu.

Page 5: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

5Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

No, Really, I want immediate mode.

• Ok, immediate mode is still available if you include OpenGL’s compatibility profile. A core profile and a compatibility profile exist…

 int attribs[] =       {            WGL_CONTEXT_MAJOR_VERSION_ARB, major,            WGL_CONTEXT_MINOR_VERSION_ARB, minor,             WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,            WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,            0       };

      PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL;      wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB");

      if(wglCreateContextAttribsARB != NULL)      {            m_hrc = wglCreateContextAttribsARB(pDC->m_hDC,0, attribs);      }

Page 6: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

6

Shaders

• OpenGL 3.x• Shader Versions 1.40 and 1.50• Geometry Shader

• OpenGL 4.x• Shader Version 4.x• Tessolation Shaders

• Control shader• Primitive Generator• Evaluation shader

Page 7: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

7

VertexShader

FrameBuffer

FragmentShader

CPU

vertices vertices fragments

Rasterizer

fragments

OpenGL 2.x Shaders

Page 8: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

8

VertexShader

FrameBuffer

FragmentShader

CPU Rasterizer

OpenGL 3.x Shaders

GeometryShader

vertices

vertices

primitives

fragments fragments

Page 9: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

9

VertexShader

FrameBuffer

FragmentShader

CPU Rasterizer

OpenGL 4.x Shaders

GeometryShader

vertices

vertices

primitives

fragments fragments

Primitivegenerator

ControlShader

Evaluationshader

Optional Tessellation Shaders

Page 10: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

10Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Geometry Shaders

• Take vertices (of a primitive)• Output Primitives (triangle strip, points, line)• Can create new vertices, unlike vertex shaders, which

are limited to a 1:1 input to output ratio.• Layered rendering; this means that the GS can

specifically say that a primitive is to be rendered to a particular layer of the framebuffer.

Page 11: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

11Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Geometry Shaders

#version 150 layout(triangles) in;layout(triangle_strip, max_vertices = 3) out; void main() { for(int i = 0; i < gl_in.length(); i++) { gl_Position = gl_in[i].gl_Position; EmitVertex(); } EndPrimitive();}

Page 12: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

12Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Uniform Buffer Objects

• Allow blocks of data to be transferred as a single variable to shader.

• Need a layout parameter to define memory layout.

uniform BlockName{ vec3 blockMember1, blockMember2; float blockMember3;} glslBlockName;

Page 13: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

13Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Example (Projection/Model Matrix now need to be manually passed)

#version 140 uniform Transformation { mat4 projection_matrix; mat4 modelview_matrix;}; in vec3 vertex; void main() { gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);

}

Page 14: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

14Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Layout

• Defines memory structure of passed variables.• For UBO it can be std140, packed, or shared. Packed or

Shared needs the byte offset to access, std140 is more user friendly but may not be optimal.

• Layout can be set for other compound memory, e.g. row_major or colomn_major for matrices, triangles for vertex arrays etc.

Page 15: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

15Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Tessellation Control Processor

• Programmable unit that operates on a patch of incoming vertices and their associated data, emitting a new output patch.

• Invoked for each vertex of the output patch.• Each invocation can read the attributes of any vertex

in the input or output patches, but can only write per-vertex attributes for the corresponding output patch vertex.

• As many shaders can run, the built-in function barrier() can be used to control execution order by synchronizing invocations, dividing tessellation control shader execution into a set of phases.

Page 16: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

16Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Tessellation Evaluation Processor

• Evaluates the position and other attributes of a vertex generated by the tessellation primitive generator, using a patch of incoming vertices and their associated data.

• computes the position and attributes of a single vertex generated by the tessellation primitive generator.

• Reads the attributes of any vertex in the input patch, plus the tessellation coordinate, which is the relative location of the vertex in the primitive being tessellated.

• The executable writes the position and other attributes of the vertex.

Page 17: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

17Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

What variables do we get for free?

Vertex:

in int gl_VertexID;in int gl_InstanceID;

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

};

+ Compatability Profile:

in vec4 gl_Color;in vec4 gl_SecondaryColor;in vec3 gl_Normal;in vec4 gl_Vertex;in vec4 gl_MultiTexCoord0;in vec4 gl_MultiTexCoord1;in vec4 gl_MultiTexCoord2;in vec4 gl_MultiTexCoord3;in vec4 gl_MultiTexCoord4;in vec4 gl_MultiTexCoord5;in vec4 gl_MultiTexCoord6;in vec4 gl_MultiTexCoord7;in float gl_FogCoord;

Page 18: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

18

What variables do we get for free?

Geometry:in gl_PerVertex {vec4 gl_Position;float gl_PointSize;float gl_ClipDistance[];

} gl_in[];

in int gl_PrimitiveIDIn;in int gl_InvocationID;

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

};out int gl_PrimitiveID;out int gl_Layer;

Page 19: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

19

What variables do we get for free?

Tessellation control language:in gl_PerVertex {vec4 gl_Position;float gl_PointSize;float gl_ClipDistance[];

} gl_in[gl_MaxPatchVertices];

in int gl_PatchVerticesIn;in int gl_PrimitiveID;in int gl_InvocationID;

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

} gl_out[];

patch out float gl_TessLevelOuter[4];patch out float gl_TessLevelInner[2];

Page 20: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

20

What variables do we get for free?

tessellation evaluation language:

in gl_PerVertex {vec4 gl_Position;float gl_PointSize;float gl_ClipDistance[];

} gl_in[gl_MaxPatchVertices];

in int gl_PatchVerticesIn;in int gl_PrimitiveID;in vec3 gl_TessCoord;

patch in float gl_TessLevelOuter[4];patch in float gl_TessLevelInner[2];

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

};

Page 21: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

21

What variables do we get for free?

Fragment:

in vec4 gl_FragCoord;in bool gl_FrontFacing;in float gl_ClipDistance[];in vec2 gl_PointCoord;in int gl_PrimitiveID;in int gl_SampleID;in vec2 gl_SamplePosition;

out vec4 gl_FragColor; // deprecatedout vec4 gl_FragData[gl_MaxDrawBuffers]; // deprecatedout float gl_FragDepth;out int gl_SampleMask[];

Page 22: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

22Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Compatibility Profile State

uniform mat4 gl_ModelViewMatrix;uniform mat4 gl_ProjectionMatrix;uniform mat4 gl_ModelViewProjectionMatrix;uniform mat4 gl_TextureMatrix[gl_MaxTextureCoords];uniform mat3 gl_NormalMatrix; uniform mat4 gl_ModelViewMatrixInverse;uniform mat4 gl_ProjectionMatrixInverse;uniform mat4 gl_ModelViewProjectionMatrixInverse;uniform mat4 gl_TextureMatrixInverse[gl_MaxTextureCoords];uniform mat4 gl_ModelViewMatrixTranspose;uniform mat4 gl_ProjectionMatrixTranspose;uniform mat4 gl_ModelViewProjectionMatrixTranspose;uniform mat4 gl_TextureMatrixTranspose[gl_MaxTextureCoords];uniform mat4 gl_ModelViewMatrixInverseTranspose;uniform mat4 gl_ProjectionMatrixInverseTranspose;uniform mat4 gl_ModelViewProjectionMatrixInverseTranspose;uniform mat4 gl_TextureMatrixInverseTranspose[gl_MaxTextureCoords];uniform float gl_NormalScale;uniform vec4 gl_ClipPlane[gl_MaxClipPlanes];

Page 23: Week 4 Lecture 1: OpenGL 3.x & 4.x. 2 Objectives Changes in OpenGL 3.x 4.x Changes in GLSL 1.3/4/5 4.x

23Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009

Compatibility Profile State

struct gl_PointParameters {float size;float sizeMin;float sizeMax;float fadeThresholdSize;float distanceConstantAttenuation;float distanceLinearAttenuation;float distanceQuadraticAttenuation;};struct gl_MaterialParameters {vec4 emission; // Ecmvec4 ambient; // Acmvec4 diffuse; // Dcmvec4 specular; // Scmfloat shininess; // Srm};uniform gl_MaterialParameters gl_FrontMaterial;uniform gl_MaterialParameters gl_BackMaterial;