Results 1 to 8 of 8

Thread: 2D view only accepts Normalized co-ordinates

  1. #1
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    2D view only accepts Normalized co-ordinates

    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
    }

  2. #2
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,107

    Re: 2D view only accepts Normalized co-ordinates

    put these two lines just before glOrtho:
    Code :
    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.

  3. #3
    Guest

    Re: 2D view only accepts Normalized co-ordinates

    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.

  4. #4
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    Re: 2D view only accepts Normalized co-ordinates

    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!!

  5. #5
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    Re: 2D view only accepts Normalized co-ordinates

    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)



  6. #6
    Senior Member Regular Contributor songho's Avatar
    Join Date
    May 2003
    Location
    Canada
    Posts
    242

    Re: 2D view only accepts Normalized co-ordinates

    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.

  7. #7
    Guest

    Re: 2D view only accepts Normalized co-ordinates

    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.

  8. #8
    Member Newbie
    Join Date
    Jul 2006
    Posts
    39

    Re: 2D view only accepts Normalized co-ordinates

    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?

Similar Threads

  1. Normalized vs unnormalized
    By vincentfpgarcia in forum OpenCL
    Replies: 0
    Last Post: 01-25-2012, 03:37 AM
  2. Co-ordinates mess up
    By programmer17 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-09-2012, 12:06 PM
  3. Mouse Co-ordinates
    By Dastagir in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-13-2009, 12:16 PM
  4. co-ordinates etc.
    By incredible_hulk in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 12-07-2001, 01:18 PM
  5. glOrtho co-ordinates
    By incredible_hulk in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-11-2001, 06:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean