User
User

Reputation: 19

Python and OpenGL: Texture Not Filling Polygon Correctly in Koch Snowflake Rendering

SnowflakeImage

Question:

I’m working on rendering a Koch snowflake with a sinus-plasma texture using PYTHON and OpenGL. The initial polygon is star-shaped, evolving into the Koch snowflake. My goal is to fill this polygon precisely with the texture while keeping its outline visible.

Problem: The texture doesn’t fill the polygon correctly. Some points seem to be skipped, causing incorrect filling and visible gaps. The expected behavior is a smooth texture filling the entire polygon shape without errors.

What I’ve Tried:

Using GL_POLYGON for filling the shape. Setting texture coordinates based on vertex positions. Visualizing polygon points with red boxes to check the correct order.

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)

Upvotes: 0

Views: 41

Answers (0)

Related Questions