简体   繁体   English

OpenGL B-Spline的重量未正确渲染

[英]OpenGL B-Spline with weight isn`t rendered correctly

I'm working on a project with B-Splines in OpenGL. 我正在使用OpenGL中的B-Splines开展一个项目。

The B-Spline itself works as far as I can say but I also want to add some weight to my control-points in a range of 1-10 in order to have the spline go closer to points with higher weight. B-Spline本身就可以说,但是我也希望在1-10的范围内为控制点增加一些重量,以使样条更接近重量更高的点。

The problem that I have right now is that when I add some weight to a point the spline always goes way above that point instead of just getting a little bit closer. 我现在遇到的问题是,当我向一个点添加一些权重时,样条曲线总是高于该点,而不是仅仅稍微靠近一点。

Now what I have tried so far is to implement my program the way it is described in this article . 现在我到目前为止尝试的是按照本文中描述的方式实现我的程序。 The Last topic in this article describes how to implement B-Splines with weight but it doesn't work for me. 本文的最后一个主题描述了如何使用权重实现B-Splines,但它对我不起作用。

This is how I add weight to a control-point. 这就是我如何为控制点增加重量。

I create a control-point with a mouseclick and add weight with the value 1 as the third vector parameter 我使用鼠标点击创建一个控制点,并使用值1作为第三个矢量参数添加权重

def onMouseButton(win, button, action, mods):
    global degree
    global M
    global currentpoint
    global shift
    global yposition
    shift = 0
        if button == glfw.MOUSE_BUTTON_LEFT:
            if action == glfw.PRESS:
                p = np.array(glfw.get_cursor_pos(win))
                p[0] = (p[0] - WIDTH/2) / (WIDTH/2)
                p[1] = 1 - (p[1] / (HEIGHT/2))
                p = np.append(p, 1.0)
                controlpoints.append(p)

Then I divide a selected controlpoint by its old weight to set it back to 1 and then I multiply the point by its new weight. 然后我将选定的控制点除以旧的权重,将其设置为1,然后将该点乘以新的权重。 Currentpoint is the point I have selected to add weight to. Currentpoint是我选择增加重量的点。

def onMouseMove(win, xpos, ypos):
    global shift
    global currentpoint
    global yposition

    if shift == 1:
        for x in controlpoints:
            if np.array_equal(x, currentpoint):
                weight = yposition - ypos
                if weight >= 10:
                    weight = 10
                if weight <= 1:
                    weight = 1
                x[0] /= x[2]
                x[1] /= x[2]
                x[2] = weight
                x[0] *= x[2]
                x[1] *= x[2]
                calcSpline()

And this is how I try to render the spline For rendering I want to ignore the added weight. 这就是我尝试渲染样条曲线的方法。渲染我想忽略增加的重量。

def render():
    weightpoints = []
    for x in controlpoints:
        a = x[0]/x[2]
        b = x[1]/x[2]
        element = [a, b]
        element = np.array(element)
        weightpoints.append(element)
    # draw black control polygon
    draw(GL_LINE_STRIP, weightpoints, [0.0, 0.0, 0.0])
    #draw red control points
    draw(GL_POINTS, weightpoints, [1.0, 0.0, 0.0])
    #draw red control points
    draw(GL_LINE_STRIP, splinepoints, [0.0, 0.0, 1.0])
    # recalculate spline curve
    calcSpline()
    # draw blue bspline deboor points
    draw(GL_POINTS, splinepoints, [0.0, 1.0, 1.0])

def draw(mode, points, color):
    # draws objects
    glPointSize(3)
    glEnable(GL_POINT_SMOOTH)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glBegin(mode)
    glColor(color[0], color[1], color[2])
    for p in points:
        glVertex2f(p[0], p[1])
    glEnd()

UPDATE UPDATE

This is how I calculate the spline. 这就是我计算样条曲线的方法。 The deboor alogrithm and the findR function should be correct scince I have them from a lecture foil. deboor alogrithm和findR函数应该是正确的scince我从讲义箔中得到它们。

def calcSpline():
    # calculates spline curve with deboor
    calcKnotVector()
    del splinepoints[:]
    if len(controlpoints) >= degree:
        t = 0
        while t <= knotvector[-1]:
            r = findR(t)
            b = deBoor(controlpoints, knotvector, t, r, degree - 1)
            splinepoints.append(b)
            t += 1 /float(M - 1)
            t -= 0.0000000001

I Expected the Spline to go closer to the selected point but instead it always grows above it. 我期望Spline更接近选定的点,但它总是超越它。

UPDATE UPDATE

Thanks to Nico Schertler I got the program running and working. 感谢Nico Schertler,我得到了该程序的运行和工作。 What I did wrong was creating and working with 3D control points including their weight but I factored these points out again, turning my 3D Vectors into 2D points, which was a mistake made by me. 我做错了是创建和使用3D控制点,包括它们的重量,但我再次考虑了这些点,将我的3D矢量转换为2D点,这是我犯的错误。

Here is the draw function redone: 这是绘制函数重做:

def draw(mode, points, color):
    # draws objects
    glPointSize(5)
    glLineWidth(3)
    glEnable(GL_POINT_SMOOTH)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glBegin(mode)
    glColor(color[0], color[1], color[2])
    for x, y, w in points:
        glVertex2f(x / w, y / w)
    glEnd()

The rest of the code didn't change. 其余的代码没有改变。

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM