如何在多视口 OpenGL VC 中正确渲染纹理++应用?
已更新
如果您想要该应用程序的完整源代码,您可以从我的 github 存储库中派生它:OpenGLModeler
我构建了一个显示三个视口的 Visual C++.NET 2008 应用程序。一切都很好,直到我实现了纹理功能。正如您从我的屏幕截图中看到的,只有前视口渲染了其纹理。这是我根据定义的视口类型处理其绘图方法的代码。如果您愿意,我可以为您提供完整的源代码,但这里是代码片段:
public:
GLvoid ReSizeGLScene(GLsizei width, GLsizei height){ // Resize and initialise the gl window
MySetCurrentGLRC();
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
switch(cameraType){
case 1:
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
gluLookAt(
-4,4,4,//eye
0,0,0,//center
0,1,0);//up
break;
case 2://front : texture rendered correctly
glOrtho(-2,2,-2,2,-50,50);
gluLookAt(
0,0,2,//eye 0,0,2
0,0,0,//center 0,0,0
0,1,0);//up 0,1,0
break;
case 3://top
glOrtho(-2,2,-2,2,-50,50);
gluLookAt(
0,2,0,//eye
0,0,0,//center, 0
0,0,-1);//up
break;
}
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
UINT flags = SWP_NOZORDER | SWP_NOACTIVATE;
SetWindowPos((HWND)this->Handle.ToPointer() , 0, 0, 0,
width, height, flags);
}
编辑:解释修改后的代码 我从 codeproject 借用此代码,并将其修改为具有多视口功能。这些是我认为重要的修改:首先,OpenGL视口的实例化:
glPerspective = gcnew COpenGL(picPerspective, picPerspective->Width, picPerspective->Height,1);
glFront = gcnew COpenGL(picFront, picFront->Width, picFront->Height,2);
glTop = gcnew COpenGL(picTop, picTop->Width , picTop->Height,3);
COpenGL的最后一个参数是视口的类型:perspective(1)、front(2)和top(3)。第二个修改是我绘制场景的方式。它使用计时器,如下所示:
private: System::Void timerRefreshPicture_Tick(System::Object^ sender, System::EventArgs^ e) {
UNREFERENCED_PARAMETER(sender);
UNREFERENCED_PARAMETER(e);
glPerspective->Render(daftarObject);
glPerspective->SwapOpenGLBuffers();
glTop->Render(daftarObject);
glTop->SwapOpenGLBuffers();
glFront->Render(daftarObject);
glFront->SwapOpenGLBuffers();
}
我尝试禁用绘制 glFront 的代码,是的,glTop 的纹理正确。如果我禁用 glTop,也会发生同样的情况。我怀疑这段代码:
virtual System::Void MySetCurrentGLRC()
{
if(m_hDC)
{
if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
{
MessageBox::Show("wglMakeCurrent Failed");
}
//wglShareLists(m_hglrc,m_hglrc);<== not working
}
}
如何在此代码中使用 wlgShareList
?谢谢
UPDATED
If you want the full source code of this application in questioned, you can fork it from my github repository : OpenGLModeler
I've built a Visual C++.NET 2008 application that display three viewport. All was just fine, until I implemented the Texturing functionality. As you can see from my Screenshot here, only the front viewport got its texture rendered. This is my code for handling its drawing method, base on the type of viewport defined. I can provide you with the full source code if you want, but here is the snippet :
public:
GLvoid ReSizeGLScene(GLsizei width, GLsizei height){ // Resize and initialise the gl window
MySetCurrentGLRC();
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
switch(cameraType){
case 1:
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
gluLookAt(
-4,4,4,//eye
0,0,0,//center
0,1,0);//up
break;
case 2://front : texture rendered correctly
glOrtho(-2,2,-2,2,-50,50);
gluLookAt(
0,0,2,//eye 0,0,2
0,0,0,//center 0,0,0
0,1,0);//up 0,1,0
break;
case 3://top
glOrtho(-2,2,-2,2,-50,50);
gluLookAt(
0,2,0,//eye
0,0,0,//center, 0
0,0,-1);//up
break;
}
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
UINT flags = SWP_NOZORDER | SWP_NOACTIVATE;
SetWindowPos((HWND)this->Handle.ToPointer() , 0, 0, 0,
width, height, flags);
}
EDITED : explain modified code
I borrow this code from codeproject, and modify it to have multi viewport functionality. These are I think the important modification : first, the instantiation of OpenGL Viewport:
glPerspective = gcnew COpenGL(picPerspective, picPerspective->Width, picPerspective->Height,1);
glFront = gcnew COpenGL(picFront, picFront->Width, picFront->Height,2);
glTop = gcnew COpenGL(picTop, picTop->Width , picTop->Height,3);
The last argument of the COpenGL is the type of viewport : perspective(1), front(2) and top(3). And the second modification is the way I draw the scene. It using a timer, like this :
private: System::Void timerRefreshPicture_Tick(System::Object^ sender, System::EventArgs^ e) {
UNREFERENCED_PARAMETER(sender);
UNREFERENCED_PARAMETER(e);
glPerspective->Render(daftarObject);
glPerspective->SwapOpenGLBuffers();
glTop->Render(daftarObject);
glTop->SwapOpenGLBuffers();
glFront->Render(daftarObject);
glFront->SwapOpenGLBuffers();
}
I try to disable the code that draw the glFront, and yes, the glTop got its textured correctly. And the same happen if I disabled the glTop. I suspect this code :
virtual System::Void MySetCurrentGLRC()
{
if(m_hDC)
{
if((wglMakeCurrent(m_hDC, m_hglrc)) == NULL)
{
MessageBox::Show("wglMakeCurrent Failed");
}
//wglShareLists(m_hglrc,m_hglrc);<== not working
}
}
How can I use wlgShareList
in this code? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有多个 GL 上下文,则需要在上下文之间共享纹理,以使它们能够在一个上下文中创建并在另一个上下文中显示。
您可以查看 wglShareLists这是在创建“辅助”上下文之后调用的(“主”上下文是创建的第一个上下文)。
您可以在创建各种上下文后调用 wglShareLists :
请注意,调用 wglShareLists 时,“glFront”和“glTop”上下文不包含任何现有纹理,这一点很重要。
If you have multiple GL contexts, you need to share textures between the contexts to enable them being created in a context and displayed in another.
You may take a look at wglShareLists which is to be called after you create your 'secondary' contexts ('main' context being the first context created).
You can call wglShareLists after having created your various contexts :
Note that it's important that 'glFront' and 'glTop' contexts do not contain any existing texture when calling wglShareLists.
只是一个建议:不要将 OpenGL 状态设置拆分到多个函数上。始终在需要时设置您需要的 OpenGL 状态。因此,在您的情况下,不要在调整大小处理程序中设置视口和投影,而是在显示函数中进行设置。同样,以这种方式启用/禁用纹理。
Just a suggestion: Don't split up OpenGL state settings over multiple functions. Always set the OpenGL state you need right when you need it. So in your case instead of setting viewport and projection in a resize handler, do it in the display function. Similarily, enable/disable textures that way.