opengl 如何使用sf::Shader::setUniform在GLSL着色器中设置均匀mat4?

5vf7fwbs  于 2023-01-08  发布在  其他
关注(0)|答案(1)|浏览(172)

我正在特灵使用着色器绘制带有纹理的矩形,遵循this tutorial,这是我的代码:

主cpp

#define GLM_ENABLE_EXPERIMENTAL
#define GLEW_STATIC

#include<GL/glew.h>

#include<SFML/Graphics.hpp>
#include<SFML/OpenGL.hpp>

#include"../include/glm/glm.hpp"
#include"../include/glm/gtx/transform.hpp"
#include"../include/glm/gtc/type_ptr.hpp"

#include<iostream>

float vertices[] = {
    // positions          // texture coords
     0.5f,  0.5f, 0.0f,   1.0f, 1.0f, // top right
     0.5f, -0.5f, 0.0f,   1.0f, 0.0f, // bottom right
    -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, // bottom left
    -0.5f,  0.5f, 0.0f,   0.0f, 1.0f  // top left 
};

int main(){

    sf::ContextSettings set;
    set.depthBits = 24;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Window", sf::Style::Default, set);
    window.setActive(true);

    glewInit();
    glewExperimental = GL_TRUE;

    glEnable(GL_TEXTURE_2D);

    sf::Image im;
    im.loadFromFile("texture.jpg");
    im.flipVertically();
    sf::Texture tex1;
    tex1.loadFromImage(im);

    sf::Shader shader;
    shader.loadFromFile(
        "vertex.txt",
        "fragment.txt"
    );
    shader.setUniform("tex1", tex1);

    glm::mat4 transform = glm::mat4(1.f);
    transform = glm::translate(transform, glm::vec3(0.3f, 0.3f, 0.f));
    shader.setUniform("transform", glm::value_ptr(transform)); // !!!

    sf::Shader::bind(&shader);

    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    unsigned int VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0 * sizeof(float)));
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1); 

    glViewport(0.f, 0.f, 800.f, 600.f);
    glClearColor(0.f, 100.f, 150.f, 0.f);

    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glDrawArrays(GL_QUADS, 0, 4);
        window.display();
    }

}

顶点.txt文件:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoord;
  
uniform mat4 transform;

void main()
{
    gl_Position = transform * vec4(aPos, 1.0f);
    TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

但它只画出清晰的颜色,没有矩形。
有趣的是,没有变换它工作得非常好-如果gl_Position = transform * vec4(aPos, 1.0f);线被更改为gl_Position = vec4(aPos, 1.0f);,它会画出未变换的矩形和预期的纹理。所以我猜,这是一个设置均匀的问题,但它会很奇怪,至少纹理设置没有任何问题。我做错了什么?

kulphzqa

kulphzqa1#

sf::Shader具有sf::Glsl::Mat4类型的重载setUniform方法。需要创建一个sf::Glsl::Mat4对象。该对象可以从浮点数组创建:

sf::Glsl::Mat4 sfTransform(glm::value_ptr(transform));
shader.setUniform("transform", sfTransform);

相关问题