Question

Open GL

Asked by: mjackson110

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) ;
  glBegin(GL_QUADS) ;
  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);
  glLoadIdentity();
  glOrtho(-30.0,30.0,-20.0,20.0,-40.0,40.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

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() {
  glShadeModel(GL_SMOOTH);
  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);
}

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2001-06-29 at 13:48:52ID20143643
Tags

error

Topic

Miscellaneous Programming

Participating Experts
1
Points
100
Comments
7

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Open Gl
    i want to ask, how to compile the example(source code) in open gl programming, i'm using visual c++ 5 and i've also include the gl and opengl library. but it still has error. so what do we need to configure before compile? by the way i'm new in using vC++.
  2. Open GL/Direkt X
    Does somebody know some good Open GL/Direkt X tutorials?
  3. Open GL subsystem
    I have a game that when I try to load it I get an erro message "cannot open open open gl susbsytem. I have a Nvidia GeForce 200 card. The game was working but now will not. what has happened?
  4. open gl
    I just reinstalled my windows xp, but now when I try to play my games, it says that my graphics card will not support the open gl mode (I have a geForce 2). I've tried to reinstall the driver and change the resolution but it still won't work

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: smitty1276Posted on 2001-06-29 at 23:27:38ID: 6240304

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

 

by: smitty1276Posted on 2001-06-29 at 23:37:49ID: 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) ;
}
----------------------------------

 

by: mjackson110Posted on 2001-07-01 at 15:45:42ID: 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--------------------
Linking...
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
Error executing link.exe.

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

 

by: smitty1276Posted on 2001-07-01 at 23:13:53ID: 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).

 

by: smitty1276Posted on 2001-07-06 at 11:40:31ID: 6260227

Hello?

 

by: smitty1276Posted on 2001-07-08 at 11:50:39ID: 6262982

Hey, a little feedback would be nice.

 

by: mjackson110Posted on 2001-07-09 at 11:53:17ID: 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.

Sorry about the delay in replying

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...