大家好,
昨天我同樣的問題。我希望上傳的圖片顯示我的程序輸出,但由於垃圾郵件保護,我被告知我需要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();
}