3 D Game Programming 3 D Primitive MingTe

  • Slides: 68
Download presentation
3 D Game Programming 3 D Primitive Ming-Te Chi Department of Computer Science, National

3 D Game Programming 3 D Primitive Ming-Te Chi Department of Computer Science, National Chengchi University

Wolfenstein 3 D 1992 Doom 1993 (Top) Wolfenstein 3 D (Bottom) A landmark 1993

Wolfenstein 3 D 1992 Doom 1993 (Top) Wolfenstein 3 D (Bottom) A landmark 1993 first-person shooter (FPS)video game by id Software.

3 D Graphics – evolution

3 D Graphics – evolution

3 D Graphics – evolution

3 D Graphics – evolution

3 D Graphics

3 D Graphics

BASIC

BASIC

Cartesian Plane +y (4, 3) (0, 0) (-3, -2) +z +x

Cartesian Plane +y (4, 3) (0, 0) (-3, -2) +z +x

Coordinate Clipping +y (150, 100) (0, 0) (-75, -50) (75, 50) +x

Coordinate Clipping +y (150, 100) (0, 0) (-75, -50) (75, 50) +x

2 D Game world Super Mario Bros. Nintendo

2 D Game world Super Mario Bros. Nintendo

3 D Game world Super Mario 3 D Land. Nintendo

3 D Game world Super Mario 3 D Land. Nintendo

Projection (camera) Getting 3 D to 2 D – Orthographic projections – Perspective projections

Projection (camera) Getting 3 D to 2 D – Orthographic projections – Perspective projections

Viewport (camera) Mapping drawing coordinates to windows coordinates +y (0, 0) clipping space +x

Viewport (camera) Mapping drawing coordinates to windows coordinates +y (0, 0) clipping space +x Window space

Representing Visuals 3 D objects – Mesh: geometry – Materials – Texture maps Lighting

Representing Visuals 3 D objects – Mesh: geometry – Materials – Texture maps Lighting Shader

Start Unity in 3 D

Start Unity in 3 D

Pipeline Graphics State Application Vertices (3 D) Vertex Processor Rasterizer Xformed, Lit Vertices (2

Pipeline Graphics State Application Vertices (3 D) Vertex Processor Rasterizer Xformed, Lit Vertices (2 D) CPU Programmable vertex processor! Fragments (pre-pixels) GPU Pixel Processor Final pixels (Color, Depth) Video Memory (Textures) Render-to-texture • Programmable pixel processor!

GPU Pipeline: Transform Vertex Processor (multiple operate in parallel) – Transform from “world space”

GPU Pipeline: Transform Vertex Processor (multiple operate in parallel) – Transform from “world space” to “image space” – Compute per-vertex lighting World space Image space

GPU Pipeline: Rasterizer – Convert geometric rep. (vertex) to image rep. (fragment) • Fragment

GPU Pipeline: Rasterizer – Convert geometric rep. (vertex) to image rep. (fragment) • Fragment = image fragment – Pixel + associated data: color, depth, stencil, etc. – Interpolate per-vertex quantities across pixels vertex Geometry primitive pixels

GPU Pipeline: Shade Fragment Processors (multiple in parallel) – Compute a color for each

GPU Pipeline: Shade Fragment Processors (multiple in parallel) – Compute a color for each pixel – Optionally read colors from textures (images) Image from nvidia slide

Outline Open. GL Primitives – Immediate mode (4 rd Ch 3) – Vertex processing

Outline Open. GL Primitives – Immediate mode (4 rd Ch 3) – Vertex processing and Drawing (6 th ch 7) Fragment Processing and Buffers

PRIMITIVES

PRIMITIVES

Open. GL Geometric Primitives All geometric primitives are specified by vertices GL_POINTS GL_LINE_STRIP GL_LINE_LOOP

Open. GL Geometric Primitives All geometric primitives are specified by vertices GL_POINTS GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_QUADS GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUAD_STRIP

Immediate mode gl. Begin(GLenum mode); … gl. Vertex 3 f(…); … … gl. End();

Immediate mode gl. Begin(GLenum mode); … gl. Vertex 3 f(…); … … gl. End(); Open. GL 1. X, 2. X

Points gl. Begin(GL_POINTS); z = -50. 0 f; for(angle = 0. 0 f; angle

Points gl. Begin(GL_POINTS); z = -50. 0 f; for(angle = 0. 0 f; angle <= (2. 0 f*GL_PI)*3. 0 f; angle += 0. 1 f) { x = 50. 0 f*sin(angle); y = 50. 0 f*cos(angle); // Specify the point and move the Z value up a little gl. Vertex 3 f(x, y, z); z += 0. 5 f; } gl. End(); // Done drawing points

Point. Size // Specify the point size before the primitive is specified gl. Point.

Point. Size // Specify the point size before the primitive is specified gl. Point. Size(cur. Size); // Draw the point gl. Begin(GL_POINTS); gl. Vertex 3 f(x, y, z); gl. End();

Line gl. Begin(GL_LINES); z = 0. 0 f; for(angle = 0. 0 f; angle

Line gl. Begin(GL_LINES); z = 0. 0 f; for(angle = 0. 0 f; angle <= GL_PI; angle += (GL_PI / 20. 0 f)) { // Top half of the circle x = 50. 0 f*sin(angle); y = 50. 0 f*cos(angle); gl. Vertex 3 f(x, y, z); // Bottom half of the circle x = 50. 0 f*sin(angle+GL_PI); y = 50. 0 f*cos(angle+GL_PI); gl. Vertex 3 f(x, y, z); } // Done drawing points gl. End();

LINE_STRIP gl. Begin(GL_LINE_STRIP);

LINE_STRIP gl. Begin(GL_LINE_STRIP);

Line. Width gl. Line. Width(f. Curr. Size); // Draw the line gl. Begin(GL_LINES); gl.

Line. Width gl. Line. Width(f. Curr. Size); // Draw the line gl. Begin(GL_LINES); gl. Vertex 2 f(-80. 0 f, y); gl. Vertex 2 f(80. 0 f, y); gl. End();

Line. Stipple GLushort pattern = 0 x 5555; // Stipple pattern gl. Line. Stipple(factor,

Line. Stipple GLushort pattern = 0 x 5555; // Stipple pattern gl. Line. Stipple(factor, pattern); // Draw the line gl. Begin(GL_LINES); gl. Vertex 2 f(-80. 0 f, y); gl. Vertex 2 f(80. 0 f, y); gl. End();

Triangle float v[9][3]; gl. Begin(GL_TRANGLES); gl. Vertex 3 fv(v[0]); gl. Vertex 3 fv(v[1]); gl.

Triangle float v[9][3]; gl. Begin(GL_TRANGLES); gl. Vertex 3 fv(v[0]); gl. Vertex 3 fv(v[1]); gl. Vertex 3 fv(v[2]); … gl. End(); 4 5 0 3 1 2 GL_TRIANGLES 1 2 3 4 0 5 GL_TRIANGLE_FAN 1 0 2 4 3 5 7 6 GL_TRIANGLE_STRIP

Winding By default, front face is defined by CCW( Counter. Clockwise Winding ) 0

Winding By default, front face is defined by CCW( Counter. Clockwise Winding ) 0 1 Winding change 2 – gl. Front. Face(GL_CW); // clockwise winding

gl. Begin(GL_TRIANGLE_FAN); // Pinnacle of cone is shared vertex for fan, moved up Z

gl. Begin(GL_TRIANGLE_FAN); // Pinnacle of cone is shared vertex for fan, moved up Z axis to produce a cone instead of a circle gl. Vertex 3 f(0. 0 f, 75. 0 f); // Loop around in a circle and specify even points along the circle // as the vertices of the triangle fan for(angle = 0. 0 f; angle < (2. 0 f*GL_PI); angle += (GL_PI/8. 0 f)) { // Calculate x and y position of the next vertex x = 50. 0 f*sin(angle); y = 50. 0 f*cos(angle); // Specify the next vertex for the triangle fan gl. Vertex 2 f(x, y); } // Done drawing fan for cone gl. End();

Hidden Surface Remove Without hidden surface remove gl. Enable(GL_DEPTH_TEST);

Hidden Surface Remove Without hidden surface remove gl. Enable(GL_DEPTH_TEST);

Polygon Construction Rules All polygon must be planar. The polygon’s edge must not intersect,

Polygon Construction Rules All polygon must be planar. The polygon’s edge must not intersect, and the polygon must be convex. Convex polygon Non-Convex polygon

Polygon Non-Convex polygon (Concave) – Must be composited by multi-triangle

Polygon Non-Convex polygon (Concave) – Must be composited by multi-triangle

RENDER PRIMITIVE IN MODERN OPENGL Application Client side GPU Server side

RENDER PRIMITIVE IN MODERN OPENGL Application Client side GPU Server side

Vertex Arrays void gl. Vertex. Pointer(GLint size, GLenum type, GLsizei stride, const GLvoid *

Vertex Arrays void gl. Vertex. Pointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer); – define an array of vertex data void gl. Draw. Arrays(GLenum mode, GLint first, GLsizei count); – render primitives from array data

Vertex Arrays- example const int SMALL_STAR=100; Glfloat v. Small. Stars[SMALL_STAR*2]; //(x, y) //assign position

Vertex Arrays- example const int SMALL_STAR=100; Glfloat v. Small. Stars[SMALL_STAR*2]; //(x, y) //assign position into v. Small. Stars … gl. Enable. Client. State(GL_VERTEX_ARRAY); gl. Vertex. Pointer(2, GL_FLOAT, 0, v. Small. Stars); gl. Draw. Arrays(GL_POINTS, 0, SMALL_STAR);

Vertex Array Object (VAO) Open. GL 3. 0 Web. GL Open. GL ES In

Vertex Array Object (VAO) Open. GL 3. 0 Web. GL Open. GL ES In modern Open. GL, VAO with VBO is the main way to render primitive. A VAO is an Open. GL Object that encapsulates all of the state needed to specify vertex data. Note that VAOs do not contain the arrays themselves; the arrays are stored in Buffer Objects

VAO – triangle example // An array of 3 vectors which represents 3 vertices

VAO – triangle example // An array of 3 vectors which represents 3 vertices static const GLfloat g_vertex_buffer_data[] = { -1. 0 f, 0. 0 f, 1. 0 f, 0. 0 f, }; Shader. Info shaders[] = { { GL_VERTEX_SHADER, "triangles. vert" }, { GL_FRAGMENT_SHADER, "triangles. frag" }, { GL_NONE, NULL } }; GLuint program = Load. Shaders(shaders); gl. Use. Program(program);

// Generate and Bind VAO GLuint Vertex. Array. ID; gl. Gen. Vertex. Arrays(1, &Vertex.

// Generate and Bind VAO GLuint Vertex. Array. ID; gl. Gen. Vertex. Arrays(1, &Vertex. Array. ID); gl. Bind. Vertex. Array(Vertex. Array. ID); Application Client side GPU Server side Glfloat [] VAO VBO // This will identify our vertex buffer GLuint vertexbuffer; // Generate 1 buffer, put the resulting identifier in vertexbuffer gl. Gen. Buffers(1, &vertexbuffer); // The following commands will talk about our 'vertexbuffer' buffer gl. Bind. Buffer(GL_ARRAY_BUFFER, vertexbuffer); // Give our vertices to Open. GL. gl. Buffer. Data(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

VAO in display() gl. Use. Program(program); GPU - Server side VAO VBO program Vertex

VAO in display() gl. Use. Program(program); GPU - Server side VAO VBO program Vertex shader 001010 … in vec 3 position // 1 st attribute buffer : vertices in vec 3 color gl. Bind. Buffer(GL_ARRAY_BUFFER, vertexbuffer); gl. Vertex. Attrib. Pointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); gl. Enable. Vertex. Attrib. Array(0); // Draw the triangle ! Starting from vertex 0; 3 vertices total -> 1 triangle gl. Draw. Arrays(GL_TRIANGLES, 0, 3); gl. Disable. Vertex. Attrib. Array(0);

Reference Open. GL Coursenote in SIGGRAPH 2013 – http: //www. cs. unm. edu/~angel/SIGGRAPH 13/An%20

Reference Open. GL Coursenote in SIGGRAPH 2013 – http: //www. cs. unm. edu/~angel/SIGGRAPH 13/An%20 Introduction%20 to%20 Open. GL %20 Programming. pptx