我需要为我的网格工具包编写一个简单的可视化工具。我正在使用的对象始终位于[-1,1]^3框(含)内,因此我需要确保该对象将完全由用户可见。我也想有一个可能性,像用户在对象周围“飞行”一样旋转对象周围的相机。如何正确设置OpenGL场景以便可视化单个对象
这就是我正在做的:
static void Reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float maxDistance = sqrt(2);
if (w <= h)
{
glOrtho(-maxDistance, maxDistance, -maxDistance * (GLfloat)h/(GLfloat)w,
maxDistance * (GLfloat)h/(GLfloat)w, -6.0, 6.0);
}
else
{
glOrtho(-maxDistance * (GLfloat)w/(GLfloat)h, maxDistance * (GLfloat)w/(GLfloat)h,
-maxDistance, maxDistance, -6.0, 6.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void PolarView(GLdouble distance, GLdouble twist, GLdouble elevation)
{
double centerx, centery, centerz;
double eyex, eyey, eyez;
eyex = distance * cos(-twist) * cos(elevation);
eyey = distance * sin(-twist) * cos(elevation);
eyez = distance * sin(elevation);
centerx = centery = centerz = 0;
gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, 0, 0, 1);
}
的重塑功能在初始设置和可视化控制的每一个调整大小后调用,PolarView函数调用的每个重绘与某些角度和距离大比3的平方根(真的很重要?)。该代码可以很好地处理像立方体或球体这样的凸对象,但是它在圆环对象上有一些问题(通过其他方面可以看到一些面孔),所以我相信这是关于深度测试的。我的设置有什么问题?截图:
Bad torus http://s5.tinypic.com/1039b1y.jpg
Bad torus filled http://s5.tinypic.com/2h54paf.jpg
我做了一些网络上搜索,发现了这样的问题在情况发生时,有什么问题我近及远平面参数。在我的情况下,这些值是什么? 我的绘画过程是这样的:
glEnable(GL_DEPTH_TEST);
glClearDepth(1);
glPolygonMode(GL_FRONT, GL_LINE); // Changing of GL_LINE to GL_FILL doesn't fixing my problem, it just changing the appearance of the model.
glClearColor(BackColor.R/255.0f, BackColor.G/255.0f, BackColor.B/255.0f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
PolarView(sqrt(3), _phi, _theta);
// .. only draws
我PIXELFORMATDESCRIPTOR:
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
32, // Depth buffer size
0,
0,
PFD_MAIN_PLANE,
0,
0,
0,
0
};
我已经找到一些方法来解决此:
- 交换价值为我近及远飞机
- 将glDepthFunc设置为GL_GREATER,并将g lClearDepth为0
好的,如果偶数个错误发生,我的代码将会正常工作。但是第一个错误在哪里?
您需要更详细地说明问题所在。这很难理解。甚至可能是一个屏幕截图? – shoosh 2009-02-22 14:54:03