Jump to content
  • Advertisement
Sign in to follow this  
23yrold3yrold

OpenGL Dark Orb of Doom ....

This topic is 4517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've encountered some odd graphical behavior in my program maybe someone around here is familiar with (my OpenGL is extremely rusty). Basically I have a quad in the back acting as a scrolling starfield, a quadratic sphere in front of that that spins, and a masked image I'm blitting over the whole thing (code lifted from NeHe's Lesson 20). Everything seems to work fine but the masked blit; it was coming out more blended. I played with it for ages wondering what I was doing wrong and then it struck me; the quadratic! If I remove it, not only do the masks work, but the starfield shows up a lot brighter. If I display everything, it's all too dark (including the quadratic). If I removed the masked image, the quadratic displays fine and the starfield is too dark. If I remove only the quadratic, everything else is perfect. I'm at a loss as to how one function call ruins the lighting/blending/whatever. Here's the drawing code. It's just a simple hackjob to accomplish a simple visual effect, so ignore me if I've being terribly inefficent. :) See that one commented-out line? Everything shows up fine until I uncomment it. Then it still displays fine .... but way, way too dark. Anything else I should be checking for, maybe in my initialization or the way I make the textures or the lighting? Any ideas would be helpful ...
int DrawGLScene(GLvoid) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	glBindTexture(GL_TEXTURE_2D, texture[STARS]);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f + starscroll, 1.0f); glVertex3f(-3.2f, 2.4f, -50.0f);
		glTexCoord2f(1.0f + starscroll, 1.0f); glVertex3f( 3.2f, 2.4f, -50.0f);
		glTexCoord2f(1.0f + starscroll, 0.0f); glVertex3f( 3.2f,-2.4f, -50.0f);
		glTexCoord2f(0.0f + starscroll, 0.0f); glVertex3f(-3.2f,-2.4f, -50.0f);
	glEnd();
	
	glPushMatrix();
	
	glTranslatef(0.0f, 0.0f, -20.0f);

	glRotatef(-90.0f,1.0f,0.0f,0.0f);
	glRotatef(yrot,0.0f,0.0f,1.0f);

	glBindTexture(GL_TEXTURE_2D, texture[EARTH]);
	
	// gluSphere(quadratic,1.5f,32,32);

	glPopMatrix();

	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	
	glBlendFunc(GL_DST_COLOR,GL_ZERO);
	
	glBindTexture(GL_TEXTURE_2D, texture[MASK]);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.2f, -2.4f,  -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 3.2f, -2.4f,  -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 3.2f,  2.4f,  -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.2f,  2.4f,  -1.0f);
	glEnd();
	
	glBlendFunc(GL_ONE, GL_ONE);
	glBindTexture(GL_TEXTURE_2D, texture[OVERLAY]);	
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-3.2f, -2.4f,  -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 3.2f, -2.4f,  -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 3.2f,  2.4f,  -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-3.2f,  2.4f,  -1.0f);	
	glEnd();

	glEnable(GL_DEPTH_TEST);
	glDisable(GL_BLEND);

	starscroll+=0.0005;
	xrot+=xspeed;
	yrot+=yspeed;
	return TRUE;
}

Share this post


Link to post
Share on other sites
Advertisement
You aren't specifying normals for the quads you are rendering, so OpenGL uses whatever the last used normal is (or whatever your implementation sets as the default, I don't remember if OpenGL specifies a default normal or not - you can check in the OpenGL Spec to find out). Rendering the quadric will change the current normal, so it will affect the lighting calculations on the quads. Just specify a normal for each of those quads and that should fix it. It looks like (0,0,1) is the normal you want to be using for those quads.

EDIT: You can read more about lighting and the math behind it in the Lighting Chapter of the Red Book.

EDIT2: Okay, the default normal is specified to be (0,0,1) so that explains why the quads looked fine without the quadric. I'm not sure why even the quadric is rendered differently when you render everything though. It could be how you are blending your masked quad. And yeah, like Waffler says in the next post, if you don't want lighting to effect something (no lighting on the starfield makes sense [grin]) then you should disable lighting before you render that.

[Edited by - Kalidor on April 6, 2006 4:57:08 PM]

Share this post


Link to post
Share on other sites
For some of your 2D overlays, you may want to turn lighting off with glDisable(GL_LIGHTING). That way it is always bright and un-shaded. Of course, you may also want to make sure the color is currently glColor4f(1.0f, 1.0f, 1.0f, 1.0f), so it's white and fully opaque.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalidor
You aren't specifying normals for the quads you are rendering, so OpenGL uses whatever the last used normal is (or whatever your implementation sets as the default, I don't remember if OpenGL specifies a default normal or not - you can check in the OpenGL Spec to find out). Rendering the quadric will change the current normal, so it will affect the lighting calculations on the quads. Just specify a normal for each of those quads and that should fix it. It looks like (0,0,1) is the normal you want to be using for those quads.

EDIT: You can read more about lighting and the math behind it in the Lighting Chapter of the Red Book.

EDIT2: Okay, the default normal is specified to be (0,0,1) so that explains why the quads looked fine without the quadric. I'm not sure why even the quadric is rendered differently when you render everything though. It could be how you are blending your masked quad. And yeah, like Waffler says in the next post, if you don't want lighting to effect something (no lighting on the starfield makes sense [grin]) then you should disable lighting before you render that.


This looks like it's the correct explanation to me. I think when you call the function to draw the sphere, the normals are getting generated for it, and the last one stays there(OpenGL is a state machine), so everything that gets drawn afterwards uses whatever the last normal of the sphere, unless you specify the normal of the other objects. That would explain the darkness of your objects. Also, when you specify your quadric object, you are determining whether normals are generated inside or outside, so yes, they should be getting generated upon rendering of the sphere.

Share this post


Link to post
Share on other sites
Yeah, I had tried disabling lighting for everything but the quad and that did it. I had forgotten about normals. [embarrass] Although that still doesn't seem to explain why the quadratric is dark when I draw the masked texture, but it's working now so I'll blame solar flares and call it good. Thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!