Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problems with clipping lines


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 chrisprij   Members   -  Reputation: 156

Like
0Likes
Like

Posted 04 April 2013 - 01:23 PM

Hello! Working on a software renderer here. Pardon the poorly made application, this is my "quick and dirty" startup for this renderer. Recently I've been working on clipping projected lines to the screen. Unfortunately, it's not working very well . . . for some sides of the screen.

 

Attached is a video clip of me showing the clipping problem of the left and top of the screen. The bottom and right work correctly . . .

Here's my clipping code:

 

void Application::clipAndDrawLine(Vector3 &v1, Vector3 &v2) {
    
    //const int INSIDE = 0;
    const int LEFT = 1;
    const int RIGHT = 2;
    const int BOTTOM = 4;
    const int TOP = 8;
    
    int code1 = computeOutcode(v1);
    int code2 = computeOutcode(v2);
    
    bool render = true;
    
    while(true) {
        if (!(code1 | code2)) {
            break;
        } else if (code1 & code2) {
            render = false;
            break;
        } else {
            float x, y;
            
            int outcodeOut = code1 ? code1 : code2;
            
            if (outcodeOut & TOP) {
                x = v1.x + (v2.x - v1.x) * ((HEIGHT - 1) - v1.y) / (v2.y - v1.y);
                y = HEIGHT - 1;
            } else if (outcodeOut & BOTTOM) {
                x = v1.x + (v2.x - v1.x) * (0 - v1.y) / (v2.y - v1.y);
                y = 0;
            } else if (outcodeOut & RIGHT) {
                y = v1.y + (v2.y - v1.y) * ((WIDTH - 1) - v1.x) / (v2.x - v1.x);
                x = WIDTH - 1;
            } else if (outcodeOut & LEFT) {
                y = v1.y + (v2.y - v1.y) * (0 - v1.x) / (v2.x - v1.x);
                x = 0;
            }
            
            if (outcodeOut == code1) {
                v1.x = x;
                v1.y = y;
                code1 = computeOutcode(v1);
            } else {
                v2.x = x;
                v2.y = y;
                code2 = computeOutcode(v2);
            }
        }
    }
    if (render) {
        renderer.renderLine(v1.x, v1.y, v2.x, v2.y,
                            SDL_MapRGB(screen->format, 255, 255, 255));
    }
    
}

int Application::computeOutcode(Vector3 &v) {
    const int INSIDE = 0;
    const int LEFT = 1;
    const int RIGHT = 2;
    const int BOTTOM = 4;
    const int TOP = 8;
    
    int code = INSIDE;
    
    if (v.x < 0) {
        code |= LEFT;
    }
    if (v.x >= WIDTH) {
        code |= RIGHT;
    }
    if (v.y < 0) {
        code |= BOTTOM;
    }
    if (v.y >= HEIGHT) {
        code |= TOP;
    }
    
    return code;
}

 

I can't find the issue here! Does anything look wrong to any of you guys? I believe the problem is with the clipping, seeing as that's where the problem occurs......

 

-CP

Attached Files



Sponsor:

#2 olivaw   Members   -  Reputation: 119

Like
0Likes
Like

Posted 04 April 2013 - 08:05 PM

Hello, shouldn't you be swapping v1 with v2 when code2 is being used?

I liked what you did there with code1 & code2 test, smart!

#3 chrisprij   Members   -  Reputation: 156

Like
0Likes
Like

Posted 05 April 2013 - 11:13 AM

Hello, shouldn't you be swapping v1 with v2 when code2 is being used?

 

I think I did that right...see the code here:

            if (outcodeOut == code1) {
                v1.x = x;
                v1.y = y;
                code1 = computeOutcode(v1);
            } else {
                v2.x = x;
                v2.y = y;
                code2 = computeOutcode(v2);
            }

 

I've been spending a few days just flipping around v1 and v2 to see if I ever made a mistake but...so far no luck. 

 

 

I liked what you did there with code1 & code2 test, smart!

It's not my code; you can find it on wikipedia under the Cohen-Sutherland algorithm for clipping lines. smile.png






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS