I want to display the texture in openGL-ES 1, but the code below just displays a black screen
我想在openGL ES 1中显示纹理,但下面的代码只显示黑屏
class MyGLRenderer(val context: Context) : GLSurfaceView.Renderer {
private var textureId = 0
private lateinit var bitmap: Bitmap
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.bumpy_bricks_public_domain)
val textures = IntArray(1)
gl?.glGenTextures(1,textures,0)
textureId = textures[0]
gl?.glClearColor(0.0f,0.0f,0.0f,1.0f)
gl?.glBindTexture(GL10.GL_TEXTURE_2D,textureId)
gl?.glEnableClientState(GL10.GL_VERTEX_ARRAY)
gl?.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY)
gl?.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST.toFloat())
gl?.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST.toFloat())
gl?.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_S,GL10.GL_CLAMP_TO_EDGE.toFloat())
gl?.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_WRAP_T,GL10.GL_CLAMP_TO_EDGE.toFloat())
GLUtils.texImage2D(GL10.GL_TEXTURE_2D,0,bitmap,0)
bitmap.recycle()
}
override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
}
override fun onDrawFrame(gl: GL10?) {
gl?.glClear(GL10.GL_COLOR_BUFFER_BIT)
gl?.glEnable(GL10.GL_TEXTURE_2D)
gl?.glBindTexture(GL10.GL_TEXTURE_2D, textureId)
gl?.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer)
gl?.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer)
gl?.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4)
gl?.glDisable(GL10.GL_TEXTURE_2D)
}
companion object{
private val vertexData =
floatArrayOf(
-1.0f,-1.0f,0.0f,
1.0f,-1.0f,0.0f,
-1.0f,1.0f,0.0f,
1.0f,1.0f,0.0f
)
private val textureData =
floatArrayOf(
0.0f,1.0f,
1.0f,1.0f,
0.0f,0.0f,
1.0f,0.0f,
)
private val vertexBuffer = ByteBuffer.allocateDirect(vertexData.size*4).order(ByteOrder.nativeOrder()).asFloatBuffer()
private val textureBuffer = ByteBuffer.allocateDirect(textureData.size*4).order(ByteOrder.nativeOrder()).asFloatBuffer()
}
init {
vertexBuffer.put(vertexData).position(0)
textureBuffer.put(textureData).position(0)
}
}
This is my renderer class
And this is my activity class, where do I initialize my renderer class
这是我的渲染器类,这是活动类,我在哪里初始化渲染器类
class MainActivity : AppCompatActivity() {
private lateinit var glSurfaceView:GLSurfaceView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
glSurfaceView = GLSurfaceView(this)
glSurfaceView.setRenderer(MyGLRenderer(applicationContext))
setContentView(glSurfaceView)
}
}
I forgot to specify the use of openGL in the manifest, but after I specified, nothing changed.
<uses-feature android:glEsVersion="0x00010000" android:required="true"/>
I can't figure out where the error is in this code.
I would also like to know the best way to display textures in OpenGl-ES 1
我忘记在清单中指定openGL的使用,但在指定后,什么都没有改变<使用功能android:glEsVersion=“0x00010000”android:required=“true”/>我不知道这个代码中的错误在哪里。我也想知道在OpenGl ES 1中显示纹理的最佳方式
更多回答
I don't know your exact issue, but I notice a bug. That init
block that fills your byte buffers would need to be moved inside the companion object
or else you are needlessly reloading your buffers for every instance of your renderer class. Also, GL10 and EglConfig are never null. You don't need to declare them as nullable. That will allow you to remove all those question marks.
我不知道你的确切问题,但我注意到一个bug。填充字节缓冲区的init块需要在伴随对象中移动,否则您将不必要地为渲染器类的每个实例重新加载缓冲区。此外,GL10和EglConfig从不为空。您不需要将它们声明为可为null。这将允许您删除所有这些问号。
我是一名优秀的程序员,十分优秀!