2
$\begingroup$

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);
}
$\endgroup$

2 Answers 2

2
$\begingroup$

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;
$\endgroup$
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.