如何给2D结构3D深度 [英] How to give a 2D structure 3D depth

查看:100
本文介绍了如何给2D结构3D深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我请求学习OpenGL,这是分子建模项目的一部分,目前,我正在尝试渲染7个螺旋,这些螺旋在空间上彼此靠近,并且将以某些方式彼此移动,倾斜和交互.我的问题是如何赋予2D场景3维深度,以使几何结构看起来像3维的真实螺旋?

I am begging to learn OpenGL as part of a molecular modeling project, and currently I am trying to render 7 helices that will be placed spatially close to each other and will move, tilt and interact with each other in certain ways. My question is how to give the 2D scene 3-Dimensional depth so that the geometric structures look like true helices in three dimensions?

我尝试过运气不太好的投影矩阵(gulPerspective,glFrustum),以及使用glDepthRange函数.

I have tried playing around with projection matrices (gulPerspective, glFrustum) without much luck, as well as using the glDepthRange function.

我包括用于渲染螺旋的代码,但为简单起见,我插入了用于渲染一个螺旋的代码(其他6个螺旋除了它们的转换矩阵和颜色函数参数外完全相同),以及从对象映射时进行重塑的代码坐标以裁剪坐标:

I include my code for rendering the helices, but for simplicity I insert the code for rendering one helix (the other 6 helices are exactly the same except for their translation matrix and the color function parameters) and for reshaping when mapping from object coordinates to clip coordinates:

任何帮助将不胜感激.

Any help would be much appreciated.

void init() {

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    // Black background
    glLineWidth(8.0);

}

/* CALLED TO DRAW THE HELICES */ 

void RenderHelix() {

    GLfloat x,y,z; 
    GLfloat c = 2.5f;   //helical pitch 
    GLfloat theta;      //constant angle between tangent and x-axis 
    GLfloat r = 4.5f;   //radius 
    //GLint i = 1;      //loop through code to render 7 helices 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* GREEN HELIX */ 

    glBegin(GL_LINE_STRIP); 
    glColor3f(0.0, 1.0, 0.0);           
    for(theta = 0; theta <= 360; ++theta) { 
        x = r*(cos(theta));
        y = r*(sin(theta));
        z = c*theta; 
        glVertex3f(x,y,z);
    }
    glEnd();
    glLoadIdentity(); 
    glScalef(1.0,1.0,12.0); 
    glTranslatef(50.0, 100.0, 0.0);  //Move Position 
    glRotatef(90.0, 0.0, 0.0, 0.0);

/* Other 6 helices .... */

glFlush(); 
 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, -100,310);
    else
        glOrtho(-100*aspectratio,100*aspectratio,-100,100,-100,310);

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();       
}

推荐答案

我编辑了您的代码,并在此添加评论:

I edited your code and put a comment here and there:

void init() {
    /* One time OpenGL initialization would be textures and
     * other static stuff. Nothing like that here */    
}

/* CALLED TO DRAW THE HELICES */ 

void RenderHelix() {

    GLfloat x,y,z; 
    float aspectratio;
    GLfloat c = 2.5f;   //helical pitch 
    GLfloat theta;      //constant angle between tangent and x-axis 
    GLfloat r = 4.5f;   //radius 
    //GLint i = 1;      //loop through code to render 7 helices 

    /* assuming w and h are globals or somehow else accessible */
    aspectratio = (float)w/(float)h;
    glViewport(0,0,w,h);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    // Black background
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat aspectratio = (GLfloat)w/(GLfloat)h;
    if(w<=h)
        glFrustum(-10.f,10.f, -10.f/aspectratio, 10.f/aspectratio, 1.0f, 10.f);
    else
        glFrustum(-10.f*aspectratio,10.f*aspectratio,-10.f,10.f, 1.0f,10.f);

/* enable depth testing, also make sure the right depth func is used */
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);


/* GREEN HELIX */ 
    glLineWidth(8.0);    

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();

    glTranslatef(0.f, 0.f, -4.5f);

    glPushMatrix();
    /* this helix will be drawn in the center, as it's not moved anywhere. */

    glBegin(GL_LINE_STRIP); 
    glColor3f(0.0, 1.0, 0.0);
    /* trigonometric functions take radians, not degrees */
    for(theta = 0; theta <= M_PI2; theta += M_PI2*0.01;) { 
        /* instead of calculating the helices a new each rendering
         * pass you should store them to a vertex array. */
        x = r*cosf(theta);
        y = r*sinf(theta);
        z = c*theta; 
        glVertex3f(x,y,z);
    }
    glEnd();

    glPopMatrix();

/* Other 6 helices .... */
    glScalef(1.0,1.0,12.0); 
    glTranslatef(50.0, 100.0, 0.0);  //Move Position of next helix, you may want to use glPushMatrix/glPopMatrix
    glRotatef(90.0, 0.0, 0.0, 0.0);  // a null rotation, what's that's supposed to do?


glFlush(); // redundant, glFlush is implied by SwapBuffers
 glutSwapBuffers();

}

void Reshape(GLint w, GLint h) {
    /* Pleast don't set OpenGL stuff in the window reshape handler.
     * This just causes confusion, as soon one uses multiple projections
     * in a single rendering */
}

这篇关于如何给2D结构3D深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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