Android 启用lightning gl.glEnable(GL10.Glu LIGHTING)时出现分段故障;
每当我启用照明(例如GL10.GLU照明)时,android中就会出现分段故障(信号11)。当我对这行进行注释时,我能够查看解析.obj文件后得到的3d图像。代码如下:Android 启用lightning gl.glEnable(GL10.Glu LIGHTING)时出现分段故障;,android,Android,每当我启用照明(例如GL10.GLU照明)时,android中就会出现分段故障(信号11)。当我对这行进行注释时,我能够查看解析.obj文件后得到的3d图像。代码如下: public class GlRenderer implements Renderer { private square square; // the square private Context context; private float ambientMaterial[] = {
public class GlRenderer implements Renderer {
private square square; // the square
private Context context;
private float ambientMaterial[] = { 0.5f, 0.5f, 0.5f, 1.0f };
private float diffuseMaterial[] = { 1.0f, 1.0f, 1.0f, 1.0f };
private float light_position[] = { 0.0f, 0.0f, 2.0f, 1.0f };
private FloatBuffer ambientMaterialbfr;
private FloatBuffer diffuseMaterialbfr;
private FloatBuffer light_positionbfr;
/** Constructor to set the handed over context */
public GlRenderer(Context context) {
Log.d("INFO","GlRenderer:Constructor");
this.context = context;
// initialise the square
this.square = new square();
}
@Override
public void onDrawFrame(GL10 gl) {
//Log.d("INFO","GLRender:onDrawFrame");
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
//Commented By Rakesh
// gl.glTranslatef(1.0f, 1.0f, 0.0f); // move 5 units INTO the screen
// is the same as moving the camera 5 units away
square.draw(gl); // Draw the triangle
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
Log.d("INFO","GLRender:onSurfaceChanged");
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}
gl.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// Log.d("INFO","Ratio is: "+ratio);
gl.glMatrixMode(GL10.GL_PROJECTION); // set matrix to projection mode
gl.glLoadIdentity(); // reset the matrix to its default state
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); // apply the projection matrix
//gl.glFrustumf(left, right, bottom, top, zNear, zFar)
// GLU.gluPerspective(gl, 45.0f, ratio, 0.1f, 100.0f);
// gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
//gl.glLoadIdentity(); //Reset The Modelview Matrix
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
square.loadGLTexture(gl, this.context);
Log.d("INFO","GLRender:onSurfaceCreated");
gl.glEnable(GL10.GL_TEXTURE_2D);
//Enable Texture Mapping ( NEW )
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.5f, 0.4f, .5f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//Lightning
Log.d("INFO","GLRender:onSurfaceChanged:Before Lighting");
ByteBuffer abb = ByteBuffer.allocateDirect(ambientMaterial.length * 4 );
abb.order(ByteOrder.nativeOrder());
ambientMaterialbfr = abb.asFloatBuffer();
ByteBuffer dbb = ByteBuffer.allocateDirect(diffuseMaterial.length * 4 );
dbb.order(ByteOrder.nativeOrder());
diffuseMaterialbfr = dbb.asFloatBuffer();
ByteBuffer lbb = ByteBuffer.allocateDirect(light_position.length * 4 );
lbb.order(ByteOrder.nativeOrder());
light_positionbfr = lbb.asFloatBuffer();
ambientMaterialbfr = FloatBuffer.wrap(ambientMaterial);
diffuseMaterialbfr = FloatBuffer.wrap(diffuseMaterial);
light_positionbfr = FloatBuffer.wrap(light_position);
for (int j = 0; j < ambientMaterial.length; j++) {
ambientMaterialbfr.put(ambientMaterial[j]);
}
for (int j = 0; j < diffuseMaterial.length; j++) {
diffuseMaterialbfr.put(diffuseMaterial[j]);
}
for (int j = 0; j < light_position.length; j++) {
light_positionbfr.put(light_position[j]);
}
ambientMaterialbfr.position(0);
diffuseMaterialbfr.position(0);
light_positionbfr.position(0);
Log.d("INFO","Size of ambientMaterial:"+ambientMaterial.length);
Log.d("INFO","Size of diffuseMaterial:"+diffuseMaterial.length);
Log.d("INFO","Size of light_position:"+light_position.length);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientMaterialbfr);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseMaterialbfr);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, light_positionbfr);
Log.d("INFO","GLRender:onSurfaceChanged:After glLightfv");
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT1);
Log.d("INFO","GLRender:onSurfaceChanged:After COLOR_MATERIAL");
gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT,
ambientMaterialbfr);
gl.glMaterialfv(GL10.GL_FRONT,
GL10.GL_DIFFUSE, diffuseMaterialbfr);
gl.glMaterialfv(GL10.GL_FRONT,
GL10.GL_POSITION, light_positionbfr);
Log.d("INFO","GLRender:onSurfaceChanged:After MATERIAL");
}
}
public类GlRenderer实现渲染器{
私人广场;//广场
私人语境;
私有浮动环境材质[]={0.5f,0.5f,0.5f,1.0f};
专用浮动扩散材质[]={1.0f,1.0f,1.0f,1.0f};
专用浮球灯位置[]={0.0f、0.0f、2.0f、1.0f};
私有浮动缓冲环境材料BFR;
专用浮动缓冲扩散材料BFR;
专用浮子缓冲灯位置bfr;
/**构造函数来设置移交的上下文*/
公共GlRenderer(上下文){
Log.d(“INFO”,“GlRenderer:Constructor”);
this.context=上下文;
//初始化正方形
this.square=新的square();
}
@凌驾
公共框架(GL10 gl){
//Log.d(“INFO”,“GLRender:onDrawFrame”);
gl.glClear(GL10.gl_颜色_缓冲_位| GL10.gl_深度_缓冲_位);
gl.glMatrixMode(GL10.gl_投影);
gl.glLoadIdentity();//将矩阵重置为其默认状态
gl.glEnableClientState(GL10.gl_顶点数组);
gl.glEnableClientState(GL10.gl_纹理_坐标_数组);
gl.glEnableClientState(GL10.gl\u NORMAL\u数组);
//Rakesh评论
//gl.glTranslatef(1.0f,1.0f,0.0f);//在屏幕中移动5个单位
//与将相机移动5个单位相同
正方形。画(gl);//画三角形
}
@凌驾
表面上的公共空隙已更改(GL10 gl,整型宽度,整型高度){
Log.d(“INFO”,“GLRender:onSurfaceChanged”);
如果(高度==0){//防止被零除以
高度=1;//使高度等于1
}
gl.glViewport(0,0,宽度,高度);
浮动比率=(浮动)宽度/高度;
//Log.d(“信息”,“比率为:”+比率);
gl.glMatrixMode(GL10.gl_投影);//将矩阵设置为投影模式
gl.glLoadIdentity();//将矩阵重置为其默认状态
gl.glFrustumf(-ratio,ratio,-1,1,1,10);//应用投影矩阵
//gl.GLFRUSTOMF(左、右、下、上、Z轴、zFar)
//GLU.GLU(gl,45.0f,比率,0.1f,100.0f);
//gl.glMatrixMode(GL10.gl_MODELVIEW);//选择MODELVIEW矩阵
//gl.glLoadIdentity();//重置Modelview矩阵
}
@凌驾
已创建曲面上的公共void(GL10 gl、EGLConfig配置){
square.loadGLTexture(gl,this.context);
Log.d(“INFO”,“GLRender:onSurfaceCreated”);
glEnable(GL10.Glu纹理2D);
//启用纹理映射(新)
gl.glShadeModel(GL10.gl_SMOOTH);//启用平滑着色
gl.glClearColor(0.5f,0.4f,.5f,0.5f);//黑色背景
gl.glClearDepthf(1.0f);//深度缓冲区设置
gl.glEnable(GL10.gl_DEPTH_TEST);//启用深度测试
gl.glDepthFunc(GL10.gl_LEQUAL);//要执行的深度测试类型
//非常好的透视计算
gl.glHint(GL10.gl\u透视图\u校正\u提示,GL10.gl\u最佳);
//闪电
Log.d(“信息”,“GLRender:onSurfaceChanged:Before Lighting”);
ByteBuffer abb=ByteBuffer.allocateDirect(ambientMaterial.length*4);
abb.order(ByteOrder.nativeOrder());
ambientMaterialbfr=abb.asFloatBuffer();
ByteBuffer dbb=ByteBuffer.allocateDirect(DiffuseMeterial.length*4);
dbb.order(ByteOrder.nativeOrder());
diffuseMeterialBFR=dbb.asFloatBuffer();
ByteBuffer lbb=ByteBuffer.allocateDirect(灯光位置长度*4);
lbb.order(ByteOrder.nativeOrder());
灯光位置bfr=lbb.asFloatBuffer();
ambientMaterialbfr=FloatBuffer.wrap(ambientMaterial);
diffuseMaterialbfr=浮动缓冲区。包裹(diffuseMaterial);
灯位置bfr=浮动缓冲区包裹(灯位置);
对于(int j=0;j