gpt4 book ai didi

c++ - 在 OpenGL 中渲染一组三角形结构引用

转载 作者:行者123 更新时间:2023-11-28 02:36:40 25 4
gpt4 key购买 nike

我有一个 std::vector<Triangle*>我试图在 OpenGL 中渲染。我的 vector 定义为:

#pragma once

#include <glm\glm.hpp>

struct Triangle
{
glm::vec3 _vertex0, _vertex1, _vertex2;
Triangle(glm::vec3 vertex0, glm::vec3 vertex1, glm::vec3 vertex2)
{
_vertex0 = vertex0;
_vertex1 = vertex1;
_vertex2 = vertex2;
}

Triangle(glm::vec4 vertex0, glm::vec4 vertex1, glm::vec4 vertex2)
{
_vertex0 = glm::vec3(vertex0.x, vertex0.y, vertex0.z);
_vertex1 = glm::vec3(vertex1.x, vertex1.y, vertex1.z);
_vertex2 = glm::vec3(vertex2.x, vertex2.y, vertex2.z);
}
};

我的 Asset struct是:

struct Asset
{
Asset() { }
Asset(std::string assetOBJFile)
{
glGenVertexArrays(1, &vertexArrayID);
glBindVertexArray(vertexArrayID);

programID = LoadShaders("vertexShader.txt", "fragmentShader.txt");

// Read our .obj file
loadOBJ(assetOBJFile.c_str(), originalTriangles, vertices, faces);

boundingSphere = BoundingSphere(vertices);

// Load it into a VBO
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, originalTriangles.size() * sizeof(Triangle), &originalTriangles[0], GL_STATIC_DRAW);

//velocity = glm::vec3(0.0, 1.0, 1.0);
velocity = glm::vec3(0.0, 0.0, 0.0);
position = glm::vec3(0.0, 0.0, 0.0);

lastTime = glfwGetTime();
}

GLuint vertexArrayID;
GLuint programID;

GLuint vertexbuffer;

std::vector<GLuint> faces;
std::vector<glm::vec3> vertices;
std::vector<Triangle*> originalTriangles;
std::vector<Triangle*> triangles;

BoundingSphere boundingSphere;

glm::vec3 velocity;

double lastTime;
glm::vec3 position;
};

我渲染 Assets 的代码是:

void renderAsset(Asset asset)
{
glUseProgram(asset.programID);

GLuint cameraID = glGetUniformLocation(asset.programID, "camera");
glUniformMatrix4fv(cameraID, 1, GL_FALSE, &camera[0][0]);

GLuint positionID = glGetUniformLocation(asset.programID, "position");
glUniformMatrix4fv(positionID, 1, GL_FALSE, &glm::translate(glm::mat4(), asset.position)[0][0]);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, asset.vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, asset.originalTriangles.size()* 9);

glDisableVertexAttribArray(0);
}

但是,当我尝试渲染 Assets 时,我最终只会在屏幕上看到一堆乱码。如果我更改 Triangle* 的 vector 至 Triangle一切都很好,但我需要指针来构建我的空间散列。

最佳答案

您需要展平三角形 vector 。您现在将一个指针数组传递给 glBufferData,而不是您想要实际数据。

Triangle flattenedTriangles[] = new Triangle[ originalTriangles.size() ];
for(int i = 0; i<originalTriangles.size(); i++){
flattenedTriangles[i] = *(originalTriangles[i]);
}

glBufferData(GL_ARRAY_BUFFER, originalTriangles.size() * sizeof(Triangle), &flattenedTriangles[0], GL_STATIC_DRAW);
delete[] flattenedTriangles;

或者如果您不想分配数组而是映射缓冲区并就地构造:

glBufferData(GL_ARRAY_BUFFER, originalTriangles.size() * sizeof(Triangle), 0, GL_STATIC_DRAW);//allocate
do{
Triangle *flattenedTriangles = (flattenedTriangles*)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
for(int i = 0; i<originalTriangles.size(); i++){
flattenedTriangles[i] = *(originalTriangles[i]);
}
}while(glUnmapBuffer(GL_ARRAY_BUFFER));//repeat if map got invalidated

关于c++ - 在 OpenGL 中渲染一组三角形结构引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com