OpenGL with PyOpenGL Python and PyGame p.2 - Coloring Surfaces


text to screen

Coloring Surfaces as well as understand some of the basic OpenGL code


width="750" height="423" src="https://www.youtube.com/embed/D57J48UAQCs" frameborder="0" allowfullscreen="" style="box-sizing: inherit; position: absolute; top: 0px; left: 0px; width: 594.5px; height: 334.406px;">


In this PyOpenGL tutorial series for OpenGL with Python 3, we're going to discuss how you can color things, specifically surfaces. The way we color surfaces in OpenGL is by first notifying OpenGL that is what we're going to do. After that, we inform OpenGL where this "surface" to be colored is. In our case, each surface is between the connection of four vertices. Other than that, and one new OpenGL function, you're done!

So, first we need some colors to choose from. OpenGL wants you to specify colors in an RGB format, but OpenGL expects it to be between 0 and 1, where 1 is the "strongest."

For example, a nice solid green would be: (0,1,0), which translates to 0 red, 1 green, 0 blue, or no red, full green, no blue.

Now let's go ahead and define a tuple of color tuples:

colors = (
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (0,1,0),
    (1,1,1),
    (0,1,1),
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (1,0,0),
    (1,1,1),
    (0,1,1),
    )

We wont use all of these yet, and we'll modify these colors likely later.

Next, we need to add the new coloring code into our Cube() function.

The current Cube() function looks like:

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

Now, we want to add the following to this function:

    glBegin(GL_QUADS)
    for surface in surfaces:
        x = 0
        for vertex in surface:
            x+=1
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])
    glEnd()

So here you see the typical glBegin, only this time we have (GL_QUADS) as the constant. Then, for each surface (a collection of vertices) in the surfaces tuple, then for each vertex in that list of four vertices, we want to us glColor3fv, which is what will color the object we're creating here, then we use glVertex3fv, just like we had before!

The only other thing here is a crude counter, which adds 1 to the x value, which allows us to create a bit of a multi-colored cube.

Now our Cube() function looks like this:

def Cube():
    glBegin(GL_QUADS)
    for surface in surfaces:
        x = 0
        for vertex in surface:
            x+=1
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])
    glEnd()

    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

Awesome, that's it. Now you have:

text to screen

#!/usr/bin/python
# -*- coding:utf-8 -*-  
import pygame
from pygame.locals import *


from OpenGL.GL import *
from OpenGL.GLU import *


verticies = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )


edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )
surfaces = (
    
    (0,1,5,4),  #创建平面时平面的顺序不一定要相临,但是每个面的顶点一定要按顺时针或者逆时针写
    (5,4,6,7),
    (6,7,2,3),
    (2,7,5,1),
    (0,1,2,3),
    (0,3,6,4)
)
colors = (
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (1,1,0),
    (1,0,1),
    (1,1,1),
    (1,0,0),
    (0,1,0),
    (0,0,1),
    (1,0,0),
    (1,1,1),
    (0,1,1),
    )


def Cube():
    glBegin(GL_QUADS)
    x = 0
    for surface in surfaces:
        x+=1
        for vertex in surface:
            
            glColor3fv(colors[x])
            glVertex3fv(verticies[vertex])
    glEnd()
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()




def main():
    pygame.init()                              #pygame的一些初始化不用管
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)


    gluPerspective(45,(display[0]/display[1]), 0.5, 50.0)
    #参数1是我们看显示物体的远近
    #参数2是物体显示的长宽比,和窗口长宽比相同就行
    #参数3和4是z轴近和远的裁剪面的距离,但是还是不太明白要这干啥
    glTranslatef(0.0,0.0, -5) #Z轴就是我们眼睛到屏幕方向的轴,负是远,正是近,其实就是让物体相对与屏幕在XYZ各方向移动几个距离


    glRotatef(360, 1, 1, 1)   #360是让他转360度,它最终回到原来初始位置


    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:   #退出事件响应
                pygame.quit()
                quit()


        #glRotatef(0.05, 1, 1, 1)  
        #参数1是每次旋转的度数是多少, 
        #参数2是x, y and z的一个坐标,表示从(0,0,0)点到(x,y,z)这条线为轴进行旋转
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #用来删除就得画面,清空画布
        Cube()                                           #创建模型
        pygame.display.flip()                            #显示画面
        pygame.time.wait(10)                             #10ms刷新一次  




main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyOpenGLPygame是两个独立的库,但可以结合使用来创建3D游戏或图形应用程序。PyOpenGLPythonOpenGL绑定,它允许你使用OpenGL进行图形渲染和计算。而Pygame是一个用于开发2D游戏和多媒体应用程序的库,它提供了处理图像、声音、输入设备等功能。 结合使用PyOpenGLPygame,你可以利用PyOpenGL的强大功能来创建3D场景,然后使用Pygame来处理用户输入、显示图像和播放声音等。这样可以在Pygame的基础上添加3D效果,使游戏或应用程序更加丰富和引人注目。 要使用PyOpenGLPygame,首先需要安装它们。你可以使用以下命令来安装所需的库: ```shell pip install PyOpenGL Pygame ``` 安装完成后,你可以开始编写代码来创建3D场景。下面是一个简单的示例代码,演示了如何使用PyOpenGLPygame创建一个旋转的立方体: ```python import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * def draw_cube(): vertices = ( (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (1, -1, 1), (1, 1, 1), (-1, -1, 1), (-1, 1, 1) ) edges = ( (0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (0, 4), (1, 5), (2, 6), (3, 7) ) glBegin(GL_LINES) for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() def main(): pygame.init() display = (800, 600) pygame.display.set_mode(display, DOUBLEBUF | OPENGL) gluPerspective(45, (display[0] / display[1]), 0.1, 50.0) glTranslatef(0.0, 0.0, -5) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glRotatef(1, 3, 1, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) draw_cube() pygame.display.flip() pygame.time.wait(10) if __name__ == '__main__': main() ``` 这段代码创建了一个窗口,并在其中绘制了一个旋转的立方体。你可以使用鼠标拖动窗口来观察立方体的旋转效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值