View Full Version : Draw crosses instead of points

paule

08-03-2010, 09:00 AM

Hello!

I'm displaying a function plot in a OpenGL widget (using QT). It's fully 2D.

Now I'd like to add some function intersections points to the scene (approx. 50 - 1000). Instead of using points as with glBegin(GL_POINTS) I'd like to use small crosses.

The scene will be static, coordinates will be provided by two double vectors.

My approach is:

- Record one cross (basically two lines) to a display list (glNewList())

- Then call translate2f(..) for each point and then glCallList(..)

As far as I understood OpenGL works as a state machine, so after setting the first point, I'll have to reset the translate statement somehow, is that true?

Is this a good way to achieve this, or are there predefined methods I can use which are faster/smarter/easier?

(Another question: How do I convert from OpenGL scene coordinates to pixels and back?)

Thank you for your time,

Paule

strattonbrazil

08-03-2010, 02:46 PM

If you're going to be using immediate mode and display lists, I'd limit the number of calls to glTranslate and just add them in the glVertex call. If you're not moving them around all the time, you can wrap that in a display list.

glNewList(...)

glBegin(GL_LINES)

foreach point(x,y)

drawCross at (x,y)

glEnd()

glEndList()

If you want to move the points around, you can save off your stack before translation and then get rid of it.

glPushMatrix()

glTranslate(x,y)

... // draw at (x,y)

glPopMatrix() // restores matrix to what it was when pushed

What do you mean by OpenGL scene coordinates? What's a scene coordinate? World space? Camera space? Model space? Before the modelview/projection transformations of the vertices?

Rosario Leonardi

08-03-2010, 02:50 PM

Your solution is easy to implement

for(i = 0; i < number of point; i++){

glPushMatrix();

glTranslate2f(pointPosition[i].x, pointPosition[i].y);

glCallList(crossCL);

glPopMatrix();

}

A better solution can be to use point sprite. Basically is a texture attached to the point. So you can load a texture of a cross (or whatever you like) and then render a bunch of points using directly the point position, so you can directly use vertex array or even VBO. In this way you issue only a draw call.

Search point sprite on Google, it's not difficult to find a tutorial.

ugluk

08-04-2010, 04:07 AM

Hey, can you use a point sprites as impostors/billboards?

paule

08-04-2010, 06:21 AM

Thank you for your answers. Seems like using push/pop matrix to restore coordinates is the way to go ... until I try that point sprite thingy.

What do you mean by OpenGL scene coordinates? What's a scene coordinate? World space? Camera space? Model space? Before the modelview/projection transformations of the vertices?

Correct, if I'm wrong:

World space is the topmost coordinate system (which isnt limited by OpenGL, is it?). Camera space is relative to the world space, defined by the viewport. And model space is relative to the a defned point wihtin a model object.

What I mean is: I'd like to convert a pixel of my window (camera space I guesss) to a coordinate in world space.

strattonbrazil

08-04-2010, 08:23 AM

I don't know what topmost means. When you read in a model like an OBJ, it's coordinates are in model space. When it's multiplied by the modelview matrix, it changes it from model space to camera space. This is because the model-view matrix holds both the glTranslate's and such to move the model into world space and the camera transformations to move it into camera space. The projection matrix then moves it into clipping space, then normalized-device coordinate space, then screen space.

If you want to go from screen space to world space, you can use gluUnProject and pass it your matrices. Just be sure the model-view matrix you pass it doesn't contain the transformations for individual models like glTranslate. Just your camera transformation. The former will give you model coordinates while the latter will give you world coordinates.

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.