본문 바로가기
Andorid Kotlin

GLSurfaceView 배경 투명으로 하는 법

by SimonLee 2024. 10. 5.

1>  glSurface view 설정 한다.

setZorderOnTop, setEGLConfigChooser, getHolder().setFormat 을 설정한다.

val binding by lazy { ActivityShapeBinding.inflate(layoutInflater) }

with (binding) {
    glSurfaceView.setEGLContextClientVersion(2)
    glSurfaceView.setZOrderOnTop(true);
    glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0)
    glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888)
    glSurfaceView.setRenderer(this@ShapeActivity)
    glSurfaceView.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY
}

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_shape"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".ShapeActivity">

	.....
    
    <android.opengl.GLSurfaceView
        android:id="@+id/glSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_marginTop="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

2> glClear할때 마지막 알파값을 0으로 해준다.

override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
	GLES31.glClearColor(0f, 0f, 0f, 0f)
    ...
}

override fun onDrawFrame(p0: GL10?) {
	GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT)
    ....
}

 

3>frag shader에서 alpha 값을 설정한다.

void main(void) {
     ....
     FragColor = vec4(col, 0.);
}