给2D结构3D深度

时间:2011-02-11 00:58:29

标签: opengl

  

可能重复:
  How to give a 2D structure 3D depth

大家好,

昨天我发布了同样的问题。我想上传显示我的节目输出的图像,但由于垃圾邮件保护,我被告知我需要10个声望“积分”。我可以将不同投影矩阵下的输出图像发送给任何愿意的人。

我开始学习OpenGL作为分子建模项目的一部分,目前我正在尝试渲染7个螺旋,它们将在空间上彼此靠近排列,并以某些方式相互移动,倾斜,旋转和相互作用

我的问题是如何给出二维场景三维深度,使几何结构在三维空间看起来像真正的螺旋?

我尝试过使用投影矩阵(gluPerspective,glFrustum)而没有太多运气,以及使用glDepthRange函数。正如我从教科书/网站参考中所理解的,当渲染3D场景时,使用具有消失点(gluPerspective或glFrustum)的(透视)投影矩阵来在2D表面(屏幕上)创建3维的幻觉是合适的。 )

我包含了用于渲染螺旋的代码,但为了简单起见,我插入了用于渲染一个螺旋的代码(其他6个螺旋除了它们的平移矩阵和颜色函数参数之外完全相同)以及重塑处理程序。

这是输出![在此处输入图像描述] [1]当我使用正交投影(glOrtho)运行程序时,我看到它看起来像是螺旋的2D投影(三维绘制的曲线)。当我使用透视投影(在我的情况下为glFrustum)时,这是我的输出(![在此处输入图像描述] [2])。它看起来好像我在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();      

}

2 个答案:

答案 0 :(得分:1)

简单的3D应用程序不“看起来像3d”的常见原因是因为您需要设置照明系统。照明是大脑深度线索的主要来源。

这是一个关于为OpenGL程序添加光照的好教程:

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

编辑:有关更多背景信息,请参阅经典OpenGL红皮书中的相关章节:

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

请注意顶部附近的屏幕截图,显示相同的球体渲染,无论是否有光照。

答案 1 :(得分:0)

我同意如果你不能发布你的图像很困难(那可能不是那么简单)。

如果您的代码是开源的,您可能会从Blue Obelisk社区获得分子建模的帮助(http://blueobelisk.shapado.com/是一个用于回答这些问题的SE类型站点)。

在我们的社区中曾经使用了很多GL,但我不确定我是否知道一个好的代码,你可以通过它来了解最好的事情。领先的图形工具是Jmol(gfx主要是手写的,非常好的)和使用Qt的Avogadro。

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

当然,你可能会在这里得到补充帮助

相关问题