[Ti.Opengl] Weird crash when using lighting

You must Login before you can answer or comment on any questions.

Hi there.

I am trying to get started with the TI.OpenGL module.

I have hacked the provided example to show a sphere generated as a triangle strip.

I am now trying to enable lighting and pass in a normal array to glDrawArrays. However, it keeps crashing in the emulator (it just quits, no error message at all).

There is a section in the following code saying "UNCOMMENT TO MAKE CRASH". Works without that section, but crashes with.

Any ideas?

var window = Ti.UI.createWindow();
 
Ti.Opengl = require('Ti.OpenGL');
//Ti.API.info("module is => " + Ti.Opengl);
 
var opengl = Ti.Opengl.createView({
    version : 1,
    backgroundColor:"#aaf",
    top:0,
    left:0,
    bottom:0,
    right:0,
});
 
var verticies=[];
var normals=[];
create_sphere();
 
Ti.API.info(verticies.length);
Ti.API.info(normals.length);
 
var transY = 0.0;
 
var setup = function(view) {
 
    view.setCurrentContext();    
 
 
    // UNCOMMENT THE FOLLOWING FOR CRASH
    /*view.glEnable(Ti.Opengl.GL_LIGHTING); 
    var al = [0.2, 0.2, 0.2, 1.0];
    view.glLightModelfv(Ti.Opengl.GL_LIGHT_MODEL_AMBIENT, al);*/
 
    view.glMatrixMode(Ti.Opengl.GL_PROJECTION);
    view.glLoadIdentity();
 
    view.glMatrixMode(Ti.Opengl.GL_MODELVIEW);
    view.glLoadIdentity();
 
 
    view.glVertexPointer(3, Ti.Opengl.GL_FLOAT, 0, verticies);
    view.glNormalPointer(3, Ti.Opengl.GL_FLOAT, 0, normals);
 
    view.glEnableClientState(Ti.Opengl.GL_VERTEX_ARRAY);
    view.glEnableClientState(Ti.Opengl.GL_NORMAL_ARRAY);
}
 
var drawsquare = function(view) {
    view.setFrameBuffer();
    view.clear();
 
    view.glLoadIdentity();    
    view.glTranslatef(Math.sin(transY)/2.0, 0.0, 0.0);//, 0.0);
    view.glRotatef(transY*10, 0.0, 1.0, 1.0);
 
    transY += 0.075;
 
    view.glDrawArrays(Ti.Opengl.GL_TRIANGLE_STRIP, 0, verticies.length);
    view.presentFrameBuffer();
};
 
function doOpengl(e) {
    setup(opengl);
    setInterval(function(e){drawsquare(opengl);}, 33.33333);
}
 
window.add(opengl);
opengl.addEventListener('ready', doOpengl);
window.open();
 
function create_sphere(){
 
    var TWOPI = Math.PI*2;
    var PID2 = Math.PI/2;
    var cos = Math.cos;
    var sin = Math.sin; 
 
    var theta1,theta2,theta3;
 
    var i,j;
 
    var r=0.6;
    var n=15;
    var c = {x:0,y:0,z:0};
 
    var e = {x:0,y:0,z:0};
    var p = {x:0,y:0,z:0};
 
 
    var index=0;
    var data=[];
    var data_n=[];
 
    for (j=0;j<n/2;j++) {
      theta1 = j * TWOPI / n - PID2;
      theta2 = (j + 1) * TWOPI / n - PID2;
 
      //glBegin(GL_QUAD_STRIP);
      for (i=0;i<=n;i++) {
         theta3 = i * TWOPI / n;
 
         e.x = cos(theta2) * cos(theta3);
         e.y = sin(theta2);
         e.z = cos(theta2) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;
 
         //glNormal3f(e.x,e.y,e.z);
         //glTexCoord2f(i/(double)n,2*(j+1)/(double)n);
         //glVertex3f(p.x,p.y,p.z);
 
         data[index] = p.x;
         data[index+1] = p.y;
         data[index+2] = p.z;
 
         data_n[index] = e.x;
         data_n[index+1] = e.y;
         data_n[index+2] = e.z;
         index+=3;
 
         e.x = cos(theta1) * cos(theta3);
         e.y = sin(theta1);
         e.z = cos(theta1) * sin(theta3);
         p.x = c.x + r * e.x;
         p.y = c.y + r * e.y;
         p.z = c.z + r * e.z;
 
         //glNormal3f(e.x,e.y,e.z);
         //glTexCoord2f(i/(double)n,2*j/(double)n);
         //glVertex3f(p.x,p.y,p.z);
 
         data[index] = p.x;
         data[index+1] = p.y;
         data[index+2] = p.z;
 
         data_n[index] = e.x;
         data_n[index+1] = e.y;
         data_n[index+2] = e.z;
         index+=3;
      }
      //glEnd();
   }
   verticies = data;
   normals = data_n;
}

— asked 1 year ago by Al James
2 Comments
  • Hello,

    if you are using the module from the marketplace, you can get support if you send a mail with this issue to the support mail. Check it in this page.

    Best,

    Mauro

    — commented 1 year ago by Mauro Parra

  • Cheers. I will email them the issue.

    — commented 1 year ago by Al James

1 Answer

Sorry. Problem solved. Was calling glNormalPointer with the wrong number of parameters.

Should be:

view.glNormalPointer(3, Ti.Opengl.GL_FLOAT, 0, normals);

Odd that causes a complete crash of the simulator with no error though.

Closed.

Your Answer

This question has been locked and cannot accept new answers.