2016-04-25 57 views
0

我得到一个黑色的屏幕试图让OpenGL的一个简单的三角形与SDL2后...的OpenGL 3+ BlackScreen - C++

我居然用OpenGL初始化窗口,并与对象名称为“EngineOpenGL开始GLEW “:http://pastebin.com/S4YDgY45

然后我开始它应该呈现三角形的场景: ”SceneOpenGL“:http://pastebin.com/GD4f5UDj

主要文件:

#include <SDL.h> 
#include <iostream> 
#include <GL\glew.h> 
#include "EngineOpenGl.h" 

// DEBUG_MemoryLeaks 
#define _CRTDBG_MAP_ALLOC 
#include <stdlib.h> 
#include <crtdbg.h> 
// _DEBUG 

#include "SDLInitVideoException.h" 
int main(int argc, char **argv) { 
    _CrtDumpMemoryLeaks(); 

    EngineOpenGL * scene = new EngineOpenGL("OpenGL", 720, 1280); 
    try { 
     scene->init(); 
    } catch (const SDLInitVideoException &e) { 
     delete scene; 
     std::cout << "Error while initialising SDL" << std::endl; 
    } catch (const SDLCreateWindowException &e) { 
     delete scene; 
     std::cout << "Error while initialising the window" << std::endl; 
    } catch (const SDLCreateContextGLException &e) { 
     delete scene; 
     std::cout << "Error while creating the contextGL" << std::endl; 
    } catch (const GLEWInitException &e) { 
     delete scene; 
     std::cout << "Error while initialising " << std::endl; 
    } catch (const SceneOpenGLException &e) { 
     delete scene; 
     std::cout << "Error while starting the scene" << std::endl; 
    } 
    scene->start(); 
    delete scene; 

    // DEBUG_MemoryLeaks 
    _CrtDumpMemoryLeaks(); 
    // _DEBUG 
    return 0; 
} 

FPS在控制台中显示fps,输入获取用户输入。

编辑:我其实只是试图复制/粘贴代码渲染三角形,并保持不工作...

 #include "SceneOpenGl.h" 

// Shader temporaire : 
#include "Shader.h" 

SceneOpenGL::SceneOpenGL(SDL_Window* window) { 
    if (window == NULL) { 
     throw new SceneOpenGLException(); 
    } 
    // Engine 
    this->window = window; 
    // Tools 
    this->fps = new FPSCount(); 
    this->input = new Input(); 
    // Scene 
    this->run = false; 

    int w, h; 
    SDL_GetWindowSize(window, &w, &h); 
    this->projection = glm::perspective(70.0, (double)(w/h), 1.0, 100.0); 
    this->modelview = glm::mat4(1.0); 
} 


SceneOpenGL::~SceneOpenGL(){ 
    delete fps; 
    delete input; 
} 

void SceneOpenGL::start() { 
    run = true; 

    // Test en attendant ObjectGL.show() // 
    GLfloat vertices[] = { 
     0.5f, 0.5f, 0.0f, // Top Right 
     0.5f, -0.5f, 0.0f, // Bottom Right 
     -0.5f, -0.5f, 0.0f, // Bottom Left 
     -0.5f, 0.5f, 0.0f // Top Left 
    }; 
    GLuint indices[] = { // Note that we start from 0! 
     0, 1, 3, // First Triangle 
     1, 2, 3 // Second Triangle 
    }; 
    // Build and compile our shader program 
    // Vertex shader 
    const GLchar* vertexShaderSource = "#version 330 core\n" 
     "layout (location = 0) in vec3 position;\n" 
     "void main()\n" 
     "{\n" 
     "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n" 
     "}\0"; 
    const GLchar* fragmentShaderSource = "#version 330 core\n" 
     "out vec4 color;\n" 
     "void main()\n" 
     "{\n" 
     "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" 
     "}\n\0"; 

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); 
    glCompileShader(vertexShader); 
    // Check for compile time errors 
    GLint success; 
    GLchar infoLog[512]; 
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); 
    if (!success) 
    { 
     glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; 
    } 
    // Fragment shader 
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); 
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); 
    glCompileShader(fragmentShader); 
    // Check for compile time errors 
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); 
    if (!success) 
    { 
     glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; 
    } 
    // Link shaders 
    GLuint shaderProgram = glCreateProgram(); 
    glAttachShader(shaderProgram, vertexShader); 
    glAttachShader(shaderProgram, fragmentShader); 
    glLinkProgram(shaderProgram); 
    // Check for linking errors 
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); 
    if (!success) { 
     glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; 
    } 
    glDeleteShader(vertexShader); 
    glDeleteShader(fragmentShader); 

    GLuint VBO, VAO, EBO; 
    glGenVertexArrays(1, &VAO); 
    glGenBuffers(1, &VBO); 
    glGenBuffers(1, &EBO); 
    // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s). 
    glBindVertexArray(VAO); 

    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); 
    glEnableVertexAttribArray(0); 

    glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind 

    glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO 


    // /Test // 

    while (run) { 
     // FPS flag 
     if (fps->flag()) { 
      std::cout << "FPS : " << fps->getFps() << std::endl; 
     } 

     // Input check/actions 
     input->updateEvents(); 
     if (input->isKeyPush(SDL_SCANCODE_ESCAPE)) { 
      run = false; 
     } 

     // Cleaning last frame 
     glClear(GL_COLOR_BUFFER_BIT); 

     // Test // 
     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 
     glClear(GL_COLOR_BUFFER_BIT); 

     // Draw our first triangle 
     glUseProgram(shaderProgram); 
     glBindVertexArray(VAO); 
     //glDrawArrays(GL_TRIANGLES, 0, 6); 
     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
     glBindVertexArray(0); 


     // FPS add frame 
     fps->addFrame(); 

     // Rendering 
     SDL_GL_SwapWindow(window); 
    } 
} 
+0

在[mcve]中编辑。 – genpfault

+0

我看不到着色器。你是否有一个? – Robinson

+0

在我遵循的教程中,他们显然没有任何着色器的白色。 (SDL也许可以处理这个) 我让它在没有对象的方式下工作... 但我也试过像Xirema说的(与vbo和vao),我一直没有得到任何东西 – Pixeuh

回答

2

在任何时候,你加载你的顶点数据到显存。你有这样一行看起来像一个尝试到顶点加载到视频内存:

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices); 

....但是,这不是该函数的作用。

您需要创建和绑定一个顶点数组和顶点缓冲区:

GLuint vertex_array_id; 
glGenVertexArrays(1, &vertex_array_id); 
glBindVertexArray(vertex_array_id); 
GLuint vertex_buffer_id; 
glGenBuffers(1, &vertex_buffer_id); 
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_id); 

然后,在这个顶点数组和顶点缓冲区绑定,则需要到缓冲区明确提供的数据:

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

然后您使用glVertexAttribPointer来指定数据的布局方式。

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr); 
glEnableVertexAttribArray(0); 

顺便提一句,所有这些代码都应该在循环之外。然后在绘制循环内,您只需拨打glDrawArrays即可。

+0

Minor nitpick:缓冲器中的数据可以在任何时间点提供,并且不需要具有VAO。 – peppe

+0

我正在关注一个关于VBO/VAO后来的教程...无论如何,我尝试了您的解决方案,并且一直拿到黑屏。 – Pixeuh

+0

似乎你的三角形和framebuffer有一个黑色。尝试在glClear调用之前添加glClearColor(1.0,0.0,0.0,1.0)以用红色填充帧缓冲区。 –