Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. #import "ServerOpenGLView.h"
  2. #import "ServerController.h"
  3.  
  4. #import <OpenGL/OpenGL.h>
  5. #import <OpenGL/CGLMacro.h>
  6. #import <OpenGL/CGLIOSurface.h>
  7.  
  8. #include "atlantis.h"
  9.  
  10. @implementation ServerOpenGLView
  11.  
  12.  
  13. - (void) check_result:(int)result
  14. {
  15. if (result != 0)
  16. {
  17. fprintf(stderr, "CGL error: %d - %s\n", result, CGLErrorString((CGLError)result));
  18. }
  19. }
  20.  
  21.  
  22.  
  23. void checkPixelAttribute(CGLContextObj contextA, CGLContextObj contextB, CGLPixelFormatAttribute attrib)
  24. {
  25. GLint aValue, bValue;
  26.  
  27. CGLPixelFormatObj aFormat = CGLGetPixelFormat(contextA);
  28. CGLPixelFormatObj bFormat = CGLGetPixelFormat(contextB);
  29.  
  30. CGLDescribePixelFormat(aFormat, 0, attrib, &aValue);
  31. CGLDescribePixelFormat(bFormat, 0, attrib, &bValue);
  32.  
  33. if (aValue != bValue)
  34. {
  35. assert(0);
  36. return;
  37. }
  38. }
  39.  
  40.  
  41. - (id)initWithFrame:(NSRect)frameRect
  42. {
  43.  
  44. NSOpenGLPixelFormatAttribute attribs[] =
  45. {
  46. NSOpenGLPFAAllowOfflineRenderers,
  47. NSOpenGLPFAAccelerated,
  48. NSOpenGLPFADoubleBuffer,
  49. NSOpenGLPFAColorSize, 32,
  50. NSOpenGLPFADepthSize, 24,
  51. 0
  52. };
  53.  
  54. NSOpenGLPixelFormat* pixel_format;
  55.  
  56. pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
  57. if(!pixel_format)
  58. {
  59. assert(0);
  60. }
  61.  
  62. self = [super initWithFrame:frameRect pixelFormat:pixel_format];
  63.  
  64. _nsContext = [[NSOpenGLContext alloc] initWithFormat:pixel_format shareContext:nil];
  65.  
  66.  
  67. // !! uncommenting either of the below lines results in the IOSurface appearing correclty in the client
  68. //[self setOpenGLContext:_nsContext];
  69. //_nsContext = [self openGLContext];
  70.  
  71. [_nsContext makeCurrentContext];
  72.  
  73.  
  74. CGLContextObj contextA = [_nsContext CGLContextObj];
  75. CGLContextObj contextB = (CGLContextObj)[[self openGLContext] CGLContextObj];
  76.  
  77. checkPixelAttribute(contextA, contextB, kCGLPFAAllRenderers);
  78. checkPixelAttribute(contextA, contextB, kCGLPFAAllRenderers);//
  79. checkPixelAttribute(contextA, contextB, kCGLPFADoubleBuffer);//
  80. checkPixelAttribute(contextA, contextB, kCGLPFAStereo);//
  81. checkPixelAttribute(contextA, contextB, kCGLPFAAuxBuffers);//
  82. checkPixelAttribute(contextA, contextB, kCGLPFAColorSize );// = 8,
  83. checkPixelAttribute(contextA, contextB, kCGLPFAAlphaSize );// = 11,
  84. checkPixelAttribute(contextA, contextB, kCGLPFADepthSize );// = 12,
  85. checkPixelAttribute(contextA, contextB, kCGLPFAStencilSize );// = 13,
  86. checkPixelAttribute(contextA, contextB, kCGLPFAAccumSize );// = 14,
  87. checkPixelAttribute(contextA, contextB, kCGLPFAMinimumPolicy );// = 51,
  88. checkPixelAttribute(contextA, contextB, kCGLPFAMaximumPolicy );// = 52,
  89. checkPixelAttribute(contextA, contextB, kCGLPFAOffScreen );// = 53,
  90. checkPixelAttribute(contextA, contextB, kCGLPFAFullScreen );// = 54,
  91. checkPixelAttribute(contextA, contextB, kCGLPFASampleBuffers );// = 55,
  92. checkPixelAttribute(contextA, contextB, kCGLPFASamples );// = 56,
  93. checkPixelAttribute(contextA, contextB, kCGLPFAAuxDepthStencil);// = 57,
  94. checkPixelAttribute(contextA, contextB, kCGLPFAColorFloat );// = 58,
  95. checkPixelAttribute(contextA, contextB, kCGLPFAMultisample );// = 59,
  96. checkPixelAttribute(contextA, contextB, kCGLPFASupersample );// = 60,
  97. checkPixelAttribute(contextA, contextB, kCGLPFASampleAlpha );// = 61,
  98. checkPixelAttribute(contextA, contextB, kCGLPFARendererID );// = 70,
  99. checkPixelAttribute(contextA, contextB, kCGLPFASingleRenderer );// = 71,
  100. checkPixelAttribute(contextA, contextB, kCGLPFANoRecovery );// = 72,
  101. checkPixelAttribute(contextA, contextB, kCGLPFAAccelerated );// = 73,
  102. checkPixelAttribute(contextA, contextB, kCGLPFAClosestPolicy );// = 74,
  103. checkPixelAttribute(contextA, contextB, kCGLPFARobust );// = 75,
  104. checkPixelAttribute(contextA, contextB, kCGLPFABackingStore );// = 76,
  105. checkPixelAttribute(contextA, contextB, kCGLPFAMPSafe );// = 78,
  106. checkPixelAttribute(contextA, contextB, kCGLPFAWindow );// = 80,
  107. checkPixelAttribute(contextA, contextB, kCGLPFAMultiScreen );// = 81,
  108. checkPixelAttribute(contextA, contextB, kCGLPFACompliant );// = 83,
  109. checkPixelAttribute(contextA, contextB, kCGLPFADisplayMask );// = 84,
  110. checkPixelAttribute(contextA, contextB, kCGLPFAPBuffer );// = 90,
  111. checkPixelAttribute(contextA, contextB, kCGLPFARemotePBuffer );// = 91,
  112. checkPixelAttribute(contextA, contextB, kCGLPFAAllowOfflineRenderers);// = 96,
  113. checkPixelAttribute(contextA, contextB, kCGLPFAAcceleratedCompute);// = 97,
  114. checkPixelAttribute(contextA, contextB, kCGLPFAOpenGLProfile );// = 99,
  115. checkPixelAttribute(contextA, contextB, kCGLPFAVirtualScreenCount);// = 128,
  116.  
  117. if(self)
  118. InitFishs();
  119.  
  120. NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(renderNow) userInfo:nil repeats:YES];
  121. [t retain];
  122.  
  123.  
  124. return self;
  125. }
  126.  
  127. + (NSOpenGLPixelFormat *)defaultPixelFormat
  128. {
  129. return nil;
  130. }
  131.  
  132.  
  133. - (void)update
  134. {
  135. // Override to do nothing.
  136. }
  137.  
  138. - (NSArray *)rendererNames
  139. {
  140. NSMutableArray *rendererNames;
  141. GLint i, numScreens;
  142. CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  143.  
  144. rendererNames = [[NSMutableArray alloc] init];
  145.  
  146. numScreens = [[self pixelFormat] numberOfVirtualScreens];
  147. for(i = 0; i < numScreens; i++)
  148. {
  149. [_nsContext setCurrentVirtualScreen:i];
  150. [rendererNames addObject:[NSString stringWithUTF8String:(const char *)glGetString(GL_RENDERER)]];
  151. }
  152.  
  153. return [rendererNames autorelease];
  154. }
  155.  
  156. - (void)setRendererIndex:(uint32_t)index
  157. {
  158. [_nsContext setCurrentVirtualScreen:index];
  159. }
  160.  
  161. // Create an IOSurface backed texture
  162. // Create an FBO using the name of this texture and bind the texture to the color attachment of the FBO
  163. - (GLuint)setupIOSurfaceTexture:(IOSurfaceRef)ioSurfaceBuffer
  164. {
  165. GLuint name;
  166. CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  167.  
  168. glGenTextures(1, &name); CHECK_GL_ERROR();
  169.  
  170. glBindTexture(GL_TEXTURE_RECTANGLE_EXT, name); CHECK_GL_ERROR();
  171.  
  172. CGLTexImageIOSurface2D(cgl_ctx, GL_TEXTURE_RECTANGLE_EXT, GL_RGBA, 512, 512, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  173. ioSurfaceBuffer, 0); CHECK_GL_ERROR();
  174.  
  175. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR); CHECK_GL_ERROR();
  176. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CHECK_GL_ERROR();
  177. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();
  178. glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); CHECK_GL_ERROR();
  179.  
  180. // Generate an FBO using the same name with the same texture bound to it as a render target.
  181.  
  182. glBindTexture(GL_TEXTURE_RECTANGLE_EXT, 0); CHECK_GL_ERROR();
  183.  
  184. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, name); CHECK_GL_ERROR();
  185.  
  186. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_EXT, name, 0); CHECK_GL_ERROR();
  187.  
  188.  
  189. if(!_depthBufferName)
  190. {
  191. glGenRenderbuffersEXT(1, &_depthBufferName); CHECK_GL_ERROR();
  192. glRenderbufferStorageEXT(GL_TEXTURE_RECTANGLE_EXT, GL_DEPTH, 512, 512); CHECK_GL_ERROR();
  193. }
  194. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_EXT, _depthBufferName); CHECK_GL_ERROR();
  195.  
  196.  
  197. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); CHECK_GL_ERROR();
  198.  
  199. GLenum complete = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  200. switch (complete)
  201. {
  202. case GL_FRAMEBUFFER_COMPLETE_EXT:
  203. break;
  204. default:
  205. NSLog(@"Framebuffer incomplete");
  206. }
  207.  
  208.  
  209. return name;
  210. }
  211.  
  212. // Render to the current IOSurface via the corresponding FBO previously setuped in -setupIOSurfaceTexture:
  213. - (void)renderToCurrentIOSurface
  214. {
  215. CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  216.  
  217. [_nsContext clearDrawable];
  218. [_nsContext makeCurrentContext];
  219.  
  220.  
  221. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, [[NSApp delegate] currentTextureName]); CHECK_GL_ERROR();
  222.  
  223. glMatrixMode(GL_MODELVIEW); CHECK_GL_ERROR();
  224. glLoadIdentity(); CHECK_GL_ERROR();
  225.  
  226. AtlantisInit(); CHECK_GL_ERROR();
  227. AtlantisAnimate(); CHECK_GL_ERROR();
  228. AtlantisReshape(512, 512); CHECK_GL_ERROR();
  229. AtlantisDisplay(); CHECK_GL_ERROR();
  230.  
  231. glDisable(GL_LIGHTING); CHECK_GL_ERROR();
  232. glDisable(GL_FOG); CHECK_GL_ERROR();
  233.  
  234. // Bind back to system drawable.
  235. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); CHECK_GL_ERROR();
  236.  
  237. glFlush(); CHECK_GL_ERROR();
  238. }
  239.  
  240. // Fill the view with the IOSurface backed texture
  241. - (void)textureFromCurrentIOSurface
  242. {
  243. NSRect bounds = NSMakeRect(0, 0, 512, 512);
  244. CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  245.  
  246. // Render quad from our iosurface texture
  247. glViewport(0, 0, (GLint)bounds.size.width, (GLint)bounds.size.height); CHECK_GL_ERROR();
  248.  
  249. glMatrixMode(GL_PROJECTION); CHECK_GL_ERROR();
  250. glLoadIdentity(); CHECK_GL_ERROR();
  251. glOrtho(0.0, (GLfloat)bounds.size.width, 0.0f, (GLfloat)bounds.size.height, -1.0f, 1.0f); CHECK_GL_ERROR();
  252.  
  253. glMatrixMode(GL_MODELVIEW); CHECK_GL_ERROR();
  254. glLoadIdentity(); CHECK_GL_ERROR();
  255.  
  256. glClearColor(0.0f, 0.0f, 0.0f, 0.0f); CHECK_GL_ERROR();
  257. glClear(GL_COLOR_BUFFER_BIT); CHECK_GL_ERROR();
  258.  
  259. glBindTexture(GL_TEXTURE_RECTANGLE_EXT, [[NSApp delegate] currentTextureName]); CHECK_GL_ERROR();
  260. glEnable(GL_TEXTURE_RECTANGLE_EXT); CHECK_GL_ERROR();
  261. glTexEnvi(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_ENV_MODE, GL_REPLACE); CHECK_GL_ERROR();
  262.  
  263. glBegin(GL_QUADS); CHECK_GL_ERROR();
  264. glColor4f(1.0f, 1.0f, 1.0f, 1.0f); CHECK_GL_ERROR();
  265.  
  266. glTexCoord2f(0.0f, 0.0f); CHECK_GL_ERROR();
  267. glVertex2f(0.0f, 0.0f); CHECK_GL_ERROR();
  268.  
  269. glTexCoord2f(512.0f, 0.0f); CHECK_GL_ERROR();
  270. glVertex2f((GLfloat)bounds.size.width, 0.0f); CHECK_GL_ERROR();
  271.  
  272. glTexCoord2f(512.0f, 512.0f); CHECK_GL_ERROR();
  273. glVertex2f((GLfloat)bounds.size.width, (GLfloat)bounds.size.height); CHECK_GL_ERROR();
  274.  
  275. glTexCoord2f(0.0f, 512.0f); CHECK_GL_ERROR();
  276. glVertex2f(0.0f, (GLfloat)bounds.size.height); CHECK_GL_ERROR();
  277.  
  278. glEnd(); CHECK_GL_ERROR();
  279.  
  280. glDisable(GL_TEXTURE_RECTANGLE_EXT); CHECK_GL_ERROR();
  281. }
  282.  
  283. - (BOOL)isOpaque
  284. {
  285. return YES;
  286. }
  287.  
  288. - (void) renderNow
  289. {
  290. CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  291.  
  292. glEnable(GL_POLYGON_SMOOTH); CHECK_GL_ERROR();
  293. glEnable(GL_DEPTH_TEST); CHECK_GL_ERROR();
  294. [self renderToCurrentIOSurface];
  295. glDisable(GL_DEPTH_TEST); CHECK_GL_ERROR();
  296.  
  297. [_nsContext flushBuffer]; CHECK_GL_ERROR();
  298.  
  299. glFlush(); CHECK_GL_ERROR();
  300. }
  301.  
  302.  
  303. - (void)drawRect:(NSRect)theRect
  304. {
  305. // setNeedsDisplay is also disabled
  306.  
  307. //CGLContextObj cgl_ctx = [_nsContext CGLContextObj];
  308.  
  309. //glEnable(GL_POLYGON_SMOOTH);
  310. //glEnable(GL_DEPTH_TEST);
  311. //[self renderToCurrentIOSurface];
  312. //glDisable(GL_DEPTH_TEST);
  313. //[self textureFromCurrentIOSurface];
  314.  
  315. //[[self openGLContext] flushBuffer];
  316.  
  317. // This flush is necessary to ensure proper behavior if the MT engine is enabled.
  318. //glFlush();
  319.  
  320. }
  321.  
  322.  
  323. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement