/
JCWorldView.mm
61 lines (51 loc) · 1.82 KB
/
JCWorldView.mm
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
//
// JCWorldView.m
// Graphic3DMatrixExample
//
// Created by Jamie Cho on 2010-11-17.
// Copyright 2010 Jamie Cho. All rights reserved.
//
#import "JCWorldView.h"
@implementation JCWorldView
@synthesize world;
@synthesize camera;
- (void)drawRect:(NSRect)dirtyRect {
NSRect rect = [self bounds];
// Draw the sky
[[NSColor colorWithCalibratedRed:.9 green:.9 blue:1 alpha:1] setFill];
[[NSBezierPath bezierPathWithRect:rect] fill];
// Compute the tranformation that maps objects to this view
float nearZ, farZ;
jcho::Matrix<double> transform = [camera perspectiveMatrixForFrame:rect nearZ:&nearZ farZ:&farZ];
// Apply the transforms
NSMutableArray *polygons = [NSMutableArray array];
for(JCPolygonSet *polygonSet in world.randomObjects) {
JCPolygonSet *transformedPolygonSet = [[[JCPolygonSet alloc] initWithPolygonSet:polygonSet andTransform:transform] autorelease];
for(JCPolygon *polygon in transformedPolygonSet.polygons) {
[polygons addObject:polygon];
}
}
// Make sure we render the farthest polygons first
[polygons sortUsingSelector:@selector(compare:)];
// Render the transforms
for(JCPolygon *polygon in polygons) {
if (![polygon isBeforeZ:nearZ]) continue;
NSBezierPath *path = [[NSBezierPath alloc] init];
const std::vector<jcho::Matrix<double> > &points = [polygon points];
for(std::vector<jcho::Matrix<double> >::const_iterator it = points.begin(); it != points.end(); it++) {
const jcho::Matrix<double> p = (*it);
float wz = (float)p.get(3, 0);
NSPoint point = NSMakePoint(p.get(0, 0)/wz, p.get(1, 0)/wz);
if (it == points.begin()) {
[path moveToPoint:point];
} else {
[path lineToPoint:point];
}
}
[path closePath];
[[polygon color] setFill];
[path fill];
[path release];
}
}
@end