Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hoschi

macrumors newbie
Original poster
Sep 20, 2005
29
0
I have been trying to implement a program in Cocoa where I take user input values from the user and draw a triangle in response to the values entered . However for some reason the program does not work.. Here is the source code for the implementation file...

#import "Triangle.h"
#include <OpenGL/gl.h>

@implementation Triangle

- (IBAction)now:(id)sender
{
ver1x = [vertex1_x floatValue]; //
ver1y = [vertex1_y floatValue];
ver2x = [vertex2_x floatValue];
ver2y = [vertex2_y floatValue];
ver3x = [vertex3_x floatValue];
ver3y = [vertex3_y floatValue];
[self setBounds:(NSMakeRect(0, 0, 500, 200) ) ];
[self setNeedsDisplay:YES];
NSLog(@"Taking Inputs");
NSLog(@"of vals %f", ver1x);
NSLog(@"Printing Self%@", self);

}

-(void)drawRect:(NSRect)bounds{

NSLog(@"DRAWING NOW");
NSLog(@"with values %f", ver1x);

glClearColor( 1, 0, 0, 0 ) ;
glClear( GL_COLOR_BUFFER_BIT ) ;
glColor3f( 0.0f, 1.0f, 0.0f ) ;
glBegin( GL_TRIANGLES) ;
{
glVertex3f( ver1x, ver1y, 0.0 ) ;
glVertex3f( ver2x, ver2y, 0.0 ) ;
glVertex3f( ver3x , ver3y, 0.0 ) ;
}
glEnd() ;
glFlush() ;
}
@end



I am a beginner in Cocoa and have no clue why the thing is not being redrawn.. could any one offer some suggestions as to how I can make buttons and fields in the interface talk to NSView???

Thanks a ton in advance

Vyom
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I'm not sure as I've never used Open GL in an NSView but I'm not sure you can just call gl* methods in drawRect:

Can you post a whole project?
 

hoschi

macrumors newbie
Original poster
Sep 20, 2005
29
0
robbieduncan said:
I'm not sure as I've never used Open GL in an NSView but I'm not sure you can just call gl* methods in drawRect:

Can you post a whole project?


I am sure that we cana use OpenGL from drawRect because I saw a example here

http://developer.apple.com/document...ide/5OpenGLCocoaProg/chapter_5_section_1.html

Actually all I need to know is how should I read the entered values in textfields into drawRect??

Here is the header file for the project... (I only have one class definition so there are no other files..)

/* Triangle */

#import <Cocoa/Cocoa.h>

@interface Triangle : NSOpenGLView
{
IBOutlet id now;
IBOutlet id vertex1_x;
IBOutlet id vertex1_y;
IBOutlet id vertex2_x;
IBOutlet id vertex2_y;
IBOutlet id vertex3_x;
IBOutlet id vertex3_y;
BOOL valuesSet;
@public
float ver1x, ver1y, ver2x, ver2y, ver3x, ver3y;
}
- (IBAction)now:(id)sender;
- (void)drawRect:(NSRect)bounds;

@end


Thanks
 

hoschi

macrumors newbie
Original poster
Sep 20, 2005
29
0
AlmostThere said:
Have you worked through the currency converter tutorial?

http://developer.apple.com/document...ial/index.html#//apple_ref/doc/uid/TP40000863

That does exactly what you want.

Yes I did look through the curency converter tute. Here the problem is a little different becuase when I start the program it directly goes to drawrect:. So if I have to input values I need to enter values and press the button (titled now). After having read these values in the IBAction: instance I am unable to ask the drawrect to redraw the triangle,... this is where the problem lies... I tried using the command [self setNeedsDisplay:YES]; but this somehow does not work...

SO if I know how to speak to drawrect form some other fucntion (assuming whats said above isnt true), this should work...

thanks a ton
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You want some sort of controller class that gets the values from the interface then sets then in the view via setter methods then calls [view setNeedsDisplay:YES];

I've attached a project that uses your drawing code to show this. Note that there seems to be some sort of clipping problem.

See (-1,0)(-1,1)(4,2) for an example.

Edit: remember to hit return in the text boxes! This could all be done some much more neatly with bindings: I leave that up to you (hint the controller can be totally removed as can the setters in the view class).
 

Attachments

  • GLTriangle.zip
    38.5 KB · Views: 164

hoschi

macrumors newbie
Original poster
Sep 20, 2005
29
0
robbieduncan said:
You want some sort of controller class that gets the values from the interface then sets then in the view via setter methods then calls [view setNeedsDisplay:YES];

I've attached a project that uses your drawing code to show this. Note that there seems to be some sort of clipping problem.

See (-1,0)(-1,1)(4,2) for an example.

Edit: remember to hit return in the text boxes! This could all be done some much more neatly with bindings: I leave that up to you (hint the controller can be totally removed as can the setters in the view class).

Thanks a ton dude.. Finally the problem turns out to be that I was not pressing enter after entering every value. Wondering where I could read more on Bindings??

Thanks
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Just to add to the above mmalcs examples are some of the best and really help to get to grips with bindings.

Also if you use delegation you can be informed when the value in the text boxes changes without the user hitting enter. Hitting enter triggers the action method.

Edit to add: Apple Conceptual documentation on delegation probably worth reading if you have not already as delegation is used all over the place in Cocoa.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.