0
votes

I'm trying to map a generated 2D texture on a quad. So far the quad is rendered, but it is gray if I don't set a color and if I set colors for the vertices it shows the colors, yet the texture is not shown.

The reason for using fixed pipeline instead of shaders is that in order to generate the texture I would have to pass too much data to the shaders to generate the texture efficiently. So ideally I do not want to have a shader for something that should be as simple as rendering a texture on a quad. Although the program uses shaders extensively.

I tried using GLubyte for passing the data to glTexImage2D but got the same result. Also I tried various positions to call glEnable(GL_TEXTURE_2D), glBindTexture, glTexParameteri and glTexImage2D. Furthermore, I commented glTexEnvf, glBindTexture (unbinding), glDisable(GL_TEXTURE_2D) and glDeleteTextures(...). All with the same result.

Maybe it is not possible to use fixed pipeline texture rendering in a program that also uses shaders? Or did I make a mistake in my code? As far as I can see and according to Google the code below should render the texture on the quad. It is bugging me for a few days nows and probably it is a simple mistake...

glGetError returns 0 at all positions in the snippet.

    std::cout<<glGetError()<<std::endl;

    GLuint texName;

    glGenTextures(1, &texName);

    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    std::cout<<glGetError()<<std::endl;

    GLfloat image[64][64][4];

    for(unsigned int i = 0; i<64; i++)
    {
        for(unsigned int j = 0; j<64; j++) 
        {
            image[i][j][0] = 1.0f;
            image[i][j][1] = 0.5f;
            image[i][j][2] = (float)i/64;
            image[i][j][3] = 1.0f;
        }
    }

    std::cout<<glGetError()<<std::endl;

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 
        64, 0, GL_RGBA, GL_FLOAT, 
        image);

    std::cout<<glGetError()<<std::endl;

    glEnable(GL_TEXTURE_2D);

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);

    glBindTexture(GL_TEXTURE_2D, texName);

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0);     glVertex2i(10,  10);
        glTexCoord2f(0, 1);     glVertex2i(10,  110);
        glTexCoord2f(1, 1);     glVertex2i(110, 110);
        glTexCoord2f(1, 0);     glVertex2i(110, 10);
    glEnd();        

    glBindTexture(GL_TEXTURE_2D, 0);

    glDisable(GL_TEXTURE_2D);

    glDeleteTextures(1, &texName);

    std::cout<<glGetError()<<std::endl;
1
Why not generate the textures then pass them to a shader? If your software makes extensive use of them then your framework for this is already tried and testedD Hansen
That is the alternative option, doing it with a shader adds clutter and certainly doesn't make the code more readable. I don't like adding much (potentially unnecessary) code for something that should be simple.invalid_id
Without seeing how you've designed the code I can't comment either way. Will look into the current problem you have if I get chance. Hope you can find the answer!D Hansen
you can't have shaders and fixed function at the same time. So did you disable your shaders to draw with fixed function glUseProgram(0) ? Also, I'm with D Hansen that I'm not sure I understand what's so different with a shader path for this.Bahbar

1 Answers

0
votes

I should have mentioned in my question that I wanted the texture to be on the screen (in 2D).

A collegue was able to help me and suggested to use glDrawPixels which works, without disabling the shaders running (I didn't know they had to be disabled since most fixed pipeline stuff seemed to work, so thank you Bahbar).