OpenGLで文字列

OpenGLで文字列を描画したく、いろいろ探してみたのですが、みんな苦労しているみたいです。

とりあえずわかったことが2つ、日本語はちょっと難しい。

  • まずGLUTを使うやり方

これの場合、日本語は描画できません。ただし簡単に書くことができます

http://d.hatena.ne.jp/osyo-manga/20110827/1314417606

こちらのサイトを参考に

-(void)paint{
		glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // default background color
		glClear(GL_COLOR_BUFFER_BIT);
		
		[self renderString:@"kana" x:0.5 y:0.5f];
		glFlush();
}

-(void)renderString:(NSString *)str_ x:(float)x_ y:(float)y_{
		glRasterPos2d(x_, y_);
		const char* charStr=[str_ UTF8String];
		int len=(int)[str_ length];
		for(int i=0;i

  • テクスチャを使うやり方

日本語を表示したい場合にはテクスチャを使うみたいです。結構重いとのことなので決まった文字列の場合には画像をそのまま貼付けた方が良さそうです

http://null-null.net/blog/2007/10/566.php

こちらのサイトを参考に

+ (void) draw:(NSString *)string_	x:(float)x_ y:(float)y_{
	NSAttributedString *attrString;
	GLuint *texId;

		glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
		glClearDepth( 1.0f );
		glDisable(GL_CULL_FACE);
		glEnable( GL_BLEND );
		glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		
		int i, strSize;
		NSFont *nsfont;
		NSDictionary *attrsDictionary;
		NSAttributedString *singleChar;
		NSImage **images;
		NSBitmapImageRep **imageReps;
		NSImage* img;
		NSPoint point;
		NSSize size;

		int texWidth_=10;
		int texHeight_=10;
		
		// alloc texture id and image buffer
		strSize = (int)[string_ length];
		texId = (GLuint*)malloc( sizeof(GLuint)*strSize );
		images = (NSImage**)malloc( sizeof(NSImage*)*strSize );
		imageReps = (NSBitmapImageRep**)malloc( sizeof(NSBitmapImageRep*)*strSize );
		for( i = 0; i < strSize; i++ ){
				images[i] = [[NSImage alloc] initWithSize:NSMakeSize( texWidth_, texHeight_ )];
		}
		
		// font settings
		NSSize screenResolution = [[[[NSScreen mainScreen] deviceDescription] objectForKey:NSDeviceResolution] sizeValue];
		int fontSize_ = texWidth_ * 72.0/screenResolution.width;
		
		nsfont = [NSFont fontWithName:@"HiraKakuPro-W6" size:fontSize_];
		attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
											 nsfont, NSFontAttributeName,
											 [NSColor whiteColor], NSForegroundColorAttributeName,
											 [NSColor clearColor], NSBackgroundColorAttributeName,
											 nil
											 ];
		
		// alloc attributed string
		attrString = [[NSAttributedString alloc] initWithString:string_ attributes:attrsDictionary];
		
		// create texture id
		glEnable( GL_TEXTURE_2D );
		glGenTextures( strSize, texId );
		
		// build texture image
		for( i = 0; i < strSize; i++ ){
				img = images[i];
				singleChar = [attrString attributedSubstringFromRange:NSMakeRange(i,1)];
				// setting background color
				[img setBackgroundColor:[NSColor clearColor]];
				// calc center position
				size = [singleChar size];
				point = NSMakePoint( (texWidth_-size.width)/2, (texHeight_-size.height)/2 );
				// draw character to image
				[img lockFocus];
				[singleChar drawAtPoint:point];
				[img unlockFocus];
				
				// alloc bitmap image from NSImage
				imageReps[i] = [[NSBitmapImageRep alloc] initWithData:[img TIFFRepresentation]];
				
				// texture settings
				glBindTexture( GL_TEXTURE_2D, texId[i] );
				glTexImage2D(
										 GL_TEXTURE_2D, 0, GL_RGBA,
										 texWidth_, texHeight_, 0,
										 [imageReps[i] hasAlpha] ? GL_RGBA :GL_RGB,
										 GL_UNSIGNED_BYTE,
										 [imageReps[i] bitmapData]
										 );
				glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
				glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
				glBindTexture( GL_TEXTURE_2D, 0 );
		}
		glDisable( GL_TEXTURE_2D );
		
		// release
		for( i = 0; i < strSize; i++ ){
				[imageReps[i] release];
				[images[i] release];
		}
		free( imageReps );
		free( images );
		
	strSize = (int)[string_ length];
	glEnable( GL_TEXTURE_2D );
	for( i = 0; i < strSize; i++ ){
		glBindTexture( GL_TEXTURE_2D, texId[i] );
		glBegin(GL_POLYGON);

						float wf_=0.1f; // TODO Viewのサイズからきちんと求める
						float hf_=0.1f;

						glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
						glTexCoord2f( 0, 1 );
						glVertex2f( x_+i*wf_, y_ );
						glTexCoord2f( 1, 1 );
						glVertex2f( x_+(i+1)*wf_, y_ );
						glTexCoord2f( 1, 0 );
						glVertex2f( x_+(i+1)*wf_, y_+hf_ );
						glTexCoord2f( 0, 0 );
						glVertex2f( x_+i*wf_, y_+hf_);
		glEnd();
	}
	glDisable( GL_TEXTURE_2D );

		glDeleteTextures((int) [string_ length], texId );
		free( texId );
		[attrString release];
}

ちょっといい加減ですがこんな感じ