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 230<br />

GLuint shaderProgram;<br />

/* These pointers will receive the contents of our shader source code<br />

files */<br />

GLchar *vertexSource, *fragmentSource;<br />

/* These are handles used to reference the shaders */<br />

GLuint vertexShader, fragmentShader;<br />

const unsigned int shaderAtribute = 0;<br />

const float NUM_OF_VERTICES_IN_DATA=3;<br />

/* Vertices of a triangle (counter-clockwise winding) */<br />

float data[3][3] = {<br />

};<br />

{ 0.0, 1.0, 0.0 },<br />

{ -1.0, -1.0, 0.0 },<br />

{ 1.0, -1.0, 0.0 }<br />

/*---------------------- Initialise VBO - (Note: do only once, at start<br />

of program) ---------------------*/<br />

/* Create a new VBO and use the variable "triangleVBO" to store the VBO<br />

id */<br />

glGenBuffers(1, &triangleVBO);<br />

/* Make the new VBO active */<br />

glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);<br />

/* Upload vertex data to the video device */<br />

glBufferData(GL_ARRAY_BUFFER, NUM_OF_VERTICES_IN_DATA * 3 *<br />

sizeof(float), data, GL_STATIC_DRAW);<br />

/* Specify that our coordinate data is going into attribute index<br />

0(shaderAtribute), and contains three floats per vertex */<br />

glVertexAttribPointer(shaderAtribute, 3, GL_FLOAT, GL_FALSE, 0, 0);<br />

/* Enable attribute index 0(shaderAtribute) as being used */<br />

glEnableVertexAttribArray(shaderAtribute);<br />

/* Make the new VBO active. */<br />

glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);<br />

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

/*--------------------- Load Vertex and Fragment shaders from files and<br />

compile them --------------------*/

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

Saved successfully!

Ooh no, something went wrong!