동적 메쉬 클래스를 만들고 싶습니다만 정점 색상을 사용할 때 뭔가 잘못해야합니다. 예제에서는 두 조각 쉐이더 (fragment.glsl -> 단색 빨강/fragmentExp.glsl -> vertexColor)가 있습니다. 다음으로 저는 하나의 삼각형과 하나의 큐브 두 개의 프리미티브를 만들고이 데이터를 두 개의 메쉬에 넣습니다.OpenGL Mesh 클래스 VertexColor
솔리드 컬러 조각 셰이더를 사용할 때 정점 색상을 설정하지 않으면 잘 작동합니다. 이제 메쉬 그들에 vertexColors이 있고 vertexColor 조각 쉐이더를 사용 : 내가 다음 줄을 변경하면 문제가 나타납니다
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);
}
:
은 여기 내 작업 예입니다.
결과로 검은 색 화면이 나타납니다. 내가 뭘 잘못하고 있니?
다음#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);
}
내 버텍스 쉐이더이며 내 조각 쉐이더 : 그것은 ... 내 메쉬 클래스와
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();
그리고 마지막으로 내 일반적인 메쉬 클래스를 뭔가를 할 수있다
#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;
};