在OpenGL视图中绘制文本

时间:2010-03-09 19:31:32

标签: cocoa opengl

我需要在OpenGL视图上绘制一个计数器。这是我的视频帧显示源代码。

cgl_ctx = CGLContextObj ( [[self openGLContext]  CGLContextObj]);

glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &_surfaceTexture);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
GLint swapInt = 1;//100;//1;

[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; 
[[self openGLContext] makeCurrentContext];//make the current opengl the priority

glMatrixMode(GL_TEXTURE);

glGenTextures(1, &_surfaceTexture);
glBindTexture(GL_TEXTURE_2D, _surfaceTexture);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);

glTexImage2D(
     GL_TEXTURE_2D,
     0,
     GL_RGBA,
     displayWidth,//960,//1920,
     displayHeight,//540,//1080,
     0,
     GL_BGRA,//GL_YCBCR_422_APPLE,//GL_RGB,//GL_APPLE_ycbcr_422,
     GL_UNSIGNED_BYTE,//GL_BYTE,//GL_UNSIGNED_BYTE,//GL_UNSIGNED_SHORT_8_8_REV_APPLE,
     IOSurfaceGetBaseAddress(videoBuffer->ioSurfaceDataBuffer[bufferCounter].RGBDest ));

glBegin(GL_QUADS);
  glTexCoord2f(0.0, 0.0);
  glVertex3f(-1.0, -1.0, 0.0);
  glTexCoord2f(1.0, 0.0);
  glVertex3f(1.0, -1.0, 0.0);
  glTexCoord2f(1.0, 1.0);
  glVertex3f(1.0, 1.0, 0.0);
  glTexCoord2f(0.0, 1.0);
  glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glFlush();  
threadTracking ("view 11\n");
printf("view 2.2) videoBuffer->ioSurfaceDataBuffer[bufferCounter].played  %d\n",videoBuffer->ioSurfaceDataBuffer[bufferCounter].played);

[[self openGLContext] flushBuffer]; 

1 个答案:

答案 0 :(得分:0)

我从另一个显示视频信息的视频播放器获取了源代码,这是我添加的用于直接使用openGL添加文本的显示帧。

char frameNumberBuffer[256];
CGColorSpaceRef frameCounterColorSpace;
CGContextRef frameCounterCglCtx;
frameCounterColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);

frameCounterCglCtx = CGBitmapContextCreate( IOSurfaceGetBaseAddress (videoBuffer->ioSurfaceDataBuffer[bufferCounter].RGBDest),
                                                              displayWidth, displayHeight, 8, IOSurfaceGetBytesPerRow(videoBuffer->ioSurfaceDataBuffer[bufferCounter].RGBDest),
                                                              frameCounterColorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);


CGContextSelectFont(frameCounterCglCtx, "Helvetica", 72.0, kCGEncodingMacRoman);
snprintf(frameNumberBuffer,256,"Frame Counter:  %d",playCounter);

CGContextSetRGBFillColor(frameCounterCglCtx, 1.0f, 0.0f, 0.0f, 1.0f);
CGContextShowTextAtPoint(frameCounterCglCtx, 100.0f, 100.0f, frameNumberBuffer, strlen(frameNumberBuffer));         

snprintf(frameNumberBuffer,256,"Buffer Counter:  %d",bufferCounter);

CGContextSetRGBFillColor(frameCounterCglCtx, 0.0f, 0.0f, 1.0f, 1.0f);
CGContextShowTextAtPoint(frameCounterCglCtx, 100.0f, 200.0f, frameNumberBuffer, strlen(frameNumberBuffer));         

其中IOSurfaceGetBaseAddress(videoBuffer-> ioSurfaceDataBuffer [bufferCounter] .RGBDest)将数据指针返回到存储在IOSurface上的原始帧数据