[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Color problems with pygame and pyopengl?



I was having some problems saving an image from a program that i was
writing, and made this test program to see what was going on.
On two different systems, this seems to show a colorwheel, but when i
save a screenshot (down arrow), the colors are not right (green channel
is copied over blue channel?).

#!/usr/bin/env python

from OpenGL.GL import *
from OpenGL.GLU import *
import pygame, pygame.image
from pygame.locals import *
import math
import traceback

def gl_resize((width, height)):
    if height==0:
        height=1.0
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(67.5, 1.0*width/height, 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

def gl_init():
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

class color_wheel_test:

    def render(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        glTranslatef(0.0, 0.0, -1)

        sc=0.5

        glScale(sc,sc,sc)

        glBegin(GL_TRIANGLE_FAN)
        glColor3f(0.5,0.5,0.5)
        glVertex3f(0,0,0)
        f = math.pi/3.0
        glColor3f(1,0,0)
        glVertex3f(1,0,0)
        
        glColor3f(1,1,0)
        glVertex3f(math.cos(1*f),math.sin(1*f),0)

        glColor3f(0,1,0)
        glVertex3f(math.cos(2*f),math.sin(2*f),0)

        glColor3f(0,1,1)
        glVertex3f(math.cos(3*f),math.sin(3*f),0)

        glColor3f(0,0,1)
        glVertex3f(math.cos(4*f),math.sin(4*f),0)
        
        glColor3f(1,0,1)
        glVertex3f(math.cos(5*f),math.sin(5*f),0)
        
        glColor3f(1,0,0)
        glVertex3f(1,0,0)
            
        glEnd()
        
        if self.tagme == 1:
            pygame.image.save(self.screen,"temp.bmp")

        pygame.display.flip()        


    def main(self):
        gamequit = 0

        video_flags = OPENGL|DOUBLEBUF#|FULLSCREEN
        pygame.init()
        try:
    #        res = (1024,768)
    #        res = (800,600)
    #        res = (640,480)
    #        res = (512,384)
            res = (320,240)
    #        res = (160,120)

    #        res = (320,480)
            self.screen = pygame.display.set_mode(res, video_flags)

            gl_init()
            gl_resize(res)

            self.tagme = 0

            while gamequit == 0:
                self.tagme = 0
                remainingEvents = pygame.event.get()
                for event in remainingEvents:
                    if event.type == QUIT:
                        gamequit = 1
                    if event.type == KEYDOWN:
                        if event.key == K_ESCAPE:
                            gamequit = 1
                        if event.key == K_DOWN:
                            self.tagme = 1

                self.render()

        finally:
            pygame.quit()

The_Test = color_wheel_test()
The_Test.main()