简体   繁体   English

OpenGL-着色器中的顶点颜色被交换

[英]OpenGL - vertex color in shader gets swapped

I'm trying to send colors to the shader but the colors get swapped, I send 0xFF00FFFF (magenta) but I get 0xFFFF00FF (yellow) in the shader. 我正在尝试将颜色发送到着色器,但是颜色被交换,我发送了0xFF00FFFF(洋红色),但是在着色器中却收到了0xFFFF00FF(黄色)。

I think is happening something like this, by experimenting: 我认为通过实验,正在发生以下情况:

图片

My vertex shader: 我的顶点着色器:

#version 330 core

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec4 color;

uniform mat4 pr_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

out DATA
{
    vec4 position;
    vec3 normal;
    vec4 color;
} vs_out;

void main()
{
    gl_Position = pr_matrix * vw_matrix * ml_matrix * position;

    vs_out.position = position;
    vs_out.color = color;
    vs_out.normal = normalize(mat3(ml_matrix) * normal);
}

And the fragment shader: 和片段着色器:

#version 330 core

layout(location = 0) out vec4 out_color;

in DATA
{
    vec3 position;
    vec3 normal;
    vec4 color;
} fs_in;

void main()
{
    out_color = fs_in.color;

    //out_color = vec4(fs_in.color.y, 0, 0, 1);
    //out_color = vec4((fs_in.normal + 1 / 2.0), 1.0);
}

Here is how I set up the mesh: 这是我设置网格的方法:

struct Vertex_Color {
    Vec3 vertex;
    Vec3 normal;
    GLint color; // GLuint tested
};


std::vector<Vertex_Color> verts = std::vector<Vertex_Color>();

[loops]
    int color = 0xFF00FFFF; // magenta, uint tested
    verts.push_back({ vert, normal, color });


glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex_Color), &verts[0], GL_DYNAMIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex_Color), (const GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex_Color), (const GLvoid*)(offsetof(Vertex_Color, normal)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex_Color), (const GLvoid*)(offsetof(Vertex_Color, color)));
glEnableVertexAttribArray(2);

Here are some examples: 这里有些例子:

例子

I can't figure it out what's wrong. 我不知道怎么了。 Thanks in advance. 提前致谢。

Your code is reinterpreting an int as 4 consecutive bytes in memory. 您的代码将int解释为内存中的4个连续字节。 The internal encoding for int (and all other types) is machine-specific. int (和所有其他类型)的内部编码是特定于机器的。 In your case, you got 32 bit integers stored in little endian byte order, which is kind of the typical case for PC environments. 在您的情况下,您以小尾数字节顺序存储了32位整数,这是PC环境中的典型情况。

You could use an array like GLubyte color[4] to explicitely get a defined memory layout. 您可以使用类似GLubyte color[4]的数组来显式地获取定义的内存布局。

If you really want to use an integer type, you could send the data as a an integer attribute with glVertexAttribIPointer (note the I there) and use unpackUnorm4x8 om the shader to get a normalized float vector. 如果您确实想使用整数类型,则可以使用glVertexAttribIPointer (请注意那里的I)将数据作为整数属性发送,并使用着色器中的unpackUnorm4x8来获取标准化的浮点向量。 However, that requires at least GLSL 4.10, and might be less efficient than the standard approach. 但是,这至少需要GLSL 4.10,并且效率可能不如标准方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM