Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4103168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:55:25+00:00 2026-05-20T20:55:25+00:00

I am new to OpenGL as learning exercise I decided to draw a set

  • 0

I am new to OpenGL as learning exercise I decided to draw a set of horizontal lines from a grid of m x n matrix containing the vertices locations

This is what I have
enter image description here

and If I use LINE_STRIP
enter image description here

A code snippet using vertex arrays and indices will be great, I cant seem to be able to get the concept just from a text book I need to see and play with a code example
Any help will be much appreciated!


@Thomas
Got it working with the following code

totalPoints = GRID_ROWS * 2 * (GRID_COLUMNS - 1);
indices = new int[totalPoints];

points = new GLModel(this, totalPoints, LINES, GLModel.DYNAMIC);
int n = 0;
points.beginUpdateVertices();
for ( int row = 0; row < GRID_ROWS; row++ ) {
  for ( int col = 0; col < GRID_COLUMNS - 1; col++ ) {
    int rowoffset = row * GRID_COLUMNS;
    int n0 = rowoffset + col;
    int n1 = rowoffset + col + 1;

    points.updateVertex( n, pointsPos[n0].x, pointsPos[n0].y, pointsPos[n0].z );
    indices[n] = n0;
    n++;

    points.updateVertex( n, pointsPos[n1].x, pointsPos[n1].y, pointsPos[n1].z );
    indices[n] = n1;
    n++;
  }
}
points.endUpdateVertices();

Then I update and draw by doing

points.beginUpdateVertices();
for ( int n = 0; n < totalPoints; n++ ) {
   points.updateVertex( n, pointsPos[indices[n]].x, pointsPos[indices[n]].y, pointsPos[indices[n]].z );
}
points.endUpdateVertices();

This is the result

enter image description here


Fix it by changing the nested for loop

for ( int col = 0; col < GRID_COLUMNS; col++ ) {
for ( int row = 0; row < GRID_ROWS - 1; row++ ) {
    int offset = col * GRID_ROWS;
    int n0 = offset + row;
    int n1 = offset + row + 1;
    indices[n++] = n0;
    indices[n++] = n1;
  }
}

Now I can have any number of rows and columns

Thanks agin!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:55:26+00:00Added an answer on May 20, 2026 at 8:55 pm

    You need to draw a line for each segment and resuse an index, i.e. for the first part you’d draw a line for (0,1), (1,2), (2,3) and so on.

    Edit:

    Suppose you have a 4×5 array (4 lines, 5 vertices per line). You could then calculate the indices like this (pseudo code):

    Vertex[] v = new Vertex[20]; // 20 vertices in the grid
    for(int row = 0; row < numrows; row++) // numrows = 4
    {
      int rowoffset = row * numcols ; //0, 4, 8, 12
      for(int col = 0; col < (numcols - 1); col++) //numcols = 5
      {
         addLineIndices(rowoffset + col, rowoffset + col +1); //adds (0,1), (1,2), (2,3) and (3, 4) for the first row
      }
    }
    

    Then issue the draw call for numrows * (numcols - 1) linesegments (GL_LINES), i.e. 16 in the example. Note that addLineIndices would be a function that adds the index pair for one line segment to an index array which is then supplied to the draw call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to opengl. As a learning exercise I started manually building some basic
I am new to OpenGL and Qt, and I am learning both simultaneously(3 days
I'm new to opengl-es and I wonder how people are able to draw these
What are some good resources for learning OpenGL 4.1 aimed at someone relatively new
I am new to opengl I tried to make an isosurface by reading from
I am completely new to openGl. I have so far studied how to draw
Having only recently started learning the 'new' OpenGL (programmable as opposed to fixed-function, I
I am really really new to opengl, and I am learning basics now. I
I am new to OpenGL. Wondering if there is any good Scenegraph API/framework for
I'm somewhat new to OpenGL. I've used it for extremely simple stuff like rendering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.