You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python与OpenGL科赫雪花渲染中纹理无法正确填充多边形的问题求助

Python与OpenGL科赫雪花渲染中纹理无法正确填充多边形的问题求助

SnowflakeImage

问题描述

我正在用Python结合OpenGL做一个带正弦等离子纹理的科赫雪花渲染效果,初始图形是星形,会逐步演变成标准的科赫雪花。我的核心目标是让纹理能精准填充整个多边形区域,同时保留雪花的轮廓线条清晰可见。

遇到的问题

目前纹理没办法正确填充多边形,能看到有些顶点区域好像被跳过了,导致填充出现错误和明显的空隙。我期望的效果是纹理能平滑覆盖整个多边形,没有任何填充错误或空隙。

我已经尝试过的方法

  • 使用GL_POLYGON模式来填充多边形区域
  • 根据顶点的屏幕位置计算并设置对应的纹理坐标
  • 用红色的点标记出所有多边形顶点,用来检查顶点的顺序是否正确

相关代码

import math
import random
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *


def render_snowflake_with_plasma(snowflake, plasma_texture):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, plasma_texture)

    # paint texturized area
    glColor3f(1.0, 1.0, 1.0)
    glBegin(GL_POLYGON)
    for point in snowflake:
        u = point[0] / 800
        v = point[1] / 600
        glTexCoord2f(u, v)
        glVertex2f(point[0] / 400 - 1, -(point[1] / 300 - 1))
    glEnd()

    # Draw outline of snowflake in constant color
    glLineWidth(3.0)
    glDisable(GL_TEXTURE_2D)
    glColor3f(0.0, 0.0, 1.0)  # constant blue for outline
    glBegin(GL_LINE_LOOP)
    for point in snowflake:
        glVertex2f(point[0] / 400 - 1, -(point[1] / 300 - 1))
    glEnd()

    # show polygon points separately
    for i, point in enumerate(snowflake):
        size = 6.0 + i * 5.0  # box size grows with index number
        glPointSize(size)
        glColor3f(1.0, 0.0, 0.0)  # red color for boxes
        glBegin(GL_POINTS)
        glVertex2f(point[0] / 400 - 1, -(point[1] / 300 - 1))
        glEnd()

    pygame.display.flip()



def main():
   pygame.init()
   display = (800, 600)
   pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
   gluOrtho2D(-1, 1, -1, 1)
   side_length = 260
   height = math.sqrt(3) / 2 * side_length
   base_triangle = [
       (400, 300 - (2 / 3) * height),
       (400 - side_length / 2, 300 + (1 / 3) * height),
       (400 + side_length / 2, 300 + (1 / 3) * height)
   ]
   plasma = create_sinus_plasma(800, 600, 0.2, 0.4, 1.5)
   plasma_texture = glGenTextures(1)
   glBindTexture(GL_TEXTURE_2D, plasma_texture)
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB,   GL_UNSIGNED_BYTE, plasma)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   clock = pygame.time.Clock()
   rotation_angle = 0
   while True:
       for event in pygame.event.get():
           if event.type == QUIT:
               pygame.quit()
               return
       iteration_depth = 1
       iteration_modes = [0, 0, 0, 0, 0]
       snowflake = koch_snowflake(base_triangle, iteration_depth, iteration_modes)
       print("Raw Snowflake Points:")
       for point in snowflake:
           print(point)
       rotated_snowflake = [rotate_point(p, (400, 300), rotation_angle) for p in snowflake]
       render_snowflake_with_plasma(rotated_snowflake, plasma_texture)
       rotation_angle = (rotation_angle + 1) % 360
       clock.tick(60)

备注:内容来源于stack exchange,提问作者User

火山引擎 最新活动