Sign in to follow this  
Renran

OpenGL Opengl 2D problem

Recommended Posts

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); }

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)


Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by Renran
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


Might be offtopic, but with math it is always correct too (if it draws tiles, even with offset etc) and is much quicker/efficient and requires less code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Renran
Do You have an example of this code?


So, I guess you have the X and Y coordinate of a certain pixel and want to know which tile that is? You know the width of the tiles, and their height. Also do you know the X and Y position of where the tile grid starts.

We take a window of 1000 by 800. Where there is a field of tiles at X = 200 and Y = 100. This field has W = 600 and H = 600 (the field is from point (200, 100) to (800, 700), leaving 200 pixels from field to border left and right, and 100 pixels above and beneath).

We take a pixel, where by mouse click or something else, at point (534, 333). Since this point is of the whole screen and starts at (0, 0) we need to substract 200 from X and 100 from Y to make the pixel counting start at (200, 100), the point where we draw tiles.

If the point has values larger then or equal to 0 and smaller then 600, we go further. We divide the X and Y values by the tile width and tile height respectively. Since we need a round number, we need to 'floor' that number. That will give us the Xth and Yth number of a tile.

Assume tiles are 48 * 48.

For the X value we do this: floor((534 - 200) / 48) = the 6th tile horizontally (counting left to right)
For Y: floor((333 - 100) / 48) = the 4th tile vertically (counting up to down)

Code I use:
int x = 534;
int y = 222;
if (x > 199 && x < 800 && y > 99 && y < 700)
{
int tile_x = (int) floor((x - 200) / 48);
int tile_y = (int) floor((y - 100) / 48);
}


Hope this is what you are looking for.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this