出于练习目的,我转换了OpenGL Red Book 9ed ex。 3.7从“旧”功能改为4.5功能。我之前已经做过,并且可以在我的系统上使用,但是这次,它没有显示任何内容,仅显示glClearBufferfv()
颜色。我尝试按照示例进行三通,但我没有忽略任何内容。我刚刚将glBufferData()
转换为glNamedBufferStorage()
。还有其他一些事情。 Here's原始代码as出现在书中。但是,让我复制粘贴自己的代码和着色器:
Main.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也没有问题,因为我以前使用它时没有任何问题。
非常感谢。
答案 0 :(得分:0)
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个缓冲区。
如果绑定了缓冲区,则最后一个参数( pointer )被视为缓冲区对象的数据存储区中的字节偏移量。在您的情况下,该参数必须为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);