pygame pyopengl构造一个多边变化的多边形

问题描述

因此,在这站点的帮助下,我最近在pygame / pyopengl中进行了二维水模拟。我现在要继续使用我制作的水纹理对水模拟进行纹理化。但是,当我尝试执行此操作时,无论我做什么,纹理似乎都无法正确显示。它是,但它们全部伸延且无序。我希望纹理平铺并且不被破坏,如何获得呢?

这是我的代码

import pygame
import random
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.glu import *

def loadTexture():
    textureSurface = pygame.image.load('test_image.png')
    textureData = pygame.image.tostring(textureSurface,"RGBA",1)
    width = textureSurface.get_width()
    height = textureSurface.get_height()

    glEnable(GL_TEXTURE_2D)
    texid = glGenTextures(1)

    glBindTexture(GL_TEXTURE_2D,texid)
    glTexImage2D(GL_TEXTURE_2D,GL_RGB,width,height,GL_RGBA,GL_UNSIGNED_BYTE,textureData)

    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)

    return texid


def draw_water(polygon_points):
    glBegin(GL_polyGON)
    for i in polygon_points:
        glTexCoord2f(*i)
        glVertex2f(*i)
    glEnd()

class surface_water_particle():
    
    def __init__(self,x,y):
        self.x_pos = x
        self.y_pos = y
        self.target_y = y
        self.veLocity = 0
        self.k = 0.04  
        self.d = 0.08
        self.time = 1

    def update(self):
        x =  self.y_pos - self.target_y
        a = -(self.k * x + self.d * self.veLocity)

        if self.y_pos > self.target_y:
            self.y_pos -= 0.1
        if self.y_pos < self.target_y:
            self.y_pos += 0.1
        self.veLocity = round(self.veLocity)

        self.y_pos += self.veLocity
        self.veLocity += a

        self.time += 1

class water_tile():
    def __init__(self,x_start,x_end,y_start,y_end,segment_length):
        self.springs = []
        self.x_start = x_start
        self.y_start = y_start
        self.x_end = x_end
        self.y_end = y_end - 10
        for i in range(abs(x_end - x_start) // segment_length):
            self.springs.append(surface_water_particle(i * segment_length + x_start,y_end))

    def update(self,spread):
        passes = 4  # more passes = more splash spreading
        for i in range(len(self.springs)):
            self.springs[i].update() 

        leftDeltas = [0] * len(self.springs)
        rightDeltas = [0] * len(self.springs)
        for p in range(passes):  
            for i in range(0,len(self.springs)):
                if i > 0:  
                    leftDeltas[i] = spread * (self.springs[i].y_pos - self.springs[i - 1].y_pos)
                    self.springs[i - 1].veLocity += leftDeltas[i]
                if i < len(self.springs):
                    rightDeltas[i] = spread * (self.springs[i].y_pos - self.springs[(i + 1)%len(self.springs)].y_pos)
                    self.springs[(i + 1)%len(self.springs)].veLocity += rightDeltas[i]

            for i in range(0,len(self.springs)):
                if round (leftDeltas[i],12) == 0 or round (rightDeltas[i],12) == 0:
                    self.springs[i - 1].y_pos = self.y_end+10
                if i > 0:
                    self.springs[i - 1].y_pos += leftDeltas[i]  
                if i < len(self.springs):
                    self.springs[(i + 1)%len(self.springs)].y_pos += rightDeltas[i]
                
    def splash(self,index,speed):
        if index >= 0 and index < len(self.springs):
            self.springs[index].veLocity = speed 

    def draw(self):
        water_surface = pygame.Surface((abs(self.x_end-self.x_start),abs(self.y_start - self.y_end)),depth=8).convert_alpha()
        water_surface.fill((0,100))
        water_surface.set_colorkey((0,0))
        polygon_points = []
        polygon_points.append((self.x_start,360-self.y_start))
        for spring in range(len(self.springs)):
            polygon_points.append((self.springs[spring].x_pos,360-self.springs[spring].y_pos))
        polygon_points.append((self.springs[len(self.springs) - 1].x_pos,360-self.y_start))

        return polygon_points
        

class water_object:
    def __init__(self,segment_length,x_pos,y_pos):
        self.water = water_tile(x_start,segment_length)

    def update(self):
        self.water.update(0.1)
        self.polypoints = self.water.draw()
        
water_list = [water_object(0,276+16,64,16,20)]

def main():
    pygame.init()
    display = (640,360)
    pygame.display.set_mode(display,DOUBLEBUF|OPENGL)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,display[0],display[1],-1,1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    loadTexture()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == MOUSEBUTTONDOWN:
                print (len(water.water.springs))
                water.water.splash(random.randint(0,len(water.water.springs) - 1),50)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
       
        for water in water_list:
            water.update()
            draw_water(water.polypoints)
        pygame.display.flip()
        pygame.time.wait(10)

if __name__ == "__main__":
    main()

黄色是我的质感

my texture

解决方法

如果要用彩色填充水线下方的区域,则建议生成GL_TRIANGLE_STRIP primitive

def draw_polygon(polygon_points):
    glBegin(GL_TRIANGLE_STRIP)
    for pt in polygon_points:
        glVertex2f(*pt)
        glVertex2f(pt[0],WINDOW_SIZE[1])
    glEnd()

查看示例:

import pygame,random
import math as m

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

pygame.init()

WINDOW_SIZE = (640,480)
 
screen = pygame.display.set_mode(WINDOW_SIZE,flags=DOUBLEBUF|OPENGL,depth=32,display=0) # initiate the window
clock = pygame.time.Clock()

def draw_polygon(polygon_points):
    glBegin(GL_TRIANGLE_STRIP)
    for pt in polygon_points:
        glVertex2f(*pt)
        glVertex2f(pt[0],WINDOW_SIZE[1])
    glEnd()
    
class surface_water_particle():
    
    def __init__(self,x,y):
        self.x_pos = x
        self.y_pos = y
        self.target_y = y
        self.velocity = 0
        self.k = 0.04  
        self.d = 0.08
        self.time = 1

    def update(self):
        x =  self.y_pos - self.target_y
        a = -(self.k * x + self.d * self.velocity)

        if self.y_pos > self.target_y:
            self.y_pos -= 0.1
        if self.y_pos < self.target_y:
            self.y_pos += 0.1
        self.velocity = round(self.velocity)

        self.y_pos += self.velocity
        self.velocity += a

        self.time += 1

class water():
    
    def __init__(self,x_start,x_end,y_start,y_end,segment_length):
        self.springs = []
        self.x_start = x_start
        self.y_start = y_start
        self.x_end = x_end
        self.y_end = y_end - 10
        for i in range(abs(x_end - x_start) // segment_length):
            self.springs.append(surface_water_particle(i * segment_length + x_start,y_end))

    def update(self,spread):
        passes = 4  # more passes = more splash spreading
        for i in range(len(self.springs)):
            self.springs[i].update() 

        leftDeltas = [0] * len(self.springs)
        rightDeltas = [0] * len(self.springs)
        for p in range(passes):  
            for i in range(0,len(self.springs) -1 ):
                if i > 0:  
                    leftDeltas[i] = spread * (self.springs[i].y_pos - self.springs[i - 1].y_pos)
                    self.springs[i - 1].velocity += leftDeltas[i]
                if i < len(self.springs) - 1:
                    rightDeltas[i] = spread * (self.springs[i].y_pos - self.springs[i + 1].y_pos)
                    self.springs[i + 1].velocity += rightDeltas[i]

            for i in range(0,len(self.springs) -1):
                if i > 0:
                    self.springs[i - 1].y_pos += leftDeltas[i]  # you were updating velocity here!
                if i < len(self.springs) - 1:
                    self.springs[i + 1].y_pos += rightDeltas[i]

               
                
    def splash(self,index,speed):
        if index > 0 and index < len(self.springs) :
            self.springs[index].velocity = speed
                
    def draw(self):
        water_surface = pygame.Surface((abs(self.x_start - self.x_end),abs(self.y_start - self.y_end))).convert_alpha()
        water_surface.fill((0,0))
        water_surface.set_colorkey((0,0))
        polygon_points = []
        polygon_points.append((self.x_start,self.y_start))
        for spring in range(len(self.springs)):
            polygon_points.append((self.springs[spring].x_pos,self.springs[spring].y_pos))
        polygon_points.append((self.springs[len(self.springs) - 1].x_pos,self.y_start))

        glColor3f(0,1)
        draw_polygon(polygon_points)
        
water_test = water(0,800,200,3)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,WINDOW_SIZE[0],WINDOW_SIZE[1],-1,1)

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

glClearColor(1,1,1)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        if event.type == MOUSEBUTTONDOWN:
            water_test.splash(50,100)
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    
    water_test.update(0.025)
    water_test.draw()

    pygame.display.flip()
    clock.tick(60)