Set background for 320 * 70 EAGLView - iphone

I want to set the background image for 320 * 70 EAGLView (using openGL). I set
glOrthof(0, backingWidth, 0, backingHeight, -1.0f, 1.0f);
where backingWidth = 320; backingHeight = 70;
Sorry if my issue is obvious or some of the points are incorrect. The question is how to make the coordinates in a vertices array to set background properly. I'm new to openGL, but as far as I know these coordinates should be having degrees of 2. How should I set these coordinates to 320 and 70.
I've investigated Apple's sample code of GLImageProcessing and they have 512 * 512 vertex array that covered 320 * 480 of the view. How can I relate 512 * 512 vertex array of ImageCoordinate on the screen to real coordinates?
This is my code, and now part of image is now shown:
// Clear the view
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(1., 1., 1., 1.);
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
{
// Draw our background oscilloscope screen
const GLfloat vertices[] = {
0., 0.,
512., 0.,
0., 256.,
512., 256.,
};
const GLshort texCoords[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
glBindTexture(GL_TEXTURE_2D, bgTexture);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_SHORT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

Related

OpenGL ES How to Correctly Combine Orthof and Frustum

I am beginning to learn OpenGL ES 1.1 for iPhone and would like to draw a 2D image in orthographic projection behind a few 3D objects. Using Jeff Lamarche's tutorials and the book Pro OpenGL ES for iPhone and I've come up with the following couple methods to attempt to do this. If I disable the call to drawSunInRect the 3D objects are rendered just fine and I can move them with touch controls etc. If I uncomment that call and try to draw the sun image, the image appears in the CGRect I supply, but I cannot see any of my other 3D objects - the rest of the screen is black. I've tried to disable/enable depth testing in various places, pass different parameters to glOrthof(), and move around the rectangle but I keep getting the sun image only when the drawSunInRect method is called. I'm assuming it is covering my 3D objects.
// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {
// Get screen bounds
CGRect frame = [[UIScreen mainScreen] bounds];
// Calculate vertices from passed CGRect and depth
GLfloat vertices[] =
{
rect.origin.x, rect.origin.y, depth,
rect.origin.x + rect.size.width , rect.origin.y, depth,
rect.origin.x, rect.size.height+rect.origin.y , depth,
rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
};
// Map the texture coords - no repeating
static GLfloat textureCoords[] =
{
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};
// Disable DEPTH test and setup Ortho
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000.0);
// Enable blending and configure
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
// Enable VERTEX and TEXTURE client states
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Enable Textures
glEnable(GL_TEXTURE_2D);
// Projection Matrix Mode for ortho and reset
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// No lighting or Depth Mask for 2D - no Culling
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
glDisableClientState(GL_COLOR_ARRAY);
// Define ortho
glOrthof(0,frame.size.width,frame.size.height,0,0.1f,1000);
// From Jeff Lamarche Tutorials
// glOrthof(-1.0, // Left
// 1.0, // Right
// -1.0 / (rect.size.width / rect.size.height), // Bottom
// 1.0 / (rect.size.width / rect.size.height), // Top
// 0.01, // Near
// 10000.0); // Far
// Setup Model View Matrix
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Bind and draw the Texture
glBindTexture(GL_TEXTURE_2D,sunInt);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT,0,textureCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Re-enable lighting
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDepthMask(GL_TRUE);
}
// Override the draw in rect function
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
// Initialize and init the rotation stuff for the object
// Identity Matrix
glLoadIdentity();
static GLfloat rot = 0.0;
// Clear any remnants in the drawing buffers
// and fill the background with black
glClearColor(0.0f,0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the Sun - 2D Sun Image in CGRect
//[self drawSunInRect:CGRectMake(100, 100, 50, 50) withDepth:-30.0]; // 2D Sun Image
// Render the BattleCruiser - 3D Battlecruiser
[self renderTheBattleCruiser];
// Calculate a time interval to use to rotate the cruiser lives
static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rot += 75 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
}
UPDATE
I modified the draw sun method to have only 1 push and 1 pop for the GL_PROJECTION. I am still having the same issue. The sun image appears but I cannot see my 3D Objects. I have tried to re-arrange the calls to the render methods so the 3D objects are rendered first but I get the same results. I would appreciate other ideas on how to see my orthographic texture and 3D objects together.
// Draw Sun in Rect and with Depth
- (void)drawSunInRect:(CGRect)rect withDepth:(float)depth {
// Get screen bounds
CGRect frame = [[UIScreen mainScreen] bounds];
// Calculate vertices from passed CGRect and depth
GLfloat vertices[] =
{
rect.origin.x, rect.origin.y, depth,
rect.origin.x + rect.size.width , rect.origin.y, depth,
rect.origin.x, rect.size.height+rect.origin.y , depth,
rect.origin.x + rect.size.width , rect.size.height+rect.origin.y ,depth
};
// Map the texture coords - no repeating
static GLfloat textureCoords[] =
{
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glDisable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisableClientState(GL_COLOR_ARRAY);
glOrthof(0,frame.size.width,frame.size.height,0,0,1000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor4f(1,1,1,1);
glBindTexture(GL_TEXTURE_2D,sunInt);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT,0,textureCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
I realized I was calling setClipping only once in my viewController viewDidLoad. I moved it to my glkView method and now I get my Sun image and my 3d object.

How to change the perspective on a 3D cube in OpenGL ES?

I draw a cube with OpenGL ES and I want to have an above perspective on it.But I don't know what should I modify...Maybe you can help.
- (void)render:(CADisplayLink*)displayLink {
//-(void) render{
glClearColor(255.0, 255.0/255.0, 255.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//projection
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:8];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
// _currentScaling += displayLink.duration * 1.0;
[modelView populateFromTranslation:CC3VectorMake(sin(30), _currentScaling , -7)];
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
// [modelView scaleByY:_currentScaling];
if(_currentScaling > 2 ){
[displayLink invalidate];
_running = 1;
// scale = 0;
}
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
// 3
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]),
GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
I recently started looking into OpenGL myself and found this same example. I believe what you are looking for is to change this line to change the perspective:
[modelView rotateBy:CC3VectorMake(0, -15, 0)];
and specify the degrees of rotation you want for the x,y,z axes.

How to set background image using OpenGL ES 1.1 for iPhone

Please help me to set a background image in EaglView. Here my source code with the image:
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
brushImage = [UIImage imageNamed:#"Particle.png"].CGImage;
width = CGImageGetWidth(brushImage);
height = CGImageGetHeight(brushImage);
if(brushImage) {
brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushImage);
CGContextRelease(brushContext);
glGenTextures(1, &brushTexture);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
free(brushData);
}
self.contentScaleFactor = 1.0;
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(width / kBrushScale);`
Instead of using a point sprite should simply draw a textured quad.
GLfloat vert_tex[] = {
0., 0.,
1., 0.,
1., 1.,
0., 1.
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_name);
glDrawArrays(GL_QUADS, 0, 4);
EDIT2:
// Erases the screen
- (void) erase
{
[EAGLContext setCurrentContext:context];
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
GLfloat vert_tex[] = {
0., 0.,
frame.size.width * scale, 0.,
frame.size.width * scale, frame.size.height * scale,
0., frame.size.height * scale
};
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vert_tex);
glTexCoordPointer(2, GL_FLOAT, 0, vert_tex);
// Setup the view port in Pixels
glMatrixMode(GL_PROJECTION);
glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
// background_image texture must be initialized with the desired
// content; see initWithCoder of original GLPaint example for
// example code how to load textures.
glBindTexture(GL_TEXTURE_2D, background_image);
// Clear the buffer with background image
glDrawArrays(GL_QUADS, 0, 4);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
In renderLineFromPoint you must add a line to bind the brush texture before drawing:
// Drawings a line onscreen based on where the user touches
- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
/* ... */
// add this line here v v v v v v v v v v v
glBindTexture(GL_TEXURE_2D, brushTexture);
// ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

OpenGL ES Framebuffer weird mirroring when drawing

I really can't wrap my mind around this:
Previously I couldn't get Framebuffers to work, but I've got it going now. However, there is this incredibly weird mirroring going on with the texture generated from the framebuffer, and I have no idea why. Basically, I will try to draw a texture at 0,0 using GL_TRIANGLE_FAN, and the texture appears as normal (more or less) in the top right corner, but also appears in the bottom left corner, mirrored. If I fill up most or all of my viewport area with the same texture, the result is an ugly z-fighting overlap.
Screenshots will do this more justice.
Original image:
Original http://img301.imageshack.us/img301/1518/testsprite.png
Drawn 80x80 at (0,0)
80x80 http://img407.imageshack.us/img407/8339/screenshot20100106at315.png
Drawn 100x180 at (0,0)
100x180 http://img503.imageshack.us/img503/2584/screenshot20100106at316.png
Drawn 320x480 at (0,0)
320x480 http://img85.imageshack.us/img85/9172/screenshot20100106at317.png
And here is my code:
Set up the view:
//Apply the 2D orthographic perspective.
glViewport(0,0,320,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, 320, 480, 0, -10000.0f, 100.0f);
//Disable depth testing.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
//Enable vertext and texture coordinate arrays.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glShadeModel(GL_SMOOTH);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glGetError(); // Clear error codes
sprite = [Sprite createSpriteFromImage:#"TestSprite.png"];
[sprite retain];
[self createTextureBuffer];
Create the texture buffer.
- (void) createTextureBuffer
{
// generate texture
glGenTextures(1, &bufferTexture);
glBindTexture(GL_TEXTURE_2D, bufferTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // check if this is right
// generate FBO
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
// associate texture with FBO
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, bufferTexture, 0);
// clear texture bind
glBindTexture(GL_TEXTURE_2D,0);
// check if it worked (probably worth doing :) )
GLuint status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if (status != GL_FRAMEBUFFER_COMPLETE_OES)
{
printf("FBO didn't work...");
}
}
Run the render loop.
- (void)drawView
{
[self drawToTextureBuffer];
// Make sure that you are drawing to the current context
[EAGLContext setCurrentContext:context];
//Bind the GLView's buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, 320, 480);
//Clear the graphics context.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Push the matrix so we can keep it as it was previously.
glPushMatrix();
//Rotate to match landscape mode.
glRotatef(90.0, 0, 0, 1);
glTranslatef(0.0f, -320.0f, 0.0f);
//Store the coordinates/dimensions from the rectangle.
float x = 0.0f;
float y = 0.0f;
float w = 480.0f;
float h = 320.0f;
// Set up an array of values to use as the sprite vertices.
GLfloat vertices[] =
{
x, y,
x, y+h,
x+w, y+h,
x+w, y
};
// Set up an array of values for the texture coordinates.
GLfloat texcoords[] =
{
0, 0,
0, 1,
1, 1,
0, 1
};
//Render the vertices by pointing to the arrays.
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
// Set the texture parameters to use a linear filter when minifying.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//Enable 2D textures.
glEnable(GL_TEXTURE_2D);
//Bind this texture.
glBindTexture(GL_TEXTURE_2D, bufferTexture);
//Finally draw the arrays.
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
//Restore the model view matrix to prevent contamination.
glPopMatrix();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
NSLog(#"Error on draw. glError: 0x%04X", err);
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
On the first pass, the render loop will draw the image into the FBO.
- (void)drawToTextureBuffer
{
if (!bufferWasCreated)
{
// render to FBO
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
// set the viewport as the FBO isn't be the same dimension as the screen
glViewport(0, 0, 512, 512);
glPushMatrix();
//Store the coordinates/dimensions from the rectangle.
float x = 0.0f;
float y = 0.0f;
float w = 320.0f;
float h = 480.0f;
// Set up an array of values to use as the sprite vertices.
GLfloat vertices[] =
{
x, y,
x, y+h,
x+w, y+h,
x+w, y
};
// Set up an array of values for the texture coordinates.
GLfloat texcoords[] =
{
0, 0,
0, 1,
1, 1,
1, 0
};
//Render the vertices by pointing to the arrays.
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
// Set the texture parameters to use a linear filter when minifying.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//Allow transparency and blending.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Enable 2D textures.
glEnable(GL_TEXTURE_2D);
//Bind this texture.
glBindTexture(GL_TEXTURE_2D, sprite.texture);
//Finally draw the arrays.
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
//Restore the model view matrix to prevent contamination.
glPopMatrix();
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
NSLog(#"Error on draw. glError: 0x%04X", err);
}
//Unbind this buffer.
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
bufferWasCreated = YES;
}
}
Your texcoords in - (void)drawView seem to be wrong
GLfloat texcoords[] =
{
0, 0,
0, 1,
1, 1,
0, 1 << HERE should be 1, 0
};

glFrustumf displays only clear color, glOrthof displays as expected (OpenGL ES)

I'm new to OpenGL, so I'm sure this is a dummy mistake, but I've read every post, and reviewed sample code, and I can't find a difference, explaining why glFrustum wont display as I'd like it to.
I initialize OpenGL like:
- (void) initOpenGL{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrthof(0.0f, self.bounds.size.width, self.bounds.size.height, 0.0f, -10.0f, 10.0f);
const GLfloat zNear = -0.1, zFar = 1000.0, fieldOfView = 60.0;
GLfloat size;
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
// This give us the size of the iPhone display
CGRect rect = self.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();
#if 0
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
#endif
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
#if TARGET_IPHONE_SIMULATOR
glColor4f(0.0, 0.0, 0.0, 0.0f);
#else
glColor4f(0.0, 0.0, 0.0, 0.0f);
#endif
[[Texture2D alloc] initWithImage:[UIImage imageNamed:#"GreenLineTex.png"] filter:GL_LINEAR];
glInitialised = YES;
And my drawing is done like:
- (void)drawView {
if(!glInitialised) {
[self initOpenGL];
}
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
static const GLfloat texCoords[] = {
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
1.0, 1.0
};
// draw the edges
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glBindTexture(GL_TEXTURE_2D, 1);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
for (int i = 0; i < connectionNumber; i++){
Vertex2DSet(&vertices[0], connectionLines[i].lineVertexBeginPoint.x, connectionLines[i].lineVertexBeginPoint.y);
Vertex2DSet(&vertices[1], connectionLines[i].lineVertexBeginPoint.x+connectionLines[i].normalVector.x, connectionLines[i].lineVertexBeginPoint.y+connectionLines[i].normalVector.y);
Vertex2DSet(&vertices[2], connectionLines[i].lineVertexEndPoint.x, connectionLines[i].lineVertexEndPoint.y);
Vertex2DSet(&vertices[3], connectionLines[i].lineVertexEndPoint.x+connectionLines[i].normalVector.x, connectionLines[i].lineVertexEndPoint.y+connectionLines[i].normalVector.y);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Where the block in the for loop is a set of vertices that make up some triangle strips.
If I uncomment the glOrthof() line, then I can see my display, however it's orthographic, and I'd like to move the camera in and out, to change the scaling of the whole scene.
What have I done incorrectly that causes glFrustumf() to display only the clear color?
Short answer: you are looking in the wrong direction.
Long answer:
Your frustum is symmetric while your orthographic matrix isn't. So if your model is set up to be visible in the glOrtho case, it may not be visible with your glFrustum.
Also you shouldn't use glOrtho AND glFrustum together, because the matrices are multiplied and will surely yield a funny projection matrix.
You can use Nate Robins' GL tutors at http://www.xmission.com/~nate/tutors.html to experiment with glFrustum and glOrtho (in the "projection" application).