🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Why is OpenGL drawing a straight line between my bezier paths?

Started by
9 comments, last by Ultraporing 2 years, 1 month ago

I have created a bezier curve tool and each time I draw a bezier curve segment I get a straight line between each mouse click. I don't understand why this is happening and was wondering if someone could take a look at my render function and see the error. This forum is my last hope, I've tried Reddit and the Computer Graphics Stack Exchange.

void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);

    //vector<Point> bezierPoints;

    if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
    {
        drawLine(pointsVector[points], Cursor);
    }

    for (int i = 1; i < points; i++)
    {
        // draw main line & dot
        glColor3f(255, 0, 0);
        drawDot(pointsVector[i - 1]);

        // draw tangent lines and tangent dots
        glColor3f(0, 255, 0);
        drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
        glColor3f(0, 0, 255);
        drawDot(controlPoints[i + 1]);
        drawDot(reverseControlPoints[i + 1]);

        vector<Point> finalPoints = final4Points();
        vector<Point> finalPointsReverseP1 = final4PointsReverseP1();

        Point p1;
        if (firstSegmentDrawn == false)
        {
            p1.setxy(pointsVector[0].x, pointsVector[0].y);
            bezierPointsMyDisplay.push_back(p1);
        }
        else
        {
            p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
            bezierPointsMyDisplay.push_back(p1);
        }

        if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
        {
            if (firstSegmentDrawn == true)
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 100; j++)
                {
                    i = j / 100;
                    // The Green Lines
                    int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
                    int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
                    int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
                    int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
                    int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
                    int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);
                    //drawDot(p1);

                    p1 = p2;
                    bezierPointsMyDisplay.push_back(p2);
                }
            }
            else
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 100; j++)
                {
                    i = j / 100;
                    // The Green Lines
                    int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
                    int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
                    int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
                    int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
                    int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
                    int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);
                    //drawDot(p1);

                    p1 = p2;
                    bezierPointsMyDisplay.push_back(p2);
                }
            }
        }
    }

    for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
    {
        //drawDot(bezierPointsMyDisplay[i]);
        drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
    }

    glutSwapBuffers();
}
Advertisement

I can only guess that you draw it because the rest of your code is missing but it looks like you push the endpoint of the line into the vector before all the bezier drawing and adding of their points. here:

Point p1;
if (firstSegmentDrawn == false)
{
  p1.setxy(pointsVector[0].x, pointsVector[0].y);
  bezierPointsMyDisplay.push_back(p1);
}
else
{
  p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
  bezierPointsMyDisplay.push_back(p1);
}

And then straight up drawing the line beginning and endpoint connected, before all the bezier line segments.

    for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
    {
        //drawDot(bezierPointsMyDisplay[i]);
        drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
    }

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

@Ultraporing Thanks for the response. I was looking at those lines as well but I don't know what to replace them with. Do you think you could show me the code? Here's the full code:

#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <vector>

using namespace std;


//Point class for taking the points
class Point {
public:
    float x, y;

    void setxy(float x2, float y2)
    {
        x = x2; y = y2;
    }
    //operator overloading for '=' sign
    const Point& operator=(const Point& rPoint)
    {
        x = rPoint.x;
        y = rPoint.y;
        return *this;
    }

};

int SCREEN_HEIGHT = 500;
int points = 0;
Point Cursor;
vector<Point> pointsVector(100);
vector<Point> controlPoints(100);
int controlPointCount;
vector<Point> reverseControlPoints(100);
bool firstSegmentDrawn = new bool(false);

vector<Point> bezierPointsMyDisplay;

void myInit() {
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);
    glPointSize(3);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 500.0);
}

void drawDot(Point p1) {
    glBegin(GL_POINTS);
    glVertex2i(p1.x, p1.y);
    glEnd();
}

void drawLine(Point p1, Point p2) {
    glBegin(GL_LINES);
    glVertex2f(p1.x, p1.y);
    glVertex2f(p2.x, p2.y);
    glEnd();
}

vector<Point> final4Points()
{
    vector<Point> Points;

    Points.push_back(pointsVector[points - 1]); // 0
    Points.push_back(reverseControlPoints[points - 1]); // 1
    Points.push_back(reverseControlPoints[points]); // 2
    Points.push_back(pointsVector[points]); // 3

    return Points;
}

vector<Point> final4PointsReverseP1()
{
    vector<Point> Points;

    Points.push_back(pointsVector[points - 1]); // 0
    Points.push_back(controlPoints[points - 1]); // 1
    Points.push_back(reverseControlPoints[points]); // 2
    Points.push_back(pointsVector[points]); // 3

    return Points;
}

int getPt(int n1, int n2, float perc)
{
    int diff = n2 - n1;

    return n1 + (diff * perc);
}

void myMouse(int button, int state, int x, int y)
{
    // If left button was clicked
    if (button == GLUT_LEFT_BUTTON)
    {
        if (state == GLUT_DOWN)
        {
            Point point;
            point.setxy(x, SCREEN_HEIGHT - y);
            // Draw the red  dot.
            glColor3f(255, 0, 0);
            drawDot(point);
            if ((pointsVector[0].x == NULL) && (pointsVector[0].y == NULL))
            {
                pointsVector[0] = point;
            }
            points++;
            if (points > 2)
            {
                firstSegmentDrawn = true;
            }
            pointsVector[points].setxy((float)x, (float)(SCREEN_HEIGHT - y));
        }
        else if (state == GLUT_UP)
        {
            Point Handle;
            Handle.setxy(x, SCREEN_HEIGHT - y);
            controlPoints[0] = pointsVector[0];
            controlPoints[points].setxy(Handle.x, Handle.y);
            glColor3f(0, 255, 0);
            drawDot(Handle);

            Point ReverseHandle;
            reverseControlPoints[0] = pointsVector[0];
            ReverseHandle.x = (2 * pointsVector[points].x) - Handle.x;
            ReverseHandle.y = (2 * pointsVector[points].y) - Handle.y;
            reverseControlPoints[points].setxy(ReverseHandle.x, ReverseHandle.y);
            glColor3f(0, 255, 0);
            drawDot(ReverseHandle);

            glColor3f(0, 0, 255);
        }
    }
}

void passiveMotion(int x, int y)
{
    glClear(GL_COLOR_BUFFER_BIT);

    Cursor.setxy(x, SCREEN_HEIGHT - y);
    if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
    {
        drawLine(pointsVector[points], Cursor);
    }

    for (int i = 1; i < points; i++)
    {
        // draw main line & dot
        glColor3f(255, 0, 0);
        drawDot(pointsVector[i - 1]);

        // draw tangent lines and tangent dots
        glColor3f(0, 255, 0);
        drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
        glColor3f(0, 0, 255);
        drawDot(controlPoints[i + 1]);
        drawDot(reverseControlPoints[i + 1]);

        vector<Point> finalPoints = final4Points();
        vector<Point> finalPointsReverseP1 = final4PointsReverseP1();

        Point p1;
        Point p2;

        if (firstSegmentDrawn == false)
        {
            p1.setxy(pointsVector[0].x, pointsVector[0].y);
        }
        else
        {
            //The first segment has been drawn.
            p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
        }

        if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
        {
            if (firstSegmentDrawn == true)
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;

                    // The Green Lines
                    int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
                    int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
                    int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
                    int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
                    int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
                    int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);

                    p1 = p2;
                }
            }
            else
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;
                    // The Green Lines
                    int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
                    int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
                    int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
                    int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
                    int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
                    int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    //Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);

                    p1 = p2;
                }
            }
        }

    }

    for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
    {
        drawDot(bezierPointsMyDisplay[i]);
        //drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
    }

    glutSwapBuffers();
}

void motion(int x, int y)
{
    glClear(GL_COLOR_BUFFER_BIT);

    Point Cursor;
    Cursor.setxy(x, SCREEN_HEIGHT - y);
    Point reverseCursor;
    reverseCursor.x = (2 * pointsVector[points].x) - Cursor.x;
    reverseCursor.y = (2 * pointsVector[points].y) - Cursor.y;
    glColor3f(0, 255, 0);
    drawLine(pointsVector[points], Cursor);
    drawLine(pointsVector[points], reverseCursor);

    glColor3f(0, 0, 255);
    drawDot(Cursor);
    drawDot(reverseCursor);

    vector<Point> finalPoints;

    if (firstSegmentDrawn == true)
    {
        finalPoints = final4PointsReverseP1();
    }
    else
    {
        finalPoints = final4Points();
    }

    Point p1;
    if (firstSegmentDrawn == false)
    {
        p1.setxy(pointsVector[0].x, pointsVector[0].y);
    }
    else
    {
        p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
    }

    if ((finalPoints[1].x != NULL) && (finalPoints[1].y != NULL))
    {
        float i;
        // draw bezier curve
        for (float j = 0; j <= 10; j++)
        {
            i = j / 10;
            // The Green Lines
            int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
            int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
            int xb = getPt(finalPoints[1].x, reverseCursor.x, i);
            int yb = getPt(finalPoints[1].y, reverseCursor.y, i);
            int xc = getPt(reverseCursor.x, finalPoints[3].x, i);
            int yc = getPt(reverseCursor.y, finalPoints[3].y, i);

            // The Blue Line
            int xm = getPt(xa, xb, i);
            int ym = getPt(ya, yb, i);
            int xn = getPt(xb, xc, i);
            int yn = getPt(yb, yc, i);

            // The Black Dot
            int x2 = getPt(xm, xn, i);
            int y2 = getPt(ym, yn, i);

            Point p2;
            p2.setxy(x2, y2);

            drawLine(p1, p2);
            //drawDot(p1);

            p1 = p2;
        }
    }

    for (int i = 1; i < points; i++)
    {
        // draw main line & dot
        glColor3f(255, 0, 0);
        drawDot(pointsVector[i - 1]);
        glColor3f(0, 0, 0);
        //drawLine(pointsVector[i], pointsVector[i + 1]);

        // draw tangent lines and tangent dots
        glColor3f(0, 255, 0);
        drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
        glColor3f(0, 0, 255);
        drawDot(controlPoints[i + 1]);
        drawDot(reverseControlPoints[i + 1]);

        vector<Point> finalPoints = final4Points();
        vector<Point> finalPointsReverseP1 = final4PointsReverseP1();

        Point p1;
        p1.setxy(NULL, NULL);
        if (firstSegmentDrawn == false)
        {
            p1.setxy(pointsVector[0].x, pointsVector[0].y);
        }
        else
        {
            //The first segment has been drawn.
            p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
        }

        if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
        {
            if (firstSegmentDrawn == true)
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;

                    // The Green Lines
                    int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
                    int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
                    int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
                    int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
                    int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
                    int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);

                    p1 = p2;
                }
            }
            else
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;
                    // The Green Lines
                    int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
                    int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
                    int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
                    int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
                    int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
                    int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);
                    //drawDot(p1);

                    p1 = p2;
                }
            }
        }
    }

    for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
    {
        drawDot(bezierPointsMyDisplay[i]);
        //drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
    }

    glutSwapBuffers();
}

void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);

    if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
    {
        drawLine(pointsVector[points], Cursor);
    }

    for (int i = 1; i < points; i++)
    {
        // draw main line & dot
        glColor3f(255, 0, 0);
        drawDot(pointsVector[i - 1]);

        // draw tangent lines and tangent dots
        glColor3f(0, 255, 0);
        drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
        glColor3f(0, 0, 255);
        drawDot(controlPoints[i + 1]);
        drawDot(reverseControlPoints[i + 1]);

        vector<Point> finalPoints = final4Points();
        vector<Point> finalPointsReverseP1 = final4PointsReverseP1();

        Point p1;
        if (firstSegmentDrawn == false)
        {
            p1.setxy(pointsVector[0].x, pointsVector[0].y);
            bezierPointsMyDisplay.push_back(p1);
        }
        else
        {
            p1.setxy(pointsVector[points - 1].x, pointsVector[points - 1].y);
            bezierPointsMyDisplay.push_back(p1);
        }

        if ((finalPoints[2].x != NULL) && (finalPoints[2].y != NULL))
        {
            if (firstSegmentDrawn == true)
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;
                    // The Green Lines
                    int xa = getPt(finalPointsReverseP1[0].x, finalPointsReverseP1[1].x, i);
                    int ya = getPt(finalPointsReverseP1[0].y, finalPointsReverseP1[1].y, i);
                    int xb = getPt(finalPointsReverseP1[1].x, finalPointsReverseP1[2].x, i);
                    int yb = getPt(finalPointsReverseP1[1].y, finalPointsReverseP1[2].y, i);
                    int xc = getPt(finalPointsReverseP1[2].x, finalPointsReverseP1[3].x, i);
                    int yc = getPt(finalPointsReverseP1[2].y, finalPointsReverseP1[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);
                    //drawDot(p1);

                    p1 = p2;
                    bezierPointsMyDisplay.push_back(p2);
                }
            }
            else
            {
                float i;
                // draw bezier curve
                for (float j = 0; j <= 10; j++)
                {
                    i = j / 10;
                    // The Green Lines
                    int xa = getPt(finalPoints[0].x, finalPoints[1].x, i);
                    int ya = getPt(finalPoints[0].y, finalPoints[1].y, i);
                    int xb = getPt(finalPoints[1].x, finalPoints[2].x, i);
                    int yb = getPt(finalPoints[1].y, finalPoints[2].y, i);
                    int xc = getPt(finalPoints[2].x, finalPoints[3].x, i);
                    int yc = getPt(finalPoints[2].y, finalPoints[3].y, i);

                    // The Blue Line
                    int xm = getPt(xa, xb, i);
                    int ym = getPt(ya, yb, i);
                    int xn = getPt(xb, xc, i);
                    int yn = getPt(yb, yc, i);

                    // The Black Dot
                    int x2 = getPt(xm, xn, i);
                    int y2 = getPt(ym, yn, i);

                    Point p2;
                    p2.setxy(x2, y2);

                    drawLine(p1, p2);
                    //drawDot(p1);

                    p1 = p2;
                    bezierPointsMyDisplay.push_back(p2);
                }
            }
        }
    }

    for (int i = 1; i < bezierPointsMyDisplay.size(); i++)
    {
        //drawDot(bezierPointsMyDisplay[i]);
        //printf("%f, %f -> %f, %f\n", bezierPointsMyDisplay[i - 1].x, bezierPointsMyDisplay[i - 1].y, 
            //bezierPointsMyDisplay[i].x, bezierPointsMyDisplay[i].y);
        drawLine(bezierPointsMyDisplay[i - 1], bezierPointsMyDisplay[i]);
    }

    glutSwapBuffers();
}

void timer(int)
{
    glutTimerFunc(1000 / 60, timer, 0);
    glutPostRedisplay();
}

int main(int argc, char* argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(640, 500);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("Bezier Curve");
    glutDisplayFunc(myDisplay);
    glutIdleFunc(myDisplay);
    glutTimerFunc(0, timer, 0);
    glutMouseFunc(myMouse);
    glutPassiveMotionFunc(passiveMotion);
    glutMotionFunc(motion);
    myInit();
    glutMainLoop();

    return 0;
}

@distinct_cabinet I did run your program and noticed more problems.
The most important one at the moment is that you continue to add bezierPoints into the bezierPointsMyDisplay vector once you clicked for the first time. Those are 5 clicks and its already at 24946 bezierPoints as shown in the screenshot. This NEEDS to be fixed because it will crash your program once there are enough of them in the vector.

I can't fix your code at the moment because I have to go to work, but I would advice to completely refactor it or start over. You are doing a lot of redundant render calls and point adding per clicked point every frame. Maybe someone else can repair it.

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

I think this does warrant a start over. Too much code can be simplified. This time I can take the correct implementation steps as opposed to improvising each time. I will update you on the progress.

distinct_cabinet said:

I think this does warrant a start over. Too much code can be simplified. This time I can take the correct implementation steps as opposed to improvising each time. I will update you on the progress.

Great choice ?. I would also advice you to split your rendering and update logic so you can for example add or change your line segment start and end positions when clicking, dragging or whenever you want and the render function just deals with putting them on the screen with draw calls. And keeping track of the blue line and the Tangents separately in their own vectors will help with this too. As well as reusing your calculated Line Segments (putting it into global scope for example) so you only have to change their positional values when dragging the Tangents. Also if you redraw only when something in the data that gets rendered changes, then its even more efficient.

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

@Ultraporing I struggled with that the first time through. How do I get functions like passiveMotion and Motion to draw in the render function? Is it just about passing data? I think I did this once but the data was not updating fast enough.

distinct_cabinet said:

@Ultraporing I struggled with that the first time through. How do I get functions like passiveMotion and Motion to draw in the render function? Is it just about passing data? I think I did this once but the data was not updating fast enough.

Here you can use this answer: https://community.khronos.org/t/glutdisplayfunc-not-updating/15480/3

Only use the display function to actually render stuff, and just manipulate data with the events ect. and trigger a redraw like in the answer there.
The render function should redraw the updated data anyways when a redraw is triggered.
Sadly glut is entirely event based and does not like it if you hammer the redraw function manually quickly like you do for games n stuff.

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

Hey man, I tried re-writing it but it was an even bigger mess. The program works fine if I draw the curve with pixels. I've tried Stack Overflow, Reddit, and Computer Graphics Stack Exchange but no one can solve this although we did break through a lot of bonus bugs along the way.

If you think you have any ideas please don't hesitate to drop them below, I will definitely try them.

distinct_cabinet said:

Hey man, I tried re-writing it but it was an even bigger mess. The program works fine if I draw the curve with pixels. I've tried Stack Overflow, Reddit, and Computer Graphics Stack Exchange but no one can solve this although we did break through a lot of bonus bugs along the way.

If you think you have any ideas please don't hesitate to drop them below, I will definitely try them.

Hi, I googled a bit since I've never done a project like yours and found a website with code examples + binaries. So you can basically take a look and pick what you need. The Curve examples look especially interesting.
Link: https://nccastaff.bournemouth.ac.uk/jmacey/OldWeb/RobTheBloke/www/opengl_programming.html

“It's a cruel and random world, but the chaos is all so beautiful.”
― Hiromu Arakawa

This topic is closed to new replies.

Advertisement