Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL Uniform buffers?
    primarykey
    data
    text
    <p>I'm trying to use uniform buffers but it doesn't work as supposed. I have two uniform buffers, one is lighting and the other is for material. The problem is that the colors aren't what they are supposed to be and they change every time I move the camera. This problem didn't exist when I used normal uniforms. Here's pictures to show what I mean: <a href="http://localhostr.com/files/tMmz1e1/OpenGL.png">When using uniform buffers</a> and <a href="http://localhostr.com/files/uXxothg/OpenGL.png">when using normal uniforms</a>!</p> <p>This is my fragment shader: </p> <pre><code>#version 400 // Fragment Shader uniform layout(std140); in vec3 EyePosition; in vec3 EyeNormal; in vec2 TexCoord; out vec4 FragColor; uniform sampler2D Texture; uniform LightBlock { vec4 Position; vec4 Intensity; } Light; uniform MaterialBlock { vec4 Ambient; vec4 Diffuse; } Material; vec4 PointLight(in int i, in vec3 ECPosition, in vec3 ECNormal) { vec3 n = normalize(ECNormal); vec3 s = normalize(Light.Position.xyz - ECPosition); return Light.Intensity * (Material.Ambient + Material.Diffuse * max(dot(s, n), 0.0)); } void main() { FragColor = texture(Texture, TexCoord); FragColor *= PointLight(0, EyePosition, EyeNormal); } </code></pre> <p>I'm not sure I have done everything right but here's how I create the uniform buffers:</p> <pre><code>glGenBuffers(1, &amp;light_buffer); glGenBuffers(1, &amp;material_buffer); glBindBuffer(GL_UNIFORM_BUFFER, light_buffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(LightBlock), nullptr, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, material_buffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(MaterialBlock), nullptr, GL_DYNAMIC_DRAW); GLuint program = Shaders.GetProgram(); light_index = glGetUniformBlockIndex(program, "LightBlock"); material_index = glGetUniformBlockIndex(program, "MaterialBlock"); glUniformBlockBinding(program, light_index, 0); glUniformBlockBinding(program, material_index, 1); glBindBufferBase(GL_UNIFORM_BUFFER, 0, light_buffer); glBindBufferBase(GL_UNIFORM_BUFFER, 1, material_buffer); </code></pre> <p>EDIT: Here's how I fill the buffers:</p> <pre><code>// Global structures struct LightBlock { Vector4 Position; // Vector4 is a vector class I made Vector4 Intensity; }; struct MaterialBlock { Vector4 Ambient; Vector4 Diffuse; }; // This is called for every object rendered LightBlock Light; Light.Position = Vector3(0.0f, 5.0f, 5.0f) * Camera.GetCameraMatrix(); Light.Intensity = Vector4(1.0f); glBindBuffer(GL_UNIFORM_BUFFER, light_buffer); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(LightBlock), &amp;Light); MaterialBlock Material; Material.Diffuse = Vector4(1.0f); Material.Ambient = Material.Diffuse * Vector4(0.3f); glBindBuffer(GL_UNIFORM_BUFFER, material_buffer); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(MaterialBlock), &amp;Material); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload