132001f49Smrg/* 232001f49Smrg * Test glMapBuffer() and glMapBufferRange() 332001f49Smrg * 432001f49Smrg * Fill a VBO with vertex data to draw several colored quads. 532001f49Smrg * On each redraw, update the geometry for just one rect in the VBO. 632001f49Smrg * 732001f49Smrg * Brian Paul 832001f49Smrg * 4 March 2009 932001f49Smrg */ 1032001f49Smrg 1132001f49Smrg 1232001f49Smrg#include <assert.h> 1332001f49Smrg#include <math.h> 1432001f49Smrg#include <stdio.h> 1532001f49Smrg#include <stdlib.h> 1632001f49Smrg#include <string.h> 1732001f49Smrg#include <GL/glew.h> 1832001f49Smrg#include <GL/glew.h> 1932001f49Smrg#include "glut_wrap.h" 2032001f49Smrg 2132001f49Smrgstatic GLuint Win; 2232001f49Smrgstatic const GLuint NumRects = 10; 2332001f49Smrgstatic GLuint BufferID; 2432001f49Smrgstatic GLboolean Anim = GL_TRUE; 2532001f49Smrgstatic GLboolean UseBufferRange = GL_FALSE; 2632001f49Smrg 2732001f49Smrg 2832001f49Smrg 2932001f49Smrgstatic const float RectData[] = { 3032001f49Smrg /* vertex */ /* color */ 3132001f49Smrg 0, -1, 0, 1, 0, 0, 3232001f49Smrg 1, 0, 0, 1, 1, 0, 3332001f49Smrg 0, 1, 0, 0, 1, 1, 3432001f49Smrg -1, 0, 0, 1, 0, 1 3532001f49Smrg}; 3632001f49Smrg 3732001f49Smrg 3832001f49Smrg/** 3932001f49Smrg * The buffer contains vertex positions (float[3]) and colors (float[3]) 4032001f49Smrg * for 'NumRects' quads. 4132001f49Smrg * This function updates/rotates one quad in the buffer. 4232001f49Smrg */ 4332001f49Smrgstatic void 4432001f49SmrgUpdateRect(int r, float angle) 4532001f49Smrg{ 4632001f49Smrg float *rect; 4732001f49Smrg int i; 4832001f49Smrg 4932001f49Smrg assert(r < NumRects); 5032001f49Smrg 5132001f49Smrg glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferID); 5232001f49Smrg if (UseBufferRange) { 5332001f49Smrg GLintptr offset = r * sizeof(RectData); 5432001f49Smrg GLsizeiptr length = sizeof(RectData); 5532001f49Smrg GLbitfield access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT; 5632001f49Smrg float *buf = (float *) glMapBufferRange(GL_ARRAY_BUFFER_ARB, 5732001f49Smrg offset, length, access); 5832001f49Smrg rect = buf; 5932001f49Smrg } 6032001f49Smrg else { 6132001f49Smrg /* map whole buffer */ 6232001f49Smrg float *buf = (float *) 6332001f49Smrg glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); 6432001f49Smrg rect = buf + r * 24; 6532001f49Smrg } 6632001f49Smrg 6732001f49Smrg /* set rect verts/colors */ 6832001f49Smrg memcpy(rect, RectData, sizeof(RectData)); 6932001f49Smrg 7032001f49Smrg /* loop over four verts, updating vertices */ 7132001f49Smrg for (i = 0; i < 4; i++) { 7232001f49Smrg float x = 0.2 * RectData[i*6+0]; 7332001f49Smrg float y = 0.2 * RectData[i*6+1]; 7432001f49Smrg float xpos = -2.5 + 0.5 * r; 7532001f49Smrg float ypos = 0.0; 7632001f49Smrg 7732001f49Smrg /* translate and rotate vert */ 7832001f49Smrg rect[i * 6 + 0] = xpos + x * cos(angle) + y * sin(angle); 7932001f49Smrg rect[i * 6 + 1] = ypos + x * sin(angle) - y * cos(angle); 8032001f49Smrg } 8132001f49Smrg 8232001f49Smrg glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); 8332001f49Smrg} 8432001f49Smrg 8532001f49Smrg 8632001f49Smrgstatic void 8732001f49SmrgLoadBuffer(void) 8832001f49Smrg{ 8932001f49Smrg static int frame = 0; 9032001f49Smrg float angle = glutGet(GLUT_ELAPSED_TIME) * 0.001; 9132001f49Smrg UpdateRect(frame % NumRects, angle); 9232001f49Smrg frame++; 9332001f49Smrg} 9432001f49Smrg 9532001f49Smrg 9632001f49Smrgstatic void 9732001f49SmrgDraw(void) 9832001f49Smrg{ 9932001f49Smrg glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferID); 10032001f49Smrg glVertexPointer(3, GL_FLOAT, 24, 0); 10132001f49Smrg glEnableClientState(GL_VERTEX_ARRAY); 10232001f49Smrg 10332001f49Smrg glColorPointer(3, GL_FLOAT, 24, (void*) 12); 10432001f49Smrg glEnableClientState(GL_COLOR_ARRAY); 10532001f49Smrg 10632001f49Smrg glDrawArrays(GL_QUADS, 0, NumRects * 4); 10732001f49Smrg 10832001f49Smrg if (0) 10932001f49Smrg glFinish(); 11032001f49Smrg} 11132001f49Smrg 11232001f49Smrg 11332001f49Smrgstatic void 11432001f49SmrgDisplay(void) 11532001f49Smrg{ 11632001f49Smrg glClear(GL_COLOR_BUFFER_BIT); 11732001f49Smrg Draw(); 11832001f49Smrg glutSwapBuffers(); 11932001f49Smrg} 12032001f49Smrg 12132001f49Smrg 12232001f49Smrgstatic void 12332001f49SmrgReshape(int width, int height) 12432001f49Smrg{ 12532001f49Smrg glViewport(0, 0, width, height); 12632001f49Smrg glMatrixMode(GL_PROJECTION); 12732001f49Smrg glLoadIdentity(); 12832001f49Smrg glOrtho(-3.0, 3.0, -1.0, 1.0, -1.0, 1.0); 12932001f49Smrg glMatrixMode(GL_MODELVIEW); 13032001f49Smrg glLoadIdentity(); 13132001f49Smrg} 13232001f49Smrg 13332001f49Smrg 13432001f49Smrgstatic void 13532001f49SmrgIdle(void) 13632001f49Smrg{ 13732001f49Smrg LoadBuffer(); 13832001f49Smrg glutPostRedisplay(); 13932001f49Smrg} 14032001f49Smrg 14132001f49Smrg 14232001f49Smrgstatic void 14332001f49SmrgKey(unsigned char key, int x, int y) 14432001f49Smrg{ 14532001f49Smrg (void) x; 14632001f49Smrg (void) y; 14732001f49Smrg if (key == 'a') { 14832001f49Smrg Anim = !Anim; 14932001f49Smrg glutIdleFunc(Anim ? Idle : NULL); 15032001f49Smrg } 15132001f49Smrg else if (key == 's') { 15232001f49Smrg LoadBuffer(); 15332001f49Smrg } 15432001f49Smrg else if (key == 27) { 15532001f49Smrg glutDestroyWindow(Win); 15632001f49Smrg exit(0); 15732001f49Smrg } 15832001f49Smrg glutPostRedisplay(); 15932001f49Smrg} 16032001f49Smrg 16132001f49Smrg 16232001f49Smrgstatic void 16332001f49SmrgInit(void) 16432001f49Smrg{ 16532001f49Smrg GLuint BufferSize = NumRects * sizeof(RectData); 16632001f49Smrg float *buf; 16732001f49Smrg 16832001f49Smrg if (!glutExtensionSupported("GL_ARB_vertex_buffer_object")) { 16932001f49Smrg printf("GL_ARB_vertex_buffer_object not found!\n"); 17032001f49Smrg exit(0); 17132001f49Smrg } 17232001f49Smrg 17332001f49Smrg UseBufferRange = glutExtensionSupported("GL_ARB_map_buffer_range"); 17432001f49Smrg printf("Use GL_ARB_map_buffer_range: %c\n", "NY"[UseBufferRange]); 17532001f49Smrg 17632001f49Smrg printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 17732001f49Smrg 17832001f49Smrg /* initially load buffer with zeros */ 17932001f49Smrg buf = (float *) calloc(1, BufferSize); 18032001f49Smrg 18132001f49Smrg glGenBuffersARB(1, &BufferID); 18232001f49Smrg glBindBufferARB(GL_ARRAY_BUFFER_ARB, BufferID); 18332001f49Smrg glBufferDataARB(GL_ARRAY_BUFFER_ARB, BufferSize, buf, GL_DYNAMIC_DRAW_ARB); 18432001f49Smrg 18532001f49Smrg free(buf); 18632001f49Smrg} 18732001f49Smrg 18832001f49Smrg 18932001f49Smrgint 19032001f49Smrgmain(int argc, char *argv[]) 19132001f49Smrg{ 19232001f49Smrg glutInit(&argc, argv); 19332001f49Smrg glutInitWindowSize(800, 200); 19432001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); 19532001f49Smrg Win = glutCreateWindow(argv[0]); 19632001f49Smrg glewInit(); 19732001f49Smrg glewInit(); 19832001f49Smrg glutReshapeFunc(Reshape); 19932001f49Smrg glutKeyboardFunc(Key); 20032001f49Smrg glutDisplayFunc(Display); 20132001f49Smrg glutIdleFunc(Anim ? Idle : NULL); 20232001f49Smrg Init(); 20332001f49Smrg glutMainLoop(); 20432001f49Smrg return 0; 20532001f49Smrg} 206