出于练习目的,我将OpenGL红皮书9ed ex. 3.7从"旧"函数转换为4.5函数。我以前做过,它在我的系统上工作过,但这一次,它没有显示任何东西,只显示glClearBufferfv()
颜色。我试图按照这个例子到发球台,我没有遗漏任何东西。我刚刚将glBufferData()
转换为glNamedBufferStorage()
.还有一些其他的事情。这是书中出现的原始代码。但是让我复制粘贴我自己的代码和着色器:
主.cpp
#include <iostream>
#include <glad/glad.h>
#include <KHR/khrplatform.h>
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SOIL2/SOIL2.h>
#include <Shader.h>
#include <Camera.h>
enum VAO_IDs { Triangles, NumVAOs, ID3 };
enum Buffer_IDs { ArrayBuffer1, ArrayBuffer2, EBOBuffer, NumBuffers
};
enum Attrib_IDs { vPosition = 0, cPosition = 1 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
void init()
{
GLfloat cube_positions[] = {
-1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
GLfloat cube_colors[] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f
};
GLushort cube_indices[] = {
0, 1, 2, 3, 6, 7, 4, 5,
0xFFFF,
2, 6, 0, 4, 1, 5, 3, 7
};
glCreateVertexArrays(NumVAOs, VAOs);
glCreateBuffers(NumBuffers, Buffers);
glNamedBufferStorage(Buffers[ArrayBuffer1], sizeof(cube_positions), cube_positions, GL_ARRAY_BUFFER);
glNamedBufferStorage(Buffers[ArrayBuffer2], sizeof(cube_colors), cube_colors, GL_ARRAY_BUFFER);
glNamedBufferStorage(Buffers[EBOBuffer], sizeof(cube_indices), cube_indices, GL_ELEMENT_ARRAY_BUFFER);
Shader myShader("TirangleVert.glsl", "TriangleFrag.glsl");
myShader.Use();
glBindVertexArray(VAOs[Triangles]);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer1]);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer2]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[EBOBuffer]);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(cPosition, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sizeof(cube_positions));
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(cPosition);
}
void display()
{
GLfloat black[] = { 0.2f, 0.5f, 0.6f };
glClearBufferfv(GL_COLOR, 0, black);
glBindVertexArray(VAOs[Triangles]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[EBOBuffer]);
glLineWidth(20);
glPointSize(20);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0xFFFF);
glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL);
//glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL);
//glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT,
// (const GLvoid *)(9 * sizeof(GLushort)));
}
void dealloc()
{
glDeleteVertexArrays(NumVAOs, VAOs);
glDeleteBuffers(NumBuffers, Buffers);
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
init();
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
display();
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
dealloc();
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
TirangleVert.glsl:
#version 450 core
layout (location = 0) in vec4 cube_pos;
layout (location = 1) in vec4 colors;
out vec4 myColor;
void main()
{
gl_Position = cube_pos;
myColor = colors;
}
TriangleFrag.glsl:
#version 450 core
layout (location = 0) in vec4 myColor;
out vec4 color;
void main()
{
color = myColor;
}
PS:我意识到顶点着色器的名称中有拼写错误,但这一切都得到了解释。另外,Shader.h没有问题,因为我以前使用过它,没有问题。
非常感谢。
glNamedBufferStorage
的最后一个参数是位字段,它指定缓冲区数据存储的预期用法,而不是GL_ARRAY_BUFFER
或GL_ELEMENT_ARRAY_BUFFER
。这将导致GL_INVALID_VALUE
错误:
GLbitfield flags = GL_DYNAMIC_STORAGE_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT;
glNamedBufferStorage(Buffers[ArrayBuffer1], sizeof(cube_positions), cube_positions, flags);
glNamedBufferStorage(Buffers[ArrayBuffer2], sizeof(cube_colors), cube_colors, flags);
glNamedBufferStorage(Buffers[EBOBuffer], sizeof(cube_indices), cube_indices, flags);
glVertexAttribPointer
指定一个顶点数组,该数组引用当前绑定的数组缓冲区。
在调用glVertexAttribPointer
之前,必须由 glBindBuffer
绑定正确的缓冲区。请注意,当前只能绑定 1 个缓冲区。
如果绑定了缓冲区,则最后一个参数(指针)将被视为缓冲区对象数据存储中的字节偏移量。在您的情况下,此参数必须为 0。请注意,在核心配置文件上下文中,您必须使用数组缓冲区,直接寻址内存无效。
glBindVertexArray(VAOs[Triangles]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Buffers[EBOBuffer]);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer1]);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer2]);
glVertexAttribPointer(cPosition, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(cPosition);