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, 0.85, -0.90, -0.90, 0.85, ● 4 3D vertices? 0.90, -0.85, 0.90, 0.90, -0.85, 0.90 }; Transfer data to GPU // 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
Data Setup (Describe) CPU GPU vertices f0 f1 .. f10 f11 v0x v0y v1x v1y v2x v2y v3x v3y v4x v4y v5x v5y 12 numbers transferred Read mesh on CPU for 6 2D verts, no colors 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 }; Describe the data // 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);
Data Setup (Describe) CPU GPU vertices f0 f1 .. f10 f11 v0x v0y v0z v1x v1y v1z v2x v2y v2z v3x v3y v3z 12 numbers transferred Read mesh on CPU for 4 3D verts, no colors 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 }; Describe the data // 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);
Data Setup (Describe) CPU GPU vertices f0 f1 .. f10 f11 v0x v0y v0z ? ? ? v2x v2y v2z ? ? ? 12 numbers transferred Read mesh on CPU for 2 3D verts, no colors 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 }; Describe the data // 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);
Data Setup (Describe) CPU GPU vertices f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b 12 numbers transferred Read mesh on CPU For 2 3D verts with colors 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 }; Describe the data // 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))
Data Setup (Describe) CPU GPU vertices f0 f1 .. f10 f11 v0x v0y v0z v1x v1y v1z c0r c0g c0b c1r c1g c1b 12 numbers transferred Read mesh on CPU For 2 3D verts with colors 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 }; Describe the data // 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))
Vertex Shader CPU GPU vertices corei works corej works on vertex0 on vertex1 f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b #version 330 core in vec4 vertexCoords; // in specifies input will come from vertex buffer in vec4 vertexColor; out 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 }
Fragment Shader CPU GPU vertices corei works corej works on vertex0 on vertex1 f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b #version 330 core #version 330 core in vec4 fragmentColor; // any out in VS sent in vec4 vertexCoords; // as in to FS in vec4 vertexColor; out vec4 finalColor; // must have exactly one // out – final color out vec4 fragmentColor; void main(void) { void main(void) finalColor = fragmentColor; { } gl_Position = vertexCoords; fragmentColor = vec4(1, 0, 0, 1); }
Fragment Shader CPU GPU vertices corei works corej works on vertex0 on vertex1 f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b #version 330 core #version 330 core in vec4 fragmentColor; // any out in VS sent in vec4 vertexCoords; // as in to FS in vec4 vertexColor; out vec4 finalColor; // must have exactly one // out – final color out vec4 fragmentColor; void main(void) { void main(void) finalColor = fragmentColor; { } gl_Position = vertexCoords; fragmentColor = vec4(1, 0, 0, 1); }
Connecting Shader and Application CPU GPU vertices corei works corej works on vertex0 on vertex1 f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b On CPU side (application progam) On GPU side (shader) // need to load the prgram with attributes to set #version 330 core GLuint program = loadProgram("vert...glsl", "frag...glsl"); in vec4 vertexCoords; GLint posAttr = glGetAttribLocation( program, "vertexCoords" ); glVertexAttribPointer( posAttr , // describe attribute ); GLint colorAttr = glGetAttribLocation( program, "vertexColor" ); in vec4 vertexColor; glVertexAttribPointer( colorAttr , // describe attribute ); glEnableVertexAttribArray( posAttr ); glEnableVertexAttribArray( colorAttr ); out vec4 fragmentColor; void main(void) { . . . }
Uniform Variables CPU GPU vertices corei works corej works on vertex0 on vertex1 f0 f1 .. f10 f11 v0x v0y v0z c0r c0g c0b v1x v1y v1z c1r c1g c1b On CPU side (application progam) On GPU side (shader) #version 330 core in vec4 vertexCoords; in vec4 vertexColor; // need to load the prgram with variable to set GLuint program = loadProgram("vert...glsl", "frag...glsl"); uniform mat4 someMatrix; GLfloat translate[] = { 1, 0, 0, 5, .... }; GLuint matrixLoc = glGetUniformLocation(program, "someMatrix" ); out vec4 fragmentColor; glUniformMatrix4fv( matrixLoc , // where in shader 1, // sending 1 matrix void main(void) GL_TRUE, // convert to column-major { translate); // the matrix to send gl_Position = someMatrix*vertexCoords; } uniform : same for all vertices
Recommend
More recommend