SDL OpenGL Rendering Issue

╄→гoц情女王★ 提交于 2019-12-11 02:13:51

问题


I am just learning how to use SDL and openGL. I've been through the tutorials over at SDLTutorials.com. I am now trying to take things further and create a loading screen for an application; an oop menu system.

I am starting off real simple to test things out. I have the main backdrop of the window done via a texture class. The two relevant functions are defined here:

void Texture::Bind() {
   glBindTexture(GL_TEXTURE_2D, TextureID);
}

void Texture::RenderQuad(int X, int Y, int Width, int Height) {
Bind();

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
        glTexCoord2f(1, 0); glVertex3f(X + Width, Y, 0);
        glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
        glTexCoord2f(0, 1); glVertex3f(X, Y + Height, 0);
    glEnd();
}

I also have created a menu class that will basically be the parent of any menu created in the system and classes to be developed would be like a click button or an edit box. So far, the only relevent function in class Menu is:

void Menu::OnRender() {
    if( Visible ) {
        if(MenuTexture == NULL) {
            glColor4f(1.,G,B,A);
            glBegin(GL_QUADS);
                glVertex2d(X, Y);
                glVertex2d(X+Width,Y);
                glVertex2d(X+Width,Y+Height);
                glVertex2d(X,Y+Height);
            glEnd();
        }else{
            MenuTexture->RenderQuad(X,Y,Width,Height);
        }
    }
}

Finally, my application has created one texture that it binds to the whole window. i.e. the backround.

void App::OnRender() {


    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();


    MainScreen.RenderQuad(0,0,winWidth,winHeight); //Texture Object with bkg.jpg
    MainMenu.OnRender();  //Menu object to be 150x300 and all black


    SDL_GL_SwapBuffers();
}

I then create a menu item and try to draw it over the backdrop. If I leave the initialized colors black, the entire screen turns black almost instantly: I have one frame probably the first cycle through where the menu is drawn, the black square is placed on the window, and then it turns black.. If I have 1.0 for the Red, then the entire picture has like some sort of red filter placed on it and the menu box draws an opaque red square.

I've been through several examples of how rendering but I must be missing something. Essentially, if we put it all in line of whats being done, I bind the texture to a surface, define the coordinates of the surface and texture binding. Then I render a quad. I've put the color call in my menu inside the glBegin(), defining the color at all 4 vertices.

Thanks to my chosen answer: This solved the issue. However, it didn't render the Menu object with the correct color until I returned it to modulate. Not sure, I'll be playing around with it and reading up on this to learn about it. Thank you!

[SOVLED] Changes only made to App::OnRender():

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
MainScreen.RenderQuad(0,0,winWidth,winHeight);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
MainMenu.OnRender();

回答1:


GL_TEXTURE_ENV_MODE defaults to GL_MODULATE which multiplies incoming texel values by the current color.

If the current color is black (RGB(0,0,0)) then that will render all your textures black. Same for single-channel colors: RGB(1,0,0) * RGB(x,y,z) == RGB(x,0,0).

Try using GL_DECAL.



来源:https://stackoverflow.com/questions/17026162/sdl-opengl-rendering-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!