-
Notifications
You must be signed in to change notification settings - Fork 3
/
BabyScreen.m
122 lines (101 loc) · 2.49 KB
/
BabyScreen.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// BabyScreen.m
// BabyFingers
//
// Created by Baris Metin
//
#import "Face3D.h"
#import "Vertex3D.h"
#import "OBJ.h"
#import "OBJParser.h"
#import "BabyScreen.h"
@implementation BabyScreen
- (id)initWithFrame: (NSRect)rect pixelFormat:(NSOpenGLPixelFormat *)format
{
self = [super initWithFrame:rect pixelFormat:format];
[self initOpenGLView];
return self;
}
-(BOOL)acceptsFirstResponder { return YES; }
-(BOOL)becomeFirstResponder { return YES; }
-(BOOL)resignFirstResponder { return YES; }
- (void)initOpenGLView
{
glContext = [self openGLContext];
[glContext makeCurrentContext];
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
}
- (void) playSound: (NSString*)file
{
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:file byReference:YES];
[sound play];
}
- (void) playRandomSound
{
// TODO
}
- (void)keyDown: (NSEvent *)event
{
[self playRandomSound];
[self drawRect:[self bounds]];
}
- (void)mouseMoved: (NSEvent *)event
{
[self drawRect:[self bounds]];
}
- (OBJ*) loadOBJ:(NSString*)file
{
OBJParser* parser = [[OBJParser alloc] initWithFile:file];
[parser parse];
OBJ* obj = [parser object];
[obj retain];
[parser release];
return obj;
}
- (void) drawObj:(OBJ*) obj
{
NSUInteger num_faces = [[obj faces] count];
glBegin(GL_TRIANGLES);
for (int f=0; f < num_faces; f++) {
Face3D* face = [[obj faces] objectAtIndex:f];
for (int i=0; i < 3; i++) {
Vertex3D* v = [[obj vertices] objectAtIndex:([face v][i] - 1)];
Vertex3D* n = [[obj normals] objectAtIndex:([face n][i] - 1)];
glVertex3f([v x], [v y], [v z]);
glNormal3f([n x], [n y], [n z]);
}
}
glEnd();
}
- (void) drawRandomObject
{
glPushMatrix();
// TESTING
static double i = 10;
i += 10;
if (i >=720) i = 0;
srand(time(0) * i);
int k = rand();
// TEST animation
glRotated(i, i/k, i*k, 0);
glScaled(4.0, 4.0, 4.0);
static OBJ* obj = 0;
if (!obj)
obj = [self loadOBJ:[[NSBundle mainBundle] pathForResource:@"butterfly" ofType:@"obj"]];
[self drawObj:obj];
// [obj release];
glPopMatrix();
}
- (void)drawRect: (NSRect)bounds
{
glClearColor(0.5, 0.5, 0.8, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
[self drawRandomObject];
[glContext flushBuffer];
}
@end