Coordinate -> Viewport scaling

Hello, i’m having a little difficulty here. I’ve got an application where i’m doing some very simple drawing but having proplems with scaling.

How can i scale the vertex2f input
parameters into the viewport,that is
(vertex2f coordinates->viewport coordinates).

Drawing a dot at (0.0, 0.0) draws a dot into the middle of the screen, now drawing again at (1.0, 1.0) and i’m drawing way outside the viewport. How can i scale this so that i’m drawing, lets say (50,50) before its drawing outside my viewport.

I’ve been trying to work around this for a while but see nothing to my aid.

Any suggestions on solving this issue ?

You have to set a correct projection…take a look for a functions glOrho , gluOrtho2D, glFrustum and gluPerspective and choose one of them to setup your projection matrix

i’ve tried both glOrtho and gluPerspective already, still messing around with them to get it right

The easiest function is glOrtho. You just give it the coordinates that the viewport borders should have.

For example, if you want the cube from -50 to 50 along every axis to be just inside the viewport, you call:
glOrtho(-50, 50, -50, 50, -50, 50);

thanks overmind for your answer but i’m afraid it didnt solve my proplem. I tried puting the values you gave me into my Ortho function but i still have to draw in coordinates about -0.95f to 1.0f on x and y axis if i dont want to draw outside the screen.

Is there no way to scale those numbers so i can draw from lets say ±X before drawing outside the viewport. Where as X is some chosen number?

The glOrtho call should do exactly this. Perhaps you have called it in the wrong place?

This is the complete code:

glViewport(0, 0, x, y);
glOrtho(-50, 50, -50, 50, -50, 50);

x and y are the dimensions of your window. This should be called once at program start and whenever the size of the window changes. If you use glut, the reshape function does exactly this. You can also put it at the beginning of your drawing code, but that would be slower…

Make sure that glOrtho really gets called. Perhaps put a debug output around it.

No mather what projection you’re using, the center of the screen is always (0, 0), because the gl camera is located by default at (0,0,0) and looking at the negative z-axis. You’ll have to make a translation of the coordinate system before you draw anything.

fWidth = m_rWindowSize.width;
fHeight = m_rWindowSize.height;

glViewport(0, 0, 480, 480);

glOrtho(-50, 50, -50, 50, -50, 50);

Unfortunately my code looks exacly like you described it should be, no i’m not using glut for window creation, i dont find it flexible enough.

I suspect there is a proplem somewhere with the initialisation of the window. So i’m currently doing some extensive testing and debugging trying to find it out. Thnx for your replies, if anyone knows the solution to my proplem a post would be appreciated :slight_smile:

oh, thnx for you post Hernan, i’ll look into that, hopefully it will solve itself :slight_smile:

I found out what my proplem was, forgot to specify depth testing type (bangs head into wall).

Thnx for your posts everyone :slight_smile: , esp you hernan, your post set me on path and gave me a clue about what the proplem was. Found the dots “dissapearing” when i zoomed in or out on the Z so i figured it was the depth testing.