简体   繁体   English

OS X OpenGL 3.2核心(黑屏)

[英]OS X OpenGL 3.2 Core (Black Screen)

I want to render a Quad via VAO, IBO and VBO but nothing is drawn. 我想通过VAO,IBO和VBO渲染四边形,但是什么也没画。 I'm using glDrawRangeElements in OS X OpenGL 3.2 Core context. 我在OS X OpenGL 3.2 Core上下文中使用glDrawRangeElements。 The screen is completely black without any error. 屏幕是全黑的,没有任何错误。 GLFW3 is used to create window and context. GLFW3用于创建窗口和上下文。

Window opening/Context creation code 窗口打开/上下文创建代码

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
_mainWindow  = glfwCreateWindow(width, height, title, monitor, NULL);
if(_mainWindow == NULL)
{
    return false;
}
_mainWindowWidth  = width;
_mainWindowHeight = height;
glfwSetKeyCallback(_mainWindow, _onKeyEvent);
glfwMakeContextCurrent(_mainWindow);

glewExperimental = GL_TRUE;
glewInit();

_openGLVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));

Shader sources (compiled successfully). 着色器源(成功编译)。 Custom FragColor is binded. 自定义FragColor被绑定。

_vertexShaderSource =
            "#version 150 core\n"
            "in vec3 in_Position;\n"
            "in vec3 in_Normal;\n"
            "in vec2 in_TexCoord;\n"
            "uniform mat4 ModelViewProjection;\n"
            "void main()\n"
            "{\n"
            "   gl_Position = ModelViewProjection * vec4(in_Position, 1.0);\n"
            "}\n";
_fragmentShaderSource =
            "#version 150 core\n"
            "out vec4 FColor;"
            "void main()\n"
            "{\n"
            "   FColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
            "}\n";

Vertices 顶点

Vertex* vertices = new Vertex[6];

vertices[0].nz = 1.0f;
vertices[1].nz = 1.0f;
vertices[2].nz = 1.0f;
vertices[3].nz = 1.0f;
vertices[4].nz = 1.0f;
vertices[5].nz = 1.0f;

vertices[1].y  = height;
vertices[1].v0 = 1.0f;

vertices[2].x  = width;
vertices[2].y  = height;
vertices[2].u0 = 1.0f;
vertices[2].v0 = 1.0f;

vertices[4].x  = width;
vertices[4].u0 = 1.0f;

vertices[5].x  = width;
vertices[5].y  = height;
vertices[5].u0 = 1.0f;
vertices[5].v0 = 1.0f;

_mesh->setVertices(vertices, 6);
vertices = NULL;

Uint16* indices = new Uint16[6];

indices[0] = 0;
indices[1] = 2;
indices[2] = 3;
indices[3] = 0;
indices[4] = 1;
indices[5] = 3;

_mesh->setIndices(indices);
indices = NULL;

Buffer update (checked the data, it seems to be correct) 缓冲区更新(检查数据,似乎是正确的)

// Update VBO
if(_vertexBufferObjectID == 0)
{
        glGenBuffers(1, &_vertexBufferObjectID);
}
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);

float* data = new float[_vertexCount * sizeof(Vertex) / sizeof(float)];
Uint64 begin = 0;
for(Uint32 i = 0; i < _vertexCount; i++)
{
    begin = i * 8;
    data[begin]     = _vertices[i].x;
    data[begin + 1] = _vertices[i].y;
    data[begin + 2] = _vertices[i].z;
    data[begin + 3] = _vertices[i].nx;
    data[begin + 4] = _vertices[i].ny;
    data[begin + 5] = _vertices[i].nz;
    data[begin + 6] = _vertices[i].u0;
    data[begin + 7] = _vertices[i].v0;
}

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * _vertexCount, &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

delete[] data;
data = NULL;

// Update IBO
if(_indexBufferObjectID == 0)
{
    glGenBuffers(1, &_indexBufferObjectID);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Uint16) * _vertexCount, &_indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// Update VAO
if(_vertexArrayObjectID == 0)
{
    glGenVertexArrays(1, &_vertexArrayObjectID);
}
glBindVertexArray(_vertexArrayObjectID);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);
// Vertices
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*)NULL + (0)));
// Normals
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*)NULL + (12)));
// TexCoords
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), ((char*)NULL + (24)));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObjectID);

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Rendering code 渲染代码

glUseProgram(material->_programID);
GLuint mvp = glGetUniformLocation(material->_programID, "ModelViewProjection");
glUniformMatrix4fv(mvp, 1, false, glm::value_ptr(camera_mvp));

glBindVertexArray(node->_mesh->_vertexArrayObjectID);

// Draw
glDrawRangeElements(GL_TRIANGLES, 0, 3, node->_mesh->_vertexCount, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);

Note: camera_mvp is Orthographic 注意:camera_mvp是正交的

0.00333333_0___________0 0 
0__________0.00333333__0 0 
0__________0__________-1 0 
599________599_________0 1

Program Linking 程序链接

_programID = glCreateProgram();
glAttachShader(_programID, vertex_shader);
glAttachShader(_programID, fragment_shader);

glLinkProgram(_programID);
glGetProgramiv(_programID, GL_LINK_STATUS, &result);
glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &loglen);

if(loglen > 0)
{
    char* log = new char[loglen];

    glGetProgramInfoLog(_programID, loglen, 0, log);
    _lastInfoLog = log;

    delete log;
    log = NULL;
}

if(result == GL_FALSE)
{
    glDeleteProgram(_programID);
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);
    return false;
}

glUseProgram(_programID);
glBindAttribLocation(_programID, 0, "in_Position");
glBindAttribLocation(_programID, 1, "in_Normal");
glBindAttribLocation(_programID, 2, "in_TexCoord");
glBindFragDataLocation(_programID, 0, "FColor");
glUseProgram(0);

glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);

You don't have layout in #version 150 so you have to use glGetAttribLocation() to ask OpenGL where it put your attributes and adjust the first parameter of your glVertexAttribPointer() calls appropriately. 您在#version 150没有layout ,因此必须使用glGetAttribLocation()询问OpenGL将属性放在哪里,并适当调整glVertexAttribPointer()调用的第一个参数。

Make sure you bind your program before calling glGetAttribLocation() . 确保在调用glGetAttribLocation()之前绑定程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM