我正在尝试使用NDK在Android中创建一个简单的动画,不幸的是我的代码抛出了Exception
。
A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 16198 (GLThread 739)
我不知道这个错误是什么意思。
这些是我的一些代码。
Coin.cpp
void Coin::Render(GLuint textureId, GLuint positionHandle, GLuint texCoord, GLint matrixHandle, GLint samplerLoc) {
// Bind the texture to this unit
glBindTexture(GL_TEXTURE_2D, textureId);
// Enable generic vertex attribute array
glEnableVertexAttribArray(positionHandle);
glEnableVertexAttribArray(texCoord);
// Prepare the triangle coordinate data
glVertexAttribPointer(positionHandle, 3, GL_FLOAT, GL_FALSE, 0, mVertices);
// Prepare the texture coordinates
glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 0, mUVCoord);
// Apply the projection and view transformation
glUniformMatrix4fv(matrixHandle, 1, GL_FALSE, 0);
// Set the sampler texture unit to 0, where we have saved the texture
glUniform1i(samplerLoc, 0);
// Draw the triangle
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, mIndices);
// Disable vertex array
glDisableVertexAttribArray(positionHandle);
glDisableVertexAttribArray(texCoord);
}
这就是我声明我的数组Coin
对象的方式,我认为它看起来像Java中的ArrayList<Coin>
。
CoinRenderer.h
vector<Coin> mCoinCollection;
这是错误抛出的部分,代码在没有coin.Render()
或coin.translate()
的情况下工作,但是如果我取消注释这段代码代码不起作用。
CoinRenderer.cpp
void CoinRenderer::drawCoin() {
// glUseProgram(programHandle);
glUseProgram(imageHandle);
long slowTime = GLUtils::currentTimeMillis() % 100000L;
int elapse = (int) ((0.01f) * (slowTime));
for (int i = 0; i < mCoinCollection.size(); i++) {
Coin coin = mCoinCollection[i];
if (mCurrentTime < elapse) {
//int nextCoinFace = coin.getNextCoinFace();
//coin.setTextureId(textures[i]);
// This is also the line where the error is throwing.
//coin.translate(0.0f, 0.20f);
}
// This line where the error is throwing.
coin.Render(coin.getTextureId(), mPositionHandle, mTexCoord, mMatrixHandle, mSamplerLoc);
}
if (!hasVisibleCoin()) {
LOGD("All coins y < 0");
glDeleteProgram(imageHandle);
}
mCurrentTime = elapse;
}
如果需要,这里有一些额外的代码。
CoinRenderer.kt
class CoinRenderer(private val context: Context) : GLSurfaceView.Renderer {
override fun onDrawFrame(gl: GL10?) {
nativeDrawFrame()
}
override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
nativeSurfaceChange(width, height)
}
override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
val assetManager = context.assets
nativeSurfaceCreate(assetManager)
}
external fun nativeSurfaceCreate(assetManager: AssetManager)
external fun nativeSurfaceChange(width: Int, height: Int)
external fun nativeDrawFrame()
external fun drawNewCoin(coinSize: Int)
external fun nativeClearSurface(isClearSurface: Boolean)
companion object {
init {
System.loadLibrary("coin-lib")
}
}
}
CoinGL.kt
class CoinGL(context: Context) : GLSurfaceView(context) {
private var mRenderer : CoinRenderer? = null
init {
// Set openGL version
setEGLContextClientVersion(2)
setZOrderOnTop(true)
setEGLConfigChooser(8, 8, 8, 8, 16, 0)
holder.setFormat(PixelFormat.RGBA_8888)
mRenderer = CoinRenderer(context)
setRenderer(mRenderer)
renderMode = RENDERMODE_CONTINUOUSLY
}
fun drawNewCoin(coinSize: Int) {
queueEvent ({
mRenderer?.drawNewCoin(coinSize)
mRenderer?.nativeClearSurface(false)
})
}
}
PS:我只是Android OPENGL NDK或C ++中的新手,如果您发现或看到一些非常错误的声明或善意使用请向我解释。谢谢。
任何帮助将不胜感激。谢谢。
将vector<Coin> mCoinCollection;
改为vector<Coin*> mCoinCollection;