creating objects - lecture 4

73
Creating Objects Lecture 4 Robb T. Koether Hampden-Sydney College Mon, Sep 2, 2019 Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 1 / 63

Upload: others

Post on 29-May-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating Objects - Lecture 4

Creating ObjectsLecture 4

Robb T. Koether

Hampden-Sydney College

Mon, Sep 2, 2019

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 1 / 63

Page 2: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 2 / 63

Page 3: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 3 / 63

Page 4: Creating Objects - Lecture 4

Drawing a Rectangle

In earlier versions of OpenGL, drawing a rectangle was quitesimple.

Announce that you were going to draw a rectangle:glBegin(GL_RECT);

Pass the vertices one by one:glVertex2f(0.0, 1.0)

Etc.

It is a bit more complicated now.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 4 / 63

Page 5: Creating Objects - Lecture 4

Drawing a Rectangle

The three basic steps areCreate an array of vertex attributes (data).Create a vertex buffer object (in the GPU).Create a vertex array object (structures the buffer).Issue the draw command.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 5 / 63

Page 6: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 6 / 63

Page 7: Creating Objects - Lecture 4

Vertex Attributes

Vertex AttributesGLfloat rect_data[] ={

-0.5f, -0.5f,0.5f, -0.5f,0.5f, 0.5f,-0.5f, 0.5f

};GLfloat triangle_data[] = {...};

In this first example, the only vertex attributes will be thecoordinates of the 2D vertices.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 7 / 63

Page 8: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 8 / 63

Page 9: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 9 / 63

Page 10: Creating Objects - Lecture 4

Vertex Buffer Objects

A vertex buffer object (VBO) is a buffer (memory) in the GPU thatcontains data related to the vertices of an object.

Coordinates of the vertices.Their color.Normal vectors.Etc.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 10 / 63

Page 11: Creating Objects - Lecture 4

Vertex Buffer Objects

To use a VBO, we must do three things.Generate a name (ID number) for the buffer object.“Bind” a buffer object to the name, i.e., associate the ID numberwith the buffer object and make it the current (or active) buffer.Copy the vertex data to the buffer object.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 11 / 63

Page 12: Creating Objects - Lecture 4

Vertex Buffer Objects

Symbolic Names for the VBOsenum {RectBuffer, TriangleBuffer, NumVBOs};

The enum statement will assign the values 0, 1, and 2 toRectBuffer, TriangleBuffer, and NumVBOs, respectively.Note that value of numVBOs will automatically be the number ofbuffer objects.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 12 / 63

Page 13: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

RectBuffer = 0TriangleBuffer = 1NumVBOs = 2

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 13 / 63

Page 14: Creating Objects - Lecture 4

Vertex Buffer Objects

Array of VBO IDsGLuint VBO[NumVBOs];

The array VBO will contain the ID numbers (to be assigned byOpenGL) of the buffer objects.The enums RectBuffer and TriangleBuffer are symbolicnames for the indexes of the IDs in the array VBO.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 14 / 63

Page 15: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

RectBuffer = 0TriangleBuffer = 1NumVBOs = 2

VBO

0 1

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 15 / 63

Page 16: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer ObjectglGenBuffers(NumVBOs, VBO);

Generate ID numbers for each of the buffers and store them inVBO[0] and VBO[1], also known as VBO[RectBuffer] andVBO[TriangleBuffer].

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 16 / 63

Page 17: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

RectBuffer = 0TriangleBuffer = 1NumVBOs = 2

VBO

0 1

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

7 12

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 17 / 63

Page 18: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer ObjectglBindBuffer(GL_ARRAY_BUFFER, VBO[RectBuffer]);

glBindBuffer() binds (associates) the buffer IDVBO[RectBuffer] to a new buffer object in the GPU and makesthat buffer object the current buffer.When glBindBuffer() is called subsequently with the samebuffer ID, it simply makes that buffer object the current one.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 18 / 63

Page 19: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

RectBuffer = 0TriangleBuffer = 1NumVBOs = 2

VBO

0 1

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

7 12

Uninitialized Buffer Object

Uninitialized Buffer Object

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 19 / 63

Page 20: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer ObjectglNamedBufferStorage(VBO[RectBuffer],

sizeof(rect_data), rect_data, 0);

glNamedBufferStorage() copies the data from rect_datainto the named buffer (VBO[RectBuffer]).

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 20 / 63

Page 21: Creating Objects - Lecture 4

Vertex Buffer Objects

Vertex Buffer Object

RectBuffer = 0TriangleBuffer = 1NumVBOs = 2

VBO

0 1

CPU GPU

rect_data

-0.5 -0.5 ... 0.5

triangle_data

-0.5 ... 0.5

-0.5 -0.5 ... 0.5

-0.5 ... 0.5

7 12

Initialized Buffer Object

Initialized Buffer Object

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 21 / 63

Page 22: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 22 / 63

Page 23: Creating Objects - Lecture 4

Vertex Array Objects

A vertex array object (VAO) describes the structure imposed onthe data stored in the buffer object.We follow a similar pattern with VAOs as we did with VBOs.To use a VAO, we must do three things.

Generate an ID number for the vertex array object.“Bind” that vertex array object to the active buffer object.Describe the structure (i.e., attributes) of the data in the buffer.Enable the vertex attributes.

Then we are ready to draw the object.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 23 / 63

Page 24: Creating Objects - Lecture 4

Vertex Array Objects

Symbolic Names for the VAOsenum {Rect, Triangle, NumVAOs};enum {vPosition = 0};

We use an enumerated type to create symbolic names for theVAOs.We also use an enumerated type to create symbolic names for thevertex attributes.In this example, the only attribute is the position.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 24 / 63

Page 25: Creating Objects - Lecture 4

Vertex Array Objects

Array of VAO IDsGLuint VAO[NumVAOs];

Create an array of vertex array objects.As with the VBOs, this array will hold the ID number of the VAOsin the GPU.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 25 / 63

Page 26: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglBindVertexArray(VAO[Rect]);

glBindVertexArray() will create vertex array objects in theGPU and store their IDs in the VAO array.This statement will store the ID for the rectangle VBO in VAO[0].It is necessary that VBO[RectBuffer] be the current VBO.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 26 / 63

Page 27: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).GL_FLOAT tells the type of object in the attribute.GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 28: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).

GL_FLOAT tells the type of object in the attribute.GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 29: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).GL_FLOAT tells the type of object in the attribute.

GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 30: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).GL_FLOAT tells the type of object in the attribute.GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).

the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 31: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).GL_FLOAT tells the type of object in the attribute.GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.

BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 32: Creating Objects - Lecture 4

Vertex Array Objects

Vertex Array ObjectglVertexAttribPointer(vPosition, 2, GL_FLOAT,

GL_FALSE, 0, BUFFER_OFFSET(0));

This statement associates the attribute ID vPosition (i.e., 0)with the following information.

The 2 indicates the number of objects that constitute a singleattribute (2 floats = a 2D point).GL_FLOAT tells the type of object in the attribute.GL_FALSE tells the GPU not to “normalize” the data (more on thatlater).the 0 is the stride, i.e., the number of bytes to skip over from oneattribute value to the next. The value 0 means that the data arepacked.BUFFER_OFFSET(0) gives the offset, in bytes, to the first attributevalue.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 27 / 63

Page 33: Creating Objects - Lecture 4

Vertex Array Objects

Enable the AttributeglEnableVertexAttribArray(vPosition);

This statement makes the attribute with index vPosition (i.e., 0)active.The values will be available in the shader programs.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 28 / 63

Page 34: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 29 / 63

Page 35: Creating Objects - Lecture 4

Drawing the Object

Drawing the ObjectbjectglDrawArrays(GL_TRIANGLE_FAN, 0, 4);

Invoke the glDrawArrays() function, with parametersThe type of object to draw (e.g., GL_TRIANGLE_FAN).The starting index in the array.The number of vertices.

This example will draw a rectangle.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 30 / 63

Page 36: Creating Objects - Lecture 4

Drawing the Object

There are several types of objects to draw.Primitives

GL_POINTS – individual pointsGL_LINES – line segmentsGL_TRIANGLES – triangles

NonprimitivesGL_LINE_STRIP – line segments joined in sequenceGL_LINE_LOOP – line segments joined in a circuitGL_TRIANGLE_FAN – triangles fanning out from a base pointGL_TRIANGLE_STRIP – triangles forming a strip

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 31 / 63

Page 37: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 32 / 63

Page 38: Creating Objects - Lecture 4

Color

In computer graphics, every color has three components.RedGreenBlue

Any specific color is represented by a triple (r ,g,b), with eachcomponent between 0.0 and 1.0.The RGB values are clamped to the range [0,1].

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 33 / 63

Page 39: Creating Objects - Lecture 4

Color

Black(0, 0, 0)

Green(0, 1, 0)

Red(1, 0, 0)

Blue(0, 0, 1)

Magenta(1, 0, 1)

Yellow(1, 1, 0)

Cyan(0, 1, 1)

White(1, 1, 1)

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 34 / 63

Page 40: Creating Objects - Lecture 4

Color

Black

Blue

Red

Green

Blue

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 35 / 63

Page 41: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?

Orange?Brown?Pink?Beige?Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 42: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?Orange?

Brown?Pink?Beige?Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 43: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?Orange?Brown?

Pink?Beige?Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 44: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?Orange?Brown?Pink?

Beige?Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 45: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?Orange?Brown?Pink?Beige?

Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 46: Creating Objects - Lecture 4

Color

What RGB triple would appear gray?Orange?Brown?Pink?Beige?Garnet?

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 36 / 63

Page 47: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 37 / 63

Page 48: Creating Objects - Lecture 4

Coloring a Rectangle

To color a rectangle, we need to include the color data in thebuffer along with the vertex coordinates.There are several ways to do this.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 38 / 63

Page 49: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 39 / 63

Page 50: Creating Objects - Lecture 4

One Array, Segregated Attributes

One Array, Segregated AttributesGLfloat rect_data[] ={

-0.5f, -0.5f, // 1st vertex0.5f, -0.5f, // 2nd vertex0.5f, 0.5f, // 3rd vertex

-0.5f, 0.5f, // 4th vertex1.0f, 0.0f, 0.0f, // Color of 1st1.0f, 1.0f, 0.0f, // Color of 2nd0.0f, 1.0f, 0.0f, // Color of 3rd0.0f, 0.0f, 1.0f // Color of 4th

};

We can pack all the data contiguously into one array, with theattributes segregated.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 40 / 63

Page 51: Creating Objects - Lecture 4

One Array, Segregated Attributes

Buffer Object

pos color

vertex 0

pos pos pos color color color

vertex 1 vertex 2 vertex 3 vertex 0 vertex 1 vertex 2 vertex 3

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 41 / 63

Page 52: Creating Objects - Lecture 4

Color a Rectangle

Color a Rectangleenum {vPosition = 0, vColor = 1};

Create a symbolic name for the color attribute.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 42 / 63

Page 53: Creating Objects - Lecture 4

Color a Rectangle

Color a RectangleglNamedBufferStorage(VBO[RectBuffer], sizeof(rect_data),

rect_data, 0);

Store the data in the buffer and bind the vertex array object, asbefore.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 43 / 63

Page 54: Creating Objects - Lecture 4

Color a Rectangle

Color a RectangleglBindVertexArray(VAOs[Rect]);glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE,

0, BUFFER_OFFSET(0));glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE,

0, BUFFER_OFFSET(8*sizeof(GLfloat)));

Set the position attribute as before.Give the color attribute an offset equal to the size of the positiondata.Both attributes have a stride of 0.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 44 / 63

Page 55: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 45 / 63

Page 56: Creating Objects - Lecture 4

Two Arrays, Segregated Attributes

Two Arrays, Segregated AttributesGLfloat rect_pos[] ={

-0.5f, -0.5f, // 1st vertex0.5f, -0.5f, // 2nd vertex0.5f, 0.5f, // 3rd vertex

-0.5f, 0.5f // 4th vertex};GLfloat rect_color[] ={

1.0f, 0.0f, 0.0f, // Color of 1st1.0f, 1.0f, 0.0f, // Color of 2nd0.0f, 1.0f, 0.0f, // Color of 3rd0.0f, 0.0f, 1.0f // Color of 4th

};

We can create two separate arrays, with the attributes necessarilysegregated.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 46 / 63

Page 57: Creating Objects - Lecture 4

One Array, Segregated Attributes

Buffer Object

pos color

vertex 0

pos pos pos color color color

vertex 1 vertex 2 vertex 3 vertex 0 vertex 1 vertex 2 vertex 3

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 47 / 63

Page 58: Creating Objects - Lecture 4

Two Arrays, Segregated Attributes

Two Arrays, Segregated AttributesglNamedBufferStorage(VBO[RectBuffer], sizeof(rect_pos)

+ sizeof(rect_color), NULL, 0);glNamedBufferSubData(VBO[RectBuffer], 0, sizeof(rect_pos),

rect_pos);glNamedBufferSubData(VBO[RectBuffer], sizeof(rect_pos),

sizeof(rect_color), rect_color);

We must first reserve the memory and then separately store thetwo arrays using glNamedBufferSubData().

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 48 / 63

Page 59: Creating Objects - Lecture 4

Two Arrays, Segregated Attributes

Two Arrays, Segregated AttributesglBindVertexArray(VAOs[Rect]);glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE,

0, BUFFER_OFFSET(0));glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE,

0, BUFFER_OFFSET(sizeof(rect_pos)));

Set the position attribute as before.Give the color attribute an offset equal to the size of the positiondata.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 49 / 63

Page 60: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 50 / 63

Page 61: Creating Objects - Lecture 4

One Array, Integrated Attributes

One Array, Integrated AttributesGLfloat rect_data[] ={

-0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // 1st vertex0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // 2nd vertex0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // 3rd vertex

-0.5f, 0.5f, 0.0f, 0.0f, 1.0f // 4th vertex};

We can create one array, with the attributes integrated.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 51 / 63

Page 62: Creating Objects - Lecture 4

One Array, Integrated Attributes

pos

Buffer Object

color

vertex 0

pos color

vertex 1

pos color

vertex 2

pos color

vertex 3

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 52 / 63

Page 63: Creating Objects - Lecture 4

One Array, Integrated Attributes

One Array, Integrated AttributesglNamedBufferStorage(VBO[RectBuffer], sizeof(rect_data),

rect_data, 0);

Store the data in the buffer and bind the vertex array object, asbefore.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 53 / 63

Page 64: Creating Objects - Lecture 4

One Array, Integrated Attributes

One Array, Integrated AttributesglBindVertexArray(VAOs[Rect]);glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE,

5*sizeof(GL_FLOAT), BUFFER_OFFSET(0));glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE,

5*sizeof(GL_FLOAT), BUFFER_OFFSET(2*sizeof(GLfloat)));

Set the position attribute as before.Give the color attribute an offset equal to the size of a position.Give the position and color a stride equal to the size of the data fora vertex.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 54 / 63

Page 65: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 55 / 63

Page 66: Creating Objects - Lecture 4

One Array, Structured Data

One Array, Structured Datastruct VertexData2D{

GL_FLOAT pos[2];GL_FLOAT color[3];

};

Create a VertexData2D structure.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 56 / 63

Page 67: Creating Objects - Lecture 4

One Array, Structured Data

One Array, Structured Datastruct VertexData2D{

vec2 pos;vec3 color;

};

Create a VertexData2D structure.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 57 / 63

Page 68: Creating Objects - Lecture 4

One Array, Structured Data

One Array, Structured DataVertexData2D rect_data[] ={

{{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}}, // 1st vertex{{ 0.5f, -0.5f}, {1.0f, 1.0f, 0.0f}}, // 2nd vertex{{ 0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}}, // 3rd vertex{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}} // 4th vertex

};

We can create one array of type VertexData2D.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 58 / 63

Page 69: Creating Objects - Lecture 4

One Array, Structured Data

pos

Buffer Object

color

vertex 0

pos color

vertex 1

pos color

vertex 2

pos color

vertex 3

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 59 / 63

Page 70: Creating Objects - Lecture 4

One Array, Integrated Attributes

One Array, Integrated AttributesglNamedBufferStorage(GL_ARRAY_BUFFER, sizeof(rect_data),

rect_data, 0);

Store the data in the buffer and bind the vertex array object, asbefore.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 60 / 63

Page 71: Creating Objects - Lecture 4

One Array, Integrated Attributes

One Array, Integrated AttributesglBindVertexArray(VAOs[Rect]);glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE,

sizeof(VertexData2D), BUFFER_OFFSET(0));glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE,

sizeof(VertexData2D),BUFFER_OFFSET(sizeof(vec2)));

Set the position attribute as before.Give the color attribute an offset equal to the size of a position.Give the position a stride equal to the size of a color.Give the color a stride equal to the size of a position.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 61 / 63

Page 72: Creating Objects - Lecture 4

Outline

1 Drawing a RectangleVertex AttributesVertex Buffer ObjectsVertex Array ObjectsDrawing the Object

2 Color

3 Coloring a RectangleOne Array, Segregated AttributesTwo Arrays, Segregated AttributesOne Array, Integrated AttributesOne Array, Structured Data

4 Assignment

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 62 / 63

Page 73: Creating Objects - Lecture 4

Assignment

AssignmentRead pp. 16 - 22 in The Red Book.

Robb T. Koether (Hampden-Sydney College) Creating Objects Mon, Sep 2, 2019 63 / 63