开发者

SDL/Opengl Picking Question

Hi this might seem like a difficult question(or at least to me it is) but im using glUnproject and its not working. When i use the same function with OpenGL and Winapi it worked like a charm but now is not working when using OpenGl and SDL its suppossed to be the same but seems like its not so... yeah i really need help on this heres my function for picking

void Game::Picking(int x,int y,int Which)
{

    GLint viewport[4];
    GLfloat depth[2];
    GLubyte pixel[3];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY;
    GLdouble posX, posY, posZ,oldPosZ;
    oldPosZ = posZ;

    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth );//geting depth

    glReadPixels(x, int(winY),1,1,GL_RGB,GL_UNSIGNED_BYTE,(void *)pixel);//getting Pixels
    printf("Color1 %x Color2 %p Color3 %d ",pixel[0],pixel[1],pixel[2]);
    if(Which == 0)
    {
        if(pixel[0] == MP_SinglePlayer.x && pixel[1] == MP_SinglePlayer.y && pixel[2] == MP_SinglePlayer.z)
        {
        printf("Detected Click Single Player");
            b_MenuPrincipal = false;
            b_JuegoSolo = true;
        }
        else if(pixel[0] == MP_MultiPlayer.x && pixel[1] == MP_MultiPlayer.y && pixel[2] == MP_MultiPlayer.z)
        {
        printf("Detected Click MultiPlayer");
            b_MenuPrincipal = false;
            b_MenuSrvClt = true;
        }
        else if(pixel [0] == MP_Exit.x && pixel [1] == MP_Exit.y && pixel [2] == MP_Exit.z)
        {
        printf("Detected Click Salir");
            b_MenuPrincipal = false;
            b_Exit = true;
        }
    }
}

this is suppossed to return me the colors of where i picked but when i open the stderr it gives me this

mouseX 430 , mouseY 295

Color1 0 Color2 00000000 Color3 0 mouseX 430 , mouseY 295

Color1 0 Color2 00000000 Color3 0 mouseX 430 , mouseY 295

and im printing with x,p,d cuz i just wanted to see differences but none of it is working and here's my main cycle of the game(hope its not hard to understand)

 while(Exit == 0)
  {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity(); 

    glTranslatef(-PosicionCam[0].x,-PosicionCam[0].y,-PosicionCam[0].z);
    Uint32 inicio = SDL_GetTicks();
    float delta = ((float)inicio - (float)ultimo) * 0.001f;
    SDL_PumpEvents();


    keyboardstate = SDL_GetKeyState(NULL);

    if(keyboardstate [SDLK_ESCAPE]) salir = 1;


    if(b_MenuPrincipal){
        if(SDL_GetMouseState(&mousex, &mousey) & SDL_BUTTON(1))
        {
        printf("mouseX %d , mouseY %d \n",mousex,mousey);
        Picking(mousex,mousey,0);
        }
        menup->Move(PosicionCam[0].x,PosicionCam[0].y,PosicionCam[0].z);
        menup->Draw();
    }

SDL_GL_SwapBuffer开发者_运维知识库s();

if you've got any question about it please tell me i will try to answer right away hope you can help me tyvm


Firstly, picking should be done after drawing, perhaps at SDL your render buffer is cleared.

Second, most important, I didn't noticed code that uses glReadPixels(..., GL_DEPTH_COMPONENT, .., depth) nor gluUnProject. Is your question about color picking or unprojecting after all? It looks like a crazy mix of both. You read depth, ignore it, then read RGB value and treat it as XYZ coords..

You should be reading depth and unprojecting it with gluUnProject. Then IF you need - read color pixels with glReadPixels(..., GL_RGB, ...) and use it somehow.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜