C ++ OpenGL窗口只跟踪背景 [英] C++ OpenGL window only tracks background

查看:157
本文介绍了C ++ OpenGL窗口只跟踪背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道为什么当在OpenGL中生成一个简单的Square时会发生这种情况。



我使用了以下源代码 :从理论到实验。

  ///////// ///////////////////////////////////////// 
//平方.cpp
//
//剥离绘制一个正方形的OpenGL程序。
//
// Sumanta Guha。
//////////////////////////////////////////////// //////

#include< iostream>

#ifdef __APPLE__
#include< GLUT / glut.h>
#else
#include< GL / glut.h>
#endif

using namespace std;

//绘图(显示)例程。
void drawScene(void)
{
//将屏幕清除为背景颜色。
glClear(GL_COLOR_BUFFER_BIT);

//设置前景(或绘图)颜色。
glColor3f(0.0,0.0,0.0);

//绘制具有指定顶点的多边形。
glBegin(GL_POLYGON);
glVertex3f(20.0,20.0,0.0);
glVertex3f(80.0,20.0,0.0);
glVertex3f(80.0,80.0,0.0);
glVertex3f(20.0,80.0,0.0);
glEnd();

//将创建的对象刷入屏幕,即强制渲染。
glFlush();
}

//初始化例程。
void setup(void)
{
//设置背景(或清除)颜色。
glClearColor(1.0,1.0,1.0,0.0);
}

// OpenGL窗口重塑例程。
void resize(int w,int h)
{
//将视口大小设置为整个OpenGL窗口。
glViewport(0,0,(GLsizei)w,(GLsizei)h);

//将矩阵模式设置为投影。
glMatrixMode(GL_PROJECTION);

//清除当前投影矩阵以标识。
glLoadIdentity();

//指定正投影(或垂直)投影,
//即定义查看框。
glOrtho(0.0,100.0,0.0,100.0,-1.0,1.0);

//将矩阵模式设置为modelview。
glMatrixMode(GL_MODELVIEW);

//清除当前模型视图矩阵以标识。
glLoadIdentity();
}

//键盘输入处理程序。
void keyInput(unsigned char key,int x,int y)
{
switch(key)
{
//按转义键退出。
case 27:
exit(0);
break;
默认值:
break;
}
}

//主例程:定义窗口属性,创建窗口,
//注册回调例程并开始处理。
int main(int argc,char ** argv)
{
//初始化GLUT。
glutInit(& argc,argv);

//将显示模式设置为单缓冲和RGB颜色。
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

//设置OpenGL窗口大小。
glutInitWindowSize(500,500);

//设置OpenGL窗口左上角的位置。
glutInitWindowPosition(100,100);

//创建带标题的OpenGL窗口。
glutCreateWindow(square.cpp);

//初始化。
setup();

//注册显示例程。
glutDisplayFunc(drawScene);

//注册重塑例程。
glutReshapeFunc(resize);

//注册键盘例程。
glutKeyboardFunc(keyInput);

//开始处理。
glutMainLoop();

return 0;
}

我一直在尝试最久的时间...



有关详细信息:



屏幕打开时仅显示背景,如果将其拖动,跟踪背景,这是将窗口移动到底部,然后再次将其移动到原始位置的结果。我测试了相同的源代码在linux机器,它的工作正常... :(



编辑:我试过使用glutSwapBuffers()和didn'



解决方案

在Windows Vista和更新的Windows操作系统上,有一个称为管理器(DWM),它有一个特殊的模式称为桌面合成,将窗口绘制到屏幕缓冲区,然后合成它们,它提供了新的视觉效果,如在Alt + Tab中的实时窗口预览



这种新体系结构的一个后果是,你不能像你一样绘制单个缓冲应用程序(在窗口模式下) Windows XP(或在Windows Vista +中禁用桌面组合)。简而言之,DWM使用渲染上下文的后台缓冲区的副本进行合成,您应该切换到双缓冲图形。



要在GLUT中使用双缓冲绘图,在调用中使用 GLUT_DOUBLE 而不是 GLUT_SINGLE code> glutInitDisplayMode(...)。此外,您需要用 glutSwapBuffers(...)替换对 glFlush(...)的调用。 / p>

Anyone knows why this happens when trying to generate a simple square in OpenGL?

I am using the following source code from the book Computer Graphics Through OpenGL: From Theory to Experiments.

////////////////////////////////////////////////////          
// square.cpp
//
// Stripped down OpenGL program that draws a square.
// 
// Sumanta Guha.
////////////////////////////////////////////////////

#include <iostream>

#ifdef __APPLE__
#  include <GLUT/glut.h>
#else
#  include <GL/glut.h>
#endif

using namespace std;

// Drawing (display) routine.
void drawScene(void)
{
   // Clear screen to background color.
   glClear(GL_COLOR_BUFFER_BIT);

   // Set foreground (or drawing) color.
   glColor3f(0.0, 0.0, 0.0);

   // Draw a polygon with specified vertices.
   glBegin(GL_POLYGON);
      glVertex3f(20.0, 20.0, 0.0);
      glVertex3f(80.0, 20.0, 0.0);
      glVertex3f(80.0, 80.0, 0.0);
      glVertex3f(20.0, 80.0, 0.0);
   glEnd();

   // Flush created objects to the screen, i.e., force rendering.
   glFlush(); 
}

// Initialization routine.
void setup(void) 
{
   // Set background (or clearing) color.
   glClearColor(1.0, 1.0, 1.0, 0.0); 
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
   // Set viewport size to be entire OpenGL window.
   glViewport(0, 0, (GLsizei)w, (GLsizei)h);

   // Set matrix mode to projection.
   glMatrixMode(GL_PROJECTION);

   // Clear current projection matrix to identity.
   glLoadIdentity();

   // Specify the orthographic (or perpendicular) projection, 
   // i.e., define the viewing box.
   glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);

   // Set matrix mode to modelview.
   glMatrixMode(GL_MODELVIEW);

   // Clear current modelview matrix to identity.
   glLoadIdentity();
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
   switch(key) 
   {
  // Press escape to exit.
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}

// Main routine: defines window properties, creates window,
// registers callback routines and begins processing.
int main(int argc, char **argv) 
{  
   // Initialize GLUT.
   glutInit(&argc, argv);

   // Set display mode as single-buffered and RGB color.
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 

   // Set OpenGL window size.
   glutInitWindowSize(500, 500);

   // Set position of OpenGL window upper-left corner.
   glutInitWindowPosition(100, 100); 

   // Create OpenGL window with title.
   glutCreateWindow("square.cpp");

   // Initialize.
   setup(); 

   // Register display routine.
   glutDisplayFunc(drawScene); 

   // Register reshape routine.
   glutReshapeFunc(resize);  

   // Register keyboard routine.
   glutKeyboardFunc(keyInput);

   // Begin processing.
   glutMainLoop(); 

   return 0;  
}

I've been trying for the longest time...

For more details here:

The screen opens displaying only the background, if you drag it along, it will then proceed to track the background, and this is the result of moving the window to the bottom and then moving it up to the original position again. I've tested the same source code on a linux machine, and it works fine... :(

EDIT: I have tried using glutSwapBuffers() and that didn't seem to work either.

解决方案

On Windows Vista and newer Windows operating systems, there is a component known as the Desktop Window Manager (DWM) which has a special mode called "Desktop Composition" that draws windows into offscreen buffers and then composites them. It does this to provide new visual effects such as live window previews in the Alt+Tab screen.

A consequence of this new architecture is that you cannot draw single buffered applications (in windowed mode anyway) the same way you could in Windows XP (or in Windows Vista+ with Desktop Composition disabled). In a nutshell, the DWM uses a copy of your render context's back buffer for composition. You should switch to double buffered drawing.

To use double buffered drawing in GLUT, you would use GLUT_DOUBLE instead of GLUT_SINGLE in your call to glutInitDisplayMode (...). Additionally, you need to replace your calls to glFlush (...) with glutSwapBuffers (...).

这篇关于C ++ OpenGL窗口只跟踪背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆