【问题标题】:OpenGL not rendering figureOpenGL不渲染图形
【发布时间】:2018-08-10 17:13:24
【问题描述】:

下面的代码预计会画一个雪人,然后使用键盘按键控制相机。但它只是画了一些点。

我尝试增加球体半径,但仍然无法正常工作。代码中有什么问题?如果是关于维度,那么正确的维度应该是多少?

输出应该看起来像这个图像 -

from __future__ import division
import time
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys, math

# angle of rotation for the camera direction
angle=0.0;
# actual vector representing the camera's direction
lx=0.0
lz=-1.0;
# XZ position of the camera
x=0.0
z=5.0;

def drawSnowMan():    
    glColor3f(1.0, 1.0, 1.0);

    # Draw Body
    glTranslatef(0.0 ,0.75, 0.0);
    glutSolidSphere(0.75,20,20);

    # Draw Head
    glTranslatef(0.0, 1.0, 0.0);
    glutSolidSphere(0.25,20,20);

    # Draw Eyes
    glPushMatrix();
    glColor3f(0.0,0.0,0.0);
    glTranslatef(0.05, 0.10, 0.18);
    glutSolidSphere(0.05,10,10);
    glTranslatef(-0.1, 0.0, 0.0);
    glutSolidSphere(0.05,10,10);
    glPopMatrix();

    # Draw Nose
    glColor3f(1.0, 0.5 , 0.5);
    glutSolidCone(0.08,0.5,10,2);


def renderScene():    
    global angle, lx, lz, x, z

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()

    refresh2d(width, height)

    # Set the camera
    gluLookAt(  x, 1.0, z, x+lx, 1.0,  z+lz, 0.0, 1.0,  0.0);

    # Draw ground
    glColor3f(0.9, 0.9, 0.9);
    glBegin(GL_QUADS);
    glVertex3f(-100.0, 0.0, -100.0);
    glVertex3f(-100.0, 0.0,  100.0);
    glVertex3f( 100.0, 0.0,  100.0);
    glVertex3f( 100.0, 0.0, -100.0);
    glEnd();

    # Draw 36 SnowMen
    for i in range(-3,3):
        for j in range(-3, 3):
            glPushMatrix();
            glTranslatef(i*10.0,0,j * 10.0);
            drawSnowMan();
            glPopMatrix();

    glutSwapBuffers();


def processSpecialKeys(key, xx, yy):    
    global angle, lx, lz, x, z

    fraction = 0.1

    if(key == GLUT_KEY_LEFT):
        angle -= 0.01
        lx = math.sin(angle);
        lz = -math.cos(angle);

    elif(key == GLUT_KEY_RIGHT):
        angle += 0.01
        lx = math.sin(angle);
        lz = -math.cos(angle);

    elif(key == GLUT_KEY_UP):
        x += lx * fraction;
        z += lz * fraction;

    elif(key == GLUT_KEY_DOWN):
        x -= lx * fraction;
        z -= lz * fraction; 


def refresh2d(width, height):
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-300.0, 300, -300, 300, 0.0, 10.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


# init GLUT and create window
width = 600
height = 600

glutInit();
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowPosition(100,100);
glutInitWindowSize(width, height);
glutCreateWindow("Lighthouse3D - GLUT Tutorial");

# register callbacks
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutSpecialFunc(processSpecialKeys);

# OpenGL init
glEnable(GL_DEPTH_TEST);

# enter GLUT event processing cycle
glutMainLoop();

【问题讨论】:

标签: python opengl graphics glut pyopengl


【解决方案1】:

glOrtho 指定正交投影。从 -300.0 到 300.0 的视野对于您的几何体来说非常宽,因为您的所有几何体都在 [-1.0, 1.0] 中。

如果你切换到

glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 10.0)

然后你会看到一个雪人,完全适合你的视口。

我建议使用Perspective Projection。这意味着您必须设置透视投影矩阵。

注意,投影矩阵描述了从场景的 3D 点到视口的 2D 点的映射。在正交投影中,眼睛空间中的坐标是线性映射的。但是在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口的 2D 点的映射。

这意味着您必须删除glOrtho(-300.0, 300, -300, 300, 0.0, 10.0)。请改用gluPerspective。例如:

gluPerspective(45.0, width/height, 0.5, 10.0)

预览:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2020-04-05
相关资源
最近更新 更多