我有一个文件,里面有地形坐标,如下所示:
//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
我已将这些值放入向量(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;
}
答案 0 :(得分:-1)
实际上,我无法理解你想做什么。但也许这有帮助。
读取第一行并获取行,col大小。分配多维数组。继续阅读下一行。将值放在数组中从行中提取的值。
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];
}