# Multiple viewports with modern OpenGL?

I use SDL2.

Currently my only shader has a MVP matrix, and transforms the points with it.

I calculate my View and Projection matrix for a camera with GLM:

glm::lookAt(pos, pos + forward, up);
glm::perspective(fov, aspectRatio, zNear, zFar);


I searched it, but I can only find legacy implementation of multiple viewports.

If I create for example 4 cameras and let's say that they all the same, except every camera has a different quarter of the screen to render. (So I want 4 viewports with the same content)

How can I do this? Sorry if I didn't give enough informations, feel free to ask.

• Are you specifically looking to perform the rendering 4 times, or just to render once and display the result in 4 different places? – trichoplax Jan 11 '17 at 12:50
• Perform the rendering 4 times. What I wrote is just a simplified example, for example I want the main camera to render a car at the full window, and another camera render the car from the side in a small square in one of the corners. – Tudvari Jan 11 '17 at 12:59
• If I understood this correctly so you need to split your window to for example 4 parts and in each part render other parts of the scene like that? – narthex Jan 11 '17 at 14:15
• Yes, but not strictly 4 parts. I want it to be flexible. Flexible size and position of viewport rectangle. – Tudvari Jan 11 '17 at 14:38
• – Ciro Santilli 新疆改造中心996ICU六四事件 Feb 21 '18 at 22:50

Rendering to different viewports (parts) of the same screen can be done as follows:

For example splitting screen into four parts and rendering the same scene four times to each corner with different uniforms and different viewports:

bindFramebuffer();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
scene->setConstPerFrameUniforms();

//left bottom
glViewport(0, 0, WindowWidth*0.5, WindowHeight*0.5);
scene->setVarPerFrameUniforms(1);
scene->draw();

//right bottom
glViewport(WindowWidth*0.5, 0, WindowWidth*0.5, WindowHeight*0.5);
scene->setVarPerFrameUniforms(2);
scene->draw();

//left top
glViewport(0, WindowHeight*0.5, WindowWidth*0.5, WindowHeight*0.5);
scene->setVarPerFrameUniforms(3);
scene->draw();

//right top
glViewport(WindowWidth*0.5, WindowHeight*0.5, WindowWidth*0.5, WindowHeight*0.5);
scene->setVarPerFrameUniforms(4);
scene->draw();

glViewport(0, 0, WindowWidth, WindowHeight); //restore default

• So with glViewPort() I can define where I want to draw next, and with glBlendFunc I can define how the GPU should blend the overlapping areas (framebuffers) with each other. Am I correct? – Tudvari Jan 11 '17 at 15:30
• Yes, you are free to parametrize that. As parameters of viewport are x, y of left bottom corner of chunk and width, height of chunk. With blending you can experiment. – narthex Jan 11 '17 at 15:36
• why blending? how is it related to the question ? – Raxvan Jan 11 '17 at 15:38
• You don't need blending or multiple framebuffers. Rendering will not write to any pixels outside the current glViewport rectangle, so you can just set up and draw each viewport in turn. BTW you can also use the scissor test to restrict clears to a certain rectangle, in case you want overlapping viewports. – Nathan Reed Jan 12 '17 at 1:02
• Blending has nothing to do with this and makes the answer more confusing. The setting up of the viewports is all that is needed – pauld Jan 12 '17 at 15:34

If you are writing your own Vertex/Fragment Shader there is another additional possibility to do this. It is much more complicated but might be useful for you and/or others. Additionally it speeds up the whole drawing process since it uses only one call to a draw command. The maximum number of Viewports is defined by GL_MAX_VIEWPORTS and is usually 16.

Since OpenGL 4.1 the method glViewportArrayv exists. It can define an array of Viewports. The Viewports which are created by this method have an index assigned.

This index can be used in the Vertex Shader to set the Viewport to which the scene is rendered. (You have to include the "ARB_shader_viewport_layer_array" extension in the shader code)

In your case I would suggest to do the following:

• Store the 4 camera matrices in a Shader Storage Buffer (Array of mat4) to have them in the Vertex Shader
• Use indexed drawing, for example :glDrawElementsInstanced
• use the build in gl_InstanceID of the Vertex Shader to access the camera matrix array
• set the build in variable output variable gl_ViewportIndex in the Fragment Shader to the gl_InstanceID. (for details see ARB_fragment_layer_viewport)
• "This index can be used in the Vertex Shader to set the Viewport to which the scene is rendered." No, it cannot. Well, not without the ARB_shader_viewport_layer_array extension, or the AMD equivalents. None of those are standard in GL 4.5. You are perhaps thinking of Geometry Shaders. – Nicol Bolas Jan 13 '17 at 14:38
• @Nicol, Thanks for this hint! I forgot to mention, that you have to include the extension. I will edit my answer. – Christian_B Jan 16 '17 at 7:26

This is a copy of @narthex's answer, except with only the viewports since that is all you need. I'm not sure why the frame buffer / blend stuff is included in his answer.

//left bottom
glViewport(0, 0, WindowWidth*0.5, WindowHeight*0.5);
scene->draw();

//right bottom
glViewport(WindowWidth*0.5, 0, WindowWidth*0.5, WindowHeight*0.5);
scene->draw();

//left top
glViewport(0, WindowHeight*0.5, WindowWidth*0.5, WindowHeight*0.5);
scene->draw();

//right top
glViewport(WindowWidth*0.5, WindowHeight*0.5, WindowWidth*0.5, WindowHeight*0.5);
scene->draw();

glViewport(0, 0, WindowWidth, WindowHeight); //restore default