我正在尝试编写一个简单的opengl程序,该程序使用glPointSize()和变量pointSize在屏幕的中央周期性地增大和缩小单个点。打印pointSize的值并使用调试器逐步执行代码似乎表明pointSize在每次迭代中都正确更新。当pointSize增加时,该点的呈现也似乎是正确的,但是当pointSize减小时,该点仍以屏幕上的最大大小呈现,并且无论pointSize的值增加或缩小多少,该点都保持这种显示方式-和我不知道为什么。

#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>

#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];

GLuint createShaderProgram() {
    const char *vshaderSource =
        "#version 430    \n"
        "void main(void) \n"
        "{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";

    const char *fshaderSource =
        "#version 430    \n"
        "out vec4 color; \n"
        "void main(void) \n"
        "{ color = vec4(0.0, 0.0, 1.0, 1.0); }";

    GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
    GLuint vfprogram = glCreateProgram();

    glShaderSource(vShader, 1, &vshaderSource, NULL);
    glShaderSource(fShader, 1, &fshaderSource, NULL);

    glCompileShader(vShader);
    glCompileShader(fShader);

    glAttachShader(vfprogram, vShader);
    glAttachShader(vfprogram, fShader);

    glLinkProgram(vfprogram);

    return vfprogram;
}

void init() {
    renderingProgram = createShaderProgram();
    glGenVertexArrays(numVAOs, vao);
    glBindVertexArray(vao[0]);
}

GLfloat pointSize = 100.0f;
GLfloat increment = 1.0f;

void display() {
    glUseProgram(renderingProgram);

    if (pointSize > 200 || pointSize < 2) increment *= -1.0f;
    pointSize += increment;

    glPointSize(pointSize);
    glDrawArrays(GL_POINTS, 0, 1);
}

int main(void) {
    if (!glfwInit()) exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    GLFWwindow* window = glfwCreateWindow(600, 600, "Test", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);

    glfwSwapInterval(1);

    init();

    while (!glfwWindowShouldClose(window)) {
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

最佳答案

您的代码无法使用glClear清除屏幕。因此,当点增长时,您可以看到它。当它缩小时,它只是在较大的点内“画”,而没有引起您的注意。如果您更改了点的颜色(因为它改变了大小),您可能会发现这一点。

只需清除帧开始处的颜色缓冲区即可:

void display() {
    // default clear color is black, but you can change it:
    //glClearColor(1,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(renderingProgram);
    ...

关于c++ - glpointsize()无法缩小点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61946603/

10-11 00:51