pyoepngl全解析-2

pyopengl全解析-2

  • 前言
  • 开始
      • 使用须知
      • 材质
          • 使用须知
          • 加载材质

前言

还是,番外篇链接附上。
跟新速度会稍慢一些,因为我有项目要做,请各位看官不要着急哈~

开始

使用须知

请在您的初始化代码里加入

glEnable(GL_DEPTH_TEST)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glMatrixMode(GL_PROJECTION)
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
glEnable(GL_MODELVIEW)

glBegin(mode)开始绘图,mode可以为:

  • GL_POINTS,很多个点
  • GL_LINES,很多条线段
  • GL_LINE_STRIP,连续线段
  • GL_LINE_LOOPGL_POLYGON,多边形 (自动封口)
  • GL_TRIANGLES,很多个三角形
  • GL_TRIANGLE_STRIP,一串连续三角形(见示意图)
  • GL_TRIANGLE_FAN,一串连续三角形(见示意图)
  • GL_QUADS,很多四边形(注意不一定是矩形)
  • GL_QUAD_STRIP,一串连续四边形示意图

glColor3f(r,g,b)设置颜色,均为0~1之间的整数
glVertex3f(x,y,z)绘制一个点(单独一个点或者一个顶点)
glEnd()结束绘制
glPolygonMode(face,mode)其中faceGL_FRONT(正面)或GL_BACK(背面,在2d绘图里没啥用), modeGL_LINE(只有线)或GL_FILL(不仅有线,还有填充)

画3d的体的时候需要一个面一个面的画

还有,材质待会会讲。


画一个立方体

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
 
def drawFunc():
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(1, 1, 1, 0)
    vertex = [[[0,0,1],[1,0,1],[1,1,1],[0,1,1]],[[0,0,0],[0,1,0],[1,1,0],[1,0,0]],[[0,1,0],[0,1,1],[1,1,1],[1,1,0]],[[0,0,0],[1,0,0],[1,0,1],[0,0,1]],[[1,0,0],[1,1,0],[1,1,1],[1,0,1]],[[0,0,0],[0,0,1],[0,1,1],[0,1,0]]]
    for i in range(len(vertex)):
        glColor3f(65 / 255,105 / 255,1)
        glBegin(GL_QUADS)
        glVertex3f(*vertex[i][0])
        glVertex3f(*vertex[i][1])
        glVertex3f(*vertex[i][2])
        glVertex3f(*vertex[i][3])
        glEnd()       
    glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"OpenGL")
glClearColor(1,1,1,1)
glEnable(GL_DEPTH_TEST)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glMatrixMode(GL_PROJECTION)
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
glEnable(GL_MODELVIEW)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

顶点集vertex请务必自己画一下看看
结果会是乱七八糟的,根本看不出是什么东西。pyoepngl全解析-2_第1张图片
现在,要放大招了!

材质

使用须知

加载材质之后,请务必添加如下代码到您的初始化代码里 ↓ \downarrow

glEnable(GL_TEXTURE_2D)
加载材质

请下载PIL后使用代码片

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image 
def drawFunc():
    glClear(GL_COLOR_BUFFER_BIT)
    glRotatef(1, 1, 1, 0)
    vertex = [[[0,0,1],[1,0,1],[1,1,1],[0,1,1]],[[0,0,0],[0,1,0],[1,1,0],[1,0,0]],[[0,1,0],[0,1,1],[1,1,1],[1,1,0]],[[0,0,0],[1,0,0],[1,0,1],[0,0,1]],[[1,0,0],[1,1,0],[1,1,1],[1,0,1]],[[0,0,0],[0,0,1],[0,1,1],[0,1,0]]]
    for i in range(len(vertex)):
        glBindTexture(GL_TEXTURE_2D,1)
        glBegin(GL_QUADS)
        glTexCoord2f(0.0, 0.0)
        glVertex3f(*vertex[i][0])
        glTexCoord2f(1.0, 0.0)
        glVertex3f(*vertex[i][1])
        glTexCoord2f(1.0, 1.0)
        glVertex3f(*vertex[i][2])
        glTexCoord2f(0.0, 1.0)
        glVertex3f(*vertex[i][3])
        glEnd()       
    glFlush()
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"OpenGL")
glClearColor(1,1,1,1)
glEnable(GL_DEPTH_TEST)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glMatrixMode(GL_PROJECTION)
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
glMatrixMode(GL_MODELVIEW)
glEnable(GL_TEXTURE_2D)
img = Image.open('a.jpg')
width, height = img.size
img = img.tobytes('raw','RGB',0,-1)
glGenTextures(2)
glBindTexture(GL_TEXTURE_2D, 1)
glTexImage2D(GL_TEXTURE_2D, 0, 4,width,height, 0, GL_RGB,GL_UNSIGNED_BYTE,img)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_DECAL)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

a.jpg:
a.jpg
结果:
pyoepngl全解析-2_第2张图片

>> 《pyopengl全解析-3》


###### 作者 hit-road

拜拜,下课!
回到顶部

你可能感兴趣的:(opengl,python,3d渲染)