1 of 74

卞正冠 P76094703@gs.ncku.edu.tw

姚勝翊 ND8081018@gs.ncku.edu.tw

Learn

OpenGL

2 of 74

Introduction

3 of 74

Introduction

  • Mainly considered as a specification for rendering 2D and 3D vector graphics.
  • Created in 1991 by Silicon Graphics, Inc. Now it is managed by Khronos Group.

4 of 74

Introduction

  • Cross-platform

Linux

Windows

etc.

  • Cross-Language

C & C++

Java

Python

etc.

5 of 74

Environment

(Windows, C++)

6 of 74

Requirement

  • In this slide, we will use the specification:

OpenGL version 3.3 or above.

  • With the requirement listed below:

Visual Studio - Integrated development environment (IDE)

GLFW - OpenGL utility library

GLEW - OpenGL Extension Wrangler Library

7 of 74

GLFW

8 of 74

GLEW

9 of 74

GLFW & GLEW

10 of 74

Visual Studio

11 of 74

Visual Studio

12 of 74

Visual Studio

13 of 74

Visual Studio

14 of 74

Visual Studio

15 of 74

Visual Studio

16 of 74

Visual Studio

17 of 74

Visual Studio

18 of 74

Visual Studio

19 of 74

Visual Studio

20 of 74

Visual Studio

21 of 74

Visual Studio

22 of 74

Visual Studio

23 of 74

Visual Studio

24 of 74

Visual Studio

25 of 74

Visual Studio

26 of 74

Visual Studio

27 of 74

Visual Studio

28 of 74

Visual Studio

29 of 74

Visual Studio

30 of 74

Visual Studio

31 of 74

Visual Studio

32 of 74

Visual Studio

33 of 74

Creating

a window

34 of 74

Include

35 of 74

Init GLFW

36 of 74

Create a window object

37 of 74

Init GLEW

38 of 74

Viewport

39 of 74

Render loop

40 of 74

End of the main function

41 of 74

Input

42 of 74

Create a window

43 of 74

Render pipeline

44 of 74

Pipeline

The graphics pipeline can be divided into two large parts: the first transforms your 3D coordinates into 2D coordinates and the second part transforms the 2D coordinates into actual colored pixels.

45 of 74

Shader

  • Graphics cards process your data within the graphics pipeline.
  • The processing cores run small programs on the GPU for each step of the pipeline. These small programs are called shaders.

46 of 74

Pipeline

47 of 74

Normalized Device Coordinate(NDC)

Once your vertex coordinates have been processed in the vertex shader, they should be in normalized device coordinates which is a small space where the x, y and z values vary from -1.0 to 1.0.

48 of 74

Vertex Specification

  • Vertex specification is the procedure of setting up rendering objects.
  • Submit the vertex stream data into the rendering pipeline.
  • Tell the program how the data stream is represented.

49 of 74

VBO

  • It can be used as a source of vertex array data for OpenGL context.
  • Usually stores some vertex attributes like position, normal.

50 of 74

VAO

    • Vertex array object (VAO)
    • It stores the necessary state for object data.
    • It normally stores the format of the data and the VBO provided the data.

51 of 74

EBO

An EBO is a buffer, just like a vertex buffer object, that stores indices that OpenGL uses to decide what vertices to draw.

52 of 74

Vertex Shader

  • Vertex shader handles the manipulation of the individual vertex.
  • It determines the final attribute of the single vertex.
  • Each input vertex must map to a specific output vertex.

53 of 74

Fragment Shader

  • Manipulate the fragment into colors and depth value.
  • Determine the color of the fragment.

54 of 74

Render

Code

55 of 74

Vertex data

An array containing 3d position data belonging to 3 vertices:

{ {-0.5, -0.5, 0}, {0.5, -0.5, 0}, {0, 0.5, 0} }

56 of 74

Generate the OpenGL object

57 of 74

Bind

  • Bind the VAO.
  • Bind the buffer object to vertex array object and specify the data (copy them into OpenGL context memory).

58 of 74

Shader

  • Shaders are written in the OpenGL Shading Language (GLSL).

59 of 74

Vertex Shader

60 of 74

Fragment Shader

61 of 74

Shader

62 of 74

Compile Shader

63 of 74

Create Shader Program

Create a shader program object to link the shader.

64 of 74

Vertex attribute array

  • Enable and specify the generic vertex attribute array.

65 of 74

Render the mesh in render loop

    • Clear the render buffer.
    • Bind the correct vertex array object and texture.
    • Use the proper shader program.
    • Draw the mesh.

66 of 74

Draw a triangle

67 of 74

Draw more triangle

68 of 74

Draw more triangle

69 of 74

Draw more triangle

As you can see, there is some overlap on the vertices specified. We specify bottom right and top left twice!

70 of 74

Vertex & index data

71 of 74

Generate the OpenGL object

72 of 74

Bind

  • Bind the VAO.
  • Bind the buffer object to vertex array object and specify the data (copy them into OpenGL context memory).

73 of 74

Render the mesh in render loop

74 of 74

Draw more triangle (EBO)