Friday, 15 May 2015

c++ - Vertex colors not working correctly in fragment shader -



c++ - Vertex colors not working correctly in fragment shader -

i'm trying implement simple lighting, , i've gotten myself can't figure out how fix.

i calculate color of each vertex using vertex shader:

#version 330 core layout(location = 0) in vec4 vertexpos; layout(location = 1) in vec4 lightpos; uniform vec3 lightcolor; uniform vec4 received_color; uniform mat4 translation; uniform mat4 projection; out vec4 vertex_color; vec4 normal = vec4(0.0, 0.0, -1.0, 1.0); void attenuation(out float atten, in float distance, in float range, in float a, in float b, in float c) { atten = 1.0 / ( * distance * distance + b * distance + c) * range; } void main() { /* position stuff */ float atten = 0.0; attenuation(atten, distance(lightpos, vertexpos), 20, 1.0, 0.1, 1.0); vec4 vlight = normalize(lightpos - vertexpos); float ndotl = max(0.0, dot(normal.xyz, vlight.xyz)); vertex_color = vec4(atten * ndotl * lightcolor * received_color.xyz, 1.0); gl_position = vertexpos; }

in fragment shader set color equal vertex_color

now, far know, should calculate color independently each vertex, , colors should blend nicely. however, shows up: i'm able tell varying intensity based on distance works, baffled fact color appears in upper left corner of each square.

i need color calculated , used each vertex; not 1 per square.

if interested, code utilize rendering: http://pastebin.com/g6xiwfwq

c++ opengl glsl shader vertex

No comments:

Post a Comment