OpenGLES 在WinCE6.0上写字

根据这个http://yarin.blog.51cto.com/blog/1130898/381955 修改的。

原理是: 在内存DC上写字符串,然后获取DC像素数据,判断数据不为0的,就是写字的像素,获取像素所在位置。

在OpenGLES根据位置画点。

开始直接搬那个函数不知道怎么显示不了,就乱改一通,估计那个设置不对吧。

对GLES不懂,几天没看,函数都忘光光。


void COpenGLES::glTextShow(int fontsize, int style, int x, int y, const WCHAR *string)     
{     //显示不正确
	int len, xx = 0, yy = 0, nbpoints = 0, i;     
	GLfloat *points;      
	HFONT font;     
	LOGFONTW    lf;         
	RECT rect;     
	static HBITMAP bmp;     
	static BYTE *img;     
	static HDC hdc = NULL;     
	static BITMAPINFO bi;     
	SIZE sz;     
	static EGLint width = 800, height = 480;     
	
	if(!string)     
		return;     
	if(!string[0])     
		return;     
	if(!hdc)     
	{             
		hdc = CreateCompatibleDC(GetDC(NULL));         
		ZeroMemory(&bi, sizeof(BITMAPINFO));     
		bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);     
		bi.bmiHeader.biWidth = width;     
		bi.bmiHeader.biHeight = height;      
		bi.bmiHeader.biBitCount = 8;     
		bi.bmiHeader.biPlanes = 1;     
		bmp = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&img, NULL, 0);     
		SelectObject(hdc, bmp);     
		SelectObject(hdc, GetStockObject(BLACK_BRUSH));     
		SetBkMode(hdc, TRANSPARENT);     
		SetTextColor(hdc, RGB(255, 255, 255));                 
	}     
	Rectangle(hdc, 0, 0, width, height);     
	ZeroMemory(img, width * height);     
	lf.lfCharSet = DEFAULT_CHARSET;     
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;     
	lf.lfEscapement = 0;     
	wcscpy(lf.lfFaceName, L"GLES");     
	lf.lfHeight = fontsize;     
	lf.lfItalic = (style & 1) ? TRUE : FALSE;     
	lf.lfOrientation = 0;     
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;     
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;     
	lf.lfQuality = DEFAULT_QUALITY;     
	lf.lfStrikeOut = FALSE;     
	lf.lfUnderline = (style & 4) ? TRUE : FALSE;     
	lf.lfWidth = 0;     
	lf.lfWeight = (style & 2) ? FW_BOLD : FW_NORMAL;     
	font = CreateFontIndirectW(&lf);     
	SelectObject(hdc, font);     
	len = wcslen(string);     
	GetTextExtentPointW(hdc, string, len, &sz);     
	y = 480 - y - sz.cy;
	rect.left = max(0, min(x, width));     
	rect.top = max(0, min(y, height));     
	rect.right = min(rect.left + sz.cx, width);     
	rect.bottom = min(rect.top + sz.cy, height);     
	DrawTextW(hdc, string, len, &rect, DT_LEFT | DT_BOTTOM);     
	HDC dc= GetDC(NULL);
	points = (GLfloat*)malloc(sz.cx * sz.cy * 2 * sizeof(GLfloat));     
	for(yy = rect.top; yy < rect.bottom; yy++)     
	{     
		for(xx = rect.left; xx < rect.right; xx++)     
		{     
			if(img[xx + (height - yy) * width] != 0)     
			{  
				points[nbpoints * 2 + 0] = xx - x;     
				points[nbpoints * 2 + 1] = (float)(rect.top + sz.cy - (yy - rect.top)) - y;     
				nbpoints++;     
			}     
		}     
	}    
	DeleteObject(font);     
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(0, (800.0f),0, (480.0f), -1.0f, 1.0f);     
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT);
	glColor4f(1.0f,0,0,1.0f);
	glDisable(GL_BLEND);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);//禁用颜色数组、
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(2,GL_FLOAT,0,points);
	glPushMatrix();
	glTranslatef(x,y,-0.010001f);
	glPointSize(1.0f);
	glDrawArrays(GL_POINTS,0,nbpoints);
	glPopMatrix();
	glFinish();
	EGLFlush();
	free(points);     
}   

  

不管怎么做,终于写上字了,貌似速度挺慢的。

我的屏幕800*480的,直接写死了,字体名字也改的写死了

原来博主的顶点坐标都是用GL_SHORT型的,我都改成float的了

画点也改成了glDrawArrays,

设置投影矩阵的时候也用浮点型函数glOrthof

指定顶点数组也就用了浮点型,glVertexPointer(2,GL_FLOAT,0,points);

单纯画点之前把,纹理矩阵、颜色矩阵、ALPHA、BLEND都禁止掉,不然可能看不见。

貌似投影矩阵指定的时候用的函数类型与顶点不一样会画不上去

所以设置模型的时候用浮点型函数,画图的时候最好也用浮点的坐标

,要用x型的都用x型,原因不知,但还是先不较真了。

glFrustumf对应GL_FLOAT

glFrustumx对应GL_FIX

用的时候

直接调用

gl.glTextShow(25,NULL,0,480,L"OpenGL编程指南");

就可以显示

这一个函数就会刷整个屏幕,具体使用,还得修改。估计就改成原来博主那种写法了。。。。

好久没看,竟然对以前看的东西又怀疑了,最后又发现以前是对的。。。

投影矩阵函数设置一个投影矩阵

在模型视图矩阵中画东西的时候,glLoadIdentity();之后,模型视图矩阵默认的z原点在屏幕上

要看见画的物体,要将模型视图矩阵移动到投影矩阵中。

通过函数:glTranslatef(x,y,-0.010001f);移动。

再学到更多之前,投影矩阵设置用f型函数,都用浮点型

顶点也用浮点型的数据表示  

设置投影矩阵的时候,就设置成屏幕大小的,方便坐标使用。

glOrthof(0, (800.0f),0, (480.0f), -1.0f, 1.0f);   平行投影

glFrustumf(-0.0f, 800.0f, -0.0f, 480.0f, 0.01f,1000.0f); 透视投影

单纯画点之前把,纹理矩阵、颜色矩阵、ALPHA、BLEND都禁止掉,不然可能看不见。

原文地址:https://www.cnblogs.com/ezhong/p/2171457.html