每当我启用闪电,即gl.glEnable(GL10.GL_LIGHTING)时,我就会在android中获得分段错误(信号11)。当我评论这一行时,我能够查看解析.obj文件后得到的3d图像。这是代码:
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");
}
}
答案 0 :(得分:2)
您将该数据传递给glMaterial,而不是glColorPointer。请参阅glMaterial reference page (OpenGL ES).
我希望它能帮助