Advertisement

2 questions

Started by November 19, 2004 05:34 PM
6 comments, last by Coconut Crab 20 years ago
Question #1 gluProject() takes a vertex's 3D coordinate and calculates its raster(window) coordinate. I am wondering, is there a function to calculate just the vertex's 2D coordinate from its 3D coordinate, and then another function to transform the 2D coordinate to raster coordinate? Question #2 If I draw a rectangle while giving a different color to its 4 vertexes, OpenGL can do the beautiful linear interpolation of colors for this rectangle. Is there a function that does linear color interpolation in the 2D space instead of 3D, in OpenGL? I mean, is there such a function in OpenGL, that if I give 4 raster coordinates with 4 different colors, this function can also draw the rectangle with color interpolation among all the 4 points? Thank you in advance for your help. Jack
  1. What do you mean by the 2D coordinate? In what coordinate system?

  2. OpenGL is a 3D graphics library. It only draws 3D graphics. However, if you use an axis-aligned orthographic projection 3D graphics effectively become 2D graphics (except with depth). This may sound difficult, but all you need to do is call gluOrtho2D(0, windowWidth, 0, windowHeight) instead of calling gluPerspective. Then simply draw your rectangle as you would in 3D, but with a z coordinate of zero (which is implicit if you use glVertex2{sifd}[v].


Enigma
Advertisement
#1
Yes, but isn't that a bit redundant.
gluProject requiers you to send it the different matrices, you could for some reason tamper a bit with one of them to acheve the desired result.
Read more on it here http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/project.html

#2
Open gl is 2d, the third cordinate is just an interpolated value sent to the z buffer.
To draw at raster cordinates you might want to set the projectionmatrix to ortho mode

These two functions sets the projection matrix so that you get 2d redering that can have raster cordinates (if you set it up corectly) temporarily.
void ViewOrtho(int x, int y)							// Set Up An Ortho View{	glMatrixMode(GL_PROJECTION);					// Select Projection	glPushMatrix();							// Push The Matrix	glLoadIdentity();						// Reset The Matrix	glOrtho( 0, x , y , 0, -1, 1 );				// Select Ortho Mode 	glMatrixMode(GL_MODELVIEW);					// Select Modelview Matrix	glPushMatrix();							// Push The Matrix	glLoadIdentity();						// Reset The Matrix}void ViewPerspective(void)							// Set Up A Perspective View{	glMatrixMode( GL_PROJECTION );					// Select Projection	glPopMatrix();							// Pop The Matrix	glMatrixMode( GL_MODELVIEW );					// Select Modelview	glPopMatrix();							// Pop The Matrix}


read up on glOrtho here
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/ortho.html
Hi, thank you both for your helps.

To Enigma:

If I use gluOrtho2D(0, windowWidth, 0, windowHeight) as you have suggested with my client window = 500 x 500, will the coordinates at the 4 corner of the window be (-250, 250, z), (250, 250, z), (250, -250, z), (-250, -250, z), suppose I view from z = 1 and view at (0,0,0)?

My first question is asking if there's a way to describe the object's own coordinate system in terms of my own coordinate system, without the need of z value in my own coordinate system. It's kind of like the window's coordinates, but with (0, 0) at the center of the window and maintains the same x and y's scale as in OpenGL's space. For example, my 2D coordinate system would have (0, 0) at the center of the window, and the 4 coordinates at the 4 corners of the window are always the same, no matter if the window has been resized or not, so it's pixel unrelated.

To lc_lord:

Do you mind giving me an example of how to "tamper a bit with" one of the three matrices? I don't have the knowledge of matrix multiplication yet, so I don't know which one matrix to change.
Have you tried gluUnProject?
Member of the NeHe team.
Quote: Original post by Coconut Crab
To Enigma:

If I use gluOrtho2D(0, windowWidth, 0, windowHeight) as you have suggested with my client window = 500 x 500, will the coordinates at the 4 corner of the window be (-250, 250, z), (250, 250, z), (250, -250, z), (-250, -250, z), suppose I view from z = 1 and view at (0,0,0)?

I'm not Enigma but I can answer a part of your question.
The coordiantes would be (0,0) for the bottom left corner and (500,500) for the top right one.
I don't think that changing the point of view does make any difference, though I'm not sure about this.
Advertisement
Quote: Original post by Coconut Crab
To lc_lord:

Do you mind giving me an example of how to "tamper a bit with" one of the three matrices? I don't have the knowledge of matrix multiplication yet, so I don't know which one matrix to change.


Sorry, as i don't know exctly what you want done, so you have to do it yourself.
Matrix multiplication is an intregal part of 3d math, so you better start reading up on it.

Allso, my knowledge isn't either that high so i could tell you exactly what to do.

MY advice is to find another way, like glOrtho or gluOrtho2d.
Hi, Kazade,

Do you mean to first use perspective view and gluProject() to get the screen position, then use ortho view and gluUnProject() to get my desired 2D coordinate from the screen position?

That sounds like a great idea, but will the process in converting from double to integer back to double lose some precisions?

Coco

This topic is closed to new replies.

Advertisement