Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
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
Sure! Here is a function that generates the vertices, indices, normals, and texture coordinates of a flat terrain mesh using OpenGL:
code#include <vector> #include <cmath> #include <glm/glm.hpp> // Generates a flat terrain mesh with the given dimensions. // The terrain is divided into a grid of quadrilaterals, with each quadrilateral // having the vertices (x, y, z), (x+1, y, z), (x+1, y+1, z), and (x, y+1, z). // // vertices: Output parameter for the list of vertices. // indices: Output parameter for the list of indices. // normals: Output parameter for the list of normals. // texCoords: Output parameter for the list of texture coordinates. // width: The width of the terrain. // depth: The depth of the terrain. // spacing: The distance between each vertex in the terrain grid. // height: The height of the terrain. void generateFlatTerrainMesh(std::vector<glm::vec3>& vertices, std::vector<unsigned int>& indices, std::vector<glm::vec3>& normals, std::vector<glm::vec2>& texCoords, int width, int depth, float spacing, float height) { // Generate the vertices. for (int x = 0; x < width; ++x) { for (int z = 0; z < depth; ++z) { float xPos = x * spacing; float zPos = z * spacing; vertices.push_back(glm::vec3(xPos, height, zPos)); } } // Generate the indices. for (int x = 0; x < width - 1; ++x) { for (int z = 0; z < depth - 1; ++z) { // Triangle 1 indices.push_back((x + 0) * depth + (z + 0)); indices.push_back((x + 1) * depth + (z + 0)); indices.push_back((x + 1) * depth + (z + 1)); // Triangle 2 indices.push_back((x + 0) * depth + (z + 0)); indices.push_back((x + 1) * depth + (z + 1)); indices.push_back((x + 0) * depth + (z + 1)); } } // Generate the normals. for (int i = 0; i < vertices.size(); ++i) { normals.push_back(glm::vec3(0.0f, 1.0f, 0.0f)); } // Generate the texture coordinates. float texX = 0.0f; for (int x = 0; x < width; ++x) { float texY = 0.0f; for (int z = 0; z < depth; ++z) { texCoords.push_back(glm::vec2(texX, texY)); texY += spacing; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.