簡體   English   中英

改變向量 <float> 到高度圖坐標OpenGL

[英]change Vector<float> to heightmap coordinates OpenGL

我有一個具有地形坐標的文件,如下所示:

//The first value is how many rows and columns the map has (assuming its a square) 
5    
-0.9 -0.6 -0.4 -0.6 -0.9
-0.2 0.1 0.3 0.1 -0.3
0 0.4 0.8 0.4 0
-0.2 0.1 0.3 0.1 -0.3
0.5 -0.6 -0.4 -0.6 -0.9

我已經將這些值放入vector(float)中,現在需要對其進行格式化,以便渲染頂點並創建地形。 我該怎么做呢?

std::fstream myfile("heights.csv", std::ios_base::in);

    std::vector<float> numbers;
    float a;
    while (myfile >> a){/*printf("%f ", a);*/
    numbers.push_back(a);
    }

僅供參考,這是我的其余代碼,基本上是在屏幕上渲染2個三角形的代碼,我打算稍后刪除(只是嘗試學習)

/**
 * A typical program flow and methods for rendering simple polygons
 * using freeglut and openGL + GLSL
 */

#include <stdio.h>
// GLEW loads OpenGL extensions. Required for all OpenGL programs.
#include <GL/glew.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// Utility code to load and compile GLSL shader programs
#include "shader.hpp"
#include <iostream>
#include <fstream>
#include <vector>

#define WINDOW_WIDTH    400
#define WINDOW_HEIGHT   400
#define VALS_PER_VERT 3
#define VALS_PER_COLOUR 4
#define NUM_VERTS 3         // Total number of vertices to load/render

using namespace std;

// Handle to our VAO generated in setShaderData method
unsigned int vertexVaoHandle;   
unsigned int vertexVaoHandle2;  

// Handle to our shader program
unsigned int programID;

/**
 * Sets the shader uniforms and vertex data
 * This happens ONCE only, before any frames are rendered
 * @param id, Shader program object to use
 * @returns 0 for success, error otherwise
 */
int setShaderData(const unsigned int &id) 
{
    /*
     * What we want to draw
     * Each set of 3 vertices (9 floats) defines one triangle
     * You can define more triangles to draw here
     */
    float vertices[ NUM_VERTS*VALS_PER_VERT ] = {
            -0.5f, -0.5f, -0.0f,    // Bottom left
            0.5f, -0.5f, -0.0f,     // Bottom right
            0.0f, 0.5f, -0.0f       // Top
    };

    float vertices2[ NUM_VERTS*VALS_PER_VERT ] = {
            -0.9f, -0.9f, -0.0f,    // Bottom left
            0.9f, -0.9f, -0.0f,     // Bottom right
            0.9f, 0.5f, -0.0f       // Top
    };

    float heightmap[ NUM_VERTS*VALS_PER_VERT ] = {

            -0.9f, -0.9f, -0.0f,    // Bottom left
            0.9f, -0.9f, -0.0f,     // Bottom right
            0.9f, 0.5f, -0.0f   

    };

    // Colours for each vertex; red, green, blue and alpha
    // This data is indexed the same order as the vertex data, but reads 4 values
    // Alpha will not be used directly in this example program
    float colours[ NUM_VERTS*VALS_PER_COLOUR ] = {
            0.8f, 0.7f, 0.5f, 1.0f,
            0.3f, 0.7f, 0.1f, 1.0f,
            0.8f, 0.2f, 0.5f, 1.0f,
    };

    float colours2[ NUM_VERTS*VALS_PER_COLOUR ] = {
            0.8f, 0.7f, 0.5f, 1.0f,
            0.3f, 0.7f, 0.1f, 1.0f,
            0.8f, 0.2f, 0.5f, 1.0f,
    };

    // Generate storage on the GPU for our triangle and make it current.
    // A VAO is a set of data buffers on the GPU
    glGenVertexArrays(1, &vertexVaoHandle);     
    glBindVertexArray(vertexVaoHandle);

    // Generate new buffers in our VAO
    // A single data buffer store for generic, per-vertex attributes
    unsigned int buffer[2];
    glGenBuffers(2, buffer);

    // Allocate GPU memory for our vertices and copy them over
    glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NUM_VERTS*VALS_PER_VERT, vertices, GL_STATIC_DRAW);

    // Do the same for our vertex colours
    glBindBuffer(GL_ARRAY_BUFFER, buffer[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NUM_VERTS*VALS_PER_COLOUR, colours, GL_STATIC_DRAW);

    // Now we tell OpenGL how to interpret the data we just gave it
    // Tell OpenGL what shader variable it corresponds to
    // Tell OpenGL how it's formatted (floating point, 3 values per vertex)
    int vertLoc = glGetAttribLocation(id, "a_vertex");
    glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
    glEnableVertexAttribArray(vertLoc);
    glVertexAttribPointer(vertLoc, VALS_PER_VERT, GL_FLOAT, GL_FALSE, 0, 0);

    // Do the same for the vertex colours
    int colourLoc = glGetAttribLocation(id, "a_colour");
    glBindBuffer(GL_ARRAY_BUFFER, buffer[1]);
    glEnableVertexAttribArray(colourLoc);
    glVertexAttribPointer(colourLoc, VALS_PER_COLOUR, GL_FLOAT, GL_FALSE, 0, 0);

    // An argument of zero un-binds all VAO's and stops us
    // from accidentally changing the VAO state
    glBindVertexArray(0);

    // The same is true for buffers, so we un-bind it too
    glBindBuffer(GL_ARRAY_BUFFER, 0);







    // SECOND TRI




    // Generate storage on the GPU for our triangle and make it current.
    // A VAO is a set of data buffers on the GPU
    glGenVertexArrays(1, &vertexVaoHandle2);        
    glBindVertexArray(vertexVaoHandle2);

    // Generate new buffers in our VAO
    // A single data buffer store for generic, per-vertex attributes
    unsigned int buffer2[2];
    glGenBuffers(2, buffer2);

// Allocate GPU memory for our vertices and copy them over
    glBindBuffer(GL_ARRAY_BUFFER, buffer2[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NUM_VERTS*VALS_PER_VERT, vertices2, GL_STATIC_DRAW);

    // Do the same for our vertex colours
    glBindBuffer(GL_ARRAY_BUFFER, buffer2[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NUM_VERTS*VALS_PER_COLOUR, colours2, GL_STATIC_DRAW);

    // Now we tell OpenGL how to interpret the data we just gave it
    // Tell OpenGL what shader variable it corresponds to
    // Tell OpenGL how it's formatted (floating point, 3 values per vertex)
    int vertLoc2 = glGetAttribLocation(id, "a_vertex");
    glBindBuffer(GL_ARRAY_BUFFER, buffer2[0]);
    glEnableVertexAttribArray(vertLoc2);
    glVertexAttribPointer(vertLoc2, VALS_PER_VERT, GL_FLOAT, GL_FALSE, 0, 0);

    // Do the same for the vertex colours
    int colourLoc2 = glGetAttribLocation(id, "a_colour");
    glBindBuffer(GL_ARRAY_BUFFER, buffer2[1]);
    glEnableVertexAttribArray(colourLoc2);
    glVertexAttribPointer(colourLoc2, VALS_PER_COLOUR, GL_FLOAT, GL_FALSE, 0, 0);


    // An argument of zero un-binds all VAO's and stops us
    // from accidentally changing the VAO state
    glBindVertexArray(0);

    // The same is true for buffers, so we un-bind it too
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    return 0;   // return success
}


/**
 * Renders a frame of the state and shaders we have set up to the window
 * Executed each time a frame is to be drawn.
 */
void render() 
{
    // Clear the previous pixels we have drawn to the colour buffer (display buffer)
    // Called each frame so we don't draw over the top of everything previous
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(programID);

    // Make the VAO with our vertex data buffer current
    glBindVertexArray(vertexVaoHandle);

    // Send command to GPU to draw the data in the current VAO as triangles
    glDrawArrays(GL_TRIANGLES, 0, NUM_VERTS);

    glBindVertexArray(0);   // Un-bind the VAO



    // SECOND TRIANGLE

    // Make the VAO with our vertex data buffer current
    glBindVertexArray(vertexVaoHandle2);

    // Send command to GPU to draw the data in the current VAO as triangles
    glDrawArrays(GL_TRIANGLES, 0, NUM_VERTS);

    glBindVertexArray(0);   // Un-bind the VAO

    // XXXXXXXX

    glutSwapBuffers();  // Swap the back buffer with the front buffer, showing what has been rendered

    glFlush();  // Guarantees previous commands have been completed before continuing
}


/**
 * Program entry. Sets up OpenGL state, GLSL Shaders and GLUT window and function call backs
 * Takes no arguments
 */
int main(int argc, char **argv) {


    //READ IN FILE//
    std::fstream myfile("heights.csv", std::ios_base::in);

    std::vector<float> numbers;
    float a;
    while (myfile >> a){/*printf("%f ", a);*/
    numbers.push_back(a);
    }


    for (int i=0; i<numbers.size();i++){cout << numbers[i] << endl;}

    getchar();
    //READ IN FILE//

    // Set up GLUT window
    glutInit(&argc, argv);              // Starts GLUT systems, passing in command line args
    glutInitWindowPosition(100, 0);     // Positions the window on the screen relative to top left
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);    // Size in pixels

    // Display mode takes bit flags defining properties you want the window to have;
    //  GLUT_RGBA : Set the pixel format to have Red Green Blue and Alpha colour channels
    //  GLUT_DOUBLE : Each frame is drawn to a hidden back buffer hiding the image construction
    //  GLUT_DEPTH : A depth buffer is kept so that polygons can be drawn in-front/behind others (not used in this application)
#ifdef __APPLE__    
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE);
#else
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
#endif

    glutCreateWindow("Hello World!");   // Makes the actual window and displays

    // Initialize GLEW
    glewExperimental = true; // Needed for core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Sets the (background) colour for each time the frame-buffer (colour buffer) is cleared
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

    // Set up the shaders we are to use. 0 indicates error.
    programID = LoadShaders("minimal.vert", "minimal.frag");
    if (programID == 0)
        return 1;

    // Set this shader program in use
    // This is an OpenGL state modification and persists unless changed
    glUseProgram(programID);

    // Set the vertex data for the program
    if (setShaderData(programID) != 0)
        return 1;

    // Render call to a function we defined,
    // that is called each time GLUT thinks we need to update
    // the window contents, this method has our drawing logic
    glutDisplayFunc(render);

    // Start an infinite loop where GLUT calls methods (like render)
    // set with glut*Func when needed.
    // Runs until something kills the window
    glutMainLoop();


    return 0;
}

其實,我不明白你想做什么。 但這也許有幫助。

閱讀第一行並獲取行,列大小。 分配多維數組。 繼續閱讀下一行。 將從行提取的值放入數組中。

std::ifstream file( "heights.csv" );
std::string line;
getline(file , line);
int ColRowSize;
line >> ColRowSize;

/// allocate map
float** data = malloc(ColRowSize * sizeof(float *));
for(i = 0; i < ColRowSize; i++)
    {
         array[i] = malloc(ColRowSize * sizeof(float));
    }
/// read from file 
for(i = 0; i < ColRowSize; i++)
    {
         getline(file , line);
         for(j = 0; j < ColRowSize; j++)
             line >> data[i][j];             
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM