OPENGL ES 2.0 Render Text on screen
I am currently working on an OPENGL ES 2.0 project. I am making a bar graph application. I have achieved success in making the graph. I am trying to insert text on the graph. I found out that there is no direct solution to achieve the same.
I came across a OPENGL ES 1.1 code, which has the following lines in it.
File: In GLViewController.m (from the drawView function)
Texture2D *textTex = [[Texture2D alloc开发者_Go百科] initWithString:@"Text"
dimensions:CGSizeMake(100., 40.0)
alignment:UITextAlignmentCenter
font:[UIFont boldSystemFontOfSize:10.0]];
[textTex drawAtPoint:CGPointMake(160.0, 100.0) depth:-1];
In the Texture2D.m file,
- (void) drawAtPoint:(CGPoint)point depth:(CGFloat)depth
{
GLfloat coordinates[] = {
0, _maxT,
_maxS, _maxT,
0, 0,
_maxS, 0
};
GLfloat width = (GLfloat)_width * _maxS,
height = (GLfloat)_height * _maxT;
GLfloat vertices[] = {
-width / 2 + point.x, -height / 2 + point.y, depth,
width / 2 + point.x, -height / 2 + point.y, depth,
-width / 2 + point.x, height / 2 + point.y, depth,
width / 2 + point.x, height / 2 + point.y, depth
};
glBindTexture(GL_TEXTURE_2D, texture.name);
glVertexPointer(3, GL_FLOAT, 0, vertices); //1
glTexCoordPointer(2, GL_FLOAT, 0, coordinates); //2
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
When I tried to convert this code into OPENGL ES 2.0 code, I am not able to find a suitable conversion for lines marked 1 and 2. Can someone please help me convert this code to OPENGL ES 2.0 code?
Or if an anyone is aware of an easier method to write text on screen, please let me know.
OpenGL ES 1.x and 2.x share a lot of concepts when it comes to geometry. You specify vertices, then you join them with geometry, which is turned into pixels. The difference is that you specify custom code in two places: to take the input geometry and figure out where it goes on screen and what stuff it passes on through the pipeline, and to figure out what colour an individual fragment is based on the information coming along the pipeline.
Because ES 1.x wasn't programmable in that sense, it had fixed functionality at both places. Which meant fixed data structures, and hence why you supply data via glVertexPointer
, glTexCoordPointer
, etc. You're supplying values for the fixed, predetermined fields that OpenGL has written into stone as describing a vertex.
Because ES 2.x is fully programmable and minimalist, it makes no assumptions about what fields describe a vertex and accordingly has no special case data provision methods, such as one to supply locations (glVertexPointer
), another to supply texture coordinates (glTexCoordPointer
), etc.
In ES 2.x you use glVertexAttribPointer to replace all of the fixed meaning ES 1.x calls. The first parameter identifies which attribute you're specifying by index, that index having been forced by yourself via glBindAttribLocation
or left to figure themselves out and requested back via glGetAttribLocation
.
You'll also need to write a suitable fragment and pixel shader, essentially just to pass the texture coordinates you nominate on, and to sample the texture per pixel, then pass that on. Should be just one or two lines in the body of each.
Probably you have some sort of framework in place for dealing with this stuff in your graphing. Because ES 2.x is so generic about attributes, it should be possible to reuse much the same stuff.
精彩评论