大家好,
昨天我同样的问题。我希望上传的图片显示我的程序输出,但由于垃圾邮件保护,我被告知我需要10个信誉“积分”。我可以在不同的投影矩阵下将我的输出图像发送给任何愿意的人。
我开始学习OpenGL作为分子建模项目的一部分,目前我正在尝试渲染7个螺旋,这些螺旋将在空间上彼此靠近排列,并且会在一定程度上相互移动,倾斜,旋转和互动方法。
我的问题是如何让2D场景的立体深度,这样的几何结构看起来像真正的螺旋在三个维度?
我已经试过没有多少运气玩弄投影矩阵(gluPerspective,glFrustum),以及使用glDepthRange功能。正如我从教科书/网站的引用理解,渲染3D场景时,适合使用具有消失点(或者gluPerspective或glFrustum)一(透视)投影矩阵来在2D表面创建3个维度的假象(屏幕)
我包含了渲染螺旋的代码,但为了简单起见,我插入了渲染一个螺旋的代码(除了它们的平移矩阵和颜色函数参数外,其他6个螺旋完全相同)以及重塑处理程序。
这是输出![1]当我用正交投影(glOrtho)运行我的程序时,得到的结果是螺旋线的二维投影(三维绘制的曲线)。这是我的输出(![enter image description here] [2])当我使用透视投影(glFrustum在我的情况)。它看起来好像我正在3D看我的螺旋!
也许glFrustum参数是错误的?
//GLOBALS
GLfloat x, y, z;
GLfloat c = 1.5f; //helical pitch
GLfloat theta; //constant angle between tangent and x-axis
thetarad = theta/(Pi/180.0); //angle converted from degrees to radians
GLfloat r = 7.0f; //radius
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); /* enable depth testing */
glDepthFunc(GL_LESS); /* make sure the right depth function is used */
/*CALLED TO DRAW HELICES*/
void RenderHelix() {
/**** WHITE HELIX ****/
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(-30.f, 100.f, 0.f); //Move Position
glRotatef(90.0, 0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for(theta = 0; theta <= 360; ++theta) { /* Also can use: for(theta = 0; theta <= 2*Pi; ++rad) */
x = r*(cosf(theta));
y = r*(sinf(theta));
z = c*theta;
glVertex3f(x,y,z);
}
glEnd();
glScalef(1.0,1.0,12.0); //Stretch or contract the helix
glPopMatrix();
/* Code for Other 6 Helices */
.............
glutSwapBuffers();
}
void Reshape(GLint w, GLint h) {
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat aspectratio = (GLfloat)w/(GLfloat)h;
if(w<=h)
//glOrtho(-100,100,-100/aspectratio,100/aspectratio, -50.0,310.0);
//glOrtho(-100,100,-100/aspectratio,100/aspectratio, 0.0001,1000000.0); //CLIPPING FAILSAFE TEST
//gluPerspective(122.0,(GLfloat)w/(GLfloat)h,10.0,50.0);
glFrustum(-10.f,10.f, -100.f/aspectratio, 100.f/aspectratio, 1.0f, 15.0f);
else
//glOrtho(-100*aspectratio,100*aspectratio,-100,100,-50.0,310.0);
//glOrtho(-100*aspectratio,100*aspectratio,-100,100,0.0001,1000000.0); //CLIPPING FAILSAFE TEST
//gluPerspective(122.0,(GLfloat)w/(GLfloat)h,10.0,50.0);
glFrustum(-10.f*aspectratio,10.f*aspectratio,-10.f,10.f, 1.0f,15.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}