Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ glClearColor和混合_C++_Opengl_Glfw_Blending - Fatal编程技术网

C++ glClearColor和混合

C++ glClearColor和混合,c++,opengl,glfw,blending,C++,Opengl,Glfw,Blending,我正在使用GLFW、带VSCode的OpenGL4.6和MingW64G++。我正在尝试使用glEnable(GL_-BLEND)渲染透明纹理和glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去glu SRC1_ALPHA)。虽然纹理确实在我的窗口上渲染,但glClearColor()似乎对混合效果有显著影响。我对图形还是很陌生,所以我不确定如何正确渲染纹理,不管“清晰颜色”设置为什么。另外,我是否需要启用2D游戏的深度?(shader.cpp和texture.cpp取自Che

我正在使用GLFW、带VSCode的OpenGL4.6和MingW64G++。我正在尝试使用
glEnable(GL_-BLEND)渲染透明纹理
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去glu SRC1_ALPHA)。虽然纹理确实在我的窗口上渲染,但glClearColor()似乎对混合效果有显著影响。我对图形还是很陌生,所以我不确定如何正确渲染纹理,不管“清晰颜色”设置为什么。另外,我是否需要启用2D游戏的深度?(shader.cpp和texture.cpp取自Cherno的OpenGL教程系列)

不调和

混合

混合色和黑色透明色(所需混合色)

main.cpp:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "GeometryUtil.h"
#include "Shader.h"
#include "Texture.h"
#include "Object.h"

#include <iostream>
#include <vector>

using namespace std;

int main() {
    if (!glfwInit()) { // Initialise GLFW
        cerr << "Failed to initialize GLFW" << endl;
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window {glfwCreateWindow(1280, 720, "2D GAME", NULL, NULL)};
    if (window == NULL) {
        cerr << "Failed to open GLFW window." << endl;
        glfwTerminate();
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glfwMakeContextCurrent(window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK) {
        cerr << "Failed to initialize GLEW" << endl;
        glfwTerminate();
        return -1;
    }
    
    cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl;
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC1_ALPHA);
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);

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

    Object object {608, 392};
    vector<float> vectorData;
    createQuad(object.getX(), object.getY(), vectorData);

    static const GLfloat texCoords[8] {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f
    };

    GLuint VBOs[2];
    glGenBuffers(2, VBOs);
    glBindBuffer(GL_ARRAY_BUFFER, VBOs[0]);
    glBufferData(GL_ARRAY_BUFFER, vectorData.size() * sizeof(vectorData), &vectorData.front(), GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *) 0);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *) (2 * sizeof(GLfloat)));

    glBindBuffer(GL_ARRAY_BUFFER, VBOs[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
    

    static const GLuint texIndex[6] {
        0,  1,  2,
        1,  2,  3
    };
    GLuint indexBuffer;
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(texIndex), texIndex, GL_STATIC_DRAW);

    glm::mat4 Projection {glm::ortho(0.0f, 1280.0f, 0.0f, 720.0f, 0.0f, 1.0f)};
    glm::mat4 View {glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0))};

    glm::vec3 translationA {glm::vec3(0, 0, 0)};
    glm::mat4 Model = glm::translate(glm::mat4(1.0f), translationA); // Model Matrix (Using Identity Matrix - Origin)
    glm::mat4 mvp = Projection * View * Model;

    Shader shader {"shader/vertexShader.vert", "shader/fragmentShader.frag"};
    shader.bind();
    shader.setUniformMat4f("MVP", mvp);
    int sampler[] {0, 1};
    shader.setUniform1iv("v_Textures", sampler, 2);

    Texture texture {"resources/sprites/test.png"};
    texture.bind();

    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
片段着色器:

#version 460 core

in vec2 v_TexCoords;
in float v_TexIndex;

uniform sampler2D v_Textures[2];

void main() {
    gl_FragColor = texture2D(v_Textures[int(v_TexIndex)], v_TexCoords);
}

在第一次查看时,每件事情都是正确的,是的,背景将影响作为第一个参数的
GL\u SRC\u ALPHA
的混合

问题是第二个参数,它应该是
GL\u-ONE\u-SRC\u-ALPHA
,而不是
GL\u-ONE\u-SRC1\u-ALPHA
。在您的情况下,混合没有标准化,因此太多绿色和全红色的值加起来就是淡黄色


来源:

这是预期结果。混合的工作方式是,它将您输出的颜色与缓冲区中已经存在的颜色混合。如果您的C字母的alpha值小于1,它将与背景色结合,从而使其着色。这与alpha键控不同,alpha键控根据值丢弃样本,但不将它们与缓冲区中已有的数据组合。
#version 460 core

layout(location = 0) in vec3 vertexCoords;
layout(location = 1) in vec2 textureCoords;
layout(location = 2) in float textureIndex;

out vec2 v_TexCoords;
out float v_TexIndex;

uniform mat4 MVP;

void main() {
    gl_Position = MVP * vec4(vertexCoords, 1.0);
    v_TexCoords = textureCoords;
    v_TexIndex = textureIndex;
}
#version 460 core

in vec2 v_TexCoords;
in float v_TexIndex;

uniform sampler2D v_Textures[2];

void main() {
    gl_FragColor = texture2D(v_Textures[int(v_TexIndex)], v_TexCoords);
}