Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Aug 27, 2008, 01:58 PM   #1
macrumors 68000
Soulstorm's Avatar
Join Date: Feb 2005
Displaying Large Objects into NSOpenGLView

Well, as you may recall from a previews thread, I am building a .obj file loader. The question I have concerns general OpenGL, so it doesn't require of me to show much code, or from you to know about .obj files. I succeeded in making the loader, and I also succeeded in displaying that object on screen. Materials are also used. (I hope I can implement textures into the next few days...).

I have a problem, though. I have made an application which loads and displays a model. However, as you imagine, the dimensions of each model are completely different. One model could have vertices that have coordinates like those:

x: 1000 y: 8673 z: 9344

Other models may have vertices like those:

x: 9,34 y:7,34 z: 9,44

My problem is that I can't think of any good way of resizing the view each time I load a new model to something that produces a viewable result. My display code so far is the following:

MyOpenGLView.h code:

       #import <Cocoa/Cocoa.h>
#import "ObjLoaders.h"
#include <iostream>
#include "General.h"
#include <GLUT/GLUT.h>
using namespace std;

#define FILENAME "lightcycle-low.obj"
#define FILENAME2 "lightcycle-low.txt"
#define FILENAME3 "Cube.obj"
#define FILENAME4 "Cone.obj"
#define FILENAME5 "test/lightcycle-high.obj"
#define FILENAME6 "Bau2.obj"

#define UPDATE_SECONDS 1.0f/200
#define STEP_SIZE 0.5f

@interface MyOpenGLView : NSOpenGLView {
	float yRot;
	float xRot;
	float standardRotationDegrees;
	float upAndDownTranslation;
	float leftAndRightTranslation;
	float nearAndFarTrasnlation;
	NSTimer *timer;

- (void)rotate:(float)degrees;

- (IBAction)rotateUp:(id)sender;
- (IBAction)rotateDown:(id)sender;
- (IBAction)rotateLeft:(id)sender;
- (IBAction)rotateRight:(id)sender;
- (IBAction)moveCameraLeft:(id)sender;
- (IBAction)moveCameraRight:(id)sender;
- (IBAction)moveCameraUp:(id)sender;
- (IBAction)moveCameraDown:(id)sender;
- (IBAction)moveCameraForward:(id)sender;
- (IBAction)moveCameraBackwards:(id)sender;

- (IBAction)loadOBJFile:(id)sender;
@end code:
#import "MyOpenGLView.h"

OBJModel model;

@implementation MyOpenGLView

- (void) heartbeat
	[self rotate:standardRotationDegrees];
	if(![[NSApplication sharedApplication] isHidden])
		[self setNeedsDisplay:YES];
	//[self rotateLeft:self];

- (void)prepareOpenGL
	standardRotationDegrees = 0.3f;
	timer = [NSTimer scheduledTimerWithTimeInterval:(UPDATE_SECONDS) target:self selector:@selector(heartbeat) userInfo:nil repeats:YES];
	[timer retain];
	//[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
	//[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSEventTrackingRunLoopMode];
	NSLog(@"preparing openGL...");
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
	yRot = 0.0f;
	xRot = 0.0;;
	upAndDownTranslation = 0.0f;
	leftAndRightTranslation = 0.0f;
	nearAndFarTrasnlation = 0.0f;
	//GLfloat lightpos[4] = {0.0f, 0.2f, -1.0, 1.0f};
	GLfloat lightpos[4] = {2.0f, 2.0f, 2.0, 1.0f};
	glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

- (void)reshape
	[[self openGLContext]makeCurrentContext];
	NSRect bounds = [self bounds];
	//NSLog(@"reshaping: %f, %f", NSWidth(bounds), NSHeight(bounds));
	glViewport(0, 0, NSWidth(bounds), NSHeight(bounds));
	gluPerspective(45, NSWidth(bounds)/NSHeight(bounds), 0.1f, 1000.0f);
	//	glOrtho(-20, 20, -20, 20, -100, 100);
	[NSOpenGLContext clearCurrentContext];

- (void)drawRect:(NSRect)rect {
	//glTranslatef(0.0f, 0.0f, -20.0f);
	gluLookAt(0, 0, nearAndFarTrasnlation, 0, 0, 0, 0, 1, 0);
	glTranslatef(leftAndRightTranslation, 0, 0);
	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(yRot, 0.0f, 0.0f, 1.0f);
	//draw the model
	for (int i=0; i<model.numTriangles(); i++) {

	[[self openGLContext]flushBuffer];

#pragma mark -
#pragma mark Moving The Camera
- (void)rotate:(float)degrees
	yRot = yRot - degrees;
- (IBAction)rotateUp:(id)sender
	xRot-= 5.0f;
	if(xRot < 0.1f)
		xRot = 359.9;
	[self setNeedsDisplay:YES];
- (IBAction)rotateDown:(id)sender
	xRot += 5.0f;
	if(xRot > 359.9f)
		xRot = 0.0f;
	[self setNeedsDisplay:YES];
- (IBAction)rotateLeft:(id)sender
	yRot -= 5.0f;
	if(yRot < 0.1f)
		yRot = 359.9;
	[self setNeedsDisplay:YES];
- (IBAction)rotateRight:(id)sender
	yRot += 5.0f;
	if(yRot > 359.9f)
		yRot = 0.0f;
	[self setNeedsDisplay:YES];

	if(standardRotationDegrees > 0.0f)
		standardRotationDegrees = 0.0f;

- (IBAction)moveCameraLeft:(id)sender
	leftAndRightTranslation -= STEP_SIZE;
	[self setNeedsDisplay:YES];
- (IBAction)moveCameraRight:(id)sender
	leftAndRightTranslation += STEP_SIZE;
	[self setNeedsDisplay:YES];
- (IBAction)moveCameraUp:(id)sender

- (IBAction)moveCameraDown:(id)sender
- (IBAction)moveCameraForward:(id)sender
	nearAndFarTrasnlation += STEP_SIZE;
	[self setNeedsDisplay:YES];
- (IBAction)moveCameraBackwards:(id)sender
	if(nearAndFarTrasnlation < 0)
		nearAndFarTrasnlation = 0;
	nearAndFarTrasnlation -= STEP_SIZE;
	[self setNeedsDisplay:YES];

- (IBAction)loadOBJFile:(id)sender
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	[openPanel setCanChooseDirectories:NO];
	[openPanel setCanChooseFiles:YES];
	[openPanel setAllowsMultipleSelection:NO];
	[openPanel setAllowedFileTypes:[NSArray arrayWithObject:@".obj"]];
	if ([openPanel runModalForDirectory:NSHomeDirectory() file:nil] == NSOKButton) {
		[[NSFileManager defaultManager]changeCurrentDirectoryPath:[[openPanel filename]stringByDeletingLastPathComponent]];
		model.loadOBJFile([[[openPanel filename]stringByStandardizingPath]cStringUsingEncoding:NSUTF8StringEncoding]);

- (void) dealloc
	if(timer != nil)
		[timer release];
	[super dealloc];

As you can see, I have used gluPerspective() with gluLookAt(). Although some models are displayed correctly, I have a model that has vertices like x:300.0 y:20.0 z:1000.0. That model is not displayed at all. Therefore, I need a way to resize the view to something that can hold objects of any size each time I load a new model.

Can you recommend any way of coping with that problem?
Soulstorm is offline   0 Reply With Quote
Old Aug 27, 2008, 02:40 PM   #2
macrumors 6502a
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Rather than scale the view, I would be tempted to scale the model when you draw it, ie just push a glScale on the model view matrix before drawing it. You could work out the scaling factor from the vertex bounds when you load the model.

b e n
lazydog is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Why do many people have problems with the min display port to dual link DVI adapter? Nostromo Mac Peripherals 3 Jul 7, 2011 10:40 AM
How can I insert objects into Pages? SavMBP15 Mac Applications and Mac App Store 2 Aug 6, 2010 12:17 PM
Storing NSMutableArray full of custom object into NSUserDefaults playboy16 iPhone/iPad Programming 6 Jun 29, 2009 12:00 PM
splitting up large file into several parts? manchot Mac Basics and Help 7 May 3, 2009 10:52 AM
can you import a 3D object into anime studio standard edition natatos Apple, Industry and Internet Discussion 0 Mar 11, 2009 04:50 PM

All times are GMT -5. The time now is 10:53 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC