OpenGL 不渲染图形
OpenGL not rendering figure
下面的代码预计会画一个雪人,然后使用键盘按键控制相机。但它只是画了一些点。
我试图增加球体的半径,但它仍然无法正常工作。代码中有什么问题?如果是关于尺寸,那么正确的尺寸应该是多少?
输出应该看起来像这个图像 -
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();
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 点的映射。在正交投影中,眼睛中的坐标 space 是线性映射的。但是在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口的 2D 点的映射。
这意味着您必须删除 glOrtho(-300.0, 300, -300, 300, 0.0, 10.0)
。请改用 gluPerspective
。例如:
gluPerspective(45.0, width/height, 0.5, 10.0)
预览:
下面的代码预计会画一个雪人,然后使用键盘按键控制相机。但它只是画了一些点。
我试图增加球体的半径,但它仍然无法正常工作。代码中有什么问题?如果是关于尺寸,那么正确的尺寸应该是多少?
输出应该看起来像这个图像 -
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();
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 点的映射。在正交投影中,眼睛中的坐标 space 是线性映射的。但是在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口的 2D 点的映射。
这意味着您必须删除 glOrtho(-300.0, 300, -300, 300, 0.0, 10.0)
。请改用 gluPerspective
。例如:
gluPerspective(45.0, width/height, 0.5, 10.0)
预览: