2010-06-06 3 views
5

Estoy intentando generar texturas de este modo:GlGenTextures mantiene returing de 0

#define checkImageWidth 64 
#define checkImageHeight 64 
static GLubyte checkImage[checkImageHeight][checkImageWidth][4]; 
static GLubyte otherImage[checkImageHeight][checkImageWidth][4]; 

static GLuint texName[2]; 

void makeCheckImages(void) 
{ 
    int i, j, c; 

    for (i = 0; i < checkImageHeight; i++) { 
     for (j = 0; j < checkImageWidth; j++) { 
      c = ((((i&0x8)==0)^((j&0x8))==0))*255; 
      checkImage[i][j][0] = (GLubyte) c; 
      checkImage[i][j][1] = (GLubyte) c; 
      checkImage[i][j][2] = (GLubyte) c; 
      checkImage[i][j][3] = (GLubyte) 255; 
      c = ((((i&0x10)==0)^((j&0x10))==0))*255; 
      otherImage[i][j][0] = (GLubyte) c; 
      otherImage[i][j][1] = (GLubyte) 0; 
      otherImage[i][j][2] = (GLubyte) 0; 
      otherImage[i][j][3] = (GLubyte) 255; 
     } 
    } 
} 
void init(void) 
{  
    glClearColor (1.0, 0.0, 0.0, 0.0); 
    glShadeModel(GL_FLAT); 
    glEnable(GL_DEPTH_TEST); 

    makeCheckImages(); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 

    glGenTextures(2, texName); 
    glBindTexture(GL_TEXTURE_2D, texName[0]); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
     GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
     GL_NEAREST); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, 
     checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
     checkImage); 

    glBindTexture(GL_TEXTURE_2D, texName[1]); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
     GL_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
     GL_NEAREST); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, 
     checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
     otherImage); 
    glEnable(GL_TEXTURE_2D); 

    engineGL.current.tex = texName[1]; 
} 

Pero cuando compruebo los valores de texName [0] y [1] son ​​ambos 0, No entiendo por qué, Qué estoy haciendo mal. Gracias.

+0

es que la única función que falla? – jalf

+0

No, otras cosas también están fallando, llamo a init() después de configurar mi contexto ... – jmasterx

+1

¿ha verificado que está logrando establecer su contexto? –

Respuesta

8

Intente llamar glGetError. Debería decirle con más detalle qué salió mal. En general, si una función OpenGL falla, lo primero que debe hacer es preguntarle a OpenGL por qué falló. Lo sabe, porque simplemente intentó ejecutar la función.

Es mucho más difícil para nosotros adivinar qué pudo haber salido mal.

+0

Recibo el error 1282, ¿qué es eso? – jmasterx

+3

Use 'gluErrorString()' para averiguarlo. :) http://www.opengl.org/wiki/FAQ#glGetError_.28or_.22How_do_I_check_for_GL_errors.3F.29 – jalf

+0

"operación no válida" – jmasterx

18

Probablemente esté llamando a glGenTextures antes de crear el contexto OpenGL, y eso generará un error GL. No intente crear texturas antes de inicializar OpenGL.

+0

Sí, ese fue uno de mis problemas, también estaba llamando a wglmakecurrent en mi bucle de renderización que causaba problemas – jmasterx

+0

Me salvaste 1 noche de insomnio – 5argon

+0

¡Gracias! ¡Me salvaste la vida! –

11

he tenido este problema, y ​​glGetError() regresaba 0.

En mi caso fue causado por llamar glGenTextures(...) en un hilo diferente a la que el contexto GL se creó el (porque yo estaba cargando las texturas de forma asíncrona) . Llamando desde el hilo principal después de, la carga asíncrona hecha glGenTextures(...) comienza a funcionar nuevamente.

0

En mi caso, estaba "cargando perezosamente" mi textura, por lo que glGenTexture estaba dentro de un comando glBegin/glEND.

-1

antes de llamar glGenTexture, su contexto OpenGL debe ser creado y XXXMakeCurrent'ed