Opengl 2D problem

Started by
12 comments, last by Renran 15 years, 12 months ago
Hello, I have a problem with opengl. If I put glEnable(GL_TEXTURE_2D) in the function here down below my whole opengl image gets a lot darker and I cannot think why this is? Thank You RenRan void InitOGL () { // glEnable(GL_TEXTURE_2D); glClearColor(0.0,0.0,0.0,0.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glViewport (0, 0, HEIGHT, WIDTH); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (70.0f, (GLfloat)HEIGHT / (GLfloat)WIDTH, 0.1f, 1000.0f); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glShadeModel (GL_FLAT); }
Advertisement
Are you sure the colour is set to white?

glColor4f(1.0, 1.0, 1.0, 1.0);

PS: this is 3D isn't it?
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Yes, 3D
Setting the colour to white doesn't help.
The screen just gets very dark?
But I can still see objects that are close to me?
Post your render loop. Your code snipit is just the setup code and you cant tell what state you have changed since that point.

Some debug tips:
Have you got any GL-Lighting in your scene. If so - disable it
Force a glColor3f (1,1,1) when your render each object
Are you using any textures - if so is the texel color close to black? Try swapping for a test image (that contains nice bright colors)


Giving You everything that is happening in opengl.
Not opengl matter is gone for saving space

Thanks

void InitOGL ()
{
// glEnable(GL_TEXTURE_2D); // Laat textuur toe
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);

glViewport (0, 0, HEIGHT, WIDTH); // Viewport

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (70.0f, (GLfloat)HEIGHT / (GLfloat)WIDTH, 0.1f, 1000.0f);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();


glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glShadeModel (GL_FLAT);
}

Renderloop:
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
gluLookAt(eye.x,eye.y,eye.z,
point.x,point.y,point.z,
orientation.x,orientation.y,orientation.z);

glTranslated(param.transrl,0.0,param.transud);
glRotated(charup,1.0,0.0,0.0); // up and down
glRotated(charleft,0.0,1.0,0.0); // left and right
glTranslated(-114.0, 0.0, -80.0); // let x,z in the middle of the terrain
Terrain (); // draw terrain

glSelectBuffer(BUFSIZE,selectBuf);
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(xl, viewport[3] - yl, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

glBegin (GL_TRIANGLE_FAN); // Use triangle strip
glVertex3f(x, RH, z); // begin hex V0
glVertex3f(XM, RH, ZM3);
glVertex3f(XM2, RH, z);
glVertex3f(XM2, RH, ZM);
glVertex3f(XM, RH, ZM2);
glVertex3f(x, RH, ZM);
glEnd ();

Switch(); // Switch between 2D and 3D
}


void Switch()
{
glColor4f(1.0f,1.0f,1.0f,1.0);
setOrtho(); // set 2D
glLoadIdentity();
glRasterPos2f (20.0, 270.0);
printString(pixel4);
resetPersp(); // switch to 3D
}

void setOrtho() // set 2D mode
{
// switch to projection mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();
// reset matrix
glLoadIdentity();
// set a 2D orthographic projection
glOrtho(0, 1024, 768, 0, -1.0, 1.0); //left, right, bottom, top, -1.0, 1.0);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -300, 0);
glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}

void resetPersp() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}



Well, your render loop does not require any texturing. Simple solution is not to enable GL_TEXTURE_2D.

anyways... what are you trying to achieve with these lines?

glSelectBuffer(BUFSIZE,selectBuf);
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(xl, viewport[3] - yl, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

This looks wrong ...

other things. Do you really want to clear the ALPHA channel to 1.0. Most people clear the ALPHA channel to 0.0

Your render loop includes a 2D overlay - comment this out for the moment and in your main render loop add this line before your glBegin()

glColor3f (1,0,0);

What do you see now! - should be bright red. If this is the case, then replace with:

glColor3f (1,1,1); // should now be white.
Thanks BionicBytes,

But the lines:

glSelectBuffer(BUFSIZE,selectBuf);
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(xl, viewport[3] - yl, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

is reading info of the backbuffer, were every object has his own color(ID) code
and so I can find every object (tiles in my case).
And this works fine.

My whole code works OK, so I had a hex tiled terrain and I wanted to texture
the tiles.
So, first I have to: // glEnable(GL_TEXTURE_2D);
I did and everything gets dark.
So, I did not add the texturing code yet, because I wanted to find out why
everything gets dark once I add the glEnable! That is not what it should do.

[Edited by - Renran on April 23, 2008 4:28:47 AM]
If you have tiles you can easily do the math of finding which tiles is clicked/selected. Thats what I do.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Simply calling glEnable(GL_TEXTURE_2D) does not magically make everything nicely textured. You need to load the texture images, bind them to gl and supply texture coordinates. If you don't do all this the results of enabling texturing is probably undefined.

Have a look at the following snippet from my BindTextureToGL() function

	glEnable(GL_TEXTURE_2D);	glGenTextures(1, &glID);					glBindTexture(GL_TEXTURE_2D, glID);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // blend the texture with the material color	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);	// Trilinear Filtered Minification	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);				// Linear Filtered Magnification	gluBuild2DMipmaps(GL_TEXTURE_2D, bytesPerPixel, width, height, GL_RGBA, GL_UNSIGNED_BYTE, imageData); 
Yes ElectricVodoo I know that but my problem is that whenever I only put
the statement // glEnable(GL_TEXTURE_2D);
in my code then my screen goes dark(er).

And Decrius, my code is 3D and I read colours in 1 frame (backbuffer)to find the
position of an object = no math involved + always correct

RenRan

This topic is closed to new replies.

Advertisement