OpenGL没有渲染图

问题描述 投票:1回答:1

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

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

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

Output

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个回答
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点的映射。在正交投影中,眼睛空间中的坐标被线性映射。但是在Perspective Projection中,投影矩阵描述了从针孔相机到视口的2D点看世界中3D点的映射。

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

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

预习:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.