Professional Documents
Culture Documents
Properties
Origin does not necessarily map to origin
Lines map to lines which means lines remain parallel and
evenly spaced.
Parallel lines remain parallel
Ratios are preserved like this
Affine transformation mostly implemented by including homogenous coordinates
for uniform representations of transformation classes since we use linear
transformations(rotation, scaling and other) with translation done by appending
1 to the input vector and translation vector.
Homogenous can used in all transformation class to fit the picture into proper
position.
This allows you to load a relatively coarse mesh on your graphics card,
generate more vertices and triangles dynamically and then have a mesh on the
screen that looks much smoother
Since OpenGL accepts only convex polygons for rendering, these non-convex
polygons must be tessellated before rendering.
Types of Tessellations.
Regular Tessellations
REGULAR TESSELLATIONS:
RULE #1: The tessellation must tile a floor (that goes on forever) with no overlapping or
gaps.
RULE #2: The tiles must be regular polygons - and all the same.
RULE #3: Each vertex must look the same.
Semi-Regular Tessellations
Saves lots of memory and bandwidth, which allows an application to render higher
detailed surfaces from low-resolution models.
Supports scalable-rendering techniques, such as continuous or view dependent
levels-of-detail which can be calculated on the fly.
Improves performance by performing expensive computations at lower frequency
(doing calculations on a lower-detail model). This could include blending
calculations using blend shapes or morph targets for realistic animation or physics
calculations for collision detection or soft body dynamics.
#4 Parabola
1. y = a*x*x + b*x + c
Where y and x are floating point variables and a, b and c are constants. Now,
how to transfer this to the screen? By doing a bit of clever discrete
mathematics.
Let us assume we have a canvas to plot individual pixels on. Then the actual
plotting function would look like this:
#Ellipse
Ellipse Definition
An ellipse is the locus of points in a plane, the sum of whose distances from
two fixed points is a constant value.
The two fixed points are called the foci of the ellipse.
Ellipse Equation
There are two standard equations of the ellipse. These equations are based on the
transverse axis and the conjugate axis of each of the ellipse. The standard equation of
the ellipse x2a2+y2b2=1x2a2+y2b2=1 has the transverse axis as the x-axis and the
conjugate axis as the y-axis. Further, another standard equation of the ellipse
is x2b2+y2a2=1x2b2+y2a2=1 and it has the transverse axis as the y-axis and its
conjugate axis as the x-axis. The below image shows the two standard forms of
equations of an ellipse.
#Animating flag
#incluude<windows.h>
#include<gl/glut.h>
#include<math.h>
#define PI 3.1416
typedef struct point
{
GLfloat x, y, z;
};
void bino(int n, int *C)
{
int k, j;
for(k=0;k<=n;k++)
{
C[k]=1;
for(j=n;j>=k+1; j--)
C[k]*=j;
for(j=n-k;j>=2;j--)
C[k]/=j;
}
}
void computeBezPt(float u, point *pt1, int cPt, point *pt2, int *C)
{
int k, n=cPt-1;
float bFcn;
pt1 ->x =pt1 ->y = pt1->z=0.0;
for(k=0; k< cPt; k++)
{
bFcn = C[k] * pow(u, k) * pow( 1-u, n-k);
pt1 ->x += pt2[k].x * bFcn;
pt1 ->y += pt2[k].y * bFcn;
pt1 ->z += pt2[k].z * bFcn;
}
}
void bezier(point *pt1, int cPt, int bPt)
{
point bcPt;
float u;
int *C, k;
C= new int[cPt];
bino(cPt-1, C);
glBegin(GL_LINE_STRIP);
for(k=0; k<=bPt; k++)
{
u=float(k)/float(bPt);
computeBezPt(u, &bcPt, cPt, pt1, C);
glVertex2f(bcPt.x, bcPt.y);
}
glEnd();
delete[]C;
}
float theta = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int nCtrlPts = 4, nBCPts =20;
point ctrlPts[4] = {{100, 400, 0}, {150, 450, 0}, {250, 350, 0},
{300, 400, 0}};
ctrlPts[1].x +=50*sin(theta * PI/180.0);
ctrlPts[1].y +=25*sin(theta * PI/180.0);
ctrlPts[2].x -= 50*sin((theta+30) * PI/180.0);
ctrlPts[2].y -= 50*sin((theta+30) * PI/180.0);
ctrlPts[3].x -= 25*sin((theta) * PI/180.0);
ctrlPts[3].y += sin((theta-30) * PI/180.0);
theta+=0.2;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(5);
glPushMatrix();
glLineWidth(5);
glColor3f(1, 0.4, 0.2); //Indian flag: Orange color code for(int i=0;i<50;i++)
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(1, 1, 1); //Indian flag: white color code for(int i=0;i<50;i++)
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glColor3f(0, 1, 0); //Indian flag: green color code for(int i=0;i<50;i++)
for(int i=0;i<50;i++)
{
glTranslatef(0, -0.8, 0);
bezier(ctrlPts, nCtrlPts, nBCPts);
}
glPopMatrix();
glColor3f(0.7, 0.5,0.3);
glLineWidth(5);
glBegin(GL_LINES);
glVertex2f(100,400);
glVertex2f(100,40);
glEnd();
glutPostRedisplay();
glutSwapBuffers();
}
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500,500);
glutCreateWindow("Bezier Curve");
init();
glutDisplayFunc(display);
glutMainLoop();
#Rotating Cube
#include<gl/freeglut.h>
double rot = 0;
float matCol[] = {1,0,0,0};
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(30,1,1,0);
glRotatef(rot,0,1,1);
glMaterialfv(GL_FRONT,GL_DIFFUSE,matCol);
glutWireCube(1);
glPopMatrix();
glFlush();
}
void onIdle(){
rot += 0.1;
glutPostRedisplay();
}
void reshape(int w,int h){
float ar = (float) w / (float) h ;
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glTranslatef(0,0,-10);
glMatrixMode(GL_PROJECTION);
gluPerspective(70,(GLfloat)w/(GLfloat)h,1,12);
glLoadIdentity();
glFrustum ( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void init(){
float pos[] = {1,1,1,0};
float white[] = {1,1,1,0};
float shini[] = {70};
glClearColor(.5,.5,.5,0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0,GL_AMBIENT,white);
glLightfv(GL_LIGHT0,GL_DIFFUSE,white);
glMaterialfv(GL_FRONT,GL_SHININESS,shini);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
int main(int argC, char* argV[])
{
glutInit(&argC,argV);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,500);
glutCreateWindow("Rossetta's Rotating Cube");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(onIdle);
glutMainLoop();
return 0;
}
#Simple car
#ifdef
__APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <math.h>
GLint b=300;
float counter=600.0;
void initOpenGl()
{
glClearColor(0.5,0.5,0.5,1); //Background Color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,700,0,500);
glMatrixMode(GL_MODELVIEW);
}
void wheel(int x,int y)
{
float th;
glBegin(GL_POLYGON);
glColor3f(0,0,0);
for(int i=0;i<360;i++)
{
th=i*(3.1416/180);
glVertex2f(x+20*cos(th),y+20*sin(th));
}
glEnd();
}
void car()
{
//Bottom Part
glLoadIdentity();
counter=counter-0.03;
glTranslated(counter,100,0.0);
//glScaled(0.1,0.1,0.0);
glBegin(GL_POLYGON);
glVertex2f(100,100);
glVertex2f(100,160);
glVertex2f(450,160);
glVertex2f(450,100);
//Top Part
glBegin(GL_POLYGON);
glVertex2f(150,160);
glVertex2f(200,200);
glVertex2f(400,200);
glVertex2f(450,160);
glEnd();
wheel(200,100);
wheel(380,100);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
//Push and pop matrix for separating circle object from Background
glColor3f(0.0,1.0,0.0);
car();
glutSwapBuffers();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|
GLUT_DEPTH);
glutInitWindowSize(700,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Car Moving");
initOpenGl();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}