我正在点亮一个3D场景,我希望我可以代表光源(可能会改变方向,点和聚光灯):
我试图绘制一个圆圈(使用vbo)并使用GL_BLEND,但不能正常工作,不会给出所需的结果。
指导我们如何产生这种效果。
代码(相关部分):
void TemplateLight(){ //Light source with center in (0,0,0) and radius = 1
float angle, x, y, z = 0.f;
vertices[0] = vec3(0.f);
GLuint vbo;
for(int i=0; i<=divf; ++i){
angle = (2*PI/(float)divf)*(float)i;
x = vertices[0].x + cosf(angle);
y = vertices[0].y + sinf(angle);
vertices[i+1] = vec3(x,y,z);
}
glGenBuffers(1, &vbo);
glGenVertexArrays(1, &vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao);
glEnableVertexAttribArray(attrib_vertex);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(attrib_vertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
mat4 ModelMatrix(){ //Moves drawing template to the current position of the light
return translate(mat4(1.f), vec3(position))*scale(mat4(1.f), vec3(0.05f,0.05f,1.f)); }
void Draw(){
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_FAN, 0, divf*3);
glBindVertexArray(0);
}
void Display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glUseProgram(l_program[0].id);
l_program[0].SendUniformsMatricesToShader();
l_program[0].SendUniformsMaterialToShader(scene[0].material);
l_program[0].SendUniformsLightsToShader(ambient, ambient_info);
for(int i=0; i<MAX_LIGHTS; ++i)
l_program[0].SendUniformsLightsToShader(lights[i]);
test.Draw();
glUseProgram(0);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA);
glUseProgram(light_source.id); //Draw "point lights"
for(int i=0; i<MAX_LIGHTS; ++i){
if(lights[i].active){
mvp = projection*view*lights[i].ModelMatrix(); //Estan en coordenadas de vista
light_source.SendUniformsMatricesToShader();
light_source.SendUniformsColorToShader(lights[i].GetColor());
lights[i].Draw();
}
}
glUseProgram(0);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glutSwapBuffers();
}
片段着色器:
#version 400
uniform vec4 color;
void main(){
gl_FragColor = color;
}