<p>If your intention is to render some textured or colored triangles and points colored diferently at the same time at vertex postions with one draw call:</p>

<pre><code>glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);
</code></pre>

<p>In order to access vertex data in vertex shader, you can create vertex array object which references to vertex buffer object with data you provided (vertices) for example at index 0.</p>

<p>then in vertex shader you have:</p>

<pre><code>layout (location = 0) in vec3 vPosition; //positions for each vertex
</code></pre>

<p>You might then do something like that (vertex shader):</p>

<pre><code>flat out vec3 fpos; //not interpolated vertices
out vec3 pos; //interpolated vertices (for default)
void main(void)
{
  pos = vPosition;
  fpos = vPosition;
}
</code></pre>

<p>And then in fragment shader:</p>

<pre><code>if(length(fpos-pos)&lt;threshold) //small threshold
 fragColor = pointColor;
else
 fragColor = triangleColor;
</code></pre>

CC BY-SA 3.0
2016-08-21T09:57:34.137Z
2016-08-21T09:57:34.137Z
mdkdy
<p>It's totally possible to re-use vertices. That's why when making a flat rectangle plane with four corners that there can be two triangles rendered to construct the rectangle, and the triangles share two of the vertices.</p>

CC BY-SA 3.0
2016-08-28T01:11:30.527Z
2016-08-28T01:11:30.527Z
trusktr
<p>I'm passing my vertex shader a bunch of vertices and color data. I would like to first render the triangles and then render a point at each vertex. The triangles render fine, but I can't think of a way to render the triangles and the points without making and calling a whole new method for rendering points, which I guess I could do, but I'd like to know if there is some shader magic that accomplish it for me.</p>

<p>Thanks in advance!</p>

<p>This is my vs:</p>

<pre><code>#version 120

uniform mat4 projection;

void main() {

gl_Position = projection * ftransform();
gl_PointSize = 1.0;
gl_FrontColor = gl_Color;

}
</code></pre>

<p>This is my fs:</p>

<pre><code>#version 120

#define point_color vec3(1,1,1)

void main() {

vec4 color = gl_Color;
gl_FragColor = vec4(color);

}
</code></pre>

<p>This is the code behind it. I'm using lwjgl.</p>

<pre><code>public void render() {

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnable(GL_PROGRAM_POINT_SIZE);

    glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, c_id);
    glColorPointer(4, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
    glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);

    glDrawArrays(GL_POINTS, 0, draw_count_v);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisable(GL_PROGRAM_POINT_SIZE);
}
</code></pre>

<opengl><rendering><glsl>
Can I use the same vertices to render multiple things?
GodProbablyExists
# Can I use the same vertices to render multiple things?

(Asked by: username_0 on 2016-08-21)

I'm passing my vertex shader a bunch of vertices and color data. I would like to first render the triangles and then render a point at each vertex. The triangles render fine, but I can't think of a way to render the triangles and the points without making and calling a whole new method for rendering points, which I guess I could do, but I'd like to know if there is some shader magic that accomplish it for me.

Thanks in advance!

This is my vs:

```
#version 120

uniform mat4 projection;

void main() {

gl_Position = projection * ftransform();
gl_PointSize = 1.0;
gl_FrontColor = gl_Color;

}

```

This is my fs:

```
#version 120

#define point_color vec3(1,1,1)

void main() {

vec4 color = gl_Color;
gl_FragColor = vec4(color);

}

```

This is the code behind it. I'm using lwjgl.

```
public void render() {

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnable(GL_PROGRAM_POINT_SIZE);

    glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, c_id);
    glColorPointer(4, GL_FLOAT, 0, 0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
    glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);

    glDrawArrays(GL_POINTS, 0, draw_count_v);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisable(GL_PROGRAM_POINT_SIZE);
}

```

**Tags:** opengl, rendering, glsl


---

## 2 answers

### Answer by username_1 on 2016-08-21. Score: 2 | Accepted answer

If your intention is to render some textured or colored triangles and points colored diferently at the same time at vertex postions with one draw call:

```
glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);

```

In order to access vertex data in vertex shader, you can create vertex array object which references to vertex buffer object with data you provided (vertices) for example at index 0.

then in vertex shader you have:

```
layout (location = 0) in vec3 vPosition; //positions for each vertex

```

You might then do something like that (vertex shader):

```
flat out vec3 fpos; //not interpolated vertices
out vec3 pos; //interpolated vertices (for default)
void main(void)
{
  pos = vPosition;
  fpos = vPosition;
}

```

And then in fragment shader:

```
if(length(fpos-pos)<threshold) //small threshold
 fragColor = pointColor;
else
 fragColor = triangleColor;

```


---

### Answer by username_2 on 2016-08-28. Score: 0

It's totally possible to re-use vertices. That's why when making a flat rectangle plane with four corners that there can be two triangles rendered to construct the rectangle, and the triangles share two of the vertices.


---

