Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 135 lines (109 sloc) 4.097 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // NSColor+OpenGLExtensions.m
3 // TheBigRedButton
4 //
5 // Created by Uli Kusterer on 02.09.07.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright 2007 M. Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 #import "NSColor+OpenGLExtensions.h"
29 #import <OpenGL/gl.h>
30
31 static GLuint texName = 0;
32
33
34 GLuint UKGetTextureFromImage( NSImage* theImg )
35 {
36 NSBitmapImageRep* bitmap = [NSBitmapImageRep alloc];
37 int samplesPerPixel = 0;
38 NSSize imgSize = [theImg size];
39
40 [theImg lockFocus];
41 [bitmap initWithFocusedViewRect:
42 NSMakeRect(0.0, 0.0, imgSize.width, imgSize.height)];
43 [theImg unlockFocus];
44
45 // Set proper unpacking row length for bitmap.
46 glPixelStorei(GL_UNPACK_ROW_LENGTH, [bitmap pixelsWide]);
47
48 // Set byte aligned unpacking (needed for 3 byte per pixel bitmaps).
49 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
50
51 // Generate a new texture name if one was not provided.
52 texName = 0;
53 glGenTextures( 1, &texName );
54
55 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, texName );
56
57 // Non-mipmap filtering (redundant for texture_rectangle).
58 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
59 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_REPEAT );
60 glTexParameteri( GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_REPEAT );
61 samplesPerPixel = [bitmap samplesPerPixel];
62
63 // Nonplanar, RGB 24 bit bitmap, or RGBA 32 bit bitmap.
64 if(![bitmap isPlanar] &&
65 (samplesPerPixel == 3 || samplesPerPixel == 4))
66 {
67 glTexImage2D( GL_TEXTURE_RECTANGLE_EXT, 0,
68 samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
69 [bitmap pixelsWide],
70 [bitmap pixelsHigh],
71 0,
72 samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
73 GL_UNSIGNED_BYTE,
74 [bitmap bitmapData]);
75 }
76 else
77 {
78 NSLog(@"Wrong bitmap format.");
79 }
80
81 // Clean up.
82 [bitmap release];
83
84 return texName;
85 }
86
87
88 @implementation NSColor (UKOpenGLExtensions)
89
90 -(void) setForGLContext
91 {
92 NSString* myColorSpace = [self colorSpaceName];
93 if( myColorSpace == NSPatternColorSpace )
94 {
95 NSImage* pattern = [self patternImage];
96 GLuint pat = UKGetTextureFromImage( pattern );
97 glBindTexture( GL_TEXTURE_RECTANGLE_EXT, pat );
98 }
99 else
100 {
101 NSColor* theColor = self;
102
103 if( myColorSpace != NSDeviceRGBColorSpace && myColorSpace != NSCalibratedRGBColorSpace )
104 theColor = [self colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
105
106 GLfloat red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0;
107 red = [theColor redComponent];
108 green = [theColor greenComponent];
109 blue = [theColor blueComponent];
110 alpha = [theColor alphaComponent];
111
112 glColor4f( red, green, blue, alpha );
113 }
114 }
115
116 -(void) setForClearingGLContext
117 {
118 GLfloat red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0;
119 NSString* myColorSpace = [self colorSpaceName];
120 NSColor* theColor = self;
121
122 if( myColorSpace != NSDeviceRGBColorSpace && myColorSpace != NSCalibratedRGBColorSpace )
123 theColor = [self colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
124
125 red = [theColor redComponent];
126 green = [theColor greenComponent];
127 blue = [theColor blueComponent];
128 alpha = [theColor alphaComponent];
129
130 glClearColor( red, green, blue, 1 -alpha );
131 }
132
133
134 @end
Something went wrong with that request. Please try again.