【发布时间】:2020-11-01 13:01:08
【问题描述】:
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。
【问题讨论】:
-
您的着色器程序在哪里?如果要使用
gluSphere和矩阵制服,则必须使用 GLSL 1.20 顶点着色器,它通过矩阵制服转换固定函数顶点属性。 -
@Rabbid76 矩阵和 glSpheres 在我的着色程序上工作。所以,我认为顶点着色器还可以。片段着色器适用于
VAO,但我不确定片段着色器是否也适用于gluSphere。我编辑我的片段着色器。请检查一下。 -
否,着色器不行。如果您只有一个顶点坐标,它将起作用,但它不适用于其他属性。
gluSphere不能与顶点着色器输入变量一起使用。您仅限于 GLSL 1.20 和 Vertex Shader Built-In Attributes。gluSphere几十年来已被弃用。无论如何,该函数无法猜测属性位置。 -
无论如何这是一个片段着色器而不是顶点着色器。
标签: opengl glsl shader opengl-compat glu