07.01.2013 Views

3D graphics eBook - Course Materials Repository

3D graphics eBook - Course Materials Repository

3D graphics eBook - Course Materials Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Vertex Buffer Object 231<br />

/* Read our shaders into the appropriate buffers */<br />

vertexSource = filetobuf("exampleVertexShader.vert");<br />

fragmentSource = filetobuf("exampleFragmentShader.frag");<br />

/* Assign our handles a "name" to new shader objects */<br />

vertexShader = glCreateShader(GL_VERTEX_SHADER);<br />

fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);<br />

/* Associate the source code buffers with each handle */<br />

glShaderSource(vertexShader, 1, (const GLchar**)&vertexSource, 0);<br />

glShaderSource(fragmentShader, 1, (const GLchar**)&fragmentSource, 0);<br />

/* Compile our shader objects */<br />

glCompileShader(vertexShader);<br />

glCompileShader(fragmentShader);<br />

/*-------------------------------------------------------------------------------------------------------*/<br />

/*-------------------- Create shader program, attach shaders to it and<br />

then link it ---------------------*/<br />

/* Assign our program handle a "name" */<br />

shaderProgram = glCreateProgram();<br />

/* Attach our shaders to our program */<br />

glAttachShader(shaderProgram, vertexShader);<br />

glAttachShader(shaderProgram, fragmentShader);<br />

/* Bind attribute index 0 (shaderAtribute) to in_Position*/<br />

/* "in_Position" will represent "data" array's contents in the vertex<br />

shader */<br />

glBindAttribLocation(shaderProgram, shaderAtribute, "in_Position");<br />

/* Link shader program*/<br />

glLinkProgram(shaderProgram);<br />

/*-------------------------------------------------------------------------------------------------------*/<br />

/* Set shader program as being actively used */<br />

glUseProgram(shaderProgram);<br />

/* Set background colour to BLACK */<br />

glClearColor(0.0, 0.0, 0.0, 1.0);<br />

/* Clear background with BLACK colour */<br />

glClear(GL_COLOR_BUFFER_BIT);<br />

/* Actually draw the triangle, giving the number of vertices provided<br />

by invoke glDrawArrays<br />

while telling that our data is a triangle and we want to draw 0-3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!