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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2010, 11:13 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
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
Old 02-22-2010, 11:59 AM   #2 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default

Solved! ^_^

Don't know why, but just drawing the white layer after the effects get it working (in fact it have sense... I think I've spent too many hours with OpenGL and I'm starting to feel unable to solve a 2+2 operation U_U')!
Johanovski is offline   Reply With Quote
Reply

Bookmarks

Tags
3dimensions, alpha, blend, blending, opengl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 259
20 members and 239 guests
2WeeksToGo, ADY, BrianSlick, dacapo, Dani77, Dattee, Duncan C, headkaze, IphoneSdk, jemicha, kapps11, mer10, mgon987, Punkjumper, sneaky, timle8n1
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,743
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:40 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0