2016-11-01 20 views
-1

我想創建一個動態網格類,但我必須做一些錯誤,當我啓用頂點顏色。 在我的示例中,我有2個片段着色器(fragment.glsl - >純紅色/ fragmentExp.glsl - > vertexColor)。接下來,我創建兩個基元一個三角形和一個立方體,並將這些數據放在兩個網格中。OpenGL網格類VertexColor

所以,當我使用純色片段着色器並且不設置頂點顏色時,它的工作原理很好。

這是我的工作例如: 所以,現在的網格在他們vertexColors和正在使用的頂點顏色片段着色器:當我更改以下行出現問題

Program program; 
program.LoadShader("Vertex", GL_VERTEX_SHADER, "shaders/vertex.glsl"); 
program.LoadShader("Fragment", GL_FRAGMENT_SHADER, "shaders/fragment.glsl"); 
program.AttachToProgram(); 

Program programExp; 
programExp.LoadShader("Vertex", GL_VERTEX_SHADER, "shaders/vertex.glsl"); 
programExp.LoadShader("Fragment", GL_FRAGMENT_SHADER, "shaders/fragmentExp.glsl"); 
programExp.AttachToProgram(); 

GLfloat verticesTri[] = { 
    -0.1f, -0.5f, 0.0f, 
    -0.9f, -0.5f, 0.0f, 
    -0.5f, 0.5f, 0.0f 
}; 

GLfloat verticesCube[] = { 
    0.1f, -0.5f, 0.0f, 
    0.9f, -0.5f, 0.0f, 
    0.1f, 0.5f, 0.0f, 
    0.9f, 0.5f, 0.0f 
}; 

GLuint indicesTri[] = { 
    0, 1, 2 
}; 

GLuint indicesCube[] = { 
    0, 1, 2, 
    2, 3, 1 
}; 

GLfloat colorsTri[] = { 
    1.0f, 0.0f, 0.0f, 
    0.0f, 1.0f, 0.0f, 
    0.0f, 0.0f, 1.0f 
}; 

GLfloat colorsCube[] = { 
    1.0f, 0.0f, 0.0f, 
    0.0f, 1.0f, 0.0f, 
    0.0f, 0.0f, 1.0f, 
    1.0f, 1.0f, 1.0f 
}; 

Mesh tri(program.GetProgram()); 
tri.SetVertices(verticesTri, sizeof(verticesTri)); 
tri.SetIndices(indicesTri, sizeof(indicesTri)); 
//tri.SetColors(colorsTri, sizeof(colorsTri)); 
tri.Init(); 

Mesh cube(program.GetProgram()); 
cube.SetVertices(verticesCube, sizeof(verticesCube)); 
cube.SetIndices(indicesCube, sizeof(indicesCube)); 
//cube.SetColors(colorsCube, sizeof(colorsCube)); 
cube.Init(); 

while (!glfwWindowShouldClose(window)) 
{ 
    glfwPollEvents(); 

    glClearColor(0.0, 0.0, 0.0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    tri.Draw(); 
    cube.Draw(); 

    glfwSwapBuffers(window); 
} 

因此我得到了一個黑屏。 我在做什麼錯?它做做一些與我的網類...

Mesh tri(programExp.GetProgram()); 
tri.SetVertices(verticesTri, sizeof(verticesTri)); 
tri.SetIndices(indicesTri, sizeof(indicesTri)); 
tri.SetColors(colorsTri, sizeof(colorsTri)); 
tri.Init(); 

Mesh cube(programExp.GetProgram()); 
cube.SetVertices(verticesCube, sizeof(verticesCube)); 
cube.SetIndices(indicesCube, sizeof(indicesCube)); 
cube.SetColors(colorsCube, sizeof(colorsCube)); 
cube.Init(); 

最後我通用網格類:

#include "header\mesh.h" 

Mesh::Mesh(GLuint program) 
{ 
    this->program = program; 

    glGenVertexArrays(1, &VAO); 
    glBindVertexArray(VAO); 
} 

Mesh::~Mesh() 
{ 
    glDeleteVertexArrays(1, &VAO); 

    glDeleteBuffers(1, &VBO); 
    glDeleteBuffers(1, &EBO); 
    glDeleteBuffers(1, &CBO); 
} 

void Mesh::SetVertices(GLfloat* vertices, unsigned long long size) 
{ 
    numVetices = size; 
    glGenBuffers(1, &VBO); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); 
} 

void Mesh::SetIndices(GLuint* indices, unsigned long long size) 
{ 
    numIndices = size; 
    glGenBuffers(1, &EBO); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW); 
} 

void Mesh::SetColors(GLfloat* colors, unsigned long long size) 
{ 
    numColors = size; 
    glGenBuffers(1, &CBO); 
    glBindBuffer(GL_ARRAY_BUFFER, CBO); 
    glBufferData(GL_ARRAY_BUFFER, size, colors, GL_STATIC_DRAW); 
} 

void Mesh::Init() 
{ 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); 
    glEnableVertexAttribArray(0); 

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); 
    glEnableVertexAttribArray(1); 
} 

void Mesh::Draw() 
{ 
    glUseProgram(program); 

    glBindVertexArray(VAO); 
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
    //glDrawArrays(GL_TRIANGLES, 0, 6); 
    glBindVertexArray(0); 
} 

這是我的頂點着色器和我的片段着色器:

#version 400 

layout(location = 0) in vec3 pos; 
layout(location = 1) in vec3 col; 

out vec3 color; 

void main() 
{ 
    gl_Position = vec4(pos, 1.0); 
    color = col; 
}; 


#version 400 

in vec3 col; 

out vec3 color; 

void main() 
{ 
    color = col; 
}; 

回答

0

glVertexAttribPointer總是在當前綁定的GL_ARRAY_BUFFER上運行。在init函數中,如果沒有顏色存在,則爲VBO;如果存在顏色,則爲CBO。爲了解決這個問題,你必須在設置頂點屬性之前綁定正確的緩衝區。 (請注意,只有在顏色可用時,您才應設置顏色屬性綁定。)

void Mesh::Init() 
{ 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); 
    glEnableVertexAttribArray(0); 

    glBindBuffer(GL_ARRAY_BUFFER, CBO); 
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); 
    glEnableVertexAttribArray(1); 
}