Newbie to Open GL

BlueAvian
Unregistered
 
Post: #1
Ok, so I have started to work with triangles and stuff like.
Code:
- (void)drawRect:(NSRect)rect
{
    glClear( GL_COLOR_BUFFER_BIT );
    
    glBegin( GL_TRIANGLES );
    {
        glColor3f( 1.0f, 0.0, 0.0f );
        glVertex3f(-1.0, 1.0, 0.0f);
        glColor3f( 0.0f, 1.0, 0.0f );
        glVertex3f(1.0, 1.0, 0.0f);
        glColor3f( 0.0f, 0.0, 1.0f );
        glVertex3f(0.0, -1.0, 0.0f);
    }

    
    glEnd();
      
    glFinish();
        
    [[self openGLContext] flushBuffer];
}

My main goal is to make a 2d game, but use the 3d part for layering.   My problem is that I cant figure out how to work with the depth of the triangles except drawing order, please help.

This is the MyOpenGLView.m that I'm using

#import "MyOpenGLView.h"

@implementation MyOpenGLView

- (id)init
{
    self = [super init];
    
    NSOpenGLPixelFormatAttribute attrs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFAAccelerated,
        NSOpenGLPFAColorSize,
        16,
        NSOpenGLPFADepthSize,
        16
    };
    
    NSOpenGLPixelFormat* pixFmt;
    
    pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs] ;
    [self setPixelFormat: pixFmt] ;
    
    glShadeModel( GL_SMOOTH );
    glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
    glClearDepth( 3.0f );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    
    return self;
}

- (void)slideX:(float)x Y:(float)y Z:(float)z
{
    glTranslatef( x, y, z);
}

- (void)drawRect:(NSRect)rect
{
    glClear( GL_COLOR_BUFFER_BIT );
    
    glBegin( GL_TRIANGLES );
    {
        glColor3f( 1.0f, 0.0, 0.0f );
        glVertex3f(-1.0, 1.0, 0.0f);
        glColor3f( 0.0f, 1.0, 0.0f );
        glVertex3f(1.0, 1.0, 0.0f);
        glColor3f( 0.0f, 0.0, 1.0f );
        glVertex3f(0.0, -1.0, 0.0f);
    }

    
    glEnd();
      
    glFinish();
        
    [[self openGLContext] flushBuffer];
}

@end

[edited by FÎanor for code tags]

Chase
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #2
You probably wanto to use glOrtho() to reset the projection matrix.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #3
You mean changing where on the z axis your triangle is? Before you draw, call glTranslatef(xPos, yPos, zPos);
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #4
Quote:Originally posted by BlueAvian

- (void)drawRect:(NSRect)rect
{
glClear( GL_COLOR_BUFFER_BIT );

glBegin( GL_TRIANGLES );
{
glColor3f( 1.0f, 0.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glColor3f( 0.0f, 1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glColor3f( 0.0f, 0.0, 1.0);
glVertex3f(0.0, -1.0, 0.0);
}

glEnd();

glFinish();

[[self openGLContext] flushBuffer];

I think I know what you want; although, I've never really tried "pure" 2D in OGL. First things first though don't use the glFinish(); unless your making a game that uses networking. The [[self openGLContext] flushBuffer]; does all this anyway so it's like calling glFinish/glFlush twice. If you want to change the depth of one object over the other i.e. have object "A" overlap object "B" use something like the code below:
Code:
- (void)drawRect
{
    glClear( GL_COLOR_BUFFER_BIT );

//Object "B"
[self triangle];

//Object "A" this will be on top of "B"
[self triangle];
    [[self openGLContext] flushBuffer];
}

- (void)triangle
{
  // Since you're writting in 2-D all you need is glVertex2f()
    glBegin( GL_TRIANGLES );
       glColor3f( 1.0f, 0.0, 0.0);
        glVertex2f(-1.0, 1.0);
        glColor3f( 0.0f, 1.0, 0.0);
        glVertex2f(1.0, 1.0);
        glColor3f( 0.0f, 0.0, 1.0);
        glVertex2f(0.0, -1.0);
    glEnd();
}

Also try the OpenGL examples on the link below they may help give you a better background of all the other cool stuff you can do with OGL's features. Here's the site: http://zerobyzero.ca/~ktatters/tutorials/

Hope This Helps,
Iceman

Note: Jabber's idea about the z axis, above will resize the triangle; however, this maybe what you want I'm not sure.
Quote this message in a reply
Feanor
Unregistered
 
Post: #5
Iceman, for layering he either has to draw in the correct order, not too hard, or he can use the z dimension and turn on the depth buffer, and like Mark said, definitely use glOrtho() for a nice flat look.

The main problem with the code that I see is the wrong initializer is use. The default initializer for NSView's is -initWithFrame:. From there you would call [super initWithFrame: pixelFormat:] instead of using two separate calls.

Then send the message [self openGLContext] and that will make sure that the context is initialized so that your nice setup code all works. (My experience is that the -init method is too early unless you pass that message; Apple has a trick in there which lets you do it early that way.)

For animation layers, you can set all your shapes like 0.1 away from each other on the Z axis. So, how do you animate nicely? Most people use timers. You can set a timer to go off at a nice fine time period, like every 1/30 of a second or whatever.

Also, put your drawing code in a custom method, not in drawRect, which has too much overhead -- just wrap any drawing code not in drawRect with lockFocusIfCanDraw and unlockFocus messages or the window server gets confused.
Quote this message in a reply
Member
Posts: 196
Joined: 2002.04
Post: #6
Quote:Originally posted by Feanor
Iceman, for layering he either has to draw in the correct order, not too hard, or he can use the z dimension and turn on the depth buffer, and like Mark said, definitely use glOrtho() for a nice flat look.


Yeah, true. My method would probably look pretty horrible on an ATI 128 Rage Pro, since they don't work well with overlapping.

Quote:Originally posted by Feanor

The main problem with the code that I see is the wrong initializer is use. The default initializer for NSView's is -initWithFrame:. From there you would call [super initWithFrame: pixelFormat:] instead of using two separate calls.

That's why I recomended that OpenGL web site Grin.

Iceman
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Open GL and objects (newbie question) Taxxodium 1 2,553 Oct 25, 2002 11:14 AM
Last Post: Jeff Binder