1 /* $Id: PictureGLView.mm,v 1.6 2004/03/08 12:39:49 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
7 #include <OpenGL/OpenGL.h>
11 #include "PictureGLView.h"
15 /* XXX This file needs some serious cleaning XXX */
22 @implementation HBPictureGLView
24 - (void) SetHandle: (HBHandle*) handle
29 - (void) SetTitle: (HBTitle*) title
33 /* This is needed as the view's size may have changed */
34 [self clearGLContext];
38 - (void) ShowPicture: (int) index animate: (int) how
40 if( fOldPicture ) free( fOldPicture );
41 fOldPicture = fPicture;
44 uint8_t * tmp = HBGetPreview( fHandle, fTitle, index );
46 /* Make it be upside-down */
47 fPicture = (uint8_t*) malloc( 4 * ( fTitle->outWidthMax + 2 ) *
48 ( fTitle->outHeightMax + 2 ) );
50 uint8_t * out = fPicture +
51 4 * ( fTitle->outWidthMax + 2 ) * ( fTitle->outHeightMax + 1 );
52 for( int i = fTitle->outHeightMax + 2; i--; )
54 memcpy( out, in, 4 * ( fTitle->outWidthMax + 2 ) );
55 in += 4 * ( fTitle->outWidthMax + 2 );
56 out -= 4 * ( fTitle->outWidthMax + 2 );
61 uint32_t * p = (uint32_t*) fPicture;
62 for( int i = ( fTitle->outHeightMax + 2 ) *
63 ( fTitle->outWidthMax + 2 ); i--; )
65 *(p++) = ( ( (*p) & 0x00FFFFFF ) << 8 ) | 0xFF;
68 if( how == HB_ANIMATE_NONE )
70 [self drawRect: [self bounds]];
76 for( int i = fTitle->outHeightMax + 2; i--; )
78 memcpy( out, in, ( fTitle->outWidthMax + 2 ) * 4 );
79 in += ( fTitle->outWidthMax + 2 ) * 4;
82 glBindTexture( GL_TEXTURE_2D, texture[0] );
83 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 1024,
85 GL_UNSIGNED_BYTE, truc );
86 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
87 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
91 for( int i = fTitle->outHeightMax + 2; i--; )
93 memcpy( out, in, ( fTitle->outWidthMax + 2 ) * 4 );
94 in += ( fTitle->outWidthMax + 2 ) * 4;
97 glBindTexture( GL_TEXTURE_2D, texture[1] );
98 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 1024,
100 GL_UNSIGNED_BYTE, truc );
101 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
102 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
104 glEnable( GL_TEXTURE_2D );
105 glShadeModel( GL_SMOOTH );
106 glClearColor( 0.0f, 0.0f, 0.0f, 0.5f );
107 glClearDepth( 1.0f );
108 glEnable( GL_DEPTH_TEST );
109 glDepthFunc( GL_LEQUAL );
110 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
112 #define ANIMATION_TIME 500000
113 #define FRAME_PER_SEC 50
116 float w = ( how == HB_ANIMATE_LEFT ) ? 1.0 : -1.0;
122 translation = - PROUT - cos( rotation * M_PI / 180 ) *
123 ( 1 + w * tan( rotation * M_PI / 180 ) );
124 [self drawAnimation: how];
126 rotation += w * 90 * 1000000 / ANIMATION_TIME / FRAME_PER_SEC;
127 if( w * rotation >= 90.0 )
132 wait = 1000000 / FRAME_PER_SEC - ( HBGetDate() - date );
139 [self drawRect: [self bounds]];
142 - (id) initWithFrame: (NSRect) frame
151 NSOpenGLPFANoRecovery,
153 NSOpenGLPFAAccelerated,
154 NSOpenGLPFADoubleBuffer,
155 NSOpenGLPFAColorSize, 24,
156 NSOpenGLPFAAlphaSize, 8,
157 NSOpenGLPFADepthSize, 24,
158 NSOpenGLPFAStencilSize, 8,
159 NSOpenGLPFAAccumSize, 0,
163 NSOpenGLPixelFormat * fmt = [[NSOpenGLPixelFormat alloc]
164 initWithAttributes: (NSOpenGLPixelFormatAttribute*) attribs];
166 self = [super initWithFrame:frame pixelFormat: [fmt autorelease]];
173 [[self openGLContext] makeCurrentContext];
177 glGenTextures( 2, texture );
178 truc = (uint8_t*) malloc( 1024*1024*4 );
190 [[self openGLContext] update];
191 bounds = [self bounds];
194 glViewport( 0, 0, fTitle->outWidthMax + 2,
195 fTitle->outHeightMax + 2 );
199 - (void) drawAnimation: (int) how
201 /* Swap buffers only during the vertical retrace of the monitor.
202 http://developer.apple.com/documentation/GraphicsImaging/
203 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
204 long params[] = { 1 };
205 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
208 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
209 glMatrixMode( GL_PROJECTION );
211 glFrustum( -1.0, 1.0, -1.0, 1.0, PROUT, 20.0 );
212 glMatrixMode( GL_MODELVIEW );
214 glTranslatef( 0.0, 0.0, translation );
215 glRotatef( rotation, 0.0, 1.0, 0.0 );
217 glEnable( GL_POLYGON_SMOOTH );
218 glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
220 glBindTexture( GL_TEXTURE_2D, texture[0] );
223 glTexCoord2f( 0.0, 0.0 );
224 glVertex3f( -1.0, -1.0, 1.0 );
225 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024, 0.0 );
226 glVertex3f( 1.0, -1.0, 1.0 );
227 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024,
228 ( 2.0 + fTitle->outHeightMax ) / 1024 );
229 glVertex3f( 1.0, 1.0, 1.0 );
230 glTexCoord2f( 0.0, ( 2.0 + fTitle->outHeightMax ) / 1024 );
231 glVertex3f( -1.0, 1.0, 1.0 );
234 glBindTexture( GL_TEXTURE_2D, texture[1] );
237 if( how == HB_ANIMATE_RIGHT )
239 glTexCoord2f( 0.0, 0.0 );
240 glVertex3f( 1.0, -1.0, 1.0 );
241 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024, 0.0 );
242 glVertex3f( 1.0, -1.0, -1.0 );
243 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024,
244 ( 2.0 + fTitle->outHeightMax ) / 1024 );
245 glVertex3f( 1.0, 1.0, -1.0 );
246 glTexCoord2f( 0.0, ( 2.0 + fTitle->outHeightMax ) / 1024 );
247 glVertex3f( 1.0, 1.0, 1.0 );
251 glTexCoord2f( 0.0, 0.0 );
252 glVertex3f( -1.0, -1.0, -1.0 );
253 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024, 0.0 );
254 glVertex3f( -1.0, -1.0, 1.0 );
255 glTexCoord2f( ( 2.0 + fTitle->outWidthMax ) / 1024,
256 ( 2.0 + fTitle->outHeightMax ) / 1024 );
257 glVertex3f( -1.0, 1.0, 1.0 );
258 glTexCoord2f( 0.0, ( 2.0 + fTitle->outHeightMax ) / 1024 );
259 glVertex3f( -1.0, 1.0, -1.0 );
263 [[self openGLContext] flushBuffer];
266 - (void) drawRect: (NSRect) rect
268 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
275 glDrawPixels( fTitle->outWidthMax + 2,
276 fTitle->outHeightMax + 2, GL_RGBA,
277 GL_UNSIGNED_BYTE, fPicture );
279 [[self openGLContext] flushBuffer];