Results 1 to 2 of 2

Thread: opengles iphone question, drawing a 2D HUD

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2009
    Location
    United States
    Posts
    4

    opengles iphone question, drawing a 2D HUD

    I am having trouble with glorthof on the opengles/iphone. I have had this code that works with the standard opengl but when I go opengles, it seems that glorthof won't recognize a 2d display:

    I want to have the 3d display and also have a matrix for the heads up display to show scores and what - not.

    Here is the main part of the code:
    Code :
    - (void) renderHUD:(EAGLView*)view {
     
       /*
        <code>
        glMatrixMode(GL_PROJECTION);           
        glLoadIdentity();                      
        glOrthof( 0, 320, 480, 0, 1, 0 );               
        glMatrixMode(GL_MODELVIEW);             
        glLoadIdentity();                   
        glDepthMask(GL_FALSE);
        </code>
        */
     
        /////////////////////////////////////////////
        // Enable 2d mode for hud title displays
        /////////////////////////////////////////////
        CGRect rect = view.bounds;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glDisable(GL_LIGHTING);
     
        // Depth testing and lighting are disabled for 2D rendering until
        // we are finished rendering in 2D
        glDisable(GL_DEPTH_TEST);
        /////////////////////////////////////////////
     
        /*
         * void glOrtho ( GLdouble left , GLdouble right , GLdouble bottom , 
         *                GLdouble top , GLdouble zNear , GLdouble zFar );
         */
     
        // glOrthof(0.0f, rect.size.width, rect.size.height, rect.size.height, 
        //         -1.0f, 1.0f);
        // Set up the orthographic projection
        /*
        glOrthof(0.0f, 320.0f, 480.0f,  
                 0.0f, -1.0f, 1.0f);
                 */
        glOrthof(0.0f, rect.size.width, rect.size.height, 0.0f, 
                 -1.0f, 1.0f);
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        /////////////////////////////////////////////
     
        /// Render hud
        /*
        [self renderTextHUD:view];
        */
        const GLfloat triVertices[] = { 
             0.0f,  5.7f,  0.0f, 
            -5.7f, -5.7f,  0.0f, 
             5.7f, -5.7f,  0.0f 
        }; 
     
        glColor4f(0.0, 0.0, 1.0, 1.0);
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, triVertices); 
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
        glDisableClientState(GL_VERTEX_ARRAY);
     
        /////////////////////////////////////////////
        // Disable 2d display
        /////////////////////////////////////////////
        glEnable(GL_DEPTH_TEST);
        glMatrixMode(GL_PROJECTION);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_LIGHTING);
     
    } // End of the Method //

    Code :
    /** 
     * Main setup the OpenGL view.
     */
    -(void) setupView:(EAGLView*)view {
     
        const GLfloat   zNear       = 0.1,
                        zFar        = 1000.0,
                        fieldOfView = 60.0;
        GLfloat         size;
     
        // Set the OpenGL projection matrix
        glMatrixMode(GL_PROJECTION);
        size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
        CGRect rect = view.bounds;
        glFrustumf( -size, size, 
                    -size / (rect.size.width / rect.size.height), 
                     size / (rect.size.width / rect.size.height), 
                     zNear, zFar);  
        glViewport(0, 0, rect.size.width, rect.size.height);
     
        // Make the OpenGL modelview matrix the default
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     
        /////////////////////////////////////////////
        // Texture, text setup.
        /////////////////////////////////////////////
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_TEXTURE_2D);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
        NSString *testString = [NSString stringWithFormat: @"testing: %f", 0.0];
        // Initialize our texture with the text specified in testString
        _textures[0] = [[Texture2D alloc] initWithString:testString 
                            dimensions:CGSizeMake(140, 20) 
                             alignment:UITextAlignmentLeft 
                              fontName:@"Arial" 
                              fontSize:kLabelFontSize];
        glBindTexture(GL_TEXTURE_2D, [_textures[0] name]);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);   // Linear Filtered
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   // Linear Filtered      
        // End of graphics text setup //
     
    } // End of the Method //

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2009
    Location
    United States
    Posts
    4

    Re: opengles iphone question, drawing a 2D HUD

    I modified the example to include everything:

    Code :
    /////////////////////////////////////////////////////////////////////
    //
    //  icameraAppDelegate.m
    //  OctaneMech
    //
    //  Created by Berlin Brown on 9/2/09.
    //  Copyright __MyCompanyName__ 2009. All rights reserved.
    //
    //////////////////////////////////////////////////////////////////////
     
    #import "octanemechAppDelegate.h"
    #import "EAGLView.h"
    #import "Texture2D.h"
     
    // define text variables
    #define SCREEN_WIDTH 320
    #define SCREEN_HEIGHT 480
     
    #define NEAR   1
    #define FAR    100
    #define FOV    45
    #define ASPECT  (float)SCREEN_WIDTH/(float)SCREEN_HEIGHT
     
    #define kRenderingFrequency 60.0
     
    /**
     * Example usage: 
     *  gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
     */
    void gluPerspective(double fovy, double aspect, double zNear, double zFar) {
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        double xmin, xmax, ymin, ymax;
     
        ymax = zNear * tan(fovy * M_PI / 360.0);
        ymin = -ymax;
        xmin = ymin * aspect;
        xmax = ymax * aspect;
        glFrustumf(xmin, xmax, ymin, ymax, 
                    zNear, zFar);
     
        glMatrixMode(GL_MODELVIEW);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  
     
        glDepthMask(GL_TRUE);
    } // End of the method 
     
    void orthographic() {
     
        glMatrixMode(GL_PROJECTION);           
        glLoadIdentity();                      
        glOrthof( 0, 320, 480, 0, 1, 0 );               
        glMatrixMode(GL_MODELVIEW);             
        glLoadIdentity();                   
        glDepthMask(GL_FALSE);
     
    }
     
    void gluLookAt(GLfloat eyex,    GLfloat eyey,    GLfloat eyez,
                   GLfloat centerx, GLfloat centery, GLfloat centerz,
                   GLfloat upx,     GLfloat upy,     GLfloat upz) {
     
        GLfloat m[16];
        GLfloat x[3], y[3], z[3];
        GLfloat mag;
     
        /* Make rotation matrix */
        /* Z vector */
        z[0] = eyex - centerx;
        z[1] = eyey - centery;
        z[2] = eyez - centerz;
        mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
        if (mag) {          /* mpichler, 19950515 */
            z[0] /= mag;
            z[1] /= mag;
            z[2] /= mag;
        }
     
        /* Y vector */
        y[0] = upx;
        y[1] = upy;
        y[2] = upz;
     
        /* X vector = Y cross Z */
        x[0] =  y[1] * z[2] - y[2] * z[1];
        x[1] = -y[0] * z[2] + y[2] * z[0];
        x[2] =  y[0] * z[1] - y[1] * z[0];
     
        /* Recompute Y = Z cross X */
        y[0] =  z[1] * x[2] - z[2] * x[1];
        y[1] = -z[0] * x[2] + z[2] * x[0];
        y[2] =  z[0] * x[1] - z[1] * x[0];
     
        /* mpichler, 19950515 */
        /* cross product gives area of parallelogram, which is < 1.0 for
         * non-perpendicular unit-length vectors; so normalize x, y here
         */
     
        mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
        if (mag) {
            x[0] /= mag;
            x[1] /= mag;
            x[2] /= mag;
        }
     
        mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
        if (mag) {
            y[0] /= mag;
            y[1] /= mag;
            y[2] /= mag;
        }
     
    #define M(row,col)  m[col*4+row]
        M(0, 0) = x[0];
        M(0, 1) = x[1];
        M(0, 2) = x[2];
        M(0, 3) = 0.0;
        M(1, 0) = y[0];
        M(1, 1) = y[1];
        M(1, 2) = y[2];
        M(1, 3) = 0.0;
        M(2, 0) = z[0];
        M(2, 1) = z[1];
        M(2, 2) = z[2];
        M(2, 3) = 0.0;
        M(3, 0) = 0.0;
        M(3, 1) = 0.0;
        M(3, 2) = 0.0;
        M(3, 3) = 1.0;
    #undef M
        glMultMatrixf(m);
     
        /* Translate Eye to Origin */
        glTranslatef(-eyex, -eyey, -eyez);
     
    } // End of glulookat //
     
     
    // CLASS IMPLEMENTATION
    @implementation AppController
     
    - (void) renderHUD:(EAGLView*)view {
     
       /*
        <code>
        glMatrixMode(GL_PROJECTION);           
        glLoadIdentity();                      
        glOrthof( 0, 320, 480, 0, 1, 0 );               
        glMatrixMode(GL_MODELVIEW);             
        glLoadIdentity();                   
        glDepthMask(GL_FALSE);
        </code>
        */
     
        /////////////////////////////////////////////
        // Enable 2d mode for hud title displays
        /////////////////////////////////////////////
        CGRect rect = view.bounds;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glDisable(GL_LIGHTING);
     
        // Depth testing and lighting are disabled for 2D rendering until
        // we are finished rendering in 2D
        glDisable(GL_DEPTH_TEST);
        glDepthMask(GL_FALSE);
        /////////////////////////////////////////////
     
        /*
         * void glOrtho ( GLdouble left , GLdouble right , GLdouble bottom , 
         *                GLdouble top , GLdouble zNear , GLdouble zFar );
         */
     
        // glOrthof(0.0f, rect.size.width, rect.size.height, rect.size.height, 
        //         -1.0f, 1.0f);
        // Set up the orthographic projection
        /*
        glOrthof(0.0f, 320.0f, 480.0f,  
                 0.0f, -1.0f, 1.0f);
                 */
        glOrthof(0.0f, rect.size.width, rect.size.height, 0.0f, 
                 -1.0f, 1.0f);
     
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        /////////////////////////////////////////////
     
        /// Render hud
        /*
        [self renderTextHUD:view];
        */
        const GLfloat triVertices[] = { 
             0.0f,  5.7f,  0.0f, 
            -5.7f, -5.7f,  0.0f, 
             5.7f, -5.7f,  0.0f 
        }; 
     
        glColor4f(0.0, 0.0, 1.0, 1.0);
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, triVertices); 
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
        glDisableClientState(GL_VERTEX_ARRAY);
     
        /////////////////////////////////////////////
        // Disable 2d display
        /////////////////////////////////////////////
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
        glMatrixMode(GL_PROJECTION);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_LIGHTING);
     
    } // End of the Method //
     
    - (void) drawView:(EAGLView*)view {
        glClear(GL_COLOR_BUFFER_BIT);
        [self renderHUD:view];
    }
     
    /** 
     * Main setup the OpenGL view.
     */
    -(void) setupView:(EAGLView*)view {
     
        const GLfloat   zNear       = 0.1,
                        zFar        = 1000.0,
                        fieldOfView = 60.0;
        GLfloat         size;
     
        // Set the OpenGL projection matrix
        glMatrixMode(GL_PROJECTION);
        size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
        CGRect rect = view.bounds;
        glFrustumf( -size, size, 
                    -size / (rect.size.width / rect.size.height), 
                     size / (rect.size.width / rect.size.height), 
                     zNear, zFar);  
        glViewport(0, 0, rect.size.width, rect.size.height);
     
        // Make the OpenGL modelview matrix the default
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     
    } // End of the Method //
     
    /////////////////////////////////////////////////////////////////////
     
    - (void) applicationDidFinishLaunching:(UIApplication*)application {
     
        CGRect rect = [[UIScreen mainScreen] bounds];
     
        //Create a full-screen window
        window = [[UIWindow alloc] initWithFrame:rect];
     
        //Create the OpenGL ES view and add it to the window
        EAGLView *glView = [[EAGLView alloc] initWithFrame:rect];
        [window addSubview:glView];
     
        glView.delegate = self;
        glView.animationInterval = 1.0 / kRenderingFrequency;
        [glView startAnimation];
     
        [glView release];
     
        //Show the window
        [window makeKeyAndVisible];
    }
     
    - (void) dealloc {
     
        [window release];
        [super dealloc];
    }
     
    @end // End of Impl AppController //

Similar Threads

  1. opengles display human face in iphone
    By mathewi9 in forum OpenGL ES
    Replies: 0
    Last Post: 03-25-2013, 02:03 AM
  2. opengles display human face in iphone
    By mathewi9 in forum OpenGL: macOS
    Replies: 0
    Last Post: 03-24-2013, 11:21 PM
  3. OpenGlES 2D Vector Graphics Loading and Drawing?
    By GanMatt in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 07-28-2010, 03:50 PM
  4. Replies: 3
    Last Post: 12-10-2008, 06:56 AM
  5. question about drawing on panel
    By dxx in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-29-2004, 11:46 PM

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