SDL/OpenGL 选择问题

发布于 2024-11-17 09:46:09 字数 2779 浏览 5 评论 0原文

嗨,这似乎是一个困难的问题(或者至少对我来说是这样),但我使用 glUnproject 并且它不起作用。 当我在 OpenGL 和 Winapi 中使用相同的函数时,它的工作方式就像一个魅力,但现在在使用 OpenGl 和 SDL 时不起作用,它应该是相同的,但似乎并非如此......是的,我真的需要帮助,这是我的函数对于选择

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

,应该返回我选择的颜色,但是当我打开stderr时,它给了我这个

mouseX 430 , mouseY 295

Color1 0 Color2 00000000 Color3 0 mouseX 430 , mouseY 295

Color1 0 Color2 00000000 Color3 0 mouseX 430 , mouseY 295

和 im 用 x,p,d 打印,因为我只是想看到差异,但它们都不起作用,这是我的游戏主要周期(希望它是不难理解)

 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_SwapBuffers();

如果您有任何疑问,请告诉我,我会立即回答希望您能帮助我 tyvm

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_SwapBuffers();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-11-24 09:46:09

首先,拾取应该在绘制之后进行,也许在SDL中你的渲染缓冲区被清除了。

其次,最重要的是,我没有注意到使用 glReadPixels(..., GL_DEPTH_COMPONENT, .., height) 或 gluUnProject 的代码。您的问题到底是关于颜色选择还是非投影?它看起来像是两者的疯狂混合。您读取深度,忽略它,然后读取 RGB 值并将其视为 XYZ 坐标。

您应该读取深度并使用 gluUnProject 取消投影它。然后,如果您需要 - 使用 glReadPixels(..., GL_RGB, ...) 读取颜色像素并以某种方式使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文