PDA

View Full Version : 2D view only accepts Normalized co-ordinates



glBomb
07-30-2006, 07:32 PM
I am trying to set up a 2D rendering system using openGl gluOrtho2D(0,640,0,480) .But even though the 2D co-ordinate system is created properly i can only draw textures using normalized co-ordinates in the range from 0 to 1.
eg glTexCoord2f(1.0f, 0.0f); glVertex3d(1.0f, 0.0f,0);

As shown below the textured quad fills 1 quadrant of the screen.But i have distintly seen examples where i can use proper co-ordinates
eg glTexCoord2f(1.0f, 0.0f); glVertex3d(23.0f, 43.0f,0);
Drawing with normalized co-ordinates is inconvenient & i cant understand why its not accepting proper co-ordinates.The textured quad below should not fill 1 quadrant of the screen(from center 0,0,0 to top-right). I also use SDL for Windowing

void DrawGLScene( void )
{

/* Clear the color and depth buffers. */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );


glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[filter]);//Rotation
glRotatef(xrot, 0.0, 0.0, 1.0);


/* Send our triangle data to the pipeline. */
glBegin( GL_QUADS );

glTexCoord2f(0.0f, 0.0f); glVertex3d(0.0f, 0.0f,0);
glTexCoord2f(1.0f, 0.0f); glVertex3d(1.0f, 0.0f,0);
glTexCoord2f(1.0f, 1.0f); glVertex3d(1.0f, 1.0f,0);
glTexCoord2f(0.0f, 1.0f); glVertex3d(0.0f, 1.0f,0);

glEnd();

SDL_GL_SwapBuffers( );

xrot += xspeed;
//yrot += yspeed;

}

How i set up openGL

//set up misc openGL attributes
BOOL setup_opengl(void )
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}

//glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

/* Our shading model--Gouraud (smooth). */
glShadeModel( GL_SMOOTH );


/* Set the clear color. */
glClearColor( 0, 0, 0, 0 );

//glClearDepth(1.0f); // Depth Buffer Setup
//glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations


glColor4f(1.0f, 1.0f, 1.0f, 0.5); // Full Brightness. 50% Alpha
// glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set The Blending Function For Translucency

return TRUE; // Initialization Went OK

}
How i set up the view volume

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

// Calculate The Aspect Ratio Of The Window
gluOrtho2D(0, 640, 0, 480);


glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

k_szczech
07-30-2006, 08:05 PM
put these two lines just before glOrtho:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();And by the way - I think it's nice to clean-up your code before posting. The longer and more messy code you post, the less chance that someone will read it.

07-31-2006, 01:57 AM
as far as i know gluOrtho2D will always limit the range from 0 to 1. Just think about it: you provide min_x max_x min_y max_y but NO z-limits. These are 0 and 1.
Try
glOrtho(min_x,max_x,min_y,max_y,min_z,max_z);
This will give you what you need.

glBomb
08-01-2006, 12:56 AM
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glMatrixMode(GL_PROJECTION);
glLoadIdentity();


// Calculate The Aspect Ratio Of The Window
gluOrtho2D(0, 640, 0, 480);


glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

1.I put in the 2 lines before gluOrtho2D(0, 640, 0, 480). Now i am getting a dark screen. Nothing appears at all.

2.I tried glOrtho(0,640,0,480,0,1) as well....it does not make any difference whether i use gluOrtho2D() or glOrtho() with proper arguments. If i add the

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

lines the display always goes blank. Should i try glFrustrum()? But thats only for perspective views. Has anybody tried 2D world co-ordinates(proper 0 to 640/0 to 480 range co-ordinates) in any setting & succeeded? Nothing wrong in drawing the scene i hope

void DrawGLScene( void )
{

/* Clear the color and depth buffers. */
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );


glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture[filter]);//Rotation
glRotatef(xrot, 0.0, 0.0, 1.0);


/* Send our triangle data to the pipeline. */
glBegin( GL_QUADS );
glTexCoord2f(0.0f, 0.0f); glVertex3d(0.0f, 0.0f,0);
glTexCoord2f(1.0f, 0.0f); glVertex3d(1.0f, 0.0f,0);
glTexCoord2f(1.0f, 1.0f); glVertex3d(1.0f, 1.0f,0);
glTexCoord2f(0.0f, 1.0f); glVertex3d(0.0f, 1.0f,0);

glEnd();

SDL_GL_SwapBuffers( );
xrot += xspeed;

}

or in initial openGL setup
//set up misc openGL attributes
BOOL setup_opengl(void )
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

/* Our shading model--Gouraud (smooth). */
glShadeModel( GL_SMOOTH );

/* Set the clear color. */
glClearColor( 0, 0, 0, 0 );

return TRUE; // Initialization Went OK

}

ps:I just dont get this...the problem should have been in setting up 2D :) , not after that!!

glBomb
08-04-2006, 07:59 PM
Here at the left is how it appears & at the right is how i expect it (The right picture was created by me not openGL)


http://img73.imageshack.us/img73/4861/p3jt3.jpg (http://imageshack.us)

songho
08-05-2006, 12:44 AM
glBomb,
Please set viewport with window width and height before setting the orthogonal projection in your resize callback:

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-0.5f, 1.5f, -0.5f, 1.5f);

Notice that the params of gluOrtho2D. Because your quad size is 1 by 1 and if you want to have the projection size double, from -0.5 to 1.5, the quad will be rendered at the centre of screen.

I would draw the quad from (-0.5, -0.5) to (0.5, 0.5), so, it is same size of quad but I can easily (or simpler) set the orthogonal projection. For example,;
gluOrtho2D(-1, 1, -1, 1);

The result will be exactly same as before, but the camera is looking at (0,0,0).

Also, since the quad is square, I would use square window size, too, for example, 400x400.

08-06-2006, 11:35 AM
The code you posted should work correctly, but you need to modify the drawing code. The reason you are getting a dark screen is because your quad is only 1 pixel large (you are still using the 0,1 coords when now you should be able to use the 23,43 quads.). If I understand correctly, you want the vertex coords to correspond with pixels, am I right? In a tile engine I wrote, I use ortho2d(-1,15,17,1). This sets it up so that my tiles can use 0,1 coordinates and map easily to the grid. It seems like this would work the same way on a pixel level.

glBomb
08-09-2006, 02:49 PM
Thanks every body ,the problem was solved.I was accidentally resetting the view someplace.One more question...regarding lights.Are spot lights really diffuse lights with the spot_cutoff ,spot_direction properties set to focus the light?