Advertisement

[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

06/29/2001 at 01:48PM PDT, ID: 20143643
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Open GL

Asked by mjackson110 in Miscellaneous Programming

Tags: error

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);
}

View the Solution FREE for 30 Days
[+][-]06/29/01 11:27 PM, ID: 6240304

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/29/01 11:37 PM, ID: 6240315

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/01/01 03:45 PM, ID: 6243233

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/01/01 11:13 PM, ID: 6243797

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/06/01 11:40 AM, ID: 6260227

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Miscellaneous Programming
Tags: error
Sign Up Now!
Solution Provided By: smitty1276
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07/08/01 11:50 AM, ID: 6262982

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/09/01 11:53 AM, ID: 6266368

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090624-EE-VQP-63