question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to draw a cutout or hole on the camera's opengl view?

See original GitHub issue

I need to crop the camera view to a circle shape.

I tried using a surface view and overriding dispatchDraw(Canvas c), which worked. However it does not work when placed on top of another surface view.

So now I am using an OpenGlView and trying to figure out how to crop the display to a circle?

I assume this should be done with a filter, but I have no idea where to start.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pedroSG94commented, Feb 1, 2019

I assume that only preview should be circular right? In this case you need modify so much the library because filters are developed to apply in stream result and preview. I will try do a demo but I will not support it in the future.

0reactions
troy-lamertoncommented, Feb 18, 2019

I found a way to do it by setting the outline of the camera view to a circle. Turns out this method can be used to crop and view to a circle.

This is the Kotlin code I use to do it. Note that I crop a little extra to allow room for another view to show a border.

class CircleGlView : LightOpenGlView {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    private val circleOutlineProvider = object : ViewOutlineProvider() {
        override fun getOutline(view: View, outline: Outline) {
            view.run {
//                val borderWidth = (height * borderWidthPercent)
                val outlineSize = (height * CIRCLE_PERCENT).toInt()
                val left = (width - outlineSize) / 2
                val top = (height - outlineSize) / 2

                outline.setOval(
                    left, top,
                    left + outlineSize, top + outlineSize
                )
            }
        }
    }

    init {
        outlineProvider = circleOutlineProvider
        clipToOutline = true
    }

    override fun getOutlineProvider(): ViewOutlineProvider {
        debug("getOutlineProvider !")
        return circleOutlineProvider
    }

    companion object {
        const val BORDER_PERCENT = 0.07f
        const val CIRCLE_PERCENT = 1f - BORDER_PERCENT
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenGL Camera Tutorial - YouTube
OpenGL tutorial on writing a camera.
Read more >
Camera - LearnOpenGL
Subtracting the camera position vector from the scene's origin vector thus results in the direction vector we want. For the view matrix's coordinate...
Read more >
Support display cutouts - Android Developers
A display cutout is an area on some devices that extends into the display surface. It allows for an edge-to-edge experience while providing ......
Read more >
Animate 3 Online Help: OpenGL Drawing Preferences
You can change the value of the Full Scene Antialiasing using the Preferences dialog box, to fit the current level used in the...
Read more >
How to convert a screen coordinate into a translation for a ...
2 Answers 2 · Set up main scene (model/view/projection matrices, etc.) · Use glScissor to set the rectangle for the overlay. · Call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found