PDA

View Full Version : Writing text in a World



danu.oleg
10-15-2010, 12:39 AM
Hi, I'm new in this forum, as in OpenGL development on Iphone.
I have the next situation. I need to draw a graph. Initially I have drawn the background of it. Now I need to add labels to the axises. I am using Texture2D.h/m library of the Iphone to draw text. Now when I applied the drawing of test text, it was drawn but the background of the graph didn't. Could you please help me solving this issue? Here is my code.

- (void)drawView:(UIView *)theView
{

glColor4f(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Graph background
Vertex3D vertices[]= {
{ 2.00, -3.00, -5.0}, //Point 0
{ -2.00, -3.00, -5.0}, //Point 1
{ -2.00, 2.75, -5.0}, //Point 2
{ 2.00, 2.75, -5.0}, //Point 3
{ 2.03, 2.78, -5.0}, //4
{ -2.03, 2.78, -5.0}, //5
{ -2.03, -3.03, -5.0} //6
};

GLubyte squareFaces[] = {
0, 1, 2,
0, 3, 2
};

GLubyte coordinateLines[] = {
4, 5,
5, 6
};

//Draw coordinates
glEnableClientState(GL_VERTEX_ARRAY);

glClearColor(0.5, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//Draw graph bground
glColor4f(RedFromRGB(0xFFFFFF), GreenFromRGB(0xFFFFFF), BlueFromRGB(0xFFFFFF), 1.0);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, squareFaces);

//Draw coordinates
glColor4f(RedFromRGB(0x000000), GreenFromRGB(0x000000), BlueFromRGB(0x000000), 1.0);
glDrawElements(GL_LINES, 4, GL_UNSIGNED_BYTE, coordinateLines);

glDisableClientState(GL_VERTEX_ARRAY);

[self drawText]; //Text
}

- (void) drawText{

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);

Texture2D* lWord= [[Texture2D alloc] initWithString:@"Hello World" dimensions:CGSizeMake(128, 128) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:14];
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(RedFromRGB(0xFF0000), GreenFromRGB(0xFF0000), BlueFromRGB(0xFF0000), 1.0);
[lWord drawAtPoint:CGPointMake(self.view.bounds.size.widt h / 2, self.view.bounds.size.height * 2 / 3)];
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}

danu.oleg
10-15-2010, 12:42 AM
const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 45.0;
GLfloat size;
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

CGRect rect = view.bounds;

glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This is the code which is executed before drawing the background.

danu.oleg
10-15-2010, 06:31 AM
I have solved my problem, the idea was in using Orthogonal representation with perspective toghether. So I commented frustrum and applied orthof function, like so

glOrthof(0, rect.size.width, 0, rect.size.height, 0.0, 2.0);
glViewport(0, 0, rect.size.width, rect.size.height);