EN VI

Python - Changing the color of lines in OpenGL?

2024-03-16 23:00:09
How to Python - Changing the color of lines in OpenGL

I tried to create an I-beam and now I'm trying to change the lines to be different colors (for example: visible lines to green and those that are not supposed to be visible to pink), but is it possible to do this without changing this function too much?

def I_beam():
    glBegin(GL_LINES)
    #glColor3f(1,0,1)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

A full code if needed

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

m = 2

vertices = (# up
            (-0.4 * m, 0.2 * m, 0),  # A0
            (0.3 * m, 0.48 * m, 0),  # G1
            (0.1 * m, 0.2 * m, 0),   # M2
            (0.8 * m, 0.48 * m, 0),  # N3
            # down
            (-0.4 * m, -0.25 * m, 0),  # F4
            (0.3 * m, 0.05 * m, 0),   # Z5
            (0.1 * m, -0.25 * m, 0),   # K6
            (0.8 * m, 0.05 * m, 0),   # S7
            # front
            (-0.4 * m, 0.12 * m, 0),   # B8
            (-0.2 * m, 0.12 * m, 0),   # C9
            (-0.2 * m, -0.15 * m, 0),  # D10
            (-0.4 * m, -0.15 * m, 0),  # E11
            (0.1 * m, 0.12 * m, 0),    # L12
            (-0.1 * m, 0.12 * m, 0),   # H13
            (-0.1 * m, -0.15 * m, 0),  # I14
            (0.1 * m, -0.15 * m, 0),    # J15
            # back
            (0.3 * m, 0.4 * m, 0),  # O16
            (0.5 * m, 0.4 * m, 0),  # Q17
            (0.5 * m, 0.15 * m, 0),  # V18
            (0.3 * m, 0.15 * m, 0),  # W19
            (0.8 * m, 0.4 * m, 0),  # P20
            (0.6 * m, 0.4 * m, 0),  # R21
            (0.6 * m, 0.15 * m, 0),  # U22
            (0.8 * m, 0.15 * m, 0),  # T23
            )

edges = ((0, 1),
         (0, 2),
         (0, 8),
         (1, 3),
         (1, 16),
         (2, 3),
         (2, 12),
         (3, 20),
         (4, 5),
         (4, 6),
         (4, 11),
         (5, 7),
         (5, 19),
         (6, 7),
         (6, 15),
         (7, 23),
         (8, 16),
         (8, 9),
         (9, 17),
         (9, 10),
         (10, 18),
         (10, 11),
         (11, 19),
         (12, 20),
         (12, 13),
         (13, 21),
         (13, 14),
         (14, 22),
         (14, 15),
         (15, 23),
         (16, 17),
         (17, 18),
         (18, 19),
         (20, 21),
         (21, 22),
         (22, 23),)

def I_beam():
    glBegin(GL_LINES)
    glColor3f(1,0,1)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

def main():
    pygame.init()
    display = (900,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()

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        I_beam()
        pygame.display.flip()
  
main()

i tried to change by using glColor(), but it change color of all figure. I tried to search but didn't find yet

Solution:

In immediate mode OpenGL, glColor, glNormal, glTexCoord and so on are all per-vertex attributes. Their values are interpolated between vertices.

You can simply do this:

glBegin(GL_LINES)
for edge in edges:
    for vertex in edge:
        glColor3fv(colors[vertex])
        glVertex3fv(vertices[vertex])
glEnd()
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login