OpenGL: always same color

喜你入骨 提交于 2019-12-10 18:51:17

问题


I'm writting a program on windows using c++, opengl 2.1 and SDL and am having some issues with the vertex colors.

I'm using glColor3f to set the color for each vertex set, but it seems to not be working. I get every vertex drawn red no matter what color I pick. I checked the values being passed to glColor3f and they are indeed not 1.f,0.f,0.f...

Has anyone ever encountered such a problem, or knows what might be causing it? Am I maybe not including some lib I should? Or do you reckon it might be some issue with the SDL initialization code (it shouldn't be, as I've used it before correctly)?

EDIT4: Solved.. it was indeed a gpu issue, I got the drivers from the manufacturer and it is now working properly... go figure

EDIT: I'm also not using any lighting, textures or anything of the sorts. Graphically I just set up the window and tell opengl some vertices and colors to draw..

EDIT2: Sure, but I highly doubt there's any issue there:

int GLmain::redraw(GLvoid)                              
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
for(int i=0; i<s->divs.size(); i++){
    vec3 v = s->divs.at(i).getPosition();
    vec3 c = s->divs.at(i).getColor();
    glColor3f(c.get(0),c.get(1),c.get(2));
    glVertex3f(v.get(0),v.get(1),v.get(2));
}
glEnd();

return TRUE;
}

As you can see, pretty standard stuff.. c holds values between 0.0-1.0. I just tried running some other work I had done with OpenGL and everything is showing up red as well (it wasn't before), so I'm guessing it has something to do with the libs I'm using:

opengl32.lib sdl.lib sdlmain.lib glu32.lib

SDL is version 1.2.14. Also, could it be a problem with my gpu? Everything else shows up with normal colors though.. web browser, videos, games, etc.

EDIT3: SDL initialization code:

int done = 0;
int w = 800;
int h = 600;
GLenum gl_error;
char* sdl_error;
SDL_Event event;
Init_OpenGL(argc, argv, w, h); // start window and OpenGL
SDL_WM_SetCaption( "MySlinky", "" ); /* Set the window manager title bar */

void Init_OpenGL(int argc, char* argv[], int w, int h)
{
    int rgb_size[3];
    int bpp = 0;
    Uint32 video_flags = SDL_OPENGL;
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
        fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
        exit( 1 );
    }
    /* See if we should detect the display depth */
    if ( bpp == 0 ) {
        if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) {
            bpp = 8;
        } else {
            bpp = 16;  /* More doesn't seem to work */
        }
    }

    /* Initialize the display */
    switch (bpp) {
        case 8:
            rgb_size[0] = 3;
            rgb_size[1] = 3;
            rgb_size[2] = 2;
            break;
        case 15:
        case 16:
            rgb_size[0] = 5;
            rgb_size[1] = 5;
            rgb_size[2] = 5;
            break;
        default:
            rgb_size[0] = 8;
            rgb_size[1] = 8;
            rgb_size[2] = 8;
            break;
    }

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] ); // Sets bits per channel
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); // 3 channels per pixel (R, G and B)
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
    if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) {
        fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
        SDL_Quit();
        exit(1);
    }
}

回答1:


There are two possible causes:

  1. Either you have enabled some render state that will eventually result in red vertices. E.g. textures, materials (!?), lighting and so on.

  2. The code "around" your OpenGL calls has bugs (e.g.: are you really sure, that c.get(1) does not return 0?).

  3. EDIT: Setup of the OpenGL rendering context failed or the context does not work as expected/intended! E.g. it does not have the expected properties, bit depths and so on.

To eliminate any doubt, please add the following line, and check its results: printf("c=(%f,%f,%f)",(float)c.get(0),(float)c.get(1),(float)c.get(2));




回答2:


Can you try this :

glShadeModel( GL_SMOOTH );

and tell if the problem is fixed?

Put the call in the Init_OpenGL function.




回答3:


Not sure if this applies to you, but all I needed was one call to glUseProgram(NULL);, to tell opengl to use the fixed function pipeline, and the problem was fixed.



来源:https://stackoverflow.com/questions/4755628/opengl-always-same-color

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