简体   繁体   English

SDL2 + OpenGL彩色几何

[英]SDL2 + OpenGL colored geometry

I want to use OpenGL to draw on top of a webcam stream. 我想使用OpenGL在网络摄像头流上绘制。 I'm using an SDL_Surface named screen_surface_ containing webcam data, that I'm rendering to the screen using (1): 我正在使用一个名为screen_surface_SDL_Surface screen_surface_包含网络摄像头数据,并使用(1)渲染到屏幕上:

SDL_UpdateTexture(screen_texture_, NULL, screen_surface_->pixels, screen_surface_->pitch);
SDL_RenderClear(renderer_);
SDL_RenderCopy(renderer_, screen_texture_, NULL, NULL);

Then I try to draw some geometry on top: 然后,我尝试在顶部绘制一些几何图形:

glLoadIdentity();
glColor3f(1.0, 0.0, 1.0);
glBegin( GL_QUADS );
  glVertex3f( 10.0f, 50.0f, 0.0f ); /* Top Left */
  glVertex3f( 50.0f, 50.0f, 0.0f ); /* Top Right */
  glVertex3f( 50.0f, 10.0f, 0.0f ); /* Bottom Right */
  glVertex3f( 10.0f, 10.0f, 0.0f ); /* Bottom Left */
glEnd( );
glColor3f(1.0, 1.0, 1.0); //<- I need this to make sure the webcam stream isn't pink?

SDL_RenderPresent(renderer_);

I have initialized OpenGL using (excerpt): 我已经使用(摘录) 初始化了 OpenGL:

glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, res_width_, res_height_ );
glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, res_width_, res_height_, 0.0f, -1.0f, 1.0f);

Subquestion : If I don't reset the glColor to white the whole webcam stream is colored pink. 子问题 :如果我不将glColor重置为白色,则整个网络摄像头流将变为粉红色。 I find this odd, because I thought that SDl_RenderCopy had already rendered that texture before the first call to glColor . 我觉得这很奇怪,因为我认为SDl_RenderCopy 第一次调用glColor 之前已经渲染了该纹理。 So how does SDL_RenderCopy actually work? 那么SDL_RenderCopy实际如何工作?

Main question : I get a neat 40x40 square in the top left of the screen on top of my webcam feed (good!). 主要问题 :在网络摄像头供稿顶部,屏幕左上方有一个整齐的40x40正方形(很好!)。 However, in stead of pink, it is a kind of flickering dark purple color; 但是,它不是粉红色,而是闪烁的深紫色。 seemingly dependent on the camera feed in the background. 似乎取决于背景中的摄像机供稿。 Could you please tell me what I'm overlooking? 你能告诉我我忽略了什么吗?

Edit: 编辑:

As per @rodrigo's comment, these are some images with the color set to R, G, B and white, respectively: 根据@rodrigo的评论,以下是一些图像,其颜色分别设置为R,G,B和白色:

Red Square Green Square Blue Square White Square 红色正方形 绿色正方形 蓝色正方形 白色正方形

Looking at these, it seems that the underlying texture has some effect on the color. 综观这些,似乎基础纹理对颜色有一定影响。 Could it be that OpenGL is still applying (some part of) the texture to the quad? 可能是OpenGL仍将纹理(的一部分)应用于四边形吗?

Edit: 编辑:

I suspect now that the geometry is drawn using the render texture as a texture, even though I've called glDisable(GL_TEXTURE_2D) . 我现在怀疑,即使我将glDisable(GL_TEXTURE_2D)称为几何,也使用渲染纹理作为纹理来绘制。 Looking at the "White Square" screenshot (below), you can see that the white quad is the same color as the bottom-right pixel . 查看“白色正方形”屏幕截图(如下),您可以看到白色四边形与右下像素的颜色相同。 I guess that the quad has no texture coordinates, so only the bottom-right texel is used. 我猜四边形没有纹理坐标,因此仅使用右下角的纹理元素。 Knowing this, better question: how do I disable texturing? 知道了这个更好的问题: 如何禁用纹理化? .

白方块2

I have fixed the problem by simply adding 我已经解决了这个问题,只需添加

glcontext_ = SDL_GL_CreateContext(window_);

to the SDL_Init code. SDL_Init代码。 I think all my calls to openGL functions (like glDisable(GL_TEXTURE_2D) were applied to the wrong context. Explicitly creating a context from SDL sets the right context to be active, I'm guessing. 我想我对openGL函数的所有调用(例如glDisable(GL_TEXTURE_2D)都应用于错误的上下文。我猜想从SDL显式创建一个上下文glDisable(GL_TEXTURE_2D)正确的上下文设置为活动状态。

Something else to look out for : when using textured geometry after using SDL_RenderCopy ; 还有其他需要注意的地方 :在使用SDL_RenderCopy之后使用纹理几何体时; I had to make sure to reset 我必须确保重设

glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);

before calling glTexImage2d , because it uses 在调用glTexImage2d之前,因为它使用

GL_UNPACK_ROW_LENGTH if it is greater than 0, the width argument to the pixel routine otherwise GL_UNPACK_ROW_LENGTH,如果大于0,则返回像素例程的width参数

(from the OpenGL docs ) (来自OpenGL文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM