Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "ServerOpenGLView.h"
- #import "ServerController.h"
- #import <OpenGL/OpenGL.h>
- #import <OpenGL/CGLMacro.h>
- #import <OpenGL/CGLIOSurface.h>
- #include "atlantis.h"
- @implementation ServerOpenGLView
- - (void) check_result:(int)result
- {
- if (result != 0)
- {
- fprintf(stderr, "CGL error: %d - %s\n", result, CGLErrorString((CGLError)result));
- }
- }
- void checkPixelAttribute(CGLContextObj contextA, CGLContextObj contextB, CGLPixelFormatAttribute attrib)
- {
- GLint aValue, bValue;
- CGLPixelFormatObj aFormat = CGLGetPixelFormat(contextA);
- CGLPixelFormatObj bFormat = CGLGetPixelFormat(contextB);
- CGLDescribePixelFormat(aFormat, 0, attrib, &aValue);
- CGLDescribePixelFormat(bFormat, 0, attrib, &bValue);
- if (aValue != bValue)
- {
- assert(0);
- return;
- }
- }
- - (id)initWithFrame:(NSRect)frameRect
- {
- NSOpenGLPixelFormatAttribute attribs[] =
- {
- NSOpenGLPFAAllowOfflineRenderers,
- NSOpenGLPFAAccelerated,
- NSOpenGLPFADoubleBuffer,
- NSOpenGLPFAColorSize, 32,
- NSOpenGLPFADepthSize, 24,
- 0
- };
- NSOpenGLPixelFormat* pixel_format;
- pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
- if(!pixel_format)
- {
- assert(0);
- }
- self = [super initWithFrame:frameRect pixelFormat:pixel_format];
- _nsContext = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:nil];
- // !! uncommenting either of the below lines results in the IOSurface appearing correclty in the client
- //[self setOpenGLContext:_nsContext];
- //_nsContext = [self openGLContext];
- [_nsContext makeCurrentContext];
- CGLContextObj contextA = [_nsContext CGLContextObj];
- CGLContextObj contextB = (CGLContextObj)[[self openGLContext] CGLContextObj];
- checkPixelAttribute(contextA, contextB, kCGLPFAAllRenderers);
- checkPixelAttribute(contextA, contextB, kCGLPFAAllRenderers);//
- checkPixelAttribute(contextA, contextB, kCGLPFADoubleBuffer);//
- checkPixelAttribute(contextA, contextB, kCGLPFAStereo);//
- checkPixelAttribute(contextA, contextB, kCGLPFAAuxBuffers);//
- checkPixelAttribute(contextA, contextB, kCGLPFAColorSize );// = 8,
- checkPixelAttribute(contextA, contextB, kCGLPFAAlphaSize );// = 11,
- checkPixelAttribute(contextA, contextB, kCGLPFADepthSize );// = 12,
- checkPixelAttribute(contextA, contextB, kCGLPFAStencilSize );// = 13,
- checkPixelAttribute(contextA, contextB, kCGLPFAAccumSize );// = 14,
- checkPixelAttribute(contextA, contextB, kCGLPFAMinimumPolicy );// = 51,
- checkPixelAttribute(contextA, contextB, kCGLPFAMaximumPolicy );// = 52,
- checkPixelAttribute(contextA, contextB, kCGLPFAOffScreen );// = 53,
- checkPixelAttribute(contextA, contextB, kCGLPFAFullScreen );// = 54,
- checkPixelAttribute(contextA, contextB, kCGLPFASampleBuffers );// = 55,
- checkPixelAttribute(contextA, contextB, kCGLPFASamples );// = 56,
- checkPixelAttribute(contextA, contextB, kCGLPFAAuxDepthStencil);// = 57,
- checkPixelAttribute(contextA, contextB, kCGLPFAColorFloat );// = 58,
- checkPixelAttribute(contextA, contextB, kCGLPFAMultisample );// = 59,
- checkPixelAttribute(contextA, contextB, kCGLPFASupersample );// = 60,
- checkPixelAttribute(contextA, contextB, kCGLPFASampleAlpha );// = 61,
- checkPixelAttribute(contextA, contextB, kCGLPFARendererID );// = 70,
- checkPixelAttribute(contextA, contextB, kCGLPFASingleRenderer );// = 71,
- checkPixelAttribute(contextA, contextB, kCGLPFANoRecovery );// = 72,
- checkPixelAttribute(contextA, contextB, kCGLPFAAccelerated );// = 73,
- checkPixelAttribute(contextA, contextB, kCGLPFAClosestPolicy );// = 74,
- checkPixelAttribute(contextA, contextB, kCGLPFARobust );// = 75,
- checkPixelAttribute(contextA, contextB, kCGLPFABackingStore );// = 76,
- checkPixelAttribute(contextA, contextB, kCGLPFAMPSafe );// = 78,
- checkPixelAttribute(contextA, contextB, kCGLPFAWindow );// = 80,
- checkPixelAttribute(contextA, contextB, kCGLPFAMultiScreen );// = 81,
- checkPixelAttribute(contextA, contextB, kCGLPFACompliant );// = 83,
- checkPixelAttribute(contextA, contextB, kCGLPFADisplayMask );// = 84,
- checkPixelAttribute(contextA, contextB, kCGLPFAPBuffer );// = 90,
- checkPixelAttribute(contextA, contextB, kCGLPFARemotePBuffer );// = 91,
- checkPixelAttribute(contextA, contextB, kCGLPFAAllowOfflineRenderers);// = 96,
- checkPixelAttribute(contextA, contextB, kCGLPFAAcceleratedCompute);// = 97,
- checkPixelAttribute(contextA, contextB, kCGLPFAOpenGLProfile );// = 99,
- checkPixelAttribute(contextA, contextB, kCGLPFAVirtualScreenCount);// = 128,
- if(self)
- InitFishs();
- NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(renderNow) userInfo:nil repeats:YES];
- [t retain];
- return self;
- }
- + (NSOpenGLPixelFormat *)defaultPixelFormat
- {
- return nil;
- }
- - (void)update
- {
- // Override to do nothing.
- }
- - (NSArray *)rendererNames
- {
- NSMutableArray *rendererNames;
- GLint i, numScreens;
- CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- rendererNames = [[NSMutableArray alloc] init];
- numScreens = [[self pixelFormat] numberOfVirtualScreens];
- for(i = 0; i < numScreens; i++)
- {
- [_nsContext setCurrentVirtualScreen:i];
- [rendererNames addObject:[NSString stringWithUTF8String:(const char *)glGetString(GL_RENDERER)]];
- }
- return [rendererNames autorelease];
- }
- - (void)setRendererIndex:(uint32_t)index
- {
- [_nsContext setCurrentVirtualScreen:index];
- }
- // Create an IOSurface backed texture
- // Create an FBO using the name of this texture and bind the texture to the color attachment of the FBO
- - (GLuint)setupIOSurfaceTexture:(IOSurfaceRef)ioSurfaceBuffer
- {
- GLuint name;
- CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- glGenTextures(1, &name); CHECK_GL_ERROR();
- glBindTexture(GL_TEXTURE_RECTANGLE_EXT, name); CHECK_GL_ERROR();
- CGLTexImageIOSurface2D(cgl_ctx, GL_TEXTURE_RECTANGLE_EXT, GL_RGBA, 512, 512, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
- ioSurfaceBuffer, 0); CHECK_GL_ERROR();
- glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR); CHECK_GL_ERROR();
- glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CHECK_GL_ERROR();
- glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();
- glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();
- // Generate an FBO using the same name with the same texture bound to it as a render target.
- glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 0); CHECK_GL_ERROR();
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, name); CHECK_GL_ERROR();
- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, name, 0); CHECK_GL_ERROR();
- if(!_depthBufferName)
- {
- glGenRenderbuffersEXT(1, &_depthBufferName); CHECK_GL_ERROR();
- glRenderbufferStorageEXT(GL_TEXTURE_RECTANGLE_EXT, GL_DEPTH, 512, 512); CHECK_GL_ERROR();
- }
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_EXT, _depthBufferName); CHECK_GL_ERROR();
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); CHECK_GL_ERROR();
- GLenum complete = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
- switch (complete)
- {
- case GL_FRAMEBUFFER_COMPLETE_EXT:
- break;
- default:
- NSLog(@"Framebuffer incomplete");
- }
- return name;
- }
- // Render to the current IOSurface via the corresponding FBO previously setuped in -setupIOSurfaceTexture:
- - (void)renderToCurrentIOSurface
- {
- CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- [_nsContext clearDrawable];
- [_nsContext makeCurrentContext];
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, [[NSApp delegate] currentTextureName]); CHECK_GL_ERROR();
- glMatrixMode(GL_MODELVIEW); CHECK_GL_ERROR();
- glLoadIdentity(); CHECK_GL_ERROR();
- AtlantisInit(); CHECK_GL_ERROR();
- AtlantisAnimate(); CHECK_GL_ERROR();
- AtlantisReshape(512, 512); CHECK_GL_ERROR();
- AtlantisDisplay(); CHECK_GL_ERROR();
- glDisable(GL_LIGHTING); CHECK_GL_ERROR();
- glDisable(GL_FOG); CHECK_GL_ERROR();
- // Bind back to system drawable.
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); CHECK_GL_ERROR();
- glFlush(); CHECK_GL_ERROR();
- }
- // Fill the view with the IOSurface backed texture
- - (void)textureFromCurrentIOSurface
- {
- NSRect bounds = NSMakeRect(0, 0, 512, 512);
- CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- // Render quad from our iosurface texture
- glViewport(0, 0, (GLint)bounds.size.width, (GLint)bounds.size.height); CHECK_GL_ERROR();
- glMatrixMode(GL_PROJECTION); CHECK_GL_ERROR();
- glLoadIdentity(); CHECK_GL_ERROR();
- glOrtho(0.0, (GLfloat)bounds.size.width, 0.0f, (GLfloat)bounds.size.height, -1.0f, 1.0f); CHECK_GL_ERROR();
- glMatrixMode(GL_MODELVIEW); CHECK_GL_ERROR();
- glLoadIdentity(); CHECK_GL_ERROR();
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f); CHECK_GL_ERROR();
- glClear(GL_COLOR_BUFFER_BIT); CHECK_GL_ERROR();
- glBindTexture(GL_TEXTURE_RECTANGLE_EXT, [[NSApp delegate] currentTextureName]); CHECK_GL_ERROR();
- glEnable(GL_TEXTURE_RECTANGLE_EXT); CHECK_GL_ERROR();
- glTexEnvi(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_ENV_MODE, GL_REPLACE); CHECK_GL_ERROR();
- glBegin(GL_QUADS); CHECK_GL_ERROR();
- glColor4f(1.0f, 1.0f, 1.0f, 1.0f); CHECK_GL_ERROR();
- glTexCoord2f(0.0f, 0.0f); CHECK_GL_ERROR();
- glVertex2f(0.0f, 0.0f); CHECK_GL_ERROR();
- glTexCoord2f(512.0f, 0.0f); CHECK_GL_ERROR();
- glVertex2f((GLfloat)bounds.size.width, 0.0f); CHECK_GL_ERROR();
- glTexCoord2f(512.0f, 512.0f); CHECK_GL_ERROR();
- glVertex2f((GLfloat)bounds.size.width, (GLfloat)bounds.size.height); CHECK_GL_ERROR();
- glTexCoord2f(0.0f, 512.0f); CHECK_GL_ERROR();
- glVertex2f(0.0f, (GLfloat)bounds.size.height); CHECK_GL_ERROR();
- glEnd(); CHECK_GL_ERROR();
- glDisable(GL_TEXTURE_RECTANGLE_EXT); CHECK_GL_ERROR();
- }
- - (BOOL)isOpaque
- {
- return YES;
- }
- - (void) renderNow
- {
- CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- glEnable(GL_POLYGON_SMOOTH); CHECK_GL_ERROR();
- glEnable(GL_DEPTH_TEST); CHECK_GL_ERROR();
- [self renderToCurrentIOSurface];
- glDisable(GL_DEPTH_TEST); CHECK_GL_ERROR();
- [_nsContext flushBuffer]; CHECK_GL_ERROR();
- glFlush(); CHECK_GL_ERROR();
- }
- - (void)drawRect:(NSRect)theRect
- {
- // setNeedsDisplay is also disabled
- //CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
- //glEnable(GL_POLYGON_SMOOTH);
- //glEnable(GL_DEPTH_TEST);
- //[self renderToCurrentIOSurface];
- //glDisable(GL_DEPTH_TEST);
- //[self textureFromCurrentIOSurface];
- //[[self openGLContext] flushBuffer];
- // This flush is necessary to ensure proper behavior if the MT engine is enabled.
- //glFlush();
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement