glTranslatef()

Syphor
Unregistered
 
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??

OpenGLView.h
Code:
#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;
@end


OpenGLView.m
Code:
#import "OpenGLView.h"

@implementation MyOpenGLView


- (void)awakeFromNib
{
    x=0;
    
    renderTimer = [[NSTimer scheduledTimerWithTimeInterval:0.08
            target:self
            selector:@selector(drawRect)
            userInfo:nil repeats:YES]
            retain];
                        
    [[NSRunLoop currentRunLoop] addTimer: renderTimer forMode: NSEventTrackingRunLoopMode];
    [[NSRunLoop currentRunLoop] addTimer: renderTimer forMode: NSModalPanelRunLoopMode];
}

- (id)initWithFrame:(NSRect) frameRect
{
    NSOpenGLPixelFormatAttribute attr[] =
    {
        NSOpenGLPFANoRecovery,
        NSOpenGLPFADoubleBuffer,
    NSOpenGLPFAAccelerated,
    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);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    
    glTranslatef(0.0, 0.0, x);
    
    glBegin(GL_QUADS);
    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);
    glEnd();

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

@end
Quote this message in a reply
Luminary
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
Member
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:
glMatrixMode(GL_PROJECTION);
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 Terminal.app 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:
glMatrixMode(GL_MODELVIEW);
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

hth,
Codemattic
Quote this message in a reply
Post Reply 

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