OpenGL点精灵呈现为点而不是纹理

时间:2014-12-04 17:00:53

标签: c++ image opengl textures

这是我的代码

顶点着色器

#version 430

in vec3 position;
uniform mat4 MVP;

void main()
{
    gl_Position = MVP * vec4(position, 1.0);
}

片段着色器

#version 430

out vec4 outputColor;
uniform sampler2D tex;

void main()
{
    outputColor = texture(tex, gl_PointCoord);
}

初​​始化

void init()
{
    glPointSize(20.0f);
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    bmp.Open("C:\\users\\alon\\desktop\\star.bmp");
    bmp.ReadPixels();
    unsigned char *pixels = new unsigned char[bmp.NeededBufferSize()];
    bmp.AssignPixels(pixels);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmp.GetWidth(), bmp.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    delete[] pixels;

    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    GLuint VaoId;
    glGenVertexArrays(1, &VaoId);
    glBindVertexArray(VaoId);

    position_index = glGetAttribLocation(program, "position");
    MVP_location = glGetUniformLocation(program, "MVP");

    //                      x      y      z     s     t
    GLfloat vertices[] = {-0.5f, -0.5f, 0.0f,
                           0.5f, -0.5f, 0.0f,
                           0.5f,  0.5f, 0.0f,
                           0.5f,  0.5f, 0.0f,
                          -0.5f,  0.5f, 0.0f,
                          -0.5f, -0.5f, 0.0f};

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(position_index, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

呈现

void render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnableVertexAttribArray(position_index);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    float c;
    if(bTime)
        c = (float)std::clock() * 2.0f / CLOCKS_PER_SEC;
    else
        c = 0.0f;

    glm::mat4 modelview = glm::rotate(-c * 50.0f, glm::vec3(0.0f, 1.0f, 0.0f));
    modelview = glm::translate(glm::vec3(0.0f + fTranslateX, 0.0f, -1.75f + fTranslateZ)) * modelview;
    glm::mat4 projection = glm::perspective(60.0f, 16.0f/9.0f, 0.10f, 100.0f); //perspective
    glm::mat4 MVP = projection * modelview;

    glUniformMatrix4fv(MVP_location, 1, GL_FALSE, &MVP[0][0]);

    glDrawArrays(GL_POINTS, 0, 6);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(position_index);

    glutSwapBuffers();

    if(glGetError() != GL_NO_ERROR)
        exit(1);
}

星图是:

Star image

输出:

Screenshot

我有什么问题吗?

1 个答案:

答案 0 :(得分:2)

在兼容性配置文件(或3.1之前的任何版本的GL)中,只有在启用GL_POINT_SPRITE时才会出现您想要的行为。在核心配置文件中,该状态已被删除,gl_PointCoord始终表现得像启用GL_POINT_SPRITE一样。

要解决您的问题,您必须致电glEnable (GL_POINT_SPRITE)