-1

I'm trying to implement 2d transformation using opengl. so i created a matrix multiplication function. and separate functions for translation,rotation and scaling which call the matm,ie.matrix multiplication function. I'm creating a matrix of my vertices and passing them top the functions. I'm passing double pointers. But I'm getting segmentation fault.

#include<GL/glut.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
#define PI 3.14159265

void matm(int** a,int** b)
{
   int i,j,k;
   if(b[0][0]==100)
      glFlush();
   sleep(3);
   int **c=(int**)malloc(3*sizeof(int*));
   for(i=0;i<3;i++)
   {
      c[i]=(int*)malloc(sizeof(int));
      k=0;
      for(j=0;j<3;j++)
      {
         c[i][0]+=a[i][j]*b[k++][0];
      }
   }
   b=c;
}

void translation(int** a,int tx,int ty)
{
   int t[3][3]={{1,0,tx}, {0,1,ty},{0,0,1}};
   if(a[0][0]==100)
      sleep(3);
   matm(t,a);
}

void scaling(int** a,int sx,int sy)
{
   int t[3][3]={{sx,0,0}, {0,sy,0},{0,0,1}};
   matm(t,a);
}

void rotation(int** a,int t)
{
   int si,co;
   int val = PI / 180;
   si=sin(t*val);
   co=cos(t*val);

   int ti[3][3]={{co,-si,0}, {si,co,0},{0,0,1}};
   matm(ti,a);
}

void myinit()
{
   glClearColor(1.0,1.0,1.0,0.0);
   gluOrtho2D(0,600,0,400);
}

void mydisplay()
{
   {
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(1.0,0.0,0.0);
      glBegin(GL_POLYGON);
      glVertex2i(100,10);
      glVertex2i(300,10);
      glVertex2i(300,200);
      glVertex2i(100,200);
      glEnd();

      int x1[3][1]={{100},{10},{1}};
      int x2[3][1]={{300},{10},{1}};
      int x3[3][1]={{300},{200},{1}};
      int x4[3][1]={{100},{200},{1}};
      glClear(GL_COLOR_BUFFER_BIT);
      translation(x1,5,5);
      translation(x2,5,5);
      translation(x3,5,5);
      translation(x4,5,5);
      int a1,a2,a3,a4,b1,b2,b3,b4;
      a1=x1[0][0];
      b1=x1[1][0];
      a2=x2[0][0];
      b2=x2[1][0];
      a3=x3[0][0];
      b3=x3[1][0];
      a4=x4[0][0];
      b4=x4[1][0];
      glBegin(GL_POLYGON);
      glVertex2i(a1,b1);
      glVertex2i(a2,b2);
      glVertex2i(a3,b3);
      glVertex2i(a4,b4);
      glEnd();
      glFlush();
   }
}

int main(int argc,char** args)
{
   glutInit(&argc,args);
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
   glutInitWindowSize(640,480);
   glutInitWindowPosition(0,0);
   glutCreateWindow("Hello");
   myinit();
   glutDisplayFunc(mydisplay);
   glutMainLoop();
   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c – Lundin Sep 25 '15 at 06:38

1 Answers1

1

You have lots of places in your code where you pass a 2D array to a function whose signature expects a type int**. I am surprised your compiler does not report those as errors.

An array declared as:

int a[3][3];

does not decay to type int**. It decays to type int (*)[3].

When I compile the following program:

void translation(int** a,int tx,int ty)
{
}

int main(int argc,char** args)
{
   int a[3][3];
   translation(a, 10, 2);

   return 0;
}

I get the following error message using gcc 4.8.4.

socc.cc: In function ‘int main(int, char**)’:
socc.cc:8:28: error: cannot convert ‘int (*)[3]’ to ‘int**’ for argument ‘1’ to ‘void translation(int**, int, int)’
        translation(a, 10, 2);
                            ^
make: *** [socc] Error 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270