0

我创建这样的纹理:

GLuint PingPongShader::GenerateTexture()
{

float* pixels = new float[width*height * 4];
for(int i = 0; i < width * height; i+=4)
{
    pixels[i] = 1.0;
    pixels[i+1] = 1.0;
    pixels[i+2] = 1.0;
    pixels[i+3] = 1.0;
}
GLuint t;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
LogGLError();
return t;
}

我的显示功能如下所示:

void display()
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    shader.BindCurrentTexture();
    shader.EnableShader();
    glBegin(GL_QUADS);
    {
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(-1.0f, -1.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(1.0f, -1.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(1.0f, 1.0f);

        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(-1.0f, 1.0f);

    }
    shader.DisableShader();
    glDisable(GL_TEXTURE_2D);
    glutSwapBuffers();
}

着色器如下所示: 顶点:

varying vec2 texture_coordinate;

void main()
{
    // Transforming The Vertex
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    // Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    texture_coordinate = vec2(gl_MultiTexCoord0);
}

分段:

varying vec2 texture_coordinate; 
uniform sampler2D my_color_texture;

void main()
{
    // Sampling The Texture And Passing It To The Frame Buffer
    gl_FragColor = texture2D(my_color_texture, texture_coordinate);
}

不知何故,我只得到一个黑屏,即使像素都是 1.0(白色)。我看不出问题出在哪里:/

4

1 回答 1

0

您正在使用已弃用的函数编写代码。在 OpenGL 4.4 中你不能使用glBegin()和朋友。请VBO用于顶点和纹理坐标数据。您可以在现代 OpenGL 教程中查找它。您还可以使用glutInitContextVersion(). 确保使用 OpenGL 2.1 或为 > 3.0 的版本启用兼容性配置文件。

于 2014-02-17T15:05:46.500 回答