开发者

glTexImage2d doesn't do anything

开发者 https://www.devze.com 2023-03-22 21:17 出处:网络
This problem baffles me. I am testing some Haskell bindings to OpenGL, I create a vertex shader, a fragment sha开发者_如何学Goder, compile the program, and draw a textured rectangle to the screen afte

This problem baffles me. I am testing some Haskell bindings to OpenGL, I create a vertex shader, a fragment sha开发者_如何学Goder, compile the program, and draw a textured rectangle to the screen after transforming the vertices... except the screen is blank.

When I render the rectangle flat white instead of using a sampler in the fragment shader, it works fine. When I go into gdebugger and use the option to replace all textures with a stub texture, it also works fine.

When I look at the allocated textures in gdebugger, there are no texture objects, only the default 2d texture. When I set a breakpoint on glTexImage2d, I see that it is being called, but no texture object appears in memory when I peek at it with gdebugger.

What's going on? Am I forgetting to set some environment variables? I'm quite frustrated. The kicker is that I had this problem before, and I managed to fix it then, but I forgot what the problem was. I hate myself >_>


I ported a tutorial on OpenGL to Haskell some time ago. It includes a link to a very tiny library that, among other things, helps with loading textures.

Perhaps you could compare that code with what you have to spot the differences.


I'm not a Haskell guy by any means, but try doing the simplest thing that could possibly work and check to see where you deviate from it:

#include <GL/glut.h>

double aspect_ratio = 0;

GLuint texID = 0;
unsigned int texBuf[] = {
    0x00FFFFFF,
    0x00FF0000,
    0x0000FF00,
    0x000000FF,    
};

void display(void)
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10*aspect_ratio, 10*aspect_ratio, -10, 10, -10, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub(255,255,255);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texID);

    glScalef(8,8,8);
    glTranslatef(-0.5f, -0.5f, 0.0f);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);
    glVertex2f(0,0);
    glTexCoord2f(1,0);
    glVertex2f(1,0);
    glTexCoord2f(1,1);
    glVertex2f(1,1);
    glTexCoord2f(0,1);
    glVertex2f(0,1);
    glEnd();

    glFlush();
    glFinish();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    aspect_ratio = (double)w / (double)h;
    glViewport(0, 0, w, h);
}

void idle()
{
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(200,200);
    glutCreateWindow("Aspect Ratio");

    glGenTextures(1, &texID);
    glBindTexture(GL_TEXTURE_2D, texID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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, 4, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, (char*)texBuf);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消