Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing round points using modern OpenGL

Tags:

opengl

I know how to draw round points using fixed pipeline. However I need to do the same using modern OpenGL. Is it possible, or should I use point sprites and textures?

For the interested.Here is how it is done with fixed pipeline:

        glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_NOTEQUAL, 0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable( GL_POINT_SMOOTH );
    glPointSize( 8.0 );

    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(myMatrix);
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(myAnotherMatrix);

    glBegin(GL_POINTS);
    glColor3f(1,1,1);

    glVertex3fv(position);


    glEnd();
    glDisable(GL_POINT_SMOOTH);
    glBlendFunc(GL_NONE, GL_NONE);
    glDisable(GL_BLEND);
like image 718
Michael IV Avatar asked Jun 24 '13 11:06

Michael IV


People also ask

How do you draw points in OpenGL?

One way to draw primitives is to use the glBegin command to tell OpenGL to begin interpreting a list of vertices as a particular primitive. You then end the list of vertices for that primitive with the glEnd command. glBegin, GL_POINTS tells OpenGL that the succeeding vertices are to be interpreted and drawn as points.

How do you draw a line in modern OpenGL?

To draw a line strip with N segments, 6*(N-1) vertices have tpo be processed. We have to create an "empty" Vertex Array Object (without any vertex attribute specification): glGenVertexArrays(1, &vao); glBindVertexArray(vao); And to draw 2*(N-1) triangle ( 6*(N-1) vertices):

How do I change the size of points in OpenGL?

When you draw a single point, the size of the point is one pixel by default. You can change this size with the function glPointSize: void glPointSize(GLfloat size); The glPointSize function takes a single parameter that specifies the approximate diameter in pixels of the point drawn.

How can I render Bezier curves in modern OpenGL?

The question is: How can i render bezier curves in Modern Opengl? I'm using C++. The bèzier equations are very simple. You basically take the line segment created by the points, get the lines between them, then you linearly interpolate a point between the starting and ending point of each line. This way you get one less point than before.

Can OpenGL be used to draw complex geometry?

First, let me mention that OpenGL is a low level API, this means that it has no support for drawing complex geometrical objects. It is the programmer’s job to combine the geometrical primitives from OpenGL in complex shapes and bodies. The basic geometrical primitives that the core OpenGL profile provide to us are points, lines and triangles.

What are the basic geometrical primitives provided by OpenGL?

The basic geometrical primitives that the core OpenGL profile provide to us are points, lines and triangles. For simplicity, we are going to use only two dimensional drawings in this article, but keep in mind that OpenGL allows us to represent three dimensional objects, more on this in a future article.

Is a triangle front or back facing in OpenGL?

By default, in OpenGL, a triangle with his vertices stored in counterclockwise order is said to be front facing. Why is this distinction important? We can instruct OpenGL to render only one of the two faces of a triangle surface (the front or the back); the default is to render both faces.


2 Answers

One way would be to draw point sprites with a circle-texture and a self-made alpha test in the fragment shader:

uniform sampler2D circle;

void main()
{
    if(texture(circle, gl_PointCoord).r < 0.5)
        discard;
    ...
}

But in fact you don't even need a texture for this, since a circle is a pretty well-defined mathematical concept. So just check the gl_PointCoord only, which says in which part of the [0,1] square representing the whole point your current fragment is:

vec2 coord = gl_PointCoord - vec2(0.5);  //from [0,1] to [-0.5,0.5]
if(length(coord) > 0.5)                  //outside of circle radius?
    discard;
like image 84
Christian Rau Avatar answered Oct 19 '22 13:10

Christian Rau


  • Drawing circle with shader.
  • OpenGL ES shader sphere.
like image 7
Cfr Avatar answered Oct 19 '22 14:10

Cfr