-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.m
191 lines (149 loc) · 4.77 KB
/
test.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#import <Cocoa/Cocoa.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
@interface MyWindow: NSWindow
- (BOOL) canBecomeKeyWindow;
- (BOOL) canBecomeMainWindow;
@end
@implementation MyWindow
- (BOOL) canBecomeKeyWindow
{
return YES;
}
- (BOOL) canBecomeMainWindow
{
return YES;
}
@end
@interface AppDelegate : NSObject <NSApplicationDelegate>{
}
@end
@implementation AppDelegate
- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSNotification*) aNotification {
return YES;
}
@end
@interface MyView: NSOpenGLView {
}
@end
@implementation MyView
-(BOOL) acceptFirstResponder
{
return YES;
}
-(void) drawRect: (NSRect) bounds
{
GLint x1 = 0;
GLint y1 = 0;
GLint x2 = 320;
GLint y2 = 300;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
glLoadIdentity(); // Reset the model-view matrix
glColor3f(1.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x2, y1);
glVertex2i(x2, y2);
glVertex2i(x1, y2);
glEnd();
glFlush();
glSwapAPPLE();
}
- (void) reshape
{
// get info about the new frame
NSRect frame = [self frame];
// compute pixels units
const int factor = 2 ; // should use cocoa conversion functions here!!!
const int width = (int)(frame.size.width * factor),
height = (int)(frame.size.height * factor);
// Compute aspect ratio of the new window
if(height == 0) {
return; // To prevent divide by 0
}
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
//gluPerspective(45.0, width / height, 0.1, 100.0)
gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
//glMatrixMode(GL_MODELVIEW)
//glLoadIdentity()
}
- (void)mouseDown:(NSEvent *)theEvent
{
[[self nextResponder] mouseDown:theEvent];
}
- (void)mouseUp:(NSEvent *)theEvent
{
[[self nextResponder] mouseUp:theEvent];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
[[self nextResponder] mouseDragged:theEvent];
}
- (void) keyDown:(NSEvent *)event
{
NSLog(@"%d", event.keyCode);
[[self nextResponder] keyDown:event];
}
@end
NSOpenGLPixelFormat * createPixelFormat( )
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
//NSOpenGLPFASupersample,
//NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
//NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)4,
NSOpenGLPFAAccumSize, 32,
//NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *
pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease ];
if ( pf == NULL )
{
NSLog(@"cocoglut: error: cannot create required pixel format for the OpenGL view.");
exit(1);
}
return pf ;
}
int main(int argc, const char * argv[])
{
@autoreleasepool
{
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
NSUInteger windowStyle = NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask | NSResizableWindowMask;
NSRect windowRect = NSMakeRect(100,100,400,400);
MyWindow* window = [ [MyWindow alloc] initWithContentRect: windowRect
styleMask: windowStyle
backing: NSBackingStoreBuffered
defer: YES];
[window setTitle: @"Hello"];
[window display];
[window orderFrontRegardless];
NSOpenGLPixelFormat *pf = createPixelFormat( ) ;
MyView * newView = [[MyView alloc] initWithFrame:windowRect pixelFormat: pf];
[newView setWantsBestResolutionOpenGLSurface: YES] ;
[window setContentView: newView ];
[window makeFirstResponder: newView];
window.initialFirstResponder = newView;
[window makeKeyWindow];
AppDelegate* appDel = [[AppDelegate alloc] init];
[NSApp setDelegate: appDel];
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black and opaque
glClearDepth(1.0); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH) ; // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Nice perspective corrections
[NSApp run];
}
}