PDA

View Full Version : Coordinates - should be easy



amuirwood
08-13-2001, 06:56 AM
Using VC++ 6.0 I am plotting an array of points in a window. The array refers to an image that is let's say 640*480 pixels. Currently my image stretches when the window is resized - how do I force it to stay at 640*480 whatever the window size?

I am quite new to this so please explain in very plain English, with sample code if possible. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Michael Steinberg
08-13-2001, 08:20 AM
Use an orthogonal projection matrix. Call glOrtho( leftx, rightx, topy, bottomy, front back ). Well the parameters may be a bit different but the idea is still the same.
Let leftx and topy be 0.0f, rightx and bottom y be the extensions of the windows's client area. take small values for front and big values for back if you want.
Then, the pixels should be accesible just with their coordinates, and since you call glOrtho everytime the window's size change (since then it's client area also changes) with the updated extensions. Process some message like WM_SIZED or something and do the update there.
Anyway... if you want to display an image you might be better off with a textured quad, where the texture contains the image.

ByteZero
08-13-2001, 09:19 AM
Here is the code sample you want (i hope that's the right code for you)

GLvoid ReSizeScene(GLsizei w, GLsizei h)
{
GLfloat Entf = 100.0f; //Range Var
if(h == 0)
{
h = 1;
}

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//Clipping volume (left, right, bottom, top, near, far)
if(w <= h)
{
glOrtho(-Entf, Entf, -Entf*h/w, Entf*h/w, -Entf, Entf);
}
else
{
glOrtho(-Entf*w/h, Entf*w/h, -Entf, Entf, -Entf, Entf);
}

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

ByteZero
08-13-2001, 09:21 AM
Here is the code sample you want (i hope that's the right code for you)

GLvoid ReSizeScene(GLsizei w, GLsizei h)
{
GLfloat Entf = 100.0f; //Range Var
if(h == 0)
{
h = 1;
}

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//Clipping volume (left, right, bottom, top, near, far)
if(w <= h)
{
glOrtho(-Entf, Entf, -Entf*h/w, Entf*h/w, -Entf, Entf);
}
else
{
glOrtho(-Entf*w/h, Entf*w/h, -Entf, Entf, -Entf, Entf);
}

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

ByteZero
08-13-2001, 09:23 AM
Uupps, soorry http://www.opengl.org/discussion_boards/ubb/biggrin.gif

SED
08-13-2001, 09:51 AM
You need to handle the WM_SIZE message sent to your window, when you drag a corner or maximize the window. This message contains the new size of your window in pixels. You use these values to remap your projection matrix. How you handle this message depends on what your are using to code OpenGL: GLUT, Win32 API, or MFC.

Heres a simple GLUT example: http://personal.lig.bellsouth.net/lig/s/_/s_dolan/Resize.zip

This subject cant be covered in a few paragraphs. There are too many variables.
The OpenGL SuperBible will show you how to do this & alot more. Get a book! http://www.opengl.org/discussion_boards/ubb/smile.gif

amuirwood
08-13-2001, 11:38 PM
Byte Zero:
Where does that code go?
I tried to put it in my:

void CChildView::OnGLDraw(CDC *pDC)

function but I don't think that is right.

I'm using a simple MFC wizard program and building it up to do some simple graphics as part of an image transfer experiment. The problem is that a lot of the code is pregenerated and quite complicated.

The code (GL_POINTS) I use to plot the image is in CChildview and the code that calls to create the window is CMainFrm.

Any ideas?

Thanks for your help!!

ByteZero
08-15-2001, 03:33 AM
Hi I'am sorry I am using only Glut, MFC is to stupid, so if you want the Glut code for this mail me;

bytezero@usa.com

c u http://www.opengl.org/discussion_boards/ubb/biggrin.gif

amuirwood
08-15-2001, 03:53 AM
I moved your function to the bottom and called it within my plotting function and it worked!!

I don't know how - but frankly I don't care.

Thanks very much!! http://www.opengl.org/discussion_boards/ubb/biggrin.gif