#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
typedef struct{float x;float y;}Point2D;
float dx = 200, dy = 200, dz = 200;
/* Vertex indices for the 6 faces of a cube. */
GLint faces[6][4] = {
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
};
GLfloat v[8][3];
void init(void)
{
v[0][0] = v[1][0] = v[2][0] = v[3][0] = 100;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 300;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = 100;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 300;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 300;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = 100;
}
Point2D Project_ortho(float x, float y, float z){
Point2D p0;
p0.x = x ;
p0.y = y ;
return p0;
}
void drawBox(void)
{
Point2D p0,p1,p2,p3;
for (int f = 0; f < 6; f++) {
p0 = Project_ortho(v[ faces[f][0] ][0],v[ faces[f][0] ][1],v[ faces[f][0] ][2]);
p1 = Project_ortho(v[ faces[f][1] ][0],v[ faces[f][1] ][1],v[ faces[f][1] ][2]);
p2 = Project_ortho(v[ faces[f][2] ][0],v[ faces[f][2] ][1],v[ faces[f][2] ][2]);
p3 = Project_ortho(v[ faces[f][3] ][0],v[ faces[f][3] ][1],v[ faces[f][3] ][2]);
glBegin(GL_LINE_LOOP);
glVertex2f(p0.x,p0.y);
glVertex2f(p1.x,p1.y);
glVertex2f(p2.x,p2.y);
glVertex2f(p3.x,p3.y);
glEnd();
glFlush();
}
}
void translate(float* x, float* y, float* z, float dx, float dy, float dz){
float tx = *x, ty=*y, tz=*z;
*x = tx + dx;
*y = ty + dy;
*z = tz + dz;
}
void rotatex(float* x, float* y, float* z, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
float tx = *x, ty=*y, tz=*z;
*y = ty * cos(ang) - tz * sin(ang);
*z = ty * sin(ang) + tz * cos(ang);
*x = tx;
}
void rotatey(float* x, float* y, float* z, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
float tx = *x, ty=*y, tz=*z;
*x = tx * cos(ang) + tz * sin(ang);
*z = -tx * sin(ang) + tz * cos(ang);
*y = ty;
}
void rotatebox_x(float deg_x){
for (int i = 0; i < 8; i++) {
translate(&v[i][0],&v[i][1],&v[i][2], -dx,-dy,-dz);
rotatex(&v[i][0],&v[i][1],&v[i][2], deg_x);
translate(&v[i][0],&v[i][1],&v[i][2], dx,dy,dz);
}
glutPostRedisplay();
}
void rotatebox_y(float deg_y){
for (int i = 0; i < 8; i++) {
translate(&v[i][0],&v[i][1],&v[i][2], -dx,-dy,-dz);
rotatey(&v[i][0],&v[i][1],&v[i][2], deg_y);
translate(&v[i][0],&v[i][1],&v[i][2], dx,dy,dz);
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
}
void special(int key, int, int) {
switch (key) {
case GLUT_KEY_LEFT: rotatebox_y(-1); break;
case GLUT_KEY_RIGHT: rotatebox_y(1);break;
case GLUT_KEY_UP: rotatebox_x(-1);break;
case GLUT_KEY_DOWN: rotatebox_x(1);break;
default: return;
}
}
void myDisplay()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
drawBox();
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "3D Rotation Cube" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts
Thursday, July 24, 2014
Opengl,C++ : 3D Rotation Cube
1. Define the above cube using two separate lists: a vertex list and a face list.
2. Rotate the given cube using the arrow keys.
Opengl,C++ : Standard perspective projection
1. Project the cube on the XY plane with the COP is at (0,0,-200) and moves to the centre of the screen [i.e., move the system to (WinWidth/2, WinHeight/2)]
2. 3D Translation to project the cube with COP is at (100,100,-200).
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int WinWidth = 500, WinHeight = 500;
typedef struct{float x;float y;}Point2D;
typedef struct{float x;float y;float z;}Point3D;
Point3D COP = {0,0,-200}; // Centre Of Projection is at (0,0,-200)
Point3D COP1 = {100,100,-200};
Point3D A,B,C,D,E,F,G,H; // Vertices of the cube
void InitCube(){
A.x = 0; A.y = 0; A.z = 0; // A(0,0,0)
B.x = 200; B.y = 0; B.z = 0; // B(200,0,0)
C.x = 200; C.y = 200; C.z = 0; // C(200,200,0)
D.x = 0; D.y = 200; D.z = 0; // D(0,200,0)
E.x = 0; E.y = 200; E.z = 200; // E(0,200,200)
F.x = 0; F.y = 0; F.z = 200; // F(0,0,200)
G.x = 200; G.y = 0; G.z = 200; // G(200,0,200)
H.x = 200; H.y = 200; H.z = 200; // H(200,200,200)
}
void DrawCube(Point2D A, Point2D B, Point2D C, Point2D D, Point2D E, Point2D F, Point2D G, Point2D H){
glPointSize(1.0);
glBegin(GL_LINE_LOOP);
//DRAWING FRONT FACE
glVertex2i(A.x, A.y); glVertex2i(B.x, B.y); glVertex2i(C.x, C.y); glVertex2i(D.x, D.y);
glEnd();
glBegin(GL_LINE_LOOP);
//DRAWING BACK FACE
glVertex2i(E.x, E.y); glVertex2i(F.x, F.y); glVertex2i(G.x, G.y); glVertex2i(H.x, H.y);
glEnd();
glBegin(GL_LINES);
//DRAWING OTHER LINES
glVertex2i(A.x, A.y); glVertex2i(F.x, F.y);
glVertex2i(B.x, B.y); glVertex2i(G.x, G.y);
glVertex2i(C.x, C.y); glVertex2i(H.x, H.y);
glVertex2i(D.x, D.y); glVertex2i(E.x, E.y);
glEnd();
glFlush();
}
Point2D Project_Perspective(Point3D p, Point3D CoP){
Point2D p2;
float d = abs(CoP.z); // The distance between the COP and orign
p2.x=(d*p.x)/(p.z+d);
p2.y=(d*p.y)/(p.z+d);
//.....wite necessary equations here for perspective transformation
return p2;
}
Point2D translate2D(Point2D p, float tx, float ty){
Point2D tp2;
//.....wite the equations for 2D translation
tp2.x=p.x+tx;
tp2.y=p.y+ty;
return tp2;
}
Point3D translate3D(Point3D p, float tx, float ty, float tz){
Point3D tp3;
//.....wite the equations for 3D translation
tp3.x=p.x+tx;
tp3.y=p.y+ty;
tp3.z=p.z+tz;
return tp3;
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
Point2D Projected_A=Project_Perspective(A,COP);
Point2D Projected_B=Project_Perspective(B,COP);
Point2D Projected_C=Project_Perspective(C,COP);
Point2D Projected_D=Project_Perspective(D,COP);
Point2D Projected_E=Project_Perspective(E,COP);
Point2D Projected_F=Project_Perspective(F,COP);
Point2D Projected_G=Project_Perspective(G,COP);
Point2D Projected_H=Project_Perspective(H,COP);
DrawCube(
// Calculate the following values
//Answer for Q1
translate2D(Projected_A,WinWidth/2, WinHeight/2),
translate2D(Projected_B,WinWidth/2, WinHeight/2),
translate2D(Projected_C,WinWidth/2, WinHeight/2),
translate2D(Projected_D,WinWidth/2, WinHeight/2),
translate2D(Projected_E,WinWidth/2, WinHeight/2),
translate2D(Projected_F,WinWidth/2, WinHeight/2),
translate2D(Projected_G,WinWidth/2, WinHeight/2),
translate2D(Projected_H,WinWidth/2, WinHeight/2));
}
else if (button == GLUT_RIGHT_BUTTON)
{
DrawCube(
//Answer for Q2
translate2D(Project_Perspective(translate3D(A,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(B,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(C,-100,-100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(D,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(E,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(F,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(G,-100,-100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(H,-100, -100,0),COP1),WinWidth/2, WinHeight/2)
);
}
}
}
void myDisplay()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( WinWidth, WinHeight );
glutCreateWindow( "Projection of a Cube" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, WinWidth, 0, WinHeight );
glViewport(0, 0, WinWidth, WinHeight);
InitCube();
glutKeyboardFunc(keyboard);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Friday, July 18, 2014
Opengl,C++ : Scratch code for the standard perspective projection
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int WinWidth = 500, WinHeight = 500;
typedef struct{float x;float y;}Point2D;
typedef struct{float x;float y;float z;}Point3D;
Point3D COP = {0,0,-200}; // Centre Of Projection is at (0,0,-200)
Point3D COP1 = {100,100,-200};
Point3D A,B,C,D,E,F,G,H; // Vertices of the cube
void InitCube(){
A.x = 0; A.y = 0; A.z = 0; // A(0,0,0)
B.x = 200; B.y = 0; B.z = 0; // B(200,0,0)
C.x = 200; C.y = 200; C.z = 0; // C(200,200,0)
D.x = 0; D.y = 200; D.z = 0; // D(0,200,0)
E.x = 0; E.y = 200; E.z = 200; // E(0,200,200)
F.x = 0; F.y = 0; F.z = 200; // F(0,0,200)
G.x = 200; G.y = 0; G.z = 200; // G(200,0,200)
H.x = 200; H.y = 200; H.z = 200; // H(200,200,200)
}
void DrawCube(Point2D A, Point2D B, Point2D C, Point2D D, Point2D E, Point2D F, Point2D G, Point2D H){
glPointSize(1.0);
glBegin(GL_LINE_LOOP);
//DRAWING FRONT FACE
glVertex2i(A.x, A.y); glVertex2i(B.x, B.y); glVertex2i(C.x, C.y); glVertex2i(D.x, D.y);
glEnd();
glBegin(GL_LINE_LOOP);
//DRAWING BACK FACE
glVertex2i(E.x, E.y); glVertex2i(F.x, F.y); glVertex2i(G.x, G.y); glVertex2i(H.x, H.y);
glEnd();
glBegin(GL_LINES);
//DRAWING OTHER LINES
glVertex2i(A.x, A.y); glVertex2i(F.x, F.y);
glVertex2i(B.x, B.y); glVertex2i(G.x, G.y);
glVertex2i(C.x, C.y); glVertex2i(H.x, H.y);
glVertex2i(D.x, D.y); glVertex2i(E.x, E.y);
glEnd();
glFlush();
}
Point2D Project_Perspective(Point3D p, Point3D CoP){
Point2D p2;
float d = abs(CoP.z); // The distance between the COP and orign
p2.x=(d*p.x)/(p.z+d);
p2.y=(d*p.y)/(p.z+d);
//.....wite necessary equations here for perspective transformation
return p2;
}
Point2D translate2D(Point2D p, float tx, float ty){
Point2D tp2;
//.....wite the equations for 2D translation
tp2.x=p.x+tx;
tp2.y=p.y+ty;
return tp2;
}
Point3D translate3D(Point3D p, float tx, float ty, float tz){
Point3D tp3;
//.....wite the equations for 3D translation
tp3.x=p.x+tx;
tp3.y=p.y+ty;
tp3.z=p.z+tz;
return tp3;
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 'q' || key == 'Q')
exit(EXIT_SUCCESS);
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
/* Point2D translateA=translate2D(Projected_A,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_B,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_C,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_D,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_E,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_F,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_G,WinWidth/2, WinHeight/2);
Point2D translateA=translate2D(Projected_H,WinWidth/2, WinHeight/2);*/
}
else if (button == GLUT_RIGHT_BUTTON)
{
}
}
}
void myDisplay()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
Point2D Projected_A=Project_Perspective(A,COP);
Point2D Projected_B=Project_Perspective(B,COP);
Point2D Projected_C=Project_Perspective(C,COP);
Point2D Projected_D=Project_Perspective(D,COP);
Point2D Projected_E=Project_Perspective(E,COP);
Point2D Projected_F=Project_Perspective(F,COP);
Point2D Projected_G=Project_Perspective(G,COP);
Point2D Projected_H=Project_Perspective(H,COP);
DrawCube(
// Calculate the following values
//Q1
/*translate2D(Projected_A,WinWidth/2, WinHeight/2),
translate2D(Projected_B,WinWidth/2, WinHeight/2),
translate2D(Projected_C,WinWidth/2, WinHeight/2),
translate2D(Projected_D,WinWidth/2, WinHeight/2),
translate2D(Projected_E,WinWidth/2, WinHeight/2),
translate2D(Projected_F,WinWidth/2, WinHeight/2),
translate2D(Projected_G,WinWidth/2, WinHeight/2),
translate2D(Projected_H,WinWidth/2, WinHeight/2)*/
//Q3
translate2D(Project_Perspective(translate3D(A,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(B,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(C,-100,-100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(D,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(E,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(F,-100, -100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(G,-100,-100,0),COP1),WinWidth/2, WinHeight/2),
translate2D(Project_Perspective(translate3D(H,-100, -100,0),COP1),WinWidth/2, WinHeight/2)
);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( WinWidth, WinHeight );
glutCreateWindow( "Projection of a Cube" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, WinWidth, 0, WinHeight );
glViewport(0, 0, WinWidth, WinHeight);
InitCube();
glutKeyboardFunc(keyboard);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Opengl,C++ : Line_clipping scratch code using midpoint subdivision method
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int x0 = 0,y0 =0 ,x1 =0 ,y1=0 ;
int xmin;int ymin;int xmax;int ymax;
void DrawRect(int x0, int y0, int x1, int y1)
{
glClear(GL_COLOR_BUFFER_BIT);
glRecti(x0,y0,x1,y1);
glFlush();
}
void DrawLineSegment(int x0, int y0, int x1, int y1){
glColor3d(1,0,0);
glBegin(GL_LINES);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glEnd();
glFlush();
}
typedef int OutCode;
const int INSIDE = 0; // 0000
const int BOTTOM = 1; // 0001
const int RIGHT = 2; // 0010
const int TOP = 4; // 0100
const int LEFT = 8; // 1000
OutCode ComputeOutCode(int x, int y)
{
OutCode code;
code = INSIDE; // initialised as being inside of clip window
if (x < xmin) // to the left of clip window
code |= LEFT;
else if (x > xmax) // to the left of clip window
code |= RIGHT;
else if (y < ymin) // to the left of clip window
code |= BOTTOM;// Complete the code segment here
else if (y > ymax) // to the left of clip window
code |= TOP;
return(code);
}
void lineclip(int x0,int y0,int x1,int y1) {
OutCode code0,code1;
int midx,midy;
if(abs(x0-x1)==1 && abs(y0-y1)==1) /*Adjacent points */
return;
code0=ComputeOutCode(x0,y0);
code1=ComputeOutCode(x1,y1);
if( !(code0 | code1) ) { /*The two points are completely inside the window*/
DrawLineSegment(x0,y0,x1,y1);
return;
}
else if(code0 & code1) /*The two points are completely outside the window.*/
return ;
midx=(x0+x1)/2;
midy=(y0+y1)/2;
lineclip(midx,midy,x1,y1);
lineclip(x0,y0,midx,midy);
}
void myMouse(int button, int state, int x, int y) {
glPointSize(5.0);
if(state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
if (flag){
x0 = x;
y0 = screenheight - y;
flag = false;
} else {
x1 = x;
y1 = screenheight - y;
lineclip(x0,y0,x1,y1);
flag = true;
}
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (flag){
x0 = x;
y0 = screenheight - y;
flag = false;
} else {
x1 = x;
y1 = screenheight - y;
glColor3d(1,1,0);
DrawRect(x0,y0,x1,y1);
xmin=x0;
xmax=x1;
ymin=y0;
ymax=y1;
flag = true;
}
}
}
}
void myDisplay()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //black
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
// create window
glutCreateWindow( "midpoint subdivision method Clipping" );
// set the view frustum
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
// clear rendering surface
glViewport(0, 0, 800, 600);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Opengl,C++ : Line_clipping scratch code using Cohen-Sutherland Line Clipping method
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int x0 = 0,y0 =0 ,x1 =0 ,y1=0 ;
int xmin;int ymin;int xmax;int ymax;
void DrawRect(int x0, int y0, int x1, int y1)
{
glClear(GL_COLOR_BUFFER_BIT);
glRecti(x0,y0,x1,y1);
glFlush();
}
void DrawLineSegment(int x0, int y0, int x1, int y1){
glColor3d(1,0,0);
glBegin(GL_LINES);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glEnd();
glFlush();
}
typedef int OutCode;
const int INSIDE = 0; // 0000
const int BOTTOM = 1; // 0001
const int RIGHT = 2; // 0010
const int TOP = 4; // 0100
const int LEFT = 8; // 1000
OutCode ComputeOutCode(int x, int y)
{
OutCode code;
code = INSIDE; // initialised as being inside of clip window
if (x < xmin) // to the left of clip window
code |= LEFT;
else if (x > xmax) // to the left of clip window
code |= RIGHT;
else if (y < ymin) // to the left of clip window
code |= BOTTOM;// Complete the code segment here
else if (y > ymax) // to the left of clip window
code |= TOP;
return(code);
}
void CohenSutherlandLineClip (int x0, int y0, int x1, int y1)
{
// Complete the code segment here
OutCode outcode0=ComputeOutCode(x0,y0);
OutCode outcode1=ComputeOutCode(x1,y1);
bool accept=false;
while (true) {
if (!(outcode0 | outcode1)) {//Bitwise OR is 0. Trivially accept
accept = true;
break;
} else if (outcode0 & outcode1) {
// Bitwise AND is not 0. Trivially reject
break;
} else {
// failed both tests, so calculate the line segment to clip
// from an outside point to an intersection with clip edge
double x, y;
// At least one endpoint is outside the clip rectangle; pick it.
OutCode outcodeOut = outcode0? outcode0 : outcode1;
// Now find the intersection point;
if (outcodeOut & TOP) {// point is above the window
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
} else if (outcodeOut & BOTTOM) {// point is below the window
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
} else if (outcodeOut & RIGHT) { // point is to the right of window
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
} else { // point is to the left of window
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
//Now move outside point to intersection point and get ready for next pass.
if (outcodeOut == outcode0) {
x0 = x; y0 = y;
outcode0 = ComputeOutCode(x0, y0);
} else {
x1 = x; y1 = y;
outcode1 = ComputeOutCode(x1, y1);
}
}
}
if (accept)
DrawLineSegment(x0, y0, x1, y1);
}
void myMouse(int button, int state, int x, int y) {
glPointSize(5.0);
if(state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
if (flag){
x0 = x;
y0 = screenheight - y;
flag = false;
} else {
x1 = x;
y1 = screenheight - y;
CohenSutherlandLineClip(x0,y0,x1,y1);
flag = true;
}
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (flag){
x0 = x;
y0 = screenheight - y;
flag = false;
} else {
x1 = x;
y1 = screenheight - y;
glColor3d(1,1,0);
DrawRect(x0,y0,x1,y1);
xmin=x0;
xmax=x1;
ymin=y0;
ymax=y1;
flag = true;
}
}
}
}
void myDisplay()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //black
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
// create window
glutCreateWindow( "Cohen-Sutherland Line Clipping" );
// set the view frustum
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
// clear rendering surface
glViewport(0, 0, 800, 600);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Saturday, June 21, 2014
Opengl,C++ : Draw pattern with Geometric transformation
First specify the starting points using left mouse click.Then press "k" to draw a given pattern.
#include <windows.h>
#include<gl/GL.h>
#include<GL/glut.h>
#include<cmath>
double angle=120;
typedef struct {
float x;
float y;
}Point2D;
Point2D p1,p2,p3;
void drawLineSegment(Point2D p1,Point2D p2,Point2D p3)
{
glPointSize(1.0);
glBegin(GL_LINES);
glVertex2i(p1.x,p1.y);
glVertex2i(p2.x,p2.y);
glVertex2i(p2.x,p2.y);
glVertex2i(p3.x,p3.y);
glEnd();
glFlush();
}
void drawPolygonSegment(Point2D p1,Point2D p2,Point2D p3)
{
glPointSize(1.0);
glBegin(GL_LINES);
glVertex2i(p1.x,p1.y);
glVertex2i(p2.x,p2.y);
glVertex2i(p2.x,p2.y);
glVertex2i(p3.x,p3.y);
glVertex2i(p3.x,p3.y);
glVertex2i(p1.x,p1.y);
glEnd();
glFlush();
}
Point2D translate(Point2D p,float tx,float ty)
{
p.x=p.x+tx;
p.y=p.y+ty;
return p;
}
Point2D rotate(Point2D p, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
Point2D ref_4(Point2D p)
{
p.x=-p.x;
p.y=p.y;
return p;
}
Point2D Scale(Point2D p,float sx,float sy)
{
p.x=p.x*sx;
p.y=p.y*sy;
return p;
}
void mouse(int button,int state,int x,int y){
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
p1.x=0;
p1.y=0;
p2.x=75;
p2.y=200;
p3.x=0;
p3.y=200;
}
}
}
void trans()
{
drawLineSegment(translate(p1,500,500),translate(p2,500,500),translate(p3,500,500));
Point2D scale1_p1=translate(Scale(p1,0.75,0.75),0,200);
Point2D scale1_p2=translate(Scale(p2,0.75,0.75),0,200);
Point2D scale1_p3=translate(Scale(p3,0.75,0.75),0,200);
drawLineSegment(translate(scale1_p1,500,500),translate(scale1_p2,500,500),translate(scale1_p3,500,500));
Point2D scale2_p1=translate(Scale(p1,0.5,0.5),0,350);
Point2D scale2_p2=translate(Scale(p2,0.5,0.5),0,350);
Point2D scale2_p3=translate(Scale(p3,0.5,0.5),0,350);
drawLineSegment(translate(scale2_p1,500,500),translate(scale2_p2,500,500),translate(scale2_p3,500,500));
Point2D ref_p1=ref_4(p1);
Point2D ref_p2=ref_4(p2);
Point2D ref_p3=ref_4(p3);
drawLineSegment(translate(ref_p1,500,500),translate(ref_p2,500,500),translate(ref_p3,500,500));
Point2D refscale1_p1=ref_4(scale1_p1);
Point2D refscale1_p2=ref_4(scale1_p2);
Point2D refscale1_p3=ref_4(scale1_p3);
drawLineSegment(translate(refscale1_p1,500,500),translate(refscale1_p2,500,500),translate(refscale1_p3,500,500));
Point2D refscale2_p1=ref_4(scale2_p1);
Point2D refscale2_p2=ref_4(scale2_p2);
Point2D refscale2_p3=ref_4(scale2_p3);
drawLineSegment(translate(refscale2_p1,500,500),translate(refscale2_p2,500,500),translate(refscale2_p3,500,500));
int i=1;
while(i<=2){
Point2D rotate_p1=rotate(p1,angle*i);
Point2D rotate_p2=rotate(p2,angle*i);
Point2D rotate_p3=rotate(ref_p2,angle*i);
drawPolygonSegment(translate(rotate_p1,500,500),translate(rotate_p2,500,500),translate(rotate_p3,500,500));
Point2D rotateScale1_p1=rotate(scale1_p1,angle*i);
Point2D rotateScale1_p2=rotate(scale1_p2,angle*i);
Point2D rotateScale1_p3=rotate(refscale1_p2,angle*i);
drawPolygonSegment(translate(rotateScale1_p1,500,500),translate(rotateScale1_p2,500,500),translate(rotateScale1_p3,500,500));
Point2D rotateScale2_p1=rotate(scale2_p1,angle*i);
Point2D rotateScale2_p2=rotate(scale2_p2,angle*i);
Point2D rotateScale2_p3=rotate(refscale2_p2,angle*i);
drawPolygonSegment(translate(rotateScale2_p1,500,500),translate(rotateScale2_p2,500,500),translate(rotateScale2_p3,500,500));
i++;
}
}
void key(unsigned char key,int x,int y)
{
if(key=='k')glutIdleFunc(trans);
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 1000, 1000 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );
glViewport(0, 0, 1000, 1000);
glutDisplayFunc( myDisplay );
glutMouseFunc( mouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Friday, June 20, 2014
Opengl,C++ : Window-To_Viewport Mapping
To get the display
#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right - left, top - bottom);
}
void myInit(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // set black background color
glColor3f (1.0f, 0.0f, 0.0f); // set the drawing color
}
void drawShape()
{
glBegin(GL_LINE_LOOP);
glVertex2i(150,100);
glVertex2i(350, 100);
glVertex2i(400, 300);
glVertex2i(500, 300);
glVertex2i(250,450);
glVertex2i(00, 300);
glVertex2i(100, 300);
glEnd();
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
// set the window
int bottomX = 0;
int bottomY = 0;
int width = 64;
int height = 48;
int i=0;
int j=0;
//setViewport( left, right, bottom, top)
for(int bottomX=0;bottomX<640;bottomX+=width)
{
for(int bottomY=0;bottomY<480;bottomY+=height)
{
setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport
if((j%2==0 || i%2==1) && (j%2==1 || i%2==0)){
setWindow(0.0, 640.0, 0.0, 480.0);
drawShape();
}
else{
setWindow(0.0,640.0, 480.0, 0.0);
drawShape();
}
j++;
}
i++;
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
glutCreateWindow("Window-To-Viewport Mapping");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
Opengl,C++ : Window-To_Viewport Mapping
To get the display
#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right - left, top - bottom);
}
void myInit(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // set black background color
glColor3f (1.0f, 0.0f, 0.0f); // set the drawing color
}
void drawShape()
{
glBegin(GL_LINE_LOOP);
glVertex2i(150,100);
glVertex2i(350, 100);
glVertex2i(400, 300);
glVertex2i(500, 300);
glVertex2i(250,450);
glVertex2i(00, 300);
glVertex2i(100, 300);
glEnd();
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
setWindow(0.0, 640.0, 0.0, 480.0); // set the window
int bottomX = 0;
int bottomY = 0;
int width = 64;
int height = 48;
int i=0;
int j=0;
//setViewport( left, right, bottom, top)
for(int bottomX=0;bottomX<640;bottomX+=width)
{
for(int bottomY=0;bottomY<480;bottomY+=height)
{
setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport
drawShape();
}
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
glutCreateWindow("Window-To-Viewport Mapping");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
Opengl,C++ : Window-To_Viewport Mapping
#include <GL/glut.h>
const int screenWidth = 640;
const int screenHeight = 480;
// defining the Window
void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
// setting the Viewport
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right - left, top - bottom);
}
void myInit(void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // set black background color
glColor3f (1.0f, 0.0f, 0.0f); // set the drawing color
}
void drawShape()
{
glBegin(GL_LINE_LOOP);
glVertex2i(150,100);
glVertex2i(350, 100);
glVertex2i(400, 300);
glVertex2i(500, 300);
glVertex2i(250,450);
glVertex2i(00, 300);
glVertex2i(100, 300);
glEnd();
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
setWindow(0.0, 640.0, 0.0, 480.0); // set the window
int bottomX = 0;
int bottomY = 0;
int width = 64;
int height = 48;
//setViewport( left, right, bottom, top)
setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport
drawShape();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size
glutCreateWindow("Window-To-Viewport Mapping");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
Wednesday, June 11, 2014
Opengl,C++ : Draw pattern with Geometric transformation
First specify the starting points using left mouse click.Then press "k" to draw a given pattern.
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 90;
double sangle = 45; //The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4,sp1,sp2,sp3;
void DrawpolygonSegment(Point2D pt1, Point2D pt2,Point2D pt3,Point2D pt4)
{
glPointSize(1.0);
glBegin(GL_LINES);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glVertex2i(pt4.x, pt4.y);
glVertex2i(pt1.x, pt1.y);
glEnd();
glFlush();
}
void DrawLineSegment(Point2D pt1, Point2D pt2,Point2D pt3)
{
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glBegin(GL_LINES);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glEnd();
glFlush();
}
Point2D translate(Point2D p, float tx, float ty){
p.x =p.x+tx; //.....wite the equations for translation
p.y = p.y+ty; //.....wite the equations for translation
return p;
}
Point2D rotate(Point2D p,Point2D c, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);
ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);
return ptemp;
}
Point2D reflect_4(Point2D p){
Point2D ptemp;
ptemp.x=p.x;
ptemp.y=-p.y;
return ptemp;
}
Point2D Scale(Point2D p,double sx,double sy)
{
Point2D sp;
sp.x=sx*(p.x+90);
sp.y=sy*(p.y);
return sp;
}
void trans()
{
Point2D Trans_p1 = translate(p1,300,300);
Point2D Trans_p2 = translate(p2,300,300);
Point2D Trans_p3 = translate(p3,300,300);
DrawLineSegment(Trans_p1,Trans_p2,Trans_p3);
Point2D Scale_p1 = translate(Scale(p1,0.5,0.5),300,300);
Point2D Scale_p2 = translate(Scale(p2,0.5,0.5),300,300);
Point2D Scale_p3 = translate(Scale(p3,0.5,0.5),300,300);
DrawLineSegment(Scale_p1,Scale_p2,Scale_p3);
Point2D ref4_p1 =translate(reflect_4(p1),300,300);
Point2D ref4_p2 = translate(reflect_4(p2),300,300);
Point2D ref4_p3 = translate(reflect_4(p3),300,300);
DrawLineSegment(ref4_p1,ref4_p2,ref4_p3);
Point2D refScale4_p1 =translate(reflect_4(Scale(p1,0.5,0.5)),300,300);
Point2D refScale4_p2 = translate(reflect_4(Scale(p2,0.5,0.5)),300,300);
Point2D refScale4_p3 = translate(reflect_4(Scale(p3,0.5,0.5)),300,300);
DrawLineSegment(refScale4_p1,refScale4_p2,refScale4_p3);
//Point2D rotate_p1 =rotate(Trans_p1,angle);
int i=1;
while(i<=3){
Point2D rotate_p2 =rotate(Trans_p2,Trans_p1,angle*i);
Point2D rotate_p3 =rotate(Trans_p3,Trans_p1,angle*i);
Point2D rotate_p4 =rotate(ref4_p2,Trans_p1,angle*i);
DrawpolygonSegment(Trans_p1,rotate_p2,rotate_p3,rotate_p4);
Point2D rotate_Scalep1 =rotate(Scale_p1,Trans_p1,angle*i);
Point2D rotate_Scalep2 =rotate(Scale_p2,Trans_p1,angle*i);
Point2D rotate_Scalep3 =rotate(Scale_p3,Trans_p1,angle*i);
Point2D rotate_Scalep4 =rotate(refScale4_p2,Trans_p1,angle*i);
DrawpolygonSegment(rotate_Scalep1,rotate_Scalep2,rotate_Scalep3,rotate_Scalep4);
i++;
}
sp1.x=0;
sp1.y=0;
sp2.x=100;
sp2.y=16;
sp3.x=175;
sp3.y=0;
i=1;
while(i<=4){
if(i==1)sangle=45;
if(i==2)sangle=135;
if(i==3)sangle=225;
if(i==4)sangle=315;
Point2D Trans_sp1 = translate(sp1,300,300);
Point2D Trans_sp2 = translate(sp2,300,300);
Point2D Trans_sp3 = translate(sp3,300,300);
//DrawLineSegment(Trans_sp1,Trans_sp2,Trans_sp3);
Point2D rotate_sp2 =rotate(Trans_sp2,Trans_sp1,sangle);
Point2D rotate_sp3 =rotate(Trans_sp3,Trans_sp1,sangle);
DrawLineSegment(Trans_p1,rotate_sp2,rotate_sp3);
Point2D ref4_sp1 =translate(reflect_4(sp1),300,300);
Point2D ref4_sp2 =translate( reflect_4(sp2),300,300);
Point2D ref4_sp3 =translate( reflect_4(sp3),300,300);
Point2D rotateref_sp2 =rotate(ref4_sp2,Trans_sp1,sangle);
Point2D rotateref_sp3 =rotate(ref4_sp3,Trans_sp1,sangle);
DrawLineSegment(Trans_p1,rotateref_sp2,rotateref_sp3);
i++;
}
}
void key(unsigned char key, int x, int y)
{
if(key=='k') glutIdleFunc(trans);
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
p1.x=0;
p1.y=0;
p2.x=70;
p2.y=40;
p3.x=300;
p3.y=0;
//DrawLineSegment(p1,p2,p3);
}
}
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutMouseFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Monday, June 2, 2014
Opengl,C++ : Rotate and Translate Squre with respect to origin
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 30;
//The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D translate(Point2D p, float tx, float ty){
p.x =p.x; //.....wite the equations for translation
p.y = p.y; //.....wite the equations for translation
return p;
}
Point2D rotate(Point2D p,Point2D c,float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);
ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);
return ptemp;
}
void myMouse()
{
glClear(GL_COLOR_BUFFER_BIT);
p1.x = 200;
p1.y =200;
p2.x = 400;
p2.y = 200;
p3.x = 400;
p3.y =400;
p4.x=200;
p4.y=400;
DrawPolygonSegment(p1,p2,p3,p4);
}
void translatepolygon_R()
{
Point2D c;
c.x=(p1.x+p2.x)/2;
c.y=(p1.y+p4.y)/2;
Point2D Rotated_p1 = rotate(p1,c,angle);
Point2D Rotated_p2 = rotate(p2,c,angle);
Point2D Rotated_p3 = rotate(p3,c,angle);
Point2D Rotated_p4 = rotate(p4,c,angle);
DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
void key(unsigned char key, int x, int y)
{
if(key=='R') glutIdleFunc(translatepolygon_R);
angle=angle+4;
p1.x=p1.x+4;
p2.x=p2.x+4;
p3.x=p3.x+4;
p4.x=p4.x+4;
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutDisplayFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Opengl,C++ : Rotate Squre with respect to its centre
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 30;
//The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D rotate(Point2D p,Point2D c,float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);
ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);
return ptemp;
}
void myMouse()
{
glClear(GL_COLOR_BUFFER_BIT);
p1.x = 200;
p1.y =200;
p2.x = 400;
p2.y = 200;
p3.x = 400;
p3.y =400;
p4.x=200;
p4.y=400;
DrawPolygonSegment(p1,p2,p3,p4);
}
void translatepolygon_R()
{
Point2D c;
c.x=(p1.x+p2.x)/2;
c.y=(p1.y+p4.y)/2;
Point2D Rotated_p1 = rotate(p1,c,angle);
Point2D Rotated_p2 = rotate(p2,c,angle);
Point2D Rotated_p3 = rotate(p3,c,angle);
Point2D Rotated_p4 = rotate(p4,c,angle);
DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
void key(unsigned char key, int x, int y)
{
if(key=='R') glutIdleFunc(translatepolygon_R);
angle=angle+4;
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutDisplayFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Opengl,C++ : Rotate Squre with respect to the origin
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 30;
//The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D rotate(Point2D p, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
void myMouse()
{
glClear(GL_COLOR_BUFFER_BIT);
p1.x = 200;
p1.y =200;
p2.x = 400;
p2.y = 200;
p3.x = 400;
p3.y =400;
p4.x=200;
p4.y=400;
DrawPolygonSegment(p1,p2,p3,p4);
}
void translatepolygon_R()
{
Point2D Rotated_p1 = rotate(p1,angle);
Point2D Rotated_p2 = rotate(p2,angle);
Point2D Rotated_p3 = rotate(p3,angle);
Point2D Rotated_p4 = rotate(p4,angle);
DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
void key(unsigned char key, int x, int y)
{
if(key=='R') glutIdleFunc(translatepolygon_R);
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutDisplayFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Sunday, June 1, 2014
Opengl,C++ : Shrinked Polygon
The following program Specify a point for the Polygon by the left mouse button and shrinked polygon (say, by half) with respect to the origin, when you press the key "Z".
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D translate(Point2D p){
p.x =p.x/2; //.....wite the equations for translation
p.y = p.y/2; //.....wite the equations for translation
return p;
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
p1.x = 0;
p1.y =0;
switch(first)
{
case 0:
p2.x = x;
p2.y = screenheight - y;
first = 1;
break;
case 1:
p3.x = x;
p3.y = screenheight - y;
first = 2;
break;
case 2:
p4.x = x;
p4.y = screenheight - y;
DrawPolygonSegment(p1,p2,p3,p4);
first = 0;
break;
}
}
}
}
void Shrinked_Polygon()
{
Point2D translate_p1 = translate(p1);
Point2D translate_p2 = translate(p2);
Point2D translate_p3 = translate(p3);
Point2D translate_p4 = translate(p4);
DrawPolygonSegment(translate_p1,translate_p2,translate_p3,translate_p4);
}
void key(unsigned char key, int x, int y)
{
if(key=='Z') glutIdleFunc(Shrinked_Polygon);
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutMouseFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Monday, May 26, 2014
Opengl,C++ : Draw Polygon and Rotate Polygon Using Keyboard Characters
The following program Specify a point for the Polygon by the left mouse click, Rotate Polygon using keyboard char "R" and Translate Polygon by (100,150) coordinates using keyboard char "T".
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 30;
//The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D translate(Point2D p, float tx, float ty){
p.x =p.x+tx; //.....wite the equations for translation
p.y = p.y+ty; //.....wite the equations for translation
return p;
}
Point2D rotate(Point2D p, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
switch(first)
{
case 0:
p1.x = x;
p1.y =screenheight - y;
first = 1;
break;
case 1:
p2.x = x;
p2.y = screenheight - y;
first = 2;
break;
case 2:
p3.x = x;
p3.y = screenheight - y;
first = 3;
break;
case 3:
p4.x = x;
p4.y = screenheight - y;
DrawPolygonSegment(p1,p2,p3,p4);
first = 0;
break;
}
}
}
}
void translatepolygon_R()
{
Point2D Rotated_p1 = rotate(p1,angle);
Point2D Rotated_p2 = rotate(p2,angle);
Point2D Rotated_p3 = rotate(p3,angle);
Point2D Rotated_p4 = rotate(p4,angle);
DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
void translatepolygon_T()
{
Point2D translate_p1 = translate(p1,100,150);
Point2D translate_p2 = translate(p2,100,150);
Point2D translate_p3 = translate(p3,100,150);
Point2D translate_p4 = translate(p4,100,150);
DrawPolygonSegment(translate_p1,translate_p2,translate_p3,translate_p4);
}
void key(unsigned char key, int x, int y)
{
if(key=='T') glutIdleFunc(translatepolygon_T);
if(key=='R') glutIdleFunc(translatepolygon_R);
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutMouseFunc( myMouse );
glutKeyboardFunc(key);
glutMainLoop();
return( 0 );
}
Opengl,C++ : Draw and Rotate a Polygon using 'Mouse Clicks'
The following program rotates a given Polygon in 2D.
(Specify a point for the Polygon by the left mouse button, and click the right mouse button to rotate it)
(Specify a point for the Polygon by the left mouse button, and click the right mouse button to rotate it)
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
int first = 0;
double angle = 30;
//The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2,p3,p4;
void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){
glPointSize(1.0);
glBegin(GL_POLYGON);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt2.x, pt2.y);
glVertex2i(pt3.x, pt3.y);
glVertex2i(pt4.x, pt4.y);
glEnd();
glFlush();
}
Point2D rotate(Point2D p, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
switch(first)
{
case 0:
p1.x = x;
p1.y =screenheight - y;
first = 1;
break;
case 1:
p2.x = x;
p2.y = screenheight - y;
first = 2;
break;
case 2:
p3.x = x;
p3.y = screenheight - y;
first = 3;
break;
case 3:
p4.x = x;
p4.y = screenheight - y;
DrawPolygonSegment(p1,p2,p3,p4);
first = 0;
break;
}
}
else if (button == GLUT_RIGHT_BUTTON) {
Point2D Rotated_p1 = rotate(p1,angle);
Point2D Rotated_p2 = rotate(p2,angle);
Point2D Rotated_p3 = rotate(p3,angle);
Point2D Rotated_p4 = rotate(p4,angle);
DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
}
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "Rotate Polygon" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutDisplayFunc( myDisplay );
glutMouseFunc( myMouse );
glutMainLoop();
return( 0 );
}
Opengl,C++ : Draw a Line and Rotate using 'Mouse Clicks'
The following program rotates a given Line about the origin in 2D.
(Specify a point by the left mouse button, and click the right mouse button to rotate it)
(Specify a point by the left mouse button, and click the right mouse button to rotate it)
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
int screenheight = 600;
int screenwidth = 800;
bool flag = true;
double angle = 30; //The angle for the rotation (in degrees)
typedef struct{
float x;
float y;
}Point2D;
Point2D p1,p2;
void DrawLineSegment(Point2D pt1, Point2D pt2){
glPointSize(1.0);
glBegin(GL_LINES);
glVertex2i(pt1.x, pt1.y);
glVertex2i(pt2.x, pt2.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(pt2.x, pt2.y);
glEnd();
glFlush();
}
Point2D rotate(Point2D p, float ang){
ang = ang * 3.14 / 180.0; //angle in radians
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
void myMouse(int button, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(button == GLUT_LEFT_BUTTON) {
glClear(GL_COLOR_BUFFER_BIT);
Point2D p1;
p1.x = 0;
p1.y = 0;
p2.x = x;
p2.y = screenheight - y;
DrawLineSegment(p1, p2);
}
else if (button == GLUT_RIGHT_BUTTON) {
Point2D Rotated_p2 = rotate(p2,angle);
DrawLineSegment(p1,Rotated_p2);
}
}
}
void myDisplay(){
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "Rotate Line" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutMouseFunc( myMouse );
glutDisplayFunc( myDisplay );
glutMainLoop();
return( 0 );
}
Thursday, May 15, 2014
Opengl,C++ : Flood-Fill Algorithm Using Recursion
#include <GL/glut.h>
int ww = 600, wh = 500;
float bgCol[3] = {0.2, 0.4,0.0};
float intCol[3] = {1.0,0.0,0.0};
float fillCol[3] = {0.4,0.0,0.0};
void setPixel(int pointx, int pointy, float f[3])
{
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx,pointy);
glEnd();
glFlush();
}
void getPixel(int x, int y, float pixels[3])
{
glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
}
void drawPolygon(int x1, int y1, int x2, int y2)
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2i(x1, y1);
glVertex2i(x1, y2);
glVertex2i(x2, y2);
glVertex2i(x2, y1);
glEnd();
glFlush();
}
void display()
{
glClearColor(0.2, 0.4,0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
drawPolygon(150,250,200,300);
glFlush();
}
void floodfill4(int x,int y,float oldcolor[3],float newcolor[3])
{
float color[3];
getPixel(x,y,color);
if(color[0]==oldcolor[0] && (color[1])==oldcolor[1] && (color[2])==oldcolor[2])
{
setPixel(x,y,newcolor);
floodfill4(x+1,y,oldcolor,newcolor);
floodfill4(x-1,y,oldcolor,newcolor);
floodfill4(x,y+1,oldcolor,newcolor);
floodfill4(x,y-1,oldcolor,newcolor);
}
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
int xi = x;
int yi = (wh-y);
floodfill4(xi,yi,intCol,fillCol);
}
}
void myinit()
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(ww,wh);
glutCreateWindow("Flood-Fill-Recursive");
glutDisplayFunc(display);
myinit();
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Opengl,C++ : Boundary-Fill Algorithm Using Recursion
1: #include <GL/glut.h>
2: int ww = 600, wh = 500;
3: float fillCol[3] = {0.4,0.0,0.0};
4: float borderCol[3] = {0.0,0.0,0.0};
5: void setPixel(int pointx, int pointy, float f[3])
6: {
7: glBegin(GL_POINTS);
8: glColor3fv(f);
9: glVertex2i(pointx,pointy);
10: glEnd();
11: glFlush();
12: }
13: void getPixel(int x, int y, float pixels[3])
14: {
15: glReadPixels(x,y,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
16: }
17: void drawPolygon(int x1, int y1, int x2, int y2)
18: {
19: glColor3f(0.0,0.0,0.0);
20: glBegin(GL_LINES);
21: glVertex2i(x1, y1);
22: glVertex2i(x1, y2);
23: glEnd();
24: glBegin(GL_LINES);
25: glVertex2i(x2, y1);
26: glVertex2i(x2, y2);
27: glEnd();
28: glBegin(GL_LINES);
29: glVertex2i(x1, y1);
30: glVertex2i(x2, y1);
31: glEnd();
32: glBegin(GL_LINES);
33: glVertex2i(x1, y2);
34: glVertex2i(x2, y2);
35: glEnd();
36: glFlush();
37: }
38: void display()
39: {
40: glClearColor(0.6,0.8,0.1, 1.0);
41: glClear(GL_COLOR_BUFFER_BIT);
42: drawPolygon(150,250,200,300);
43: glFlush();
44: }
45: void boundaryFill4(int x,int y,float fillColor[3],float borderColor[3])
46: {
47: float interiorColor[3];
48: getPixel(x,y,interiorColor);
49: if((interiorColor[0]!=borderColor[0] && (interiorColor[1])!=borderColor[1] && (interiorColor[2])!=borderColor[2]) && (interiorColor[0]!=fillColor[0] && (interiorColor[1])!=fillColor[1] && (interiorColor[2])!=fillColor[2]))
50: {
51: setPixel(x,y,fillColor);
52: boundaryFill4(x+1,y,fillColor,borderColor);
53: boundaryFill4(x-1,y,fillColor,borderColor);
54: boundaryFill4(x,y+1,fillColor,borderColor);
55: boundaryFill4(x,y-1,fillColor,borderColor);
56: }
57: }
58: void mouse(int btn, int state, int x, int y)
59: {
60: if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
61: {
62: int xi = x;
63: int yi = (wh-y);
64: boundaryFill4(xi,yi,fillCol,borderCol);
65: }
66: }
67: void myinit()
68: {
69: glViewport(0,0,ww,wh);
70: glMatrixMode(GL_PROJECTION);
71: glLoadIdentity();
72: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
73: glMatrixMode(GL_MODELVIEW);
74: }
75: int main(int argc, char** argv)
76: {
77: glutInit(&argc,argv);
78: glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
79: glutInitWindowSize(ww,wh);
80: glutCreateWindow("Bountry-Fill-Recursive");
81: glutDisplayFunc(display);
82: myinit();
83: glutMouseFunc(mouse);
84: glutMainLoop();
85: return 0;
86: }
Opengl,C++ : Draw Circle With Midpoint Circle Algorithm
1: #include<stdio.h>
2: #include<GL/glut.h>
3: #include<math.h>
4: int ww=400,wh=400;
5: int first=0;
6: int xi,yi,xf,yf;
7: void putpixel(int x,int y)
8: {
9: glBegin(GL_POINTS);
10: glVertex2i(x,y);
11: glEnd();
12: glFlush();
13: }
14: int round(double n)
15: {
16: return (n>=0)?(int)(n+0.5):(int)(n-0.5);
17: }
18: void MidPoint_circle(int r)
19: {
20: int x=0,y=r,p=1-r;
21: while(x<=y){
22: //Here Transform each x,y coordinates by 250 pixels
23: putpixel(250+x, 250+y);
24: putpixel(250+y, 250+x);
25: putpixel(250-x, 250+y);
26: putpixel(250-x, 250-y);
27: putpixel(250-y, 250+x);
28: putpixel(250-y, 250-x);
29: putpixel(250+y, 250-x);
30: putpixel(250+x, 250-y);
31: if(p<0)
32: p=p+2*x+3;
33: else{
34: p=p+2*(x-y)+5;
35: y--;
36: }
37: x++;
38: }
39: }
40: void display()
41: {
42: glClearColor(0.4,0.7,0.2,1.0);
43: glColor3f(0.5,0.3,0.0);
44: glClear(GL_COLOR_BUFFER_BIT);
45: glutSwapBuffers();
46: glFlush();
47: }
48: void mouse(int btn,int state,int y,int x)
49: {
50: if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
51: {
52: xi=0;
53: yi=y;
54: MidPoint_circle(yi);
55: }
56: }
57: void myinit()
58: {
59: glViewport(0,0,ww,wh);
60: glMatrixMode(GL_PROJECTION);
61: glLoadIdentity();
62: gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
63: glMatrixMode(GL_MODELVIEW);
64: }
65: int main(int argc,char** argv)
66: {
67: glutInit(&argc,argv);
68: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
69: glutInitWindowSize(ww,wh);
70: glutCreateWindow("MidPoint-Circle");
71: glutDisplayFunc(display);
72: myinit();
73: glutMouseFunc(mouse);
74: glutMainLoop();
75: return 0;
76: }
Subscribe to:
Posts (Atom)