Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 02-22-2010, 10:13 AM   #1 (permalink)
Johanovski
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Johanovski is on a distinguished road
Default Trouble with OpenGL and blending

Hi there!

I'm working with a 3D OpenGL application and I'm having a problem with blending functions. In my scene there are some solid objects that are drawn each frame. When the player touches the screen I want to show a kind of "flash" in the screen, and I've thought I can do this by showing a white square in front of everything and then reducing it's alpha until it's gone. The effect seems to work, but the problem is that when the withe layer is transparent no others objects are shown (so solid objects disappear since the white layer has completely disappeared, and then objects are drawn again. I'm quite new to OpenGL so I don't understand how exactly it works, can anyone take a look at my code and tell me what I'm missing?

Code:
// Initializing OpenGL
const GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0;
    GLfloat size;
	
	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glShadeModel (GL_FLAT);
	
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

    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);
	
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

(...)

// Main drawing loop
[EAGLContext setCurrentContext:context];    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW);
    
    glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

if (showFlash) {
		[self showFlash];
	}

(...) <-- Other solid alpha-less objects are drawn here

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

(...)

// showFlash function
const GLfloat vertexs[] = {
		
		// Define the layer
		-1.0, 1.5, -1.0,             // top left
		-1.0, -1.5, -1.0,           // bottom left
		1.0, -1.5, -1.0,            // bottom right
		1.0, 1.5, -1.0				// top right
	};
	
	glPushMatrix();
    {		
		glVertexPointer(3, GL_FLOAT, 0, vertexs);	
        glEnableClientState(GL_VERTEX_ARRAY);

		glColor4f(1.0f, 1.0f, 1.0f, flashAlpha);

        glBindTexture(GL_TEXTURE_2D, textures[6]); // <-- It's a white image!
		glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
		
		flashAlpha -= 0.05;
		if (flashAlpha <= 0) {
			showFlash = FALSE;
		}

		glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    }
    glPopMatrix();
Any clue about this...? Thanks in advance!
Johanovski is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,524
Threads: 94,042
Posts: 402,616
Top Poster: BrianSlick (7,978)
Welcome to our newest member, tomokogk47
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.