[Mesa-users] Unable to view off-screen rendered mesh using mesa_glsl

Brian Paul brianp at vmware.com
Mon Mar 7 21:40:24 UTC 2016


On 03/07/2016 12:13 AM, vikramnrao wrote:
> Hi Brian,
>
> Currently I am unable to view anything the render using glsl shader.
> I have tried rendering a simple triangle in immediate mode and it
> renders fine(off-screen).
> But not able to clear background color.
>
> My renderScene code for Immediate mode looks something like this:
>
> glClear(GL_COLOR_BUFFER_BIT);
> glClear(GL_DEPTH_BUFFER_BIT);
> glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

You should set the color before calling glClear().


>
> // Enable depth test
> glEnable(GL_DEPTH_TEST);
> // Accept fragment if it closer to the camera than the former one
> glDepthFunc(GL_LESS);
>
> // Cull triangles which normal is not towards the camera
> glEnable(GL_CULL_FACE);
>
> glBegin(GL_TRIANGLES);
> glColor3f(0.5f, 0.0f, 0.0f);
> glVertex3f(-1.0f, 0.0f, -1.0f);
> glColor3f(0.0f, 0.5f, 0.0f);
> glVertex3f(1.0f, 0.0f, -1.0f);
> glColor3f(0.0f, 0.0f, 0.5f);
> glVertex3f(0.0f, 1.0f, 0.0f);
> glEnd();
>
> glFinish();
> SaveScreenGrab("mesa_snapshot.tga");
>
>
> But same triangle using shader doesn't render at all. However the
> glClearColor clears the screen color correctly.
>
>
> Below is the bit of code that I took from (opengl.org
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__opengl.org&d=BQMFaQ&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=T0t4QG7chq2ZwJo6wilkFznRSFy-8uDKartPGbomVj8&m=QEdFbrBKV_Dbv7s3zEN_t8kUV5UYxjVym60nXIWrkDI&s=J_tgBAEFHUAblpGuLppBJyqZRh-n_F_7Jlv3_aK-DQo&e=>)
> which render the scene into a targa image.
>
> // Dark blue background
> glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
>
> // Enable depth test
> glEnable(GL_DEPTH_TEST);
> // Accept fragment if it closer to the camera than the former one
> glDepthFunc(GL_LESS);
>
> // Create and compile our GLSL program from the shaders
> GLuint programID = LoadShaders("camera.vertexshader",
> "camera.fragmentshader");
>
> // Get a handle for our "MVP" uniform
> GLuint MatrixID = glGetUniformLocation(programID, "MVP");

Is the program linked at this point?  MatrixID should be GLint and you 
should test that MatrixID != -1.


>
> // Get a handle for our buffers
> GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID,
> "vertexPosition_modelspace");
> GLuint vertexColorID = glGetAttribLocation(programID, "vertexColor");

GLint as well, and check for non-negative values.


>
> // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
> unit <-> 100 units
> glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
>
> // Camera matrix
> glm::mat4 View = glm::lookAt(
> glm::vec3(4, 3, -3), // Camera is at (4,3,-3), in World Space
> glm::vec3(0, 0, 0), // and looks at the origin
> glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
> );
> // Model matrix : an identity matrix (model will be at the origin)
> glm::mat4 Model = glm::mat4(1.0f);
> // Our ModelViewProjection : multiplication of our 3 matrices
> glm::mat4 MVP = Projection * View * Model; // Remember, matrix
> multiplication is the other way around
>
> GLuint vertexbuffer;
> glGenBuffers(1, &vertexbuffer);
> glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
> glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data),
> g_vertex_buffer_data, GL_STATIC_DRAW);
>
> GLuint colorbuffer;
> glGenBuffers(1, &colorbuffer);
> glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
> glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data),
> g_color_buffer_data, GL_STATIC_DRAW);
>
> // Clear the screen
> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
>
> // 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]);
>
> // 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 : colors
> glEnableVertexAttribArray(vertexColorID);
> glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
> glVertexAttribPointer(
> vertexColorID,               // The attribute we want to configure
> 3,                           // size
> GL_FLOAT,                    // type
> GL_FALSE,                    // normalized?
> 0,                           // stride
> (void*)0                     // array buffer offset
> );
>
> // Draw the triangleS !
> glDrawArrays(GL_TRIANGLES, 0, 12 * 3); // 12*3 indices starting at 0 ->
> 12 triangles
>
>
> glDisableVertexAttribArray(vertexPosition_modelspaceID);
> glDisableVertexAttribArray(vertexColorID);
>
> SaveScreenGrab("mesa_snapshot.tga");
>
> // Cleanup VBO and shader
> glDeleteBuffers(1, &vertexbuffer);
> glDeleteBuffers(1, &colorbuffer);
> glDeleteProgram(programID);
>
>
>
> *vertex shader code:*
>
> #version 120
>
> // Input vertex data, different for all executions of this shader.
> attribute vec3 vertexPosition_modelspace;
> attribute vec3 vertexColor;
>
> // Output data ; will be interpolated for each fragment.
> varying vec3 fragmentColor;
> // Values that stay constant for the whole mesh.
> uniform mat4 MVP;
>
> void main(){
>
> // Output position of the vertex, in clip space : MVP * position
> gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
>
> // The color of each vertex will be interpolated
> // to produce the color of each fragment
> fragmentColor = vertexColor;
> }
>
> *fragment shader code:*
> *
> *
> #version 120
> // Interpolated values from the vertex shaders
> varying vec3 fragmentColor;
>
> void main(){
>
> gl_FragColor = fragmentColor;
>
> }
>
>
>
> Please guide me if I am doing something wrong here.

Hard to say without running the program and debugging things (which I 
don't have time to do).  If the window is blank, it could be a 
transformation matrix mistake.  Maybe you could start with identity 
matrices for the model, view and projection and see if anything shows up.

Are you calling glGetError() to check for errors?  Also, 
glGetProgramInfoLog() and glGetShaderInfoLog() should be checked for 
shader compiler/linker errors.

-Brian



More information about the mesa-users mailing list