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 228<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, sizeof(data), data, GL_STATIC_DRAW);<br />

//Draw Triangle from VBO - do each time window, view point or data<br />

changes<br />

//Establish its 3 coordinates per vertex with zero stride in this<br />

array; necessary here<br />

glVertexPointer(3, GL_FLOAT, 0, NULL);<br />

//Make the new VBO active. Repeat here incase changed since<br />

initialisation<br />

glBindBuffer(GL_ARRAY_BUFFER, triangleVBO);<br />

//Establish array contains vertices (not normals, colours, texture<br />

coords etc)<br />

glEnableClientState(GL_VERTEX_ARRAY);<br />

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

glDrawArrays(GL_TRIANGLES, 0, sizeof(data) / sizeof(float) / 3);<br />

//Force display to be drawn now<br />

glFlush();<br />

Example usage in C Using OpenGL 3.x and OpenGL 4.x<br />

Function which can read any text or binary file into char buffer:<br />

/* Function will read a text file into allocated char buffer<br />

*/<br />

char* filetobuf(char *file)<br />

{<br />

are */<br />

FILE *fptr;<br />

long length;<br />

char *buf;<br />

fptr = fopen(file, "r"); /* Open file for reading */<br />

if (!fptr) /* Return NULL on failure */<br />

return NULL;<br />

fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */<br />

length = ftell(fptr); /* Find out how many bytes into the file we<br />

buf = malloc(length+1); /* Allocate a buffer for the entire length<br />

of the file and a null terminator */<br />

*/<br />

fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file

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

Saved successfully!

Ooh no, something went wrong!