OpenGL:始终相同的颜色
我正在使用 c++
、opengl 2.1
和 SDL
在 Windows 上编写程序,并且在顶点颜色方面遇到一些问题。
我使用 glColor3f
来设置每个顶点集的颜色,但它似乎不起作用。无论我选择什么颜色,我都会将每个顶点绘制为红色。我检查了传递给 glColor3f
的值,它们确实不是 1.f,0.f,0.f...
有没有人遇到过这样的问题,或者知道可能是什么原因造成的?我可能没有包含一些我应该包含的库吗?或者您认为这可能是 SDL 初始化代码的问题(不应该是这样,因为我之前已经正确使用过它)?
编辑4:解决了..这确实是一个GPU问题,我从制造商那里得到了驱动程序,它现在工作正常......去图
编辑:我也没有使用任何照明,纹理或任何类型的东西。以图形方式,我只是设置窗口并告诉 opengl 绘制一些顶点和颜色。
编辑2:当然,但我非常怀疑那里有任何问题:
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;
}
如您所见,非常标准的东西。 c 的值在 0.0-1.0 之间。 我只是尝试运行我用 OpenGL 完成的其他一些工作,所有内容也都显示为红色(以前不是),所以我猜测它与我正在使用的库有关:
opengl32 .lib sdl库 sdlmain.lib glu32.lib
SDL 版本为 1.2.14。另外,会不会是我gpu的问题?其他一切都以正常颜色显示..网络浏览器、视频、游戏等。EDIT3
:SDL初始化代码:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两个可能的原因:
您启用了某些渲染状态,最终会导致红色顶点。例如纹理、材质(!?)、光照等等。
OpenGL 调用“周围”的代码存在错误(例如:您真的确定
c.get(1)
不返回 0 吗?)。编辑:OpenGL 渲染上下文的设置失败或上下文未按预期/预期工作!例如,它不具有预期的属性、位深度等。
为了消除任何疑问,请添加以下行,并检查其结果:
printf("c=(%f,%f,%f)",(float)c.get(0),(float)c .get(1),(float)c.get(2));
There are two possible causes:
Either you have enabled some render state that will eventually result in red vertices. E.g. textures, materials (!?), lighting and so on.
The code "around" your OpenGL calls has bugs (e.g.: are you really sure, that
c.get(1)
does not return 0?).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));
您可以尝试一下:
并判断问题是否已解决吗?
将调用放入
Init_OpenGL
函数中。Can you try this :
and tell if the problem is fixed?
Put the call in the
Init_OpenGL
function.不确定这是否适用于您,但我所需要的只是一次调用
glUseProgram(NULL);
,告诉 opengl 使用固定函数管道,问题就得到了解决。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.