Graphics
Intel® graphics drivers and software, compatibility, troubleshooting, performance, and optimization
20493 Discussions

Using Uniform Blocks for Materials ...

TMaso1
Beginner
1,935 Views

Hello,

I am attemtping to use uniform blocks for materials and for some reason with my current implementation the uniform for one material ends up "overriding" the previous drawn information for previous draw calls, which ends up with a lot of flashing on screen.

The animated gif below does a better job of explaining what happens:

Any ideas? Thank you!

Here are my draw calls and how I send data to the uniform buffer:

MVPMatrix = (*ProjectionMatrix) * (*ViewMatrix) * ModelMatrix;

NormalMatrix = glm::transpose(glm::inverse(glm::mat3(MVPMatrix)));

InverseViewMatrix = glm::inverse((*ViewMatrix));

glBindVertexArray(VertextArrayObjectID);

glUniformMatrix4fv((*AssociatedOpenGLProgram->GetModelMatrixID()), 1, GL_FALSE, glm::value_ptr(ModelMatrix));

glUniformMatrix4fv((*AssociatedOpenGLProgram->GetMVPMatrixID()), 1, GL_FALSE, glm::value_ptr(MVPMatrix));

glUniformMatrix4fv((*AssociatedOpenGLProgram->GetViewMatrixID()), 1, GL_FALSE, glm::value_ptr((*ViewMatrix)));

glUniformMatrix4fv((*AssociatedOpenGLProgram->GetInverseViewMatrixID()), 1, GL_FALSE, glm::value_ptr(InverseViewMatrix));

glUniformMatrix3fv((*AssociatedOpenGLProgram->GetNormalMatrixID()), 1, GL_FALSE, glm::value_ptr(NormalMatrix));

glBindBuffer(GL_UNIFORM_BUFFER, (*AssociatedOpenGLProgram->GetMaterialUniformBufferID()));

glBufferData(GL_UNIFORM_BUFFER, sizeof(GLfloat) * 18,

AssociatedMaterial->GetMaterialUniformValues(), GL_DYNAMIC_DRAW);

glBindBuffer(GL_UNIFORM_BUFFER, NULL);

if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

GLfloat EnableWireframe = 1.0f;

glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);

glDisable(GL_BLEND);

glDisable(GL_TEXTURE_2D);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

}

else {

if (AssociatedMaterial->HasAssociatedTexture()) {

GLfloat ObjectHasMaterial = 1.0f;

glUniform1fv((*AssociatedOpenGLProgram->GetObjectHasTextureID()), 1, &ObjectHasMaterial);

glUniform1i((*AssociatedOpenGLProgram->GetTextureSamplerID()), 1);

AssociatedMaterial->BindTexture(1);

}

else {

glBindTexture(GL_TEXTURE_2D, NULL);

}

}

glDrawElementsInstanced(GL_TRIANGLES, NumOfIndices, GL_UNSIGNED_INT, NULL, 1);

if (AssociatedMaterial->IsWireframeEnabled() != false && AssociatedOpenGLProgram->IsWireframeEnabled() == false) {

GLfloat EnableWireframe = 0.0f;

glUniform1fv((*AssociatedOpenGLProgram->GetEnableWireframeID()), 1, &EnableWireframe);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glEnable(GL_TEXTURE_2D);

glEnable(GL_BLEND);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}

This is how I set up the material uniform in question:

MaterialUniformBlockID = glGetUniformBlockIndex(programID, "Material");

glUniformBlockBinding(programID, MaterialUniformBlockID, MaterialUniformBlockBindingPoint);

glGenBuffers(1, &MaterialUniformBufferID);

glBindBuffer(GL_UNIFORM_BUFFER, MaterialUniformBufferID);

glBindBufferBase(GL_UNIFORM_BUFFER, MaterialUniformBlockBindingPoint, MaterialUniformBufferID);

glBindBuffer(GL_UNIFORM_BUFFER, NULL);

Here is my vertex shader:

# version 330 core

# extension GL_ARB_explicit_attrib_location : require

// Input vertex data, different for all executions of this shader.

layout(location = 0) in vec3 vPosition;

layout(location = 1) in vec3 Normals;

layout(location = 2) in vec2 vUV;

// Output data ; will be interpolated for each fragment.

out vec3 SurfaceNormal;

out vec4 vertexPosition;

out vec2 TextureCoordinates;

// Values that stay constant for the whole mesh.

uniform mat4 MVP;

uniform mat4 ModelMatrix;

uniform mat4 ViewMatrix;

uniform mat3 NormalMatrix;

void main(){

gl_Position = MVP * vec4(vPosition, 1.0);

vertexPosition = ModelMatrix * vec4(vPosition, 1.0);

SurfaceNormal = normalize(NormalMatrix * Normals);

TextureCoordinates = vUV;

}

And here is my fragment shader:

# version 330

# extension GL_ARB_explicit_attrib_location : require

//

// These values vary per Mesh

//

layout (std140) uniform Material

{

vec4 AmbientMeshColor;

vec4 EmissiveMeshColor;

vec4 DiffuseMeshColor;

vec4 SpecularMeshColor;

float MeshShininess;

float ObjectHasTextureFile;

};

//

// Sunlight Settings.

//

layout (std140) uniform Sunlight

{

vec4 SunlightPosition;

vec4 SunlightDiffuse;

vec4 SunlightSpecular;

vec4 SunlightDirection;

float constantAttenuation, linearAttenuation, quadraticAttenuation;

float spotCutoff, spotExponent;

float EnableLighting;

float EnableSun;

float ExtraValue;

};

uniform vec4 SceneAmbient = vec4(0.2, 0.2, 0.2, 1.0);

//

// Whether Materials are enabled at all.

//

uniform float IfEnableTextures;

//

// If we are just simply drawing the skybox.

//

uniform float DrawingSkyBox;

uniform float EnableWirefram...

0 Kudos
4 Replies
ROBERT_U_Intel
Employee
546 Views

Hi Thomas

Could you provide me with a bit of information about your system?

What chipset/CPU

What graphics driver version

What OS

Thanks

Robert

0 Kudos
TMaso1
Beginner
546 Views

Hi,

I am using Windows 8.1 Pro on my laptop.

Here are my Graphics Card specs as my system reports them:

Vendor: Intel

Renderer: Intel(R) HD Graphics 3000

Version: 3.1.0 - Build 9.17.10.3347

GLSL: 1.40 - Intel Build 9.17.10.3347

0 Kudos
TMaso1
Beginner
546 Views

Any ideas about this?

There are a lot of Intel boards out there that I would like to have my project work for.

Thanks.

0 Kudos
ROBERT_U_Intel
Employee
546 Views

Hi Thomas

Can you send a test application to reproduce the issue (executable and/or source code).

If is not possible, a captured stream with https://github.com/apitrace/apitrace apitrace will be very helpful.

Also, do you see the issue with either Nvidia or ATI graphics adapters?

Thanks

Robert

0 Kudos
Reply