2011-02-11 149 views
2

可能重复:
How to give a 2D structure 3D depth给予2D结构的3D深度

大家好,

昨天我同样的问题。我希望上传的图片显示我的程序输出,但由于垃圾邮件保护,我被告知我需要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();  

}

回答

0

我同意这是困难的,如果您不能发表您的图片(它可能不会是微不足道的,然后)。

如果您的代码是开源的,您可能会从蓝色方尖碑社区获得分子建模帮助(http://blueobelisk.shapado.com/是回答这些问题的SE类型网站)。

曾经有很多在我们的社会中使用GL的,但我不知道我知道一个好的代码,你可以砍得到做的最好的事情有所了解。领先的图形工具是Jmol(其中gfx大部分是手写的,非常好)以及使用Qt的Avogadro。

但如果你问的开源GL moelcular图形,你可能会得到帮助的例子。

当然,你可能会得到这里

1

互补的帮助通常的原因是简单的3D应用程序不“看3D”是因为你需要设置的照明系统。照明是您大脑深度线索的主要来源。

下面是关于增加照明到一个OpenGL程序一个很好的教程:

http://www.cse.msu.edu/~cse872/tutorial3.html

编辑:对于更多的上下文,这里是从经典的OpenGL红皮书的相关章节:

http://fly.cc.fer.hr/~unreal/theredbook/chapter06.html

通知的屏幕截图在顶部附近,示出了相同的球体呈现具有和不照明。