在 OpenGL 中在纹理顶部显示 2D 形状
我正在使用 OpenCV 捕获网络摄像头数据并将它们显示为 GL 窗口的纹理,这工作正常。
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);
但我还想在其上覆盖一些基本形状、Md2 模型,保留纹理在后台和同一窗口本身。
这是我的函数,它被传递到 glutDisplayFunc()
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Set Projection Matrix
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0);
// Setup the view // Switch to Model View Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
// start drawing the Background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glFlush();
glutSwapBuffers();
}
EDIT 新订单
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glBegin(GL_QUADS); //Begin quadrilateral coordinates
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
编辑: 新顺序和坐标:
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// Trapezoid to be displayed on top of texture
glVertex3f(0.2f, 0.3f, -0.6f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.4f, -0.5f, -0.5f);
glVertex3f(-0.4f, -0.5f, -0.5f);
glEnd(); //End quadrilateral coordinates
////
glEnable(GL_TEXTURE_2D); // start drawing the background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
执行上述操作并禁用 gluOrtho2D()
后的结果
I'm capturing webcam data using OpenCV and display them as the Texture for a GL Window, This works fine.
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);
But I want also to overlay some basic shapes, Md2 models on top of this, retaining the texture in the background and in the same window itself.
This is my function that is passed into glutDisplayFunc()
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
// Set Projection Matrix
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0);
// Setup the view // Switch to Model View Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
// start drawing the Background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glFlush();
glutSwapBuffers();
}
EDIT
new order
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
glBegin(GL_QUADS); //Begin quadrilateral coordinates
// Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
EDIT:
New order and coordinates:
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// Trapezoid to be displayed on top of texture
glVertex3f(0.2f, 0.3f, -0.6f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.4f, -0.5f, -0.5f);
glVertex3f(-0.4f, -0.5f, -0.5f);
glEnd(); //End quadrilateral coordinates
////
glEnable(GL_TEXTURE_2D); // start drawing the background texture
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();
The result after doing above and Disabling gluOrtho2D()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查文档中的
gluOrtho2D()
:您将梯形放置在Z = -5处,远在近剪裁平面后面:
尝试为您的Z坐标设置-0.5,或者至少在-1之间和 0。
编辑: 示例:
Check the docs for
gluOrtho2D()
:You're placing your trapezoid at Z = -5, way behind your near clipping plane:
Try -0.5 for your Z coordinates, or at least something between -1 and 0.
EDIT: Example:
如果您正在绘制不应该像梯形那样具有纹理的东西,请确保禁用纹理 (
glDisable(GL_TEXTURE_2D)
)。否则,最后设置的纹理坐标将应用于所有新顶点,在某个奇怪的位置对纹理进行采样。
Make sure to disable texturing (
glDisable(GL_TEXTURE_2D)
) if you're drawing things that shouldn't be textured like your trapezoid.Otherwise the last set texture coordinate will apply to all your new vertices, sampling your texture in some strange spot.