因此,我最近一直在尝试通过一些文本和视频教程来学习OpenGL。
我无法绘制三角形,我已经三重检查两次,以正确的顺序执行所有必要的步骤,但是显然缺少了一些东西
在添加一些代码之前,我应该声明我使用glfw,glad和ogl 4.6。
Main.cpp
//while debugging ive add extra unnecessary call to UseProgram and bindvertexarray every frame to be sure
static void Render(GLFWwindow* window)
{
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glfwSwapInterval(1);
glClearColor(.98f, .5f, .5f, 1);
std::vector<Vertex> vertsTri = {
{
Vertex(0.0f,1.0f,0.0f,0,0,0,0,0)
},
{
Vertex(-1.0f,-1.0f,0.0f,0,0,0,0,0)
},
{
Vertex(1.0f,-1.0f,0.0f,0,0,0,0,0)
}
};
Model tri(vertsTri, "basic");
tri.UseProgram();
glBindVertexArray(tri.getVAO());
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
// rendering here
tri.UseProgram();
glBindVertexArray(tri.getVAO());
glDrawArrays(GL_TRIANGLES, 0, tri.size());
glfwSwapBuffers(window);
}
}
int main()
{
glfwSetErrorCallback(errorCallback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Title", nullptr, nullptr);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetFramebufferSizeCallback(window, windowResizeCallback);
glfwSetKeyCallback(window, keyCallback);
// Resource loading and creation in here
std::thread renderThread(Render, window);
while (!glfwWindowShouldClose(window))
{
// Realtime game mechanics here
glfwPollEvents();
}
if (renderThread.joinable())
renderThread.join();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Model.cpp
//Gets called in the constructor with a Vertex vector, p_shader is created in Model constructor
void Model::Create()
{
// Generate a vertex array object and bind it
glGenVertexArrays(1, &p_vaoId);
glBindVertexArray(p_vaoId);
// Generate a vertex buffer object and bind it
glGenBuffers(1, &p_vboId);
glBindBuffer(GL_ARRAY_BUFFER, p_vboId);
glBufferData(GL_ARRAY_BUFFER, p_vertices.size()*sizeof(Vertex), &(p_vertices[0]), GL_STATIC_DRAW);
// Define attributes and enable them
glEnableVertexArrayAttrib(p_vaoId, 0);
glVertexAttribPointer(0, sizeof(Vertex::position) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::position), (void*)offsetof(Vertex, position));
glEnableVertexArrayAttrib(p_vaoId, 1);
glVertexAttribPointer(1, sizeof(Vertex::normal) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::normal), (void*)offsetof(Vertex, normal));
glEnableVertexArrayAttrib(p_vaoId, 2);
glVertexAttribPointer(2, sizeof(Vertex::uv) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::uv), (void*)offsetof(Vertex, uv));
}
Shader.cpp
// p_v/fShaderSource is set in LoadSource, while debugging ive made sure the source is gotten correctly
void Shader::Create()
{
int errorResult = 0;
// Load shader sources into memory before anything else
LoadSource();
// Creating shader objects
p_vShaderId = glCreateShader(GL_VERTEX_SHADER);
p_fShaderId = glCreateShader(GL_FRAGMENT_SHADER);
// Compile the shaders
Compile(p_vShaderId, p_vShaderSource);
Compile(p_fShaderId, p_fShaderSource);
// Create a program object
p_programId = glCreateProgram();
// Attach shaders to the program
glAttachShader(p_programId, p_vShaderId);
glAttachShader(p_programId, p_fShaderId);
//Link the program and handle errors
glLinkProgram(p_programId);
glGetProgramiv(p_programId, GL_LINK_STATUS, &errorResult);
if (!errorResult)
{
int length;
glGetShaderiv(p_programId, GL_INFO_LOG_LENGTH, &length);
char* log = new char[length];
glGetProgramInfoLog(p_programId, length, &length, log);
std::cout << "[Link Error] Failed to link program" << std::endl;
std::cout << log << std::endl;
delete log;
glDeleteShader(p_vShaderId);
glDeleteShader(p_fShaderId);
}
// Validate the program and handle errors
glValidateProgram(p_programId);
glGetProgramiv(p_programId, GL_VALIDATE_STATUS, &errorResult);
if (!errorResult)
{
int length;
glGetShaderiv(p_programId, GL_INFO_LOG_LENGTH, &length);
char* log = new char[length];
glGetProgramInfoLog(p_programId, length, &length, log);
std::cout << "[Validate Error] Failed to validate program" << std::endl;
std::cout << log << std::endl;
delete log;
glDeleteShader(p_vShaderId);
glDeleteShader(p_fShaderId);
glDeleteProgram(p_programId);
}
// Cleanup shaders after linking and validating
glDetachShader(p_programId, p_vShaderId);
glDetachShader(p_programId, p_fShaderId);
glDeleteShader(p_vShaderId);
glDeleteShader(p_fShaderId);
}
void Shader::Compile(unsigned int& shaderId, std::string source)
{
int compileResult = 0;
const char* csource = (const char*)source.c_str();
glShaderSource(shaderId, 1, &csource, 0);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileResult);
if (!compileResult)
{
int length;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);
char* log = new char[length];
glGetShaderInfoLog(shaderId, length, &length, log);
std::cout << "[Compile Error] Failed to compile shader - \"" + source + "\""<< std::endl;
std::cout << log << std::endl;
delete log;
}
}
basic.vert
#version 460 core
layout(location = 0) in vec3 position;
out vec4 fColor;
void main()
{
gl_Position.xyz = position;
gl_Position.w = 1.0;
fColor = vec4(gl_Position.xyz, 1.0);
}
basic.frag
#version 460 core
in vec4 fColor;
layout(location = 0) out vec4 diffuseColor;
void main()
{
diffuseColor = vec4(1,0,0,0);
}
我删除了一些不执行ogl调用的特定于类的代码, 如果那使诊断变得太困难,我可以添加整个代码,因为它还不错。 我已经在片段着色器中尝试了多种出彩方式,而我尝试过的所有样式都没有用,尽管如此,但请让我知道您是否认为片段着色器确实存在问题。
答案 0 :(得分:1)
: 问题是传递给glVertexAttribPointer的步幅参数
发件人:
glVertexAttribPointer(0, sizeof(Vertex::position) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::position), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, sizeof(Vertex::normal) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::normal), (void*)offsetof(Vertex, normal));
glVertexAttribPointer(2, sizeof(Vertex::uv) / sizeof(float), GL_FLOAT, false, sizeof(Vertex::uv), (void*)offsetof(Vertex, uv));
收件人:
glVertexAttribPointer(0, sizeof(Vertex::position) / sizeof(float), GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, position));
glVertexAttribPointer(1, sizeof(Vertex::normal) / sizeof(float), GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glVertexAttribPointer(2, sizeof(Vertex::uv) / sizeof(float), GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, uv));