Data Setup (Transfer) CPU GPU vertices f0 f1 .. f10 f11 f0 - - PowerPoint PPT Presentation

data setup transfer
SMART_READER_LITE
LIVE PREVIEW

Data Setup (Transfer) CPU GPU vertices f0 f1 .. f10 f11 f0 - - PowerPoint PPT Presentation

Data Setup (Transfer) CPU GPU vertices f0 f1 .. f10 f11 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 12 numbers transferred Read mesh on CPU meaning not known GLfloat vertices[12] = { // 12 floats 6 2D vertices? -0.90, -0.90,


slide-1
SLIDE 1

Data Setup (Transfer)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenBuffers(1, &dataBuffer); // prepare to use glBindBuffer(GL_ARRAY_BUFFER, dataBuffer); // load the data in the buffer glBufferData(GL_ARRAY_BUFFER, // data is vertex attributes 12*sizeof(GLfloat), // how many bytes: 12 numbers * 4 bytes per float vertices, // where to start: in beginning of array GL_STATIC_DRAW); // data will not change

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11

Transfer data to GPU 12 numbers transferred meaning not known

  • 6 2D vertices?
  • 4 3D vertices?
slide-2
SLIDE 2

Data Setup (Describe)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenVertexArrays(1, &dataLayout); // prepare to use glBindVertexArray(dataLayout); // load the data in the buffer glVertexAttribPointer(<<POS_ATTR>>, // 2 floats per pos 2, GL_FLOAT, // do not normalize GL_FALSE, // no gap 0, // from beg of buffer 0);

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

v0x v0y v1x v1y v2x v2y v3x v3y v4x v4y v5x v5y

Describe the data 12 numbers transferred for 6 2D verts, no colors

slide-3
SLIDE 3

Data Setup (Describe)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenVertexArrays(1, &dataLayout); // prepare to use glBindVertexArray(dataLayout); // load the data in the buffer glVertexAttribPointer(<<POS_ATTR>>, // 3 floats per pos 3, GL_FLOAT, // do not normalize GL_FALSE, // no gap 0, // from beg of buffer 0);

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

v0x v0y v0z v1x v1y v1z v2x v2y v2z v3x v3y v3z

Describe the data 12 numbers transferred for 4 3D verts, no colors

slide-4
SLIDE 4

Data Setup (Describe)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenVertexArrays(1, &dataLayout); // prepare to use glBindVertexArray(dataLayout); // load the data in the buffer glVertexAttribPointer(<<POS_ATTR>>, // 3 floats per pos 3, GL_FLOAT, // do not normalize GL_FALSE, // skipping 6 floats 6*sizeof(GLfloat), // from beg of buffer 0);

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

v0x v0y v0z ? ? ? v2x v2y v2z ? ? ?

Describe the data 12 numbers transferred for 2 3D verts, no colors

slide-5
SLIDE 5

Data Setup (Describe)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenVertexArrays(1, &dataLayout); // prepare to use glBindVertexArray(dataLayout); // load the data in the buffer glVertexAttribPointer(<<POS_ATTR>>, glVertexAttribPointer(<<COL_ATTR>>, // 3 floats per pos 3, GL_FLOAT, // 3 floats per pos 3, GL_FLOAT, // do not normalize GL_FALSE, // do not normalize GL_FALSE, // skipping 6 floats 6*sizeof(GLfloat), // skipping 6 floats 6*sizeof(GLfloat) // from beg of buffer 0); // start after vert0 3*sizeof(GLfloat))

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b

Describe the data 12 numbers transferred For 2 3D verts with colors

slide-6
SLIDE 6

Data Setup (Describe)

GLfloat vertices[12] = { // 12 floats

  • 0.90, -0.90, 0.85, -0.90, -0.90, 0.85,

0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; // request buff ID glGenVertexArrays(1, &dataLayout); // prepare to use glBindVertexArray(dataLayout); // load the data in the buffer glVertexAttribPointer(<<POS_ATTR>>, glVertexAttribPointer(<<COL_ATTR>>, // 3 floats per pos 3, GL_FLOAT, // 3 floats per pos 3, GL_FLOAT, // do not normalize GL_FALSE, // do not normalize GL_FALSE, // no gap 0, // no gap 0, // from beg of buffer 0); // skip all vert 6*sizeof(GLfloat))

CPU GPU

f0 f1 .. f10 f11

vertices

Read mesh on CPU

v0x v0y v0z v1x v1y v1z c0r c0g c0b c1r c1g c1b

Describe the data 12 numbers transferred For 2 3D verts with colors

slide-7
SLIDE 7

Vertex Shader

#version 330 core in vec4 vertexCoords; // in specifies input will come from vertex buffer in vec4 vertexColor;

  • ut vec4 fragmentColor; // out specifies that this will be sent to fragment shader

void main(void) { gl_Position = vertexCoords; // gl_Position must be set here for final coords fragmentColor = vec4(1, 0, 0, 1); // sending red color to fragment shader }

CPU GPU

f0 f1 .. f10 f11

vertices

v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b corei works

  • n vertex0

corej works

  • n vertex1
slide-8
SLIDE 8

Fragment Shader

#version 330 core in vec4 vertexCoords; in vec4 vertexColor;

  • ut vec4 fragmentColor;

void main(void) { gl_Position = vertexCoords; fragmentColor = vec4(1, 0, 0, 1); }

CPU GPU

vertices

corei works

  • n vertex0

corej works

  • n vertex1

#version 330 core in vec4 fragmentColor; // any out in VS sent // as in to FS

  • ut vec4 finalColor; // must have exactly one

// out – final color void main(void) { finalColor = fragmentColor; } f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b

slide-9
SLIDE 9

Fragment Shader

#version 330 core in vec4 vertexCoords; in vec4 vertexColor;

  • ut vec4 fragmentColor;

void main(void) { gl_Position = vertexCoords; fragmentColor = vec4(1, 0, 0, 1); }

CPU GPU

vertices

corei works

  • n vertex0

corej works

  • n vertex1

#version 330 core in vec4 fragmentColor; // any out in VS sent // as in to FS

  • ut vec4 finalColor; // must have exactly one

// out – final color void main(void) { finalColor = fragmentColor; } f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b

slide-10
SLIDE 10

Connecting Shader and Application

On GPU side (shader) #version 330 core in vec4 vertexCoords; in vec4 vertexColor;

  • ut vec4 fragmentColor;

void main(void) { . . . }

CPU GPU

f0 f1 .. f10 f11

vertices

v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b corei works

  • n vertex0

corej works

  • n vertex1

On CPU side (application progam) // need to load the prgram with attributes to set GLuint program = loadProgram("vert...glsl", "frag...glsl"); GLint posAttr = glGetAttribLocation( program, "vertexCoords" ); glVertexAttribPointer(posAttr, // describe attribute ); GLint colorAttr = glGetAttribLocation( program, "vertexColor" ); glVertexAttribPointer(colorAttr, // describe attribute ); glEnableVertexAttribArray(posAttr); glEnableVertexAttribArray(colorAttr);

slide-11
SLIDE 11

Uniform Variables

On GPU side (shader) #version 330 core in vec4 vertexCoords; in vec4 vertexColor; uniform mat4 someMatrix;

  • ut vec4 fragmentColor;

void main(void) { gl_Position = someMatrix*vertexCoords; }

CPU GPU

f0 f1 .. f10 f11

vertices

v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b corei works

  • n vertex0

corej works

  • n vertex1

On CPU side (application progam) // need to load the prgram with variable to set GLuint program = loadProgram("vert...glsl", "frag...glsl"); GLfloat translate[] = { 1, 0, 0, 5, .... }; GLuint matrixLoc = glGetUniformLocation(program, "someMatrix"); glUniformMatrix4fv(matrixLoc, // where in shader 1, // sending 1 matrix GL_TRUE, // convert to column-major translate); // the matrix to send

uniform: same for all vertices