如何用颜色绘制gluQuadric?

ku
struct quadricObj {
    GLUquadricObj* obj;
    GLenum drawmode{ GLU_FILL };
    GLdouble radius{1.0};
    GLint slices{20};
    GLint stacks{20};
    glm::vec3 col{ 1.0,0.0,0.0 };
    std::vector<glm::mat4> M;
    glm::mat4 world_M() {
        glm::mat4 WM(1.0f);
        std::for_each(this->M.begin(), this->M.end(), [&WM](glm::mat4& m) { WM *= m; });
        //M0*M1*M2 TRS
        return WM;
    }
    GLvoid draw() {
        gluQuadricDrawStyle(this->obj, this->drawmode);
        glUniformMatrix4fv(worldLoc, 1, GL_FALSE, glm::value_ptr(this->world_M()));
        glColor4f(this->col.r, this->col.g, this->col.b, 1.0f); // doesn't work.
        gluSphere(this->obj, this->radius, this->slices, this->stacks);
    }
};

这是我使用quadricObj的结构。我认为glColor4f必须工作,但是没有。二次曲面保持黑色。如何在GL中为二次曲面着色?

#version 330

in vec3 v_normal;
in vec2 v_texCoord;
in vec3 v_color;
in vec3 fragPos;

out vec4 gl_FragColor;

uniform vec3 lightColor;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform float ambientLight;
uniform int shine;

void main(void)
{   
    vec3 ambient = clamp(ambientLight*lightColor,0.0,1.0);

    vec3 normalVector = normalize(v_normal);
    vec3 lightDir = normalize(lightPos-fragPos);
    float diffuseLight = max(dot(normalVector,lightDir),0.0); 
    vec3 diffuse = clamp(diffuseLight * lightColor,0.0,1.0);

    vec3 viewDir = normalize(viewPos-fragPos);
    vec3 reflectDir = reflect(-lightDir,normalVector);
    float specularLight = max(dot(viewDir,reflectDir),0.0);
    specularLight = pow(specularLight,shine);
    vec3 specular = clamp(specularLight*lightColor,0.0,1.0);

    vec3 result = (ambient+diffuse)*v_color+specular*(0.8,0.8,0.8);

    gl_FragColor = vec4(result,1.0);
}

我编辑片段着色器包含phong模型。这也可以gluSphere吗?或不?我也在使用顶点着色器。inpos,col,nor,tex。out也。

拉比德76

gluSphere不能与用户定义的顶点着色器输入变量(属性)一起使用。您只能使用GLSL 1.20顶点着色器和Vertex Shader内置属性您可以将GLSL 1.20顶点着色器与片段着色器结合使用。

合适的顶点着色器可能如下所示:

#version 120

varying vec3 v_normal;
varying vec2 v_texCoord;
varying vec3 v_color;
varying vec3 fragPos;

uniform mat4 worldMatrix; // the matrix with the location worldLoc

void main()
{
    v_color     = gl_Color.rgb;
    v_texCoord  = gl_MultiTexCoord0.st;
    v_normal    = mat3(worldMatrix) * gl_Normal;
    fragPos     = (worldMatrix * gl_Vertex).xyz;
    gl_Position = gl_ProjectionMatrix * worldMatrix * gl_Vertex;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章