Error changing projection matrix?


I’m trying to draw a rectangle in a perspective projection to show the area selected by the user when he wants to select the objects inside the rectangle drawn but the rectangle is not shown.

Here’s the code.

public void DrawRectangleSelection(Point point1, Point point2)
	// Store the projection matrix
	// Change the projection to orthographic
	Gl.glOrtho(-20, 20, -20, 20, -50, 50);
	// Draw the rectangle
	Vector3d wp1 = UnProject(point1.X, point1.Y, 0);
	Vector3d wp2 = UnProject(point2.X, point2.Y, 0);

	Gl.glVertex3d(wp1.X, wp1.Y, 0);
	Gl.glVertex3d(wp1.X, wp2.Y, 0);
	Gl.glVertex3d(wp2.X, wp2.Y, 0);
	Gl.glVertex3d(wp2.X, wp1.Y, 0);
	// Restore the Projection matrix
	// Restore the ModelView matrix

There’s some extra code before the method is called that calls the glBegin(), and so on.

The custom method UnProject() works well.

Do I’ve some error in my code? I suspect I’ve an error changing the projection matrix.


PD: I’m using TAO Framework.

can you use
i m not sure but -50, 50);
may cause some problem
may be use gluOrtho2D
and read gluOrtho2D spec
if u don’t know gluOrtho2D.

You need Gl.glBegin(GL_QUADS), or some other primitive type before you call glVertex*, and a glEnd() afterwards.

i haven’t notice

Hi Todayman,

In my post I wrote:

There’s some extra code before the method is called that calls the glBegin(), and so on.

Didn’t notic that, but…

The glBegin() and end must be immediately around the glVertex commands. Matrix manipulation commands are not allowed to be between the glBegin and glEnd. When you call these function, OpenGL is probably getting an error and ignoring the primitives defined by that begin/end block.

Only normal, texture coordinate, color, attribute, and fog settings (which are per-vertex) can be set within the block.

Hi Todayman!

That’s exactly what’s happening glBegin() must be immediately around the glVertex().

Now it works!

Thank you guys :slight_smile: