Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i'm doing a simple game/application with some minimal 2d graphics: png textures and ploygons with standard glVertex calls

my setting:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc( GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA );

code for textures:

texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,     GL_UNSIGNED_BYTE, textureData )
newList = glGenLists(1)
glNewList(newList,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, texture)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(0, 0)
glTexCoord2f(0, 1); glVertex2f(0, height)
glTexCoord2f(1, 1); glVertex2f( width,  height)
glTexCoord2f(1, 0); glVertex2f(width, 0)
glEnd()
glEndList()

...

glCallList(newList)

i've found this code here: http://disruption.ca/gutil/introduction.html

if i render any texture with glCallList i no more visualize plygons! what could it be? i'm stucking with some combinations of glBlendFunc but i'm getting nothing goos..

share|improve this question
    
Try to use glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); but we need to know more info about the current state of OpenGL ( more of your code ), and if you has loaded the textures on memory, placed the texture coordinates and texture binding. –  momboco Mar 27 '11 at 0:54
    
thanks i've edited –  nkint Mar 27 '11 at 8:17

2 Answers 2

up vote 1 down vote accepted

I have tested the example2a and it worked without changes. These are the files that I have used:

gutil.py:

import pygame

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

def initializeDisplay( w, h ):
    pygame.display.set_mode((w,h),pygame.OPENGL|pygame.DOUBLEBUF)

    glClearColor( 0.0, 0.0, 0.0, 1.0 )
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluOrtho2D( 0, w, 0, h );
    glMatrixMode( GL_MODELVIEW );

    #set up texturing
    glEnable( GL_TEXTURE_2D )
    glEnable( GL_BLEND )
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )

def loadImage( image ):
    textureSurface = pygame.image.load( image )

    textureData = pygame.image.tostring( textureSurface, "RGBA", 1 )

    width = textureSurface.get_width()
    height = textureSurface.get_height()

    texture = glGenTextures(1)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR )
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR )
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData )

    return texture, width, height

def delTexture( texture ):
    glDeleteTextures( texture )

def createTexDL(texture, width, height):
    newList = glGenLists(1)
    glNewList(newList,GL_COMPILE);
    glBindTexture(GL_TEXTURE_2D, texture)
    glBegin(GL_QUADS)
    glTexCoord2f(0, 0); glVertex2f(0, 0)    # Bottom Left Of The Texture and Quad
    glTexCoord2f(0, 1); glVertex2f(0, height)    # Top Left Of The Texture and Quad
    glTexCoord2f(1, 1); glVertex2f( width,  height)    # Top Right Of The Texture and Quad
    glTexCoord2f(1, 0); glVertex2f(width, 0)    # Bottom Right Of The Texture and Quad
    glEnd()
    glEndList()

    return newList

def delDL(list):
    glDeleteLists(list, 1)

example2a.py:

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

def main():
    pygame.init()
    gutil.initializeDisplay(800, 600)

    glColor4f(1.0,1.0,1.0,1.0)

    done = False

    cowTex, w, h = gutil.loadImage('put here a correct file path to a png file')
    cow = gutil.createTexDL(cowTex, w, h)
    alienTex, w, h = gutil.loadImage('put here a correct file path to a png file')
    alien = gutil.createTexDL(alienTex, w, h)

    while not done:

        glColor4f(1.0,1.0,1.0,1.0)

        glEnable( GL_TEXTURE_2D ) # enable texture
        glLoadIdentity()
        glTranslatef(0, 0, 0)
        glCallList(cow)
        glTranslatef(400, 400, 0)
        glCallList(alien)
        glDisable( GL_TEXTURE_2D ) # disable texture


        glLoadIdentity();  # reset the MODEL_VIEW matrix

        glColor3f( 1.0, 0.0, 0.0 ) # set color to red
        glBegin( GL_TRIANGLES )
        glVertex2i( 0, 0 )
        glVertex2i( 100, 0 )
        glVertex2i( 100, 100 )
        glEnd()


        pygame.display.flip()

        eventlist = pygame.event.get()
        for event in eventlist:
            if event.type == QUIT \
                or event.type == KEYDOWN and event.key == K_ESCAPE:
                    done = True

if __name__ == '__main__':
    main()

Review that your code is exactly the same. Put extreme caution in the identation. If copy and past the code below, convert the spaces into tab. If you try this and have a problem you can write a comment.

share|improve this answer
    
example2a works perfectly, this is not my problem. if you add some glcolor4f(1,1,1,1);glBegin(TRIANGLES); and draw some random triangles.. this does not work. –  nkint Mar 27 '11 at 17:47
    
I've edited my answer. The main loop of main function draws a triangle. Look at the lines with comments. –  momboco Mar 27 '11 at 18:27
    
what's the hell! why has textures to be disabled?? –  nkint Mar 27 '11 at 21:59
    
The primitives that you draw are affected by the current state of OpenGL, think that OpenGL is a large state machine. If the next primitive to draw doesn't have textures, ensure that the GL_TEXTURE_2D is disabled because there may be things that affect to painting when the texture state is enabled. –  momboco Mar 27 '11 at 22:24

OpenGL is a state machine, whenever you set it to a state you need to revert that state after you've used it. You have set current texture to be "texture", once you've called the list you need to restore the 0 texture (no texture state).

Textures aren't stored with display lists, so there's no need to bind them when making lists.

Your code should look like so:

//Create lists and fill them up
glNewList(newList,GL_COMPILE)
glBegin(GL_QUADS)
glTexCoord2f(0, 0); glVertex2f(0, 0)
.....
glTexCoord2f(1, 0); glVertex2f(width, 0)
glEnd()
glEndList()

...

//Bind texture and call the list
glBindTexture(GL_TEXTURE_2D, texture)
glCallList(newList)

//Bind another texture and call another list
glBindTexture(GL_TEXTURE_2D, texture2)
glCallList(newList2)

//UnBind texture
glBindTexture(GL_TEXTURE_2D, 0)

//Render any other untextured polygons/triangles in any fashion (Call lists, begin/end)

The reason why you had your polygons invisible most probably is that your texture is black/transparent at 0;0 coordinates. And when you draw your polygons they had been using 0;0 texture coords (default).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.