Search code examples
openglgraphic

OpenGl - making surface


I'm trying to make surface in OpenGl. But I'm seeing something like these: http://i62.tinypic.com/2z7psow.png

This is my function to making surface:

GLfloat glObject::y_surf(GLfloat x, GLfloat z, GLfloat A, GLfloat k, GLfloat kat)
{
return (A - sqrt(x*x + z*z))*sin((A - sqrt(x*x + z*z))*k + kat);
}


void glObject::MakeSurf(GLfloat a, GLfloat b, GLfloat A, GLfloat k, GLfloat delta, GLfloat kat)
{
GLfloat x;
GLfloat z;

BeginObject(GL_TRIANGLES);

// wierzcholki fasety 
float v1[3];
float v2[3];
float v3[3];
float v4[3];

// normalna
float N[3];

SetColor(0.5, 0.0, 0.0);

x = -a;


while (x <= a) // iteruj wzdluz OX az nie uzyskasz x=a
{
    z = -b;

    while (z <= b)
    {
        v1[0] = x; v1[1] = y_surf(x, z, A, k, kat); v1[2] = z;
        v2[0] = x; v2[1] = y_surf(x, z + delta, A, k, kat); v2[2] = z + delta;
        v3[0] = x + delta; v3[1] = y_surf(x + delta, z, A, k, kat); v3[2] = z;
        v4[0] = x + delta; v4[1] = y_surf(x + delta, z + delta, A, k, kat); v4[2] = z + delta;

        // pierwszy trojkat
        CalcNormal(v1, v2, v3, N);
        Normalize(N);
        SetNormal(v1[0], v1[1], v1[2]);
        AddVertex(v1[0], v1[1], v1[2]);
        SetNormal(v2[0], v2[1], v2[2]);
        AddVertex(v2[0], v2[1], v2[2]);
        SetNormal(v3[0], v3[1], v3[2]);
        AddVertex(v3[0], v3[1], v3[2]);

        //drugi trojkat 
        CalcNormal(v1, v3, v4, N);
        Normalize(N);
        SetNormal(v1[0], v1[1], v1[2]);
        AddVertex(v1[0], v1[1], v1[2]);
        SetNormal(v3[0], v3[1], v3[2]);
        AddVertex(v3[0], v3[1], v3[2]);
        SetNormal(v4[0], v4[1], v4[2]);
        AddVertex(v4[0], v4[1], v4[2]);

        z = z + delta;
    }
    x = x + delta;
}

EndObject();
}

And my project in visual:

https://www.dropbox.com/s/vu5n1cluzhixdwi/Proj4.rar?dl=0

Can someone help me what should I do to make this surface smoothly?

Edit

But when I replace function "y_surf" to:

auto Y = [](GLfloat x, GLfloat z, GLfloat A, GLfloat k, GLfloat fi)->GLfloat{
    return (A - sqrt(x*x + z*z))*sin((A - sqrt(x*x + z*z))*k + fi); };

It works, why?


Solution

  • Seems that you have misplaced the vertices. Should be v2,v3,v4 for the other triangle. Also the Normals should not be taken from v's but from N (but that is out of scope of this question).

        //drugi trojkat 
        CalcNormal(v1, v3, v4, N);
        Normalize(N);
        SetNormal(v2[0], v2[1], v2[2]);
        AddVertex(v2[0], v2[1], v2[2]); // v2 <- here
        SetNormal(v3[0], v3[1], v3[2]);
        AddVertex(v3[0], v3[1], v3[2]);
        SetNormal(v4[0], v4[1], v4[2]);
        AddVertex(v4[0], v4[1], v4[2]);