[Mesa-dev] [demos PATCH] geom-stipple-lines: use VBO, specify number of verts on cmd line

Brian Paul brianp at vmware.com
Thu Oct 30 08:31:44 PDT 2014


If no command line argument is given, draw 50 points (25 lines).
---
 src/glsl/geom-stipple-lines.c |   59 ++++++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/src/glsl/geom-stipple-lines.c b/src/glsl/geom-stipple-lines.c
index 8b051a3..fa24605 100644
--- a/src/glsl/geom-stipple-lines.c
+++ b/src/glsl/geom-stipple-lines.c
@@ -21,8 +21,7 @@ static GLboolean Anim = GL_TRUE;
 static GLboolean UseGeomShader = GL_TRUE;
 static GLfloat Xrot = 0, Yrot = 0;
 static int uViewportSize = -1, uStippleFactor = -1, uStipplePattern = -1;
-static const int NumPoints = 50;
-static float Points[100][3], Colors[100][3];
+static int NumPoints = 50;
 
 static const GLushort StipplePattern = 0x10ff;
 static GLuint StippleFactor = 2;
@@ -66,8 +65,6 @@ SetStippleUniform(GLint factor, GLushort pattern)
 static void
 Redisplay(void)
 {
-   int i;
-
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
    glPushMatrix();
@@ -83,12 +80,7 @@ Redisplay(void)
       glEnable(GL_LINE_STIPPLE);
    }
 
-   glBegin(GL_LINES);
-   for (i = 0; i < NumPoints; i++) {
-      glColor3fv(Colors[i]);
-      glVertex3fv(Points[i]);
-   }
-   glEnd();
+   glDrawArrays(GL_LINES, 0, NumPoints / 2);
 
    glPopMatrix();
 
@@ -174,17 +166,36 @@ Key(unsigned char key, int x, int y)
 
 
 static void
-MakePoints(void)
+MakePointsVBO(void)
 {
+   struct vert {
+      GLfloat pos[3];
+      GLfloat color[3];
+   };
+   struct vert *v;
+   GLuint vbo;
    int i;
+
+   glGenBuffers(1, &vbo);
+   glBindBuffer(GL_ARRAY_BUFFER, vbo);
+   glBufferData(GL_ARRAY_BUFFER, NumPoints * sizeof(struct vert),
+                NULL, GL_STATIC_DRAW);
+
+   v = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    for (i = 0; i < NumPoints; i++) {
-      Colors[i][0] = (rand() % 1000) / 1000.0;
-      Colors[i][1] = (rand() % 1000) / 1000.0;
-      Colors[i][2] = (rand() % 1000) / 1000.0;
-      Points[i][0] = ((rand() % 2000) - 1000.0) / 500.0;
-      Points[i][1] = ((rand() % 2000) - 1000.0) / 500.0;
-      Points[i][2] = ((rand() % 2000) - 1000.0) / 500.0;
+      v[i].color[0] = (rand() % 1000) / 1000.0;
+      v[i].color[1] = (rand() % 1000) / 1000.0;
+      v[i].color[2] = (rand() % 1000) / 1000.0;
+      v[i].pos[0] = ((rand() % 2000) - 1000.0) / 500.0;
+      v[i].pos[1] = ((rand() % 2000) - 1000.0) / 500.0;
+      v[i].pos[2] = ((rand() % 2000) - 1000.0) / 500.0;
    }
+   glUnmapBuffer(GL_ARRAY_BUFFER);
+
+   glVertexPointer(3, GL_FLOAT, sizeof(struct vert), (void *) 0);
+   glEnable(GL_VERTEX_ARRAY);
+   glColorPointer(3, GL_FLOAT, sizeof(struct vert), (void *) sizeof(float[3]));
+   glEnable(GL_COLOR_ARRAY);
 }
 
 
@@ -299,7 +310,7 @@ Init(void)
    glLineStipple(StippleFactor, StipplePattern);
    SetStippleUniform(StippleFactor, StipplePattern);
 
-   MakePoints();
+   MakePointsVBO();
 }
 
 
@@ -307,6 +318,18 @@ int
 main(int argc, char *argv[])
 {
    glutInit(&argc, argv);
+
+   if (argc > 1) {
+      int n = atoi(argv[1]);
+      if (n > 0) {
+         NumPoints = n;
+      }
+      else {
+         printf("Invalid number of points\n");
+         return 1;
+      }
+   }
+
    glutInitWindowSize(WinWidth, WinHeight);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    Win = glutCreateWindow(argv[0]);
-- 
1.7.10.4



More information about the mesa-dev mailing list