GL_lines rendert ein Dreieck?C++

Programme in C++. Entwicklerforum
Anonymous
 GL_lines rendert ein Dreieck?

Post by Anonymous »

Ich versuche in der Weltachse zu rendern. Aber aus irgendeinem Grund, anstatt 3 Zeilen zu rendern, macht es ein Dreieck. Es ignoriert die Tatsache, dass ich den RenderinMode auf gl_lines gesetzt habe. Warum rendert es ein Dreieck anstelle von 3 Zeilen? Was mache ich falsch? < /P>
void Axis::render(glm::mat4x4 projectionMatrix, glm::mat4x4 viewMatrix, glm::mat4x4 parentTransform)
{
glm::mat4x4 worldTransform = transform.M() * parentTransform;
meshRenderer->bind();

glUniformMatrix4fv(meshRenderer->getShader()->getUniform("modelMatrix"), 1, GL_FALSE, glm::value_ptr(worldTransform));
glUniformMatrix4fv(meshRenderer->getShader()->getUniform("viewMatrix"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(meshRenderer->getShader()->getUniform("projectionMatrix"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
meshRenderer->render();
meshRenderer->unbind();
}

void Axis::init()
{
axisVerts = new GLfloat[18]{
0.f,0.f,0.f,
1.f,0.f,0.f,

0.f,0.f,0.f,
0.f,1.f,0.f,

0.f,0.f,0.f,
0.f,0.f,1.f
};

axisColors = new GLfloat[18]{
1.f,0.f,0.f,
1.f,0.f,0.f,

0.f,1.f,0.f,
0.f,1.f,0.f,

0.f,0.f,1.f,
0.f,0.f,1.f
};

axisIndices = new GLuint[6] {
0,1,
2,3,
4,5
};

Mesh* mesh = new Mesh(axisVerts, axisColors, axisIndices, 18, 18, 6);
meshRenderer = new MeshRenderer(mesh, shaderProgram);
meshRenderer->setRenderMode(GL_LINES);
}

void MeshRenderer::init()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glBindBuffer(GL_ARRAY_BUFFER, mesh->getVertexBufferId());
GLuint posId = shaderProgram->getAttribute("VertexPosition");
glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(posId);

if (mesh->hasNormals()) {
glBindBuffer(GL_ARRAY_BUFFER, mesh->getNormalBufferId());
GLuint normId = shaderProgram->getAttribute("VertexNormal");
glVertexAttribPointer(normId, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(normId);
}

if (mesh->hasColors()) {
glBindBuffer(GL_ARRAY_BUFFER, mesh->getColorBufferId());
GLuint colorId = shaderProgram->getAttribute("VertexColor");
glVertexAttribPointer(colorId, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(colorId);
}

if (mesh->hasUVs()) {
glBindBuffer(GL_ARRAY_BUFFER, mesh->getUVBufferId());
GLuint uvId = shaderProgram->getAttribute("VertexUV");
glVertexAttribPointer(uvId, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(uvId);
}

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->getIndexBufferId());

glBindVertexArray(0);
}

void MeshRenderer::render()
{
glBindVertexArray(vao);
mesh->render(renderMode);
glBindVertexArray(0);
}

Mesh::Mesh(GLfloat* vertices, GLfloat* colors, GLuint* indices, int vertexCount, int colorCount, int indicesCount)
{
verticesCount = vertexCount;
this->vertices = new GLfloat[verticesCount];
std::copy(vertices, vertices + verticesCount, this->vertices);

this->colorCount = colorCount;
this->colors = new GLfloat[colorCount];
std::copy(colors, colors + colorCount, this->colors);

this->indicesCount = indicesCount;
this->indices = new GLuint[indicesCount];
std::copy(indices, indices + indicesCount, this->indices);

normals = nullptr;
UVs = nullptr;

init();
}

inline void Mesh::bind()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticesCount * sizeof(GLfloat), vertices, GL_STATIC_DRAW);

if (normals != nullptr) {
glBindBuffer(GL_ARRAY_BUFFER, nbo);
glBufferData(GL_ARRAY_BUFFER, normalCount * sizeof(GLfloat), normals, GL_STATIC_DRAW);
}

if (colors != nullptr) {
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glBufferData(GL_ARRAY_BUFFER, colorCount * sizeof(GLfloat), colors, GL_STATIC_DRAW);
}

if (UVs != nullptr) {
glBindBuffer(GL_ARRAY_BUFFER, uvbo);
glBufferData(GL_ARRAY_BUFFER, uvCount * sizeof(GLfloat), UVs, GL_STATIC_DRAW);
}

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesCount * sizeof(GLuint), indices, GL_STATIC_DRAW);
}

void Mesh::freeArrays()
{
if (vertices != NULL) {
delete[] vertices;
vertices = NULL;
}
if (colors != NULL) {
delete[] colors;
colors = NULL;
}
if (indices != NULL) {
delete[] indices;
indices = NULL;
}
if (normals != NULL) {
delete[] normals;
normals = NULL;
}
if (UVs != NULL) {
delete[] UVs;
UVs = NULL;
}
}

void Mesh::init()
{
glGenBuffers(1, &vbo);
glGenBuffers(1, &cbo);
glGenBuffers(1, &ibo);
glGenBuffers(1, &nbo);
glGenBuffers(1, &uvbo);
bind();
freeArrays();
}

void Mesh::render(GLenum renderMode)
{
glDrawElements(renderMode, indicesCount, GL_UNSIGNED_INT, 0);
}
< /code>
und die Shader, die ich verwendet, < /p>
#version 140 core

in vec3 VertexPosition;
in vec3 VertexColor;

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

out vec3 color;

void main() {
color = VertexColor;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(VertexPosition, 1 );
}

#version 140 core

in vec3 color;
out vec4 LFragment;

void main() {
LFragment = vec4(color, 1.0);
}
< /code>
Bild, wie es aussieht. Es sollte 3 Zeilen in verschiedenen Richtungen sein, die die Weltachse darstellen, aber stattdessen ist dieses Dreieck.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post