Sign in to follow this  
chrisprij

Problems with clipping lines

Recommended Posts

chrisprij    157

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

Share this post


Link to post
Share on other sites
olivaw    126
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!

Share this post


Link to post
Share on other sites
chrisprij    157

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this