gpt4 book ai didi

c++ - OpenGL - 未渲染的顶点/三角形

转载 作者:行者123 更新时间:2023-11-28 04:25:34 31 4
gpt4 key购买 nike

所以我最近一直在尝试学习 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;
}

模型.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));
}

着色器.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;
}
}

基本垂直

#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);
}

基本片段

#version 460 core

in vec4 fColor;

layout(location = 0) out vec4 diffuseColor;

void main()
{
diffuseColor = vec4(1,0,0,0);
}

我删除了一些不执行任何 ogl 调用的类特定代码,如果这使得诊断变得太困难,我可以添加整个代码,因为它还不错。我已经在片段着色器中尝试了多种颜色样式,但我尝试过的都没有奏效,话虽如此,如果您认为问题确实出在片段着色器中,请告诉我。

最佳答案

@datemwolf 指出:问题出在传递给 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));

关于c++ - OpenGL - 未渲染的顶点/三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54401918/

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