Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Last active March 1, 2018 19:48
Show Gist options
  • Select an option

  • Save SohanChy/355d8be0478510ae89b107a4bd612ea9 to your computer and use it in GitHub Desktop.

Select an option

Save SohanChy/355d8be0478510ae89b107a4bd612ea9 to your computer and use it in GitHub Desktop.
scaling glut mat mul
#include <stdio.h>
#include<GL/gl.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;
int x[4],y[4];
int trans[2][1];
int scaleFactor;
int decision;
class threeCubeMatrix
{
public:
int me[3][3];
threeCubeMatrix(){};
threeCubeMatrix(int a1,int a2, int a3, int b1, int b2, int b3, int c1, int c2, int c3)
{
me[0][0]=a1;
me[0][1]=a2;
me[0][2]=a3;
me[1][0]=b1;
me[1][1]=b2;
me[1][2]=b3;
me[2][0]=c1;
me[2][1]=c2;
me[2][2]=c3;
}
void printMatrix(){
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<me[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
threeCubeMatrix operator+(const threeCubeMatrix &secondMatrix)
{
threeCubeMatrix temp;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
temp.me[i][j]= me[i][j] + secondMatrix.me[i][j];
}
}
return temp;
}
threeCubeMatrix operator*(const threeCubeMatrix &secondMatrix)
{
threeCubeMatrix temp(0,0,0,0,0,0,0,0,0);
int i,j,k;
for(i = 0; i < 3; ++i)
for(j = 0; j < 3; ++j)
for(k = 0; k < 3; ++k)
{
temp.me[i][j] += me[i][k] * secondMatrix.me[k][j];
}
return temp;
}
};
void drawQuadMatrix(int mat[][4]){
glBegin(GL_POLYGON);
for(int i = 0; i < 4; i++){
//std::cout<<mat[0][i]<<","<<mat[1][i]<<std::endl;
glVertex2i(mat[0][i], mat[1][i]);
}
glEnd();
}
void drawTrioMatrix(int mat[][3]){
glBegin(GL_POLYGON);
for(int i = 0; i < 3; i++){
//std::cout<<mat[0][i]<<","<<mat[1][i]<<std::endl;
glVertex2i(mat[0][i], mat[1][i]);
}
glEnd();
}
void translate_it(){
int mat[2][4] = {{x[0],x[1],x[2],x[3]},{y[0],y[1],y[2],y[3]}};
glColor3ub(150,0, 0);
drawQuadMatrix(mat);
for(int i = 0; i < 4; i++){
mat[0][i] = mat[0][i] + trans[0][0];
mat[1][i] = mat[1][i] + trans[1][0];
}
glColor3ub(0, 150, 0);
drawQuadMatrix(mat);
}
void scale_it(int mat[][4],int factMat[2][1]){
int original_cord[2][1] = {{mat[0][0]},{mat[1][0]}};
int trans_cord[2][1] = {{-mat[0][0]},{-mat[1][0]}};
translate_it();
for(int i = 0; i < 4; i++){
mat[0][i] = mat[0][i] * factMat[0][0];
mat[1][i] = mat[1][i] * factMat[1][0];
}
translate_it();
}
void scale_mat_mul(){
threeCubeMatrix a(1,0,x[0],0,1,y[0],0,0,1);
threeCubeMatrix b(scaleFactor,0,0,0,scaleFactor,0,0,0,1);
threeCubeMatrix c(1,0,-x[0],0,1,-y[0],0,0,1);
threeCubeMatrix res = b * c;
res = a * res;
threeCubeMatrix pos(x[0],x[1],x[2],y[0],y[1],y[2],1,1,1);
res = res * pos;
glColor3ub(0, 150, 0);
drawTrioMatrix(res.me);
glColor3ub(150, 0, 0);
drawTrioMatrix(pos.me);
}
void myDisplay(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPointSize(4.0);
glColor3ub(0, 150, 0);
if(decision == 1){
translate_it();
}
else {
scale_mat_mul();
}
glFlush ();
}
void myInit (void)
{
glClearColor(1, 3, 1, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (200, 150);
glutCreateWindow ("Hello World");
cout<<"1 for Translate, 2 for Scale, Any other to exit."<<endl;
cin>>decision;
cout<<"Enter the co-ordinates of vertex 1:";
cin>>x[0]>>y[0];
cout<<"Enter the co-ordinates of vertex 2:";
cin>>x[1]>>y[1];
cout<<"Enter the co-ordinates of vertex 3:";
cin>>x[2]>>y[2];
if(decision == 1){
cout<<"Enter the co-ordinates of vertex 4:";
cin>>x[3]>>y[3];
cout<<"Enter the Translation factor for x and y:"<<endl;
cin>>trans[0][0]>>trans[1][0];
}
else if(decision == 2){
cout<<"Enter the scaling factor:"<<endl;
cin>>scaleFactor;
}
else return 0;
glutDisplayFunc(myDisplay);
myInit ();
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment