Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How to draw a 2d object in 3d world correctly

  1. #1
    Newbie Newbie
    Join Date
    Apr 2013
    Posts
    3

    How to draw a 2d object in 3d world correctly

    I'm attempting to draw particles (which is in 2D) in my 3D world using OpenGL ES 1.1, and the platform is for iOS devices.

    The 3D world (without the 2D particles) looks like this:
    https://www.dropbox.com/s/dujs9bsrcv...18.26%20PM.png


    When trying to draw the 2D particles in the 3D world, it looks like this (notice the grid is now moved somehow to the bottom left corner):
    https://www.dropbox.com/s/erv5vopspa...17.56%20PM.png

    However, I want it to draw directly over the 3D grid. Any ideas on how to achieve this or why it is moved to the left bottom corner?

    Any help is greatly appreciated.

    Heres my work so far.

    - (void)drawFrame
    {
    [(EAGLView *)self.view setFramebuffer];

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ///////////// 3D Drawing the Grid floor

    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    static GLfloat z = 0;
    gluLookAt(0, 5, -10, 0, 0, 0, 0, 1, 0);
    z += 0.075f;

    // Rotate the scene
    glRotatef(angle, 0, 1, 0);

    // Draw the Floor
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    glDisableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, zFloorVertices);
    glDrawArrays(GL_LINES, 0, 42);
    glVertexPointer(3, GL_FLOAT, 0, xFloorVertices);
    glDrawArrays(GL_LINES, 0, 42);

    //////////// 2D drawing the particle system

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrthof(0.0f, 320.0f, 0.0f, 480.0f, -100.0f, 100.0f);

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

    [explosion1 update:UPDATE_INTERVAL];

    if (explosion1.active != YES)
    {
    explosion1.sourcePosition = Vector2fMake(200, 200);
    explosion1.active = YES;
    explosion1.duration = 1;
    explosion1.sourcePositionVariance = Vector2fMake(0, rand() % 20);
    }

    [explosion1 renderParticles];

    [(EAGLView *)self.view presentFramebuffer];
    }

    - (void)initOpenGLES1
    {
    // Set the clear color
    glClearColor(0, 0, 0, 1.0f);

    // Projection Matrix config
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    CGSize layerSize = self.view.layer.frame.size;
    gluPerspective(45.0f, (GLfloat)layerSize.width / (GLfloat)layerSize.height, 0.1f, 750.0f);

    // Modelview Matrix config
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // This next line is not really needed as it is the default for OpenGL ES
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_BLEND);

    // Enable depth testing
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);
    }

  2. #2
    Newbie Newbie
    Join Date
    Apr 2013
    Posts
    3
    Can anyone help me out here??

  3. #3
    Intern Newbie
    Join Date
    Nov 2002
    Posts
    39
    You set up perspective projection in init function.
    In draw function you switch to ortho and never go back.
    So everything is dawn in ortho except for the first frame.
    Could that be the problem?

  4. #4
    Administrator Regular Contributor Khronos_webmaster's Avatar
    Join Date
    Apr 2007
    Location
    Montreal
    Posts
    150
    @taylornrolyat Please read the guide on how to post. You will find very useful ways to get answers faster, like using [CODE ] tags for one.

    Many thanks.
    Webmaster Khronos.org and OpenGL.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •