PDA

View Full Version : draw triangle world coordinates [0,1] question



sparvu
02-16-2009, 04:58 AM
Hi,

I have a simple question about model mapping from world coordinates to screen coordinates.

I need to draw a triangle and create a simple animation
like: http://www.nbl.fi/~nbl97/solaris/perf/cpuplayer.html
where the vertexes of my triangle are defined in [0,1]
Every dot inside my triangle should stay inside this interval lets say.

I have read about mapping what OpenGL does from world coordinates
to screen coordinates and got clear the idea that my model can be easily plotted in 0,1 range and then displayed in
whatever screen size I wish. However still I have difficulties to
get this properly sized and displayed.

Any pointers in how to get this properly done and be
able as well to label each vertex of my triangle
and be readable as well ? Below my sample functions
for reshape and drawing.


Manyt thanks,
Stefan

void
display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);

glPuts(-0.8, -1, "Idle", font1);
glPuts(0.9, 1, "User", font1);
glPuts(1, 0, "Sys", font1);

/* Draw the triangle and labels */
glColor3f(0.3,0.3,0.3);

glBegin(GL_TRIANGLES);
glVertex2f( 0.0f, 0.0f);
glVertex2f( 0.5f, 1.0f);
glVertex2f( 1.0f, 0.0f);
glEnd();


glFlush();
/* glutSwapBuffers(); */
}

and the reshape function:

void
reshape (int w, int h)
{
nWidth = w;
nHeight = h;

if (h == 0) h = 1;

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

/* Reset coordinate systems */
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();


if (w <= h)
glOrtho(-10,10, -10*(GLfloat)h/(GLfloat)w,
10*(GLfloat)h/(GLfloat)w, -1, 1);
else
glOrtho(-10*(GLfloat)w/(GLfloat)h,
10*(GLfloat)w/(GLfloat)h, -10, 10 , -1, 1);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}