Write a function using OpenGL that generates the vertices, indices, normals and texture…
- User
Write a function using OpenGL that generates the vertices, indices, normals and texture coordinates of a flat terrain mesh
- Assistant
Here is an example of a function that generates the vertices, indices, normals, and texture coordinates for a flat terrain mesh using OpenGL and the GLM library for vector and matrix operations:
code#include <vector> #include <glm/glm.hpp> struct Vertex { glm::vec3 position; glm::vec3 normal; glm::vec2 texCoords; }; std::vector<Vertex> generateFlatTerrain(float width, float depth, unsigned int widthSegments, unsigned int depthSegments) { std::vector<Vertex> vertices; for (unsigned int i = 0; i <= depthSegments; ++i) { for (unsigned int j = 0; j <= widthSegments; ++j) { Vertex vertex; vertex.position.x = (j / (float)widthSegments) * width; vertex.position.y = 0; // flat terrain vertex.position.z = (i / (float)depthSegments) * depth; vertex.normal = glm::vec3(0, 1, 0); // flat terrain vertex.texCoords.x = j / (float)widthSegments; vertex.texCoords.y = i / (float)depthSegments; vertices.push_back(vertex); } } return vertices; } std::vector<unsigned int> generateFlatTerrainIndices(unsigned int widthSegments, unsigned int depthSegments) { std::vector<unsigned int> indices; for (unsigned int i = 0; i < depthSegments; ++i) { for (unsigned int j = 0; j < widthSegments; ++j) { indices.push_back((i + 1) * (widthSegments + 1) + j); indices.push_back(i * (widthSegments + 1) + j); indices.push_back(i * (widthSegments + 1) + j + 1); indices.push_back((i + 1) * (widthSegments + 1) + j); indices.push_back(i * (widthSegments + 1) + j + 1); indices.push_back((i + 1) * (widthSegments + 1) + j + 1); } } return indices; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.