Results 1 to 4 of 4

Thread: Calculate position and indices in vertex shader

  1. #1
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    17

    Calculate position and indices in vertex shader

    I know that I can calculate vertices in vertex shader. But how can I specify indices in the vertex shader. Outside of vertex shader I create GL_ELEMENT_ARRAY_BUFFER buffer for indices and GL_ARRAY_BUFFER buffer for vertices. But what when I calculate those in vertex shader, how do I specify what are vertices and what are indices?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    There is a built-in read-only integer variable available in vertex shader called "gl_VertexID" which stores an index of the vertex for the current shader's execution. All input attributes (which are read-only as well) that the shader receives - all those correspond to that particular vertex. So normally you do not need to calculate anything - it is all given as input.

  3. #3
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    17
    Quote Originally Posted by Yandersen View Post
    There is a built-in read-only integer variable available in vertex shader called "gl_VertexID" which stores an index of the vertex for the current shader's execution. All input attributes (which are read-only as well) that the shader receives - all those correspond to that particular vertex. So normally you do not need to calculate anything - it is all given as input.

    I will check this. My problem is that I want to draw thick lines. To do so I take from user points and those points I want to send to the vertex shader using buffers. Thing is when zoom is small I want to draw those points using GL_POINTS, but when zoom is big enough I want to draw thick lines which vertices are computed inside the vertex shader. I need to use the indices since some vertices are repeated. Is this possible?

  4. #4
    Senior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    Well, I personally draw regular grids without using attribute inputs at all: to get the y coordinate of the point I divide gl_VertexID by the width of the grid, and to get x coordinate I take a modulo gl_VertexID % Width. The height of the point I fetch from the texture at the resulting coordinates. Therefore it is possible to use just an index buffer to feed vertex shader with indices if you know how to calculate attributes based on those numbers. Must mention that even if all attributes are disabled you still need to have a non-zero VAO bound to make a successful draw call.

Similar Threads

  1. Replies: 4
    Last Post: 01-19-2018, 02:38 PM
  2. Replies: 3
    Last Post: 10-24-2017, 12:46 PM
  3. Calculate Normals in vertex shader?
    By Mars_999 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-12-2014, 06:22 AM
  4. Calculate vertex norm with compute shader
    By imported_tonyo_au in forum OpenGL: GLSL
    Replies: 0
    Last Post: 07-06-2013, 11:49 PM
  5. Vertex position in fragment shader
    By Vadim in forum OpenGL: GLSL
    Replies: 10
    Last Post: 01-15-2010, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean