glUniformMatrix4fv 导致 GL_INVALID_OPERATION

问题描述 投票:0回答:3

当我在下面的代码中调用 glUniformMatrix4fv 或 glUniform4fv 时,它每次都会引发 GL_INVALID_OPERATION ,我尝试更改传递矩阵数据的方式、传递 id 的计数,我检查了模型 ids 和着色器程序,但无法计算知道它为什么这样做。

这是着色器:

330版

uniform vec4 projection; /* view port / perspective */
uniform vec4 model; /* object */
uniform vec4 view; /*camera */
in vec3 position;

void main() {
    gl_Position = projection * view * model * vec4(position, 1.0);
}

这是我的代码:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <fstream>
#include <stdlib.h>

GLuint getShader(const char* shaderName, GLenum shaderType) {
    /* Read file */
    std::ifstream shaderFile;
    shaderFile.open(shaderName, std::ios_base::in);

    if(!shaderFile) {
        std::cout <<  shaderName << " not found" << std::endl;
        return 0;
    }

    std::string line;
    std::string shaderData;
    while(std::getline(shaderFile, line)) {
        shaderData += line + "\n";
    }

    shaderFile.close();

    /* Create shader */
    GLuint shader = glCreateShader(shaderType);

    if(shader == 0) {
        std::cout << "Failed to create shader for " << shaderName << std::endl;
        return 0;
    }

    const char *shaderStr = shaderData.c_str();

    glShaderSource(shader, 1, &shaderStr, nullptr);
    glCompileShader(shader);

    /* Check compile status */
    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if(status != GL_TRUE) {
        GLint logLength;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);

        GLchar* infoLog = new GLchar[logLength + 1];
        glGetShaderInfoLog(shader, logLength, nullptr, infoLog);

        std::cout << shaderName << " failed to compile: " << infoLog << std::endl;
        return 0;
    }

    return shader;
}

int end(const char* message) {
    glfwTerminate();

    if(message != nullptr) {
        std::cout << message << std::endl;
        system("PAUSE");
    }
    return 1;
}

int main() {
    /* Initial checks */
    if (!glfwInit()) {
        return end("Failed to startup GLFW");
    }

    /* Create the window */
    GLFWwindow *window = glfwCreateWindow(500, 500, "3D world prototyping", nullptr, nullptr);

    if (!window) {
        return end("Failed to create window.");
    }

    glfwMakeContextCurrent(window); //make the window active

    /* Startup GLEW */
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        return end("Failed to startup GLEW");
    }

    /* Start OpenGL constants */
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, 500, 500);

    /* Support for multiple shaders without the need to switch attributes */
    GLuint vertexArray;
    glGenVertexArrays(1, &vertexArray);
    glBindVertexArray(vertexArray);

    /* Create buffers */
    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);

    /* Shape vectors */
    GLfloat verticies[] = { //X, Y, Z
            -0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, -0.5f,
            0.5f, 0.5f, -0.5f,
            0.5f, 0.5f, -0.5f,
            -0.5f, 0.5f, -0.5f,
            -0.5f, -0.5f, -0.5f,

            -0.5f, -0.5f, 0.5f,
            0.5f, -0.5f, 0.5f,
            0.5f, 0.5f, 0.5f,
            0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, 0.5f,
            -0.5f, -0.5f, 0.5f,

            -0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, -0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f, -0.5f, 0.5f,
            -0.5f, 0.5f, 0.5f,

            0.5f, 0.5f, 0.5f,
            0.5f, 0.5f, -0.5f,
            0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, 0.5f,
            0.5f, 0.5f, 0.5f,

            -0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, -0.5f,
            0.5f, -0.5f, 0.5f,
            0.5f, -0.5f, 0.5f,
            -0.5f, -0.5f, 0.5f,
            -0.5f, -0.5f, -0.5f,

            -0.5f, 0.5f, -0.5f,
            0.5f, 0.5f, -0.5f,
            0.5f, 0.5f, 0.5f,
            0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, 0.5f,
            -0.5f, 0.5f, -0.5f,

            -1.0f, -1.0f, -0.5f,
            1.0f, -1.0f, -0.5f,
            1.0f, 1.0f, -0.5f,
            1.0f, 1.0f, -0.5f,
            -1.0f, 1.0f, -0.5f,
            -1.0f, -1.0f, -0.5f
    };
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);

    /* Load in the shaders */
    GLuint fragmentShader = getShader("fragmentShader.glsl", GL_FRAGMENT_SHADER);
    GLuint vertexShader = getShader("vertexShader.glsl", GL_VERTEX_SHADER);

    if (fragmentShader == 0 || vertexShader == 0) {
        return end("Failed to load shaders");
    }

    /* Setup the shaders */
    /* Create the shader program and use it */
    GLuint shaderProgram = glCreateProgram();

    if (shaderProgram == 0) {
        return end("Failed to create shader program");
    }

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor"); //tell opengl about the out color variable
    glLinkProgram(shaderProgram);

    if(glIsProgram(shaderProgram) != GL_TRUE) {
        return end("Failed to create shader program");
    }

    /* Positioning */
    //FOV, Screen ratio, display range short, display range long
    glm::mat4 projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
    glm::mat4 view = glm::lookAt(
        glm::vec3(4.0f, 3.0f, 3.0f), // Camera
        glm::vec3(0.0f, 0.0f, 0.0f), // Where the camera looks up
        glm::vec3(0.0f, 1.0f, 0.0f)  // Pitch of the head
    );
    glm::mat4 model = glm::mat4(1.0f);

    /* Rendering and logic */
    while (!glfwWindowShouldClose(window)) {
        /* Shutdown logic */
        if (glfwGetKey(window, GLFW_KEY_ESCAPE)) {
            break;
        }

        /* Clear */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);

        /* Logic */
        glUseProgram(shaderProgram);

        GLint projectionId = glGetUniformLocation(shaderProgram, "projection");
        GLint viewId = glGetUniformLocation(shaderProgram, "view");
        GLint modelId = glGetUniformLocation(shaderProgram, "model");

GLenum error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Uniform error " << error << ": " << glewGetErrorString(error) << std::endl;
            return end("");
        }
        else if(projectionId == -1) {
            return end("Projection ID failed to load");
        }
        else if(viewId == -1) {
            return end("View ID failed to load");
        }
        else if(modelId == -1) {
            return end("Model ID failed to load");
        }

        glUniformMatrix4fv(viewId, 3, GL_FALSE, glm::value_ptr(view));

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "View uniform matrix error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glUniformMatrix4fv(projectionId, 3, GL_FALSE, glm::value_ptr(projection));

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Projection matrix error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glUniformMatrix4fv(modelId, 3, GL_FALSE, glm::value_ptr(model));

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Model uniform matrix error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        /* Tell opengl about all the attributes */
        GLint positionAttribute = glGetAttribLocation(shaderProgram, "position");

        if(positionAttribute == -1) {
            std::cout << "Error: position attribute failed to load" << std::endl;
            end("");
        }
        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Position attribute error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glEnableVertexAttribArray(positionAttribute);

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Vertex enable error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Buffer bind error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, 0, 0); //because not just XYZ need to tell opengl the size of the stride

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Vertex attrib pointer error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        /* Draw */
        glDrawArrays(GL_TRIANGLES, 0, 36);

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "gl draw arrays " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glDisableVertexAttribArray(positionAttribute);

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "gl disable attrib error " << error << ": " << glewGetErrorString(error) << std::endl;
        }

        glfwSwapBuffers(window);
        glfwPollEvents();

        error = glGetError();
        if(error != GL_NO_ERROR) {
            std::cout << "Error " << error << ": " << glewGetErrorString(error) << std::endl;
        }
    }

    glfwDestroyWindow(window);
    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vertexBuffer);
    glDeleteVertexArrays(1, &vertexArray);
    return end(nullptr);
}
c++ opengl
3个回答
4
投票

对于有问题的特定制服,您使用了错误的 API 调用来设置它们。

这些是向量,而不是矩阵。您可以使用

glUniform4fv (...)
而不是
glUniformMatrix4fv (...)
。后一个函数假设您正在设置 1 个或多个 4x4 矩阵制服(您传递的参数告诉它
projection
应该是 3
mat4
的数组)。


我相信在您的情况下,适当的函数调用将如下所示:

glUniform4fv (modelId, 1, glm::value_ptr(model)); // Fill 1 4D vector, not an array of vectors

2
投票

当您调用的 setter 函数与您尝试设置的制服的数据类型不匹配时,设置制服将导致

GL_INVALID_OPERATION
错误。

自从您将制服声明为向量以来:

uniform vec4 projection; /* view port / perspective */
uniform vec4 model; /* object */
uniform vec4 view; /*camera */

您不能使用 matrix 设置器。您应该将 Uniformst 更改为

mat4
才能使您的代码正常工作。

作为一个小旁注:如果您尝试设置位置为 -1 的制服(意味着制服不存在或已被优化),它不会 生成错误。这是明确允许的,可能是为了允许通用框架,其中着色器可能只使用制服的子集。


0
投票
抛出此确切错误的另一个虚拟场景是未选择着色器(或像某些人所说的那样绑定)。因此,在深入研究细节之前,可能值得检查一下着色器。

© www.soinside.com 2019 - 2024. All rights reserved.