Solved

# Open GL

Posted on 2001-06-29
1,121 Views
Hi ,
I am working on implementing a line clipping algorithm by cohen -sutherland.The below code is using Open GL.

Could somebody look at it and let me know how to fix these errors ive cleaned up most of it but not successful yet.

The code :
---------------------------------------------------

#include <windows.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>

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

struct point {
int x ;
int y ; } ;

struct segment {
point pi ;
point pf ; } ;

struct rectangle {
point sg ;
point id ; } ;

static int aff = 0 ;
static segment s = { -5,0,5,0 } ;
static int p1 = 1 ;

void traceSegment(int xi,int yi,int xf,int yf,float *c) {
glColor3fv(c) ;
glBegin(GL_LINES) ;
glVertex2f((float) xi,(float) yi) ;
glVertex2f((float) xf,(float) yf) ;
glEnd() ;
}

void pixel(int x,int y,float *c) {
glColor3fv(c) ;
glVertex2f(x-0.5F,y-0.5F) ;
glVertex2f(x-0.5F,y+0.5F) ;
glVertex2f(x+0.5F,y+0.5F) ;
glVertex2f(x+0.5F,y-0.5F) ;
glEnd() ;
}

void line(int xi,int yi,int xf,int yf,float *c) {
int dx,dy,i,xinc,yinc,cumul,x,y ;
x = xi ;
y = yi ;
dx = xf - xi ;
dy = yf - yi ;
xinc = ( dx > 0 ) ? 1 : -1 ;
yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
pixel(x,y,c) ;
if ( dx > dy ) {
cumul = dx / 2 ;
for ( i = 1 ; i <= dx ; i++ ) {
x += xinc ;
cumul += dy ;
if (cumul >= dx) {
cumul -= dx ;
y += yinc ; }
pixel(x,y,c) ; } }
else {
cumul = dy / 2 ;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc ;
cumul += dx ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ; }
pixel(x,y,c) ; } }
}

void traceRectangle(rectangle *r,float *c) {
line(r->id.x,r->id.y,r->id.x,r->sg.y,c) ;
line(r->id.x,r->sg.y,r->sg.x,r->sg.y,c) ;
line(r->id.x,r->id.y,r->sg.x,r->id.y,c) ;
line(r->sg.x,r->id.y,r->sg.x,r->sg.y,c) ;
}

int code(point *p,rectangle *r) {
int c ;
if ( p->x < r->sg.x )
c = 1 ;
else
c = 0 ;
if ( p->x > r->id.x )
c += 2 ;
if ( p->y < r->sg.y )
c += 4 ;
if ( p->y > r->id.y )
c += 8 ;
return(c) ;
}

int code_nul(int c) {
return(c == 0) ;
}

int pas1commun(int c1,int c2) {
return((c1&c2) == 0) ;
}

int code0(int c) {
return(c&1) ;
}

int code1(int c) {
return(c&2) ;
}

int code2(int c) {
return(c&4) ;
}

int code3(int c) {
return(c&8) ;
}

int intersection(int ai,int bi,int af,int bf,int val) {
int res = 0 ;
if ( af-ai != 0 )
res =(int) (bi +(double) (val-ai) / (af-ai) * (bf-bi)) ;
else
res = 32767 ;
return(res) ;
}

void clipperSegment(rectangle *r,segment *s,float *c) {
segment se = *s ;
int c1,c2 ;
point p ;
c1 = code(&s->pi,r) ;
c2 = code(&s->pf,r) ;
while ( ( !code_nul(c1) || !code_nul(c2) ) && ( pas1commun(c1,c2)  ) ) {
if ( code_nul(c1) ) {
p = s->pi ;
s->pi = s->pf ;
s->pf = p ;
c1 = c2 ; }
if ( code0(c1) ) {
s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->sg.x) ;
s->pi.x = r->sg.x ; }
else
if ( code1(c1) ) {
s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->id.x) ;
s->pi.x = r->id.x ; }
else
if ( code2(c1) ) {
s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->sg.y) ;
s->pi.y = r->sg.y ; }
else
if ( code3(c1) ) {
s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->id.y) ;
s->pi.y = r->id.y ; }
c1 = code(&s->pi,r) ;
c2 = code(&s->pf,r) ; }
if ( code_nul(c1) && code_nul(c2) )
line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,c) ;
*s = se ;
}

void CALLBACK display() {
float black[] = { 0.0F,0.0F,0.0F,1.0F };
float red[] = { 1.0F,0.0F,0.0F,1.0F };
float white[] = { 0.0F,1.0F,0.0F,1.0F };
float blue[] = { 0.0F,0.0F,1.0F,1.0F } ;
float orange[] = { 1.0F,1.0F,0.0F,1.0F };
float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
float green[] = { 1.0F,1.0F,1.0F,1.0F };
rectangle r = { -17,-13,16,11 } ;
segment s1 = { -20,-18,6,1 } ;
segment s2 = { -25,3,13,18 } ;
segment s3 = { 23,13,27,-17 } ;
segment s4 = { 13,8,-11,3 } ;
segment s5 = { 23,-9,11,-18 } ;
segment s6 = { 16,-7,16,19 } ;
segment s7 = { -6,-13,9,-13 } ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
switch ( aff) {
case 0 : traceRectangle(&r,blue) ;
line(s1.pi.x,s1.pi.y,s1.pf.x,s1.pf.y,white) ;
line(s2.pi.x,s2.pi.y,s2.pf.x,s2.pf.y,red) ;
line(s3.pi.x,s3.pi.y,s3.pf.x,s3.pf.y,orange) ;
line(s4.pi.x,s4.pi.y,s4.pf.x,s4.pf.y,black) ;
line(s5.pi.x,s5.pi.y,s5.pf.x,s5.pf.y,green) ;
line(s6.pi.x,s6.pi.y,s6.pf.x,s6.pf.y,cyan) ;
line(s7.pi.x,s7.pi.y,s7.pf.x,s7.pf.y,magenta) ;
break ;
case 1 : traceRectangle(&r,blue) ;
clipperSegment(&r,&s1,white) ;
clipperSegment(&r,&s2,red) ;
clipperSegment(&r,&s3,orange) ;
clipperSegment(&r,&s4,black) ;
clipperSegment(&r,&s5,green) ;
clipperSegment(&r,&s6,cyan) ;
clipperSegment(&r,&s7,magenta) ;
break ;
case 2 : traceRectangle(&r,blue) ;
clipperSegment(&r,&s,red) ;
pixel(s.pi.x,s.pi.y,orange) ;
pixel(s.pf.x,s.pf.y,orange) ;
break ; }
glPopMatrix();
glFlush();
auxSwapBuffers() ;
}

void CALLBACK reshape(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glOrtho(-30.0,30.0,-20.0,20.0,-40.0,40.0);
glMatrixMode(GL_MODELVIEW);
}

void CALLBACK auxKeyReturn() {
aff++ ;
if ( aff == 3 )
aff = 0 ;
}

void CALLBACK auxKeyEspace() {
p1 = !p1 ;
}

void CALLBACK auxKeyUp() {
if ( p1 ) {
s.pi.y++ ;
if ( s.pi.y > 20 )
s.pi.y = 20 ; }
else {
s.pf.y++ ;
if ( s.pf.y > 20 )
s.pf.y = 20 ; }
}

void CALLBACK auxKeyDown() {
if ( p1 ) {
s.pi.y-- ;
if ( s.pi.y < -20 )
s.pi.y = -20 ; }
else {
s.pf.y-- ;
if ( s.pf.y < -20 )
s.pf.y = -20 ; }
}

void CALLBACK auxKeyRight() {
if ( p1 ) {
s.pi.x++ ;
if ( s.pi.x > 30 )
s.pi.x = 30 ; }
else {
s.pf.x++ ;
if ( s.pf.x > 30 )
s.pf.x = 30 ; }
}

void CALLBACK auxKeyLeft() {
if ( p1 ) {
s.pi.x-- ;
if ( s.pi.x < -30 )
s.pi.x = -30 ; }
else {
s.pf.x-- ;
if ( s.pf.x < -30 )
s.pf.x = -30 ; }
}

void CALLBACK init() {
glClearColor(0.8F,0.8F,0.8F,1.0F);
}

void main(void) {
auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH);
auxInitPosition (0,0,300,200);
auxInitWindow("Clipping For Cohen-Sutherland") ;
init();
auxKeyFunc(AUX_RETURN,auxKeyReturn) ;
auxKeyFunc(AUX_SPACE,auxKeyEspace) ;
auxKeyFunc(AUX_UP,auxKeyUp) ;
auxKeyFunc(AUX_DOWN,auxKeyDown) ;
auxKeyFunc(AUX_LEFT,auxKeyLeft) ;
auxKeyFunc(AUX_RIGHT,auxKeyRight) ;
auxReshapeFunc(reshape);
auxMainLoop(display);
}

0
Question by:mjackson110
• 5
• 2

LVL 2

Expert Comment

ID: 6240304
That's a lot of code... Can you post the errors you are receiving? (Line number and error)
0

LVL 2

Expert Comment

ID: 6240315
It looked like it would take a while to sort out what all of the functions are meant to do (especially without many comments).  So I glanced at some stuff.

Are you meaning to use a bitwise AND (&) in your functions, or is it supposed to be a logical AND (&&)?
Like I said... the variables are completely undescriptive (c1, c2, p, s, etc.) so I have no idea what is supposed to be accomplished, and therefore have no idea what the functions are supposed to do (because they, too, have very meaningless names like code0() and code1())

Example... you have...
----------------------------------
int pas1commun(int c1,int c2) {
return((c1&c2) == 0) ;
}
----------------------------------

Should it be
----------------------------------
int pas1commun(int c1,int c2) {
return((c1 && c2) == 0) ;
}
----------------------------------

0

Author Comment

ID: 6243233
Hi ,
The idea is to draw a line inside the rectangle and then apply the cohen sutherland algorithm ,which would check to see what part of the line is out of the rectangle and then clip that part and only display what is inside the rectangle.
The errors that i got is :
-----------------
--------------------Configuration: clipping - Win32 Debug--------------------
clipping.obj : error LNK2001: unresolved external symbol __imp__glEnd@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glVertex2f@8
clipping.obj : error LNK2001: unresolved external symbol __imp__glBegin@4
clipping.obj : error LNK2001: unresolved external symbol __imp__glColor3fv@4
clipping.obj : error LNK2001: unresolved external symbol _auxSwapBuffers@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glFlush@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glPopMatrix@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glPushMatrix@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glClear@4
clipping.obj : error LNK2001: unresolved external symbol __imp__glOrtho@48
clipping.obj : error LNK2001: unresolved external symbol __imp__glLoadIdentity@0
clipping.obj : error LNK2001: unresolved external symbol __imp__glMatrixMode@4
clipping.obj : error LNK2001: unresolved external symbol __imp__glViewport@16
clipping.obj : error LNK2001: unresolved external symbol __imp__glClearColor@16
clipping.obj : error LNK2001: unresolved external symbol __imp__glShadeModel@4
clipping.obj : error LNK2001: unresolved external symbol _auxMainLoop@4
clipping.obj : error LNK2001: unresolved external symbol _auxReshapeFunc@4
clipping.obj : error LNK2001: unresolved external symbol _auxKeyFunc@8
clipping.obj : error LNK2001: unresolved external symbol _auxInitWindowA@4
clipping.obj : error LNK2001: unresolved external symbol _auxInitPosition@16
clipping.obj : error LNK2001: unresolved external symbol _auxInitDisplayMode@4
Debug/clipping.exe : fatal error LNK1120: 21 unresolved externals

clipping.exe - 22 error(s), 0 warning(s)
0

LVL 2

Expert Comment

ID: 6243797
This is a classic case of not linking to the libraries properly.

Make sure that you link to opengl32.lib, glu32.lib, and glaux.lib.  If you are using VisualC++, you can do this on the menu bar at Project->Settings.  You will see a field on the right side that has a lot of library names separated with spaces.  Just add them there.

Once you are linked to the libraries, you will find out if you have any ACTUAL errors in your code.  Those errors just mean that it doesn't know what the functions are (because the functions are contained in the libraries, which are not linked).
0

LVL 2

Accepted Solution

smitty1276 earned 100 total points
ID: 6260227
Hello?
0

LVL 2

Expert Comment

ID: 6262982
Hey, a little feedback would be nice.
0

Author Comment

ID: 6266368
smitty1276,
This is a classic case of not linking to the libraries properly.
you were right
thanks so much i fixed the errors once this problem was solved and it works great now.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Simple Linear Regression
Progress