Post: #1
I'm having troublw with glTranslatef(x, y, z)...........

I made a loop so it refreshes my OpenGLView and decrements z... when it gets to -1 my little square disappears.....

anyone get any ideas??

#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#include <OpenGL/CGLRenderers.h>

@interface OGLView : NSOpenGLView
    // Pixel format & context stuff
    NSOpenGLPixelFormatAttribute    pfAttribs[32];
    NSOpenGLPixelFormat            *nsglFormat;
    NSOpenGLContext            *glContext;
    NSTimer *renderTimer;

- (void)drawRect;
- (id)initWithFrame:(NSRect) frameRect;

#import "OpenGLView.h"

@implementation MyOpenGLView

- (void)awakeFromNib
    renderTimer = [[NSTimer scheduledTimerWithTimeInterval:0.08
            userInfo:nil repeats:YES]
    [[NSRunLoop currentRunLoop] addTimer: renderTimer forMode: NSEventTrackingRunLoopMode];
    [[NSRunLoop currentRunLoop] addTimer: renderTimer forMode: NSModalPanelRunLoopMode];

- (id)initWithFrame:(NSRect) frameRect
    NSOpenGLPixelFormatAttribute attr[] =
    NSOpenGLPFAColorSize, 16,
    NSOpenGLPFADepthSize, 16,
        0 };

    nsglFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];

    self =  [super initWithFrame:frameRect pixelFormat:nsglFormat];
    [[self openGLContext] makeCurrentContext];

    return self;

- (void)drawRect
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glTranslatef(0.0, 0.0, x);
    glVertex3f(-0.5, -0.5, 0.0);

    glVertex3f(0.5, -0.5, 0.0);

    glVertex3f(0.5, 0.5, 0.0);

    glVertex3f(-0.5, 0.5, 0.0);

    [[self openGLContext] flushBuffer];
    x = x - 0.001;

Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
The default projection is orthographic, with x running -1 to 1, y running -1 to 1, and z running -1 to 1.

When z gets to -1 or 1, it's going to go outside either the near or far clipping planes, and disappear.

You can change the z range by using glOrtho yourself to set up your projection, but still all you'd see was a square which disappeared at some moment in time...

Probably you want to set up a perspective projection. At least then you'd see the square growing or shrinking as it moved towards and away from the viewer.

If you're more interested in getting a handle on glTranslatef, you might want to try translating in x and/or y with your current projection...
Quote this message in a reply
Posts: 304
Joined: 2002.04
Post: #3
Let me add a little to OSC's post.

You need to set up your projection matrix before your modelview matrix. The projection matrix sets up what kind of 'camera' you are using. glOrtho and gluOrtho2D are flat cameras - objects far away will look the same as objects up close. Setting glFrustum or gluPerspective will let you specify a projection from a wide angle to a telephoto lens to wacky asymmetric views. First call:
glLoadIdentity(); Reset projection matrix
and then call either glFrustum, gluPerspective, glOrtho, or gluOrtho2D. If you want to know how any of these work - look in the Red Book. If you dont have the Red Book - open and type 'man glFrustum' - all of the gl, glu, and glut functions are in man.

now I dont know what OpenGL uses as the default if you dont set the projection matrix yourself. But if OSC says it sets it to an orthographic view thats good enough for me Wink

Notice that before calling glFrustum() I first called glLoadIdentity() - this brings me to my second point. The code you have here probably isnt doing what you think its doing. The square isnt moving at a steady state - its accelerating. When you set a transformation (in this case translatef) - the modelview matrix then has whatever the modelview matrix had before the transformation **and** the new transformation. So the first time you go through the loop you are doing a glTranslatef(0.0, 0.0, 0.0); to your quad. The second time through the loop you are effectively doing: glTranslatef(0.0, 0.0, 0.0); glTranslatef(0.0, 0.0, -0.001); to your quad. The third time through the loop you are effectively doing: glTranslatef(0.0, 0.0, 0.0); glTranslatef(0.0, 0.0, -0.001); glTranslatef(0.0, 0.0, -0.002); to your quad - and so on. You are not just moving your quad by the current glTranslatef(0.0, 0.0, x); - but by all the glTranslatef's youve done so far combined. You should do:
glLoadIdentity(); Reset modelview matrix
glTranslatef(0.0, 0.0, x);

Which brings me to my third (minor) point. Why not use 'z' instead of 'x' as your variable here? What will you use when you want to move along the x-axis also? y? Grin

Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  gluLookAt vs. glRotatef/glTranslatef...etc hangt5 9 12,379 Mar 15, 2005 12:54 PM
Last Post: OneSadCookie
  when is 'glTranslatef()' reset? sealfin 5 6,900 Jan 13, 2003 07:39 AM
Last Post: DoG