0% found this document useful (0 votes)
113 views7 pages

GLUT Shapes Dem-WPS Office

This C++ program implements Cohen-Sutherland line clipping algorithm to clip a line within a defined viewport. It allows the user to click and drag to draw a line, which is then clipped using the algorithm. The boundary of the viewport is displayed and the program exits when the user presses ESC or q.

Uploaded by

Ags Slm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views7 pages

GLUT Shapes Dem-WPS Office

This C++ program implements Cohen-Sutherland line clipping algorithm to clip a line within a defined viewport. It allows the user to click and drag to draw a line, which is then clipped using the algorithm. The boundary of the viewport is displayed and the program exits when the user presses ESC or q.

Uploaded by

Ags Slm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/*

* GLUT Shapes Demo


*
* Written by Nigel Stewart November 2003
*
* This program is test harness for the sphere, cone
* and torus shapes in GLUT.
*
* Spinning wireframe and smooth shaded shapes are
* displayed until the ESC or q key is pressed. The
* number of geometry stacks and slices can be adjusted
* using the + and - keys.
*/

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>
#include <stdio.h>

static int slices = 16;


static int stacks = 16;

int click=0;int nxyget=0;


bool check=true;
int zone;
int x0,y0,x1,y1;
int x0Click,y0Click,x1Click,y1Click;
int gp=60;
int xl = -320, yl = -240, xr=319, yr=239;
int xmax=xr-gp, xmin = xl+gp, ymax=yr-gp, ymin=yl+gp;
bool writen=false;

int makeCode(int x, int y){


int code=0;
if(y>ymax)code+=8;
else if(y<ymin)code+=4;

if(x>xmax)code+=2;
else if(x<xmin)code+=1;

return code;
}

void get_zone(int x0,int y0,int x1,int y1){


int dX = x1-x0;
int dY = y1-y0;

if(dX>=0 && dY>=0)


{
if(dX>dY)
zone=0;
else
zone=1;
}
else if(dX<0 && dY>=0)
{
if(abs(dX)>dY)
zone=3;
else
zone=2;
}
else if(dX<0 && dY<0)
{
if(abs(dX)>abs(dY))
zone=4;
else
zone=5;
}
else if(dX>=0 && dY<0)
{
if(dX>abs(dY))
zone=7;
else
zone=6;
}
}

void drawLine(int x0, int y0, int x1, int y1, int zone)
{
int dX = x1-x0;
int dY = y1-y0;

int x=x0,y=y0;
int d= 2*dY-dX;

int dE=2*dY,dNE=2*(dY-dX);

// glVertex2i(x,y);

while(x<x1)
{

if(d<0)
{
x++;
d+=dE;
}
else
{
x++;
y++;
d+=dNE;
}
if(zone==0)
glVertex2i(x,y);
else if(zone==1)
glVertex2i(y,x);
else if(zone==2)
glVertex2i(-y,x);
else if(zone==3)
glVertex2i(-x,y);
else if(zone==4)
glVertex2i(-x,-y);
else if(zone==5)
glVertex2i(-y,-x);
else if(zone==6)
glVertex2i(y,-x);
else if(zone==7)
glVertex2i(x,-y);

void lineDraw(int x0, int y0, int x1, int y1)


{
get_zone(x0,y0,x1,y1);
if(zone==0){
drawLine(x0,y0,x1,y1,zone);
}
else if(zone==1){
drawLine(y0,x0,y1,x1,zone);
}
else if(zone==2){
// glColor3f(0,0,1);
drawLine(y0,-x0,y1,-x1,zone);
}
else if(zone==3){
// glColor3f(1,1,0);
drawLine(-x0,y0,-x1,y1,zone);
}
else if(zone==4){
// glColor3f(1,0,1);
drawLine(-x0,-y0,-x1,-y1,zone);
}
else if(zone==5){
// glColor3f(0,1,1);
drawLine(-y0,-x0,-y1,-x1,zone);
}
else if(zone==6){
// glColor3f(1,1,1);
drawLine(-y0,x0,-y1,x1,zone);
}
else if(zone==7){
// glColor3f(0.6,0.5,0.4);
drawLine(x0,-y0,x1,-y1,zone);
}

void mouse(int button , int state , int mousex, int mousey){


int x, y;
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
check=true;
x=mousex-320;
y=240-mousey;

if(click==0){
x0Click=x0=x;
y0Click=y0=y;
click=1;
nxyget=0;
}
else if(click ==1 ){
x1Click=x1=x;
y1Click=y1=y;
click=2;

writen=false;
}
else{
x0Click=x0=x;
y0Click=y0=y;
click =1;
nxyget=0;
}
printf("%d %d %d %d\n",x0,y0,x1,y1);
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
check=false;
}
glutPostRedisplay();
}

void CohenSutherLand(){
int code0, code1,code;
int x, y;
int top=8,bottom=4,left=1,right=2;
code0=makeCode(x0,y0);
code1=makeCode(x1,y1);

while(1){
if(!(code0|code1)){
nxyget=1;
lineDraw(x0,y0,x1,y1);
if(!writen)
printf("Completely Accepted\n"),writen=true;
break;
}
else if(code0 & code1){
if(!writen)
printf("Completely Rejected\n"),writen=true;

break;
}
else{
if(!writen)
printf("Partially Accepted\n"),writen=true;
if(code0)code=code0;
else code=code1;

if(code & top){


y=ymax;
x=x0+((y-y0)*(x1-x0)) / (y1-y0);

}
else if(code & bottom){
y=ymin;
x=x0+((y-y0)*(x1-x0)) / (y1-y0);
}
else if(code & left){
x=xmin;
y=y0+((x-x0)*(y1-y0)) / (x1-x0);
}
else{
x=xmax;
y=y0+((x-x0)*(y1-y0)) / (x1-x0);
}

if(code==code0){
x0=x;y0=y;
code0=makeCode(x0,y0);
}
else if(code==code1){
x1=x;y1=y;
code1=makeCode(x1,y1);
}
}
}
}

static void resize(int width, int height){


const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-320, 319, -240, 239, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}

static void display(void){


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,1,1);
glBegin(GL_LINES);
glVertex2i(-320, 0);
glVertex2i(319, 0);
glVertex2i(0, -240);
glVertex2i(0, 239);

glVertex2i(xl+gp,yl+gp);
glVertex2i(xl+gp,yr-gp);

glVertex2i(xl+gp,yr-gp);
glVertex2i(xr-gp,yr-gp);

glVertex2i(xr-gp,yr-gp);
glVertex2i(xr-gp,yl+gp);

glVertex2i(xr-gp,yl+gp);
glVertex2i(xl+gp,yl+gp);

glEnd();
glPointSize(5.0);
glBegin(GL_POINTS);
if(click==1){
glVertex2i(x0Click,y0Click);
}
else if(nxyget==0 and click==2){
glVertex2i(x0Click,y0Click);
glVertex2i(x1Click,y1Click);
}
else if(nxyget == 1 and click==2){
glVertex2i(x0Click,y0Click);
glVertex2i(x1Click,y1Click);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
}

glEnd();
glPointSize(1.0);
glBegin(GL_POINTS);
if(click==2) {
glColor3f(1,0,0);
lineDraw(x0Click,y0Click,x1Click,y1Click);
glColor3f(1,1,1);
CohenSutherLand();
}

glEnd();
glutSwapBuffers();
}

static void key(unsigned char key, int x, int y)


{
switch (key)
{
case 27 :
case 'q':
exit(0);
break;

case '+':
slices++;
stacks++;
break;

case '-':
if (slices>3 && stacks>3)
{
slices--;
stacks--;
}
break;
}
glutPostRedisplay();
}

static void idle(void)


{
glutPostRedisplay();
}

/* Program entry point */

int main(int argc, char *argv[]){


glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("Experiment 01");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutMainLoop();
return EXIT_SUCCESS;
}

You might also like