PDA

View Full Version : Coordinates again - sorry



amuirwood
08-14-2001, 01:59 AM
Here's the code I use to plot the contents of my wee array (*m_image). I believe the "GLortho" part is the code I need to change in order to stop the image from being resized when the window is resized. I think what I want to do is ensure that the coordinate increments are always equal to 1 pixel, rather than a fraction of the height/width of the window.

Any suggestions would be greatly appreciated. http://www.opengl.org/discussion_boards/ubb/biggrin.gif
(in patronisingly plain english too)


void CChildView::OnGLDraw(CDC *pDC)
{
glClearColor(m_redbg, m_greenbg, m_bluebg, m_alphabg);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Puts origin in the centre of the drawing window
glOrtho((-512.0 + 5), // left: half horizontal screen resolution - 4 pixels for width of side bar
(512.0 - 5), // right:
(-384.0 + 33), // bottom
(384.0 - 33), // top
1.0, // near
-1.0); // far

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for (int i = 0; i < (((m_width) * (m_height) * 3) - 1); i+=3)
{
glColor3d((m_image[i]/255.0), (m_image[i + 1]/255.0), (m_image[i + 2]/255.0));
// Colour of each pixel starting at top left corner
glBegin(GL_POINTS);

glVertex2d( (((i/3)%(m_width)) - (m_width / 2)), ( (((m_height / 2) - 1) - ((i/3)/(m_width)) )));
// Plots each pixel starting at top left corner, taking a new line when pixel-number is divisible by the width of the image

}

glEnd();
glFlush();

}

amuirwood
08-14-2001, 02:02 AM
PS. I'm using a single document MFC wizard application in VC++ and building it up - in case that helps.

Bob
08-14-2001, 08:01 AM
Just a side note. I suggest you move the glBegin(GL_POINTS) to the line before you enter the for-loop. Calling glBegin() multiple times without calling glEnd is treated as an error by OpenGL.

Do this instead:



glBegin(GL_POINTS);
for(...)
{
// your code goes here
}
glEnd();

amuirwood
08-14-2001, 11:31 PM
Thanks but that doesn't answer the big question - is there any chance of that?

Bob
08-15-2001, 01:19 AM
Well, no, you are right, that doesn't answer the question, but as I said, it was a side note, something I found when looking at the code...

I read your question, but didn't get what you wanted. Now when I read it a second time, I do get it (don't ask me why I didn't the first time http://www.opengl.org/discussion_boards/ubb/smile.gif ).

So, you want to be able to resize the window, but let one unit still be equal to the size between two pixels?

You are on the right track, you need to modify the glOrtho-call. In it's current state, you send hard coded values. That's fine if you want the viewport to have the same number of units independent of size. But this is not what you want. You need to pass values that match the size of the window. Let's say you resize your window to size (400x300), then you need to pass these new values to glOrtho instead. glOrtho(-200, 200, -150, 150, 1, -1) in your case (for half width/height), and add/subtract adjustions for bordersize and stuff like that, as you do now.