开发者

Color coded picking problem in OpenGL

I am making a game, actually a very basic replica of Minecraft, for开发者_高级运维 a class project of mine. I'm stuck in the picking process right now, which would enable me to destroy and create blocks in the game environment.

I've been trying to use OpenGL's own picking mode without any success, and building my own ray picker using math libraries seems to large a work for a project of this size. So, I've decided to use the color coded picking method, which consists of rendering every pickable object in a different color, then getting the color at the mouse position and using it to identify the picked object.

My current interface is just a 3D rendering of many boxes stacked, creating a terrain-like structure. Since I've done no texture mapping yet, all the boxes are shades of grey (lighting enabled).

Now, time for some actual code:

This is the initialization part, enabling texturing, lighting etc.

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

When a mouse button is clicked, I try to get the color at the mouse cursor's position (always the middle of the window, actually) by:

glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);   
glDisable(GL_DITHER);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT1);
    renderColors();
    GLubyte pixels[3];
    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void *)pixels); 
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_DITHER);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

Problem is, the disables do not work and I always get the RGB values of different shades of grey in my pixels array.

What could be the problem?


Perhaps you forget to clear the color buffer and disable depth buffer and all your rendered colors are causing Z-Fighting or not rendered at all (if z-test is "less"). Try to add swapbuffers code and see what gets rendered after your ColorRender code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜