Make perlin terrain with voxel's

Hey guys! I am new here, and i want make a voxel terrain in c++ with opengl. But i don’t know how to use the fast noise. Anyone can help me?

this is my code:

#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
#include <FastNoiseLite.h>

FastNoiseLite noise;

// Camera var
float angle = 0.0;                              // Angle of the camera
float lx = 0.0, ly = 0.0, lz = -1.0;            // Direction of the camera
float x = 0.0, y = 1.0,z = 0.0;                 // Position of the camera
float deltaAngleX = 0.0, deltaAngleY = 0.0;
float xOrigin = -1.0, yOrigin = -1.0;

// Terrain var
int tx = 10, ty = 10, tz = 10;

void mouseButton(int button, int state, int x, int y)
{
    if(button == GLUT_LEFT_BUTTON)
    {
        if(state == GLUT_UP)
        {
            angle += deltaAngleX;
            angle += deltaAngleY;
            xOrigin = -1.0;
            yOrigin = -1.0;
        }
        if(state == GLUT_DOWN)
        {
            xOrigin = x;
            yOrigin = y;
        }
    }
}

void mouse(int mouseX, int mouseY)
{
    float sensibility = 0.01;

    if(xOrigin >= 0)
    {
        if(yOrigin >= 0)
        {
            deltaAngleX = (mouseX - xOrigin) * sensibility;
            deltaAngleY = (mouseY - yOrigin) * sensibility;

            lx = sin(angle + deltaAngleX);
            lz = -cos(angle + deltaAngleX);

            ly = sin(angle + deltaAngleY);
            ly = -cos(angle + deltaAngleY);
        }
    }
}

void keyboard(unsigned char key, int keyX, int keyY)
{
    float speed = 1.0;

    switch(key)
    {
        case 'w': x += lx * speed; y += ly * speed; z += lz * speed; break;
        case 's': x -= lx * speed; y -= ly * speed; z -= lz * speed; break;
        //case 'a': lz -= x * speed; break;
        //case 'd': lz += x * speed; break;
    }
}

void keys(int key, int xx, int yy)
{
    float speed = 1.0;

    switch(key)
    {
        case GLUT_KEY_LEFT: angle -= 0.1; lx = sin(angle); lz = -cos(angle); break;
        case GLUT_KEY_RIGHT: angle += 0.1; lx = sin(angle); lz = -cos(angle); break;
        case GLUT_KEY_UP: x += lx * speed; z += lz * speed; break;
        case GLUT_KEY_DOWN: x -= lx * speed; z -= lz * speed; break;
    }
}

void drawVoxel(int x, int y, int z)
{
    glPushMatrix();
        glTranslatef(x,y,z);

        // Frontal face
        glBegin(GL_QUADS);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.0, 1.0, 0.0);
            glVertex3f(1.0, 1.0, 0.0);
            glVertex3f(1.0, 0.0, 0.0);
        glEnd();

        // Back face
        glBegin(GL_QUADS);
            glVertex3f(0.0, 0.0, 1.0);
            glVertex3f(0.0, 1.0, 1.0);
            glVertex3f(1.0, 1.0, 1.0);
            glVertex3f(1.0, 0.0, 1.0);
        glEnd();

        // Right face
        glBegin(GL_QUADS);
            glVertex3f(1.0, 0.0, 0.0);
            glVertex3f(1.0, 1.0, 0.0);
            glVertex3f(1.0, 1.0, 1.0);
            glVertex3f(1.0, 0.0, 1.0);
        glEnd();

        // Right face
        glBegin(GL_QUADS);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.0, 1.0, 0.0);
            glVertex3f(0.0, 1.0, 1.0);
            glVertex3f(0.0, 0.0, 1.0);
        glEnd();

        // Top face
        glBegin(GL_QUADS);
            glVertex3f(0.0, 1.0, 0.0);
            glVertex3f(0.0, 1.0, 1.0);
            glVertex3f(1.0, 1.0, 1.0);
            glVertex3f(1.0, 1.0, 0.0);
        glEnd();

        // Bottom face
        glBegin(GL_QUADS);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.0, 0.0, 1.0);
            glVertex3f(1.0, 0.0, 1.0);
            glVertex3f(1.0, 0.0, 0.0);
        glEnd();

        glColor3f(0.0, 2.0, 0.0);

    glPopMatrix();
}

void reshape(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.1,100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0,0.0,10.0,1.0);

    // Camera config
    glLoadIdentity();
    gluLookAt(x, y, z, x+lx, y+ly, z+lz, 0.0, 1.0, 0.0);

    noise.SetNoiseType(FastNoiseLite::NoiseType_Value);

    // Draw terrain
    for(int x = 0; x < tx; x++)
    {
        for(int y = 0; y < ty; y++)
        {
            for(int z = 0; z < tz; z++)
            {
                drawVoxel(x,y,z);
            }
        }
    }

    glFlush();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

    glBegin(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);
    glutInitWindowSize(1200, 800);
    glutCreateWindow("Voxel Terrain");
    glEnable(GL_DEPTH_TEST);

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);

    glutSpecialFunc(keys);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(mouse);
    glutMouseFunc(mouseButton);

    glutMainLoop();

    return 0;
}

How to use FastNoise isn’t an OpenGL problem, it’s a FastNoise problem.

Anyways, there is an example on the GitHub repository: https://github.com/Auburn/FastNoise/tree/master/Cpp.

I tried the Fast Noise, but don’t work. I use only the command noise.GetNoise, it’s right?

No. As specified on that reference page I linked, you construct a FastNoiseLite class, call it’s SetNoiseType method, and only then can you use the GetNoise method.

I made this

noise.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
float noiseData[128*128];
int index = 0;

// Draw terrain
for(int x = 0; x < tx; x++)
{
    for(int y = 0; y < ty; y++)
    {
        for(int z = 0; z < tz; z++)
        {
            noiseData[index++] = noise.GetNoise((float)x,(float)y);
            drawVoxel(x,y,z);
        }
    }
}

but don’t worked, look:

this is a 10x10x10 terrain, i want made a elevation with the noise

Well, it’s obvious: you’re not using the noise data. You get it, but don’t use it to determine the height at each 2D point.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.