[Mesa-users] Blank screen with Mesa_offscreen render 11.1.2

vikramnrao vikramnrao at gmail.com
Thu Mar 24 06:45:27 UTC 2016


Hi,

Its been quite sometime that I am looking into Off screen rendering using
Mesa 11.1.2. :)

I was able to render the textured model using Mesa 7.5.1 but somehow I
couldn't manage lighting the scene there.

However I want my code to run using Mesa 11.1.2.
I am not able to see anything in the off-screen rendered targa image.
It just appears blank.

What is wrong in the following code ?

*******
Note:
*******

 1] I am able to create/initialize a mesa context.
 2] No compilation/linker error in shaders also.
 3] Have copied osmesa.dll and opengl32.dll in the application's debug
folder.


Draw()
{


// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//**********************//
//Initialize openGL   //
//**********************//
//enable back face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

// Enable blending
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

//***************************//
//Read the mesh data  //
//***************************//
readVertexData();
readNormalData();
readIndexData();
readUVData();
create_indexed_vertex_data();

//*********************************//
//Set model view projection matrix //
//*********************************//

//projection matrix
glm::mat4 Projection = glm::ortho(-150.0f, 150.0f, -150.0f, 150.0f,
-150.0f, 10000.0f);

//Viewing transform
glm::mat4 View = glm::lookAt(
glm::vec3(4.0f, 3.0f, -3.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.1f, 0.0f)
);

// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4();

// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection *View * Model;


//*********************************//
//Read the shaders                //
//*********************************//
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("simple_vertex.vertexshader",
"simple_fragment.fragmentshader");



//*********************************//
// Get a handle for our buffers //
//*********************************//
GLint vertexPosition_modelspaceID = glGetAttribLocation(programID,
"vertexPosition_modelspace");
GLuint vertexUVID = glGetAttribLocation(programID, "vertexUV");
GLuint normalID = glGetAttribLocation(programID, "normalv");
GLuint ModelMatrixID = glGetUniformLocation(programID, "Model");
GLuint ViewPositionID = glGetUniformLocation(programID, "viewPos");
// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
//Materials handler
GLint matAmbientLoc = glGetUniformLocation(programID, "material.ambient");
GLint matDiffuseLoc = glGetUniformLocation(programID, "material.diffuse");
GLint matSpecularLoc = glGetUniformLocation(programID, "material.specular");
GLint matShineLoc = glGetUniformLocation(programID, "material.shininess");
GLfloat matAlphaloc = glGetUniformLocation(programID, "matAlpha");

//Handle for directional light
GLuint dirLighDirID = glGetUniformLocation(programID, "dirLight.direction");
GLuint dirLighAmbID = glGetUniformLocation(programID, "dirLight.ambient");
GLuint dirLighDiffID = glGetUniformLocation(programID, "dirLight.diffuse");
GLuint dirLighSpecID = glGetUniformLocation(programID, "dirLight.specular");


//*********************************//
//Register the buffers            //
//********************************//
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, indexed_vertex_data.size() *
sizeof(glm::vec3), indexed_vertex_data.data(), GL_STATIC_DRAW);

GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, uv_data.size() * sizeof(glm::vec2),
uv_data.data(), GL_STATIC_DRAW);

GLuint normalbuffer;
glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, normal_data_point.size() * sizeof(glm::vec3),
normal_data_point.data(), GL_STATIC_DRAW);

// Use our shader
glUseProgram(programID);

// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

//model matrix values to vertex sahder
glUniformMatrix4fv(ModelMatrixID, 1, FALSE, glm::value_ptr(Model));
glUniformMatrix4fv(ViewPositionID, 1, FALSE, glm::value_ptr(View));


//***************************************//
//Material values to fragment shader
//***************************************//
GLfloat materialAlpha = 1.0;
glUniform1f(matAlphaloc, materialAlpha);
glUniform4f(matAmbientLoc, 0.9f, 0.25f, 0.21f, 1.0f);
glUniform4f(matDiffuseLoc, 0.9f, 0.14f, 0.14f, 1.0f);
glUniform4f(matSpecularLoc, 0.1f, 0.1f, 0.1f, 1.0f);
glUniform1f(matShineLoc, 16.0f);

//***************************************//
//Directional light values to fragment shader
//***************************************//
glUniform3f(dirLighDirID, -0.2f, -1.0f, -0.3f);
glUniform3f(dirLighAmbID, 0.5f, 0.5f, 0.5f);
glUniform3f(dirLighDiffID, 0.9f, 0.9f, 0.8f);
glUniform3f(dirLighSpecID, 1.0f, 1.0f, 1.0f);

// Load textures
GLuint diffuseMap;
GLuint TextureID = glGetUniformLocation(programID, "MeshTexture");

glGenTextures(1, &diffuseMap);
int width, height;
unsigned char* image;
// Diffuse map
image = SOIL_load_image("Fusion_result.png", &width, &height, 0,
SOIL_LOAD_RGBA);

if (image == NULL)
{
cout << "Texture file not found!! \n";
}
else
{

glBindTexture(GL_TEXTURE_2D, diffuseMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);


// Bind our texture in Texture Unit 0
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
glUniform1i(TextureID, 0);
}

// 1rst attribute buffer : vertices
glEnableVertexAttribArray(vertexPosition_modelspaceID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
vertexPosition_modelspaceID, // The attribute we want to configure
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);

// 2ND attribute buffer : UVs
glEnableVertexAttribArray(vertexUVID);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
vertexUVID,                   // The attribute we want to configure
2,                            // size : U+V => 2
GL_FLOAT,                     // type
GL_FALSE,                     // normalized?
0,                            // stride
(void*)0                      // array buffer offset
);

// 3rd attribute buffer : normals
glEnableVertexAttribArray(normalID);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
normalID, // The attribute we want to configure
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);


glGetError();
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, indexed_vertex_data.size());

SaveScreenGrab("mesa_snapshot.tga");


glDisableVertexAttribArray(vertexPosition_modelspaceID);

// Cleanup VBO
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &normalbuffer);
glDeleteBuffers(1, &uvbuffer);
glDeleteTextures(1, &diffuseMap);
glDeleteProgram(programID);

}

*************************
Shader code
*************************
--------------------
vertex shader:
---------------------

 attribute vec3 vertexPosition_modelspace;
 attribute vec2 vertexUV;
 attribute vec3 normalv;

 varying vec2 TexCoords;
 varying vec3 normalf;
 varying vec3 FragPos;

 uniform mat4 MVP;
 uniform mat4 Model;


 void main() {

  gl_Position = MVP * vec4(vertexPosition_modelspace,1);
    FragPos = vec3(Model * vec4(vertexPosition_modelspace,1));
    normalf = vec3(Model * vec4(normalv,1));
    TexCoords = vertexUV;

 }


------------------------
Fragment shader:
-------------------------
 #version 120

struct Material{
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
    float shininess;
};

struct DirLight {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};


in vec3 FragPos;
in vec3 normalf;
in vec2 TexCoords;
in vec3 vertexColor;
vec4 texel0;


uniform sampler2D MeshTexture;
uniform DirLight dirLight;

out vec4 ResultFragColor;

uniform vec3 viewPos;
uniform Material material;
uniform float matAlpha;

//Function prototype
vec4 SurfaceColor (in Material mat, in vec4 texel);

void main()
{

     texel0 = texture2D(MeshTexture, TexCoords);

    vec3 norm = normalize(normalf);
  vec3 viewDir = normalize(viewPos - FragPos);
  vec3 lightDir = normalize(-dirLight.direction);

    // Diffuse shading
    float diff = max(dot(norm, lightDir), 0.0);

    // Specular shading
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0),
material.shininess);

vec4 surfaceColorValue ;
surfaceColorValue = mix(material.diffuse, texel0, texel0.a);
surfaceColorValue.a =  min(matAlpha*( 1 + texel0.a) , 1);

vec4 ambient = vec4(dirLight.ambient, 1)  * surfaceColorValue ;
vec4 diffuse = vec4(dirLight.diffuse, 1) * diff * surfaceColorValue ;
vec4 specular = vec4(dirLight.specular, 1)  * spec  * material.specular ;

vec4 result = ambient + diffuse + specular ;

ResultFragColor = result;

}

vec4 SurfaceColor (in Material mat, in vec4 texel)
{
 vec4 SurfColor;

 SurfColor = mix(mat.diffuse, texel, texel.a);
 SurfColor.a =  min(matAlpha*( 1 + texel.a) , 1);
 return SurfColor;

}

}




Thanks & Regards,
Vikram
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-users/attachments/20160324/40e182fd/attachment-0001.html>


More information about the mesa-users mailing list