2016-11-20 61 views
0

無論是我笨還是令人難以置信的經驗不足(可能兩者都有),但我被莫名其妙地停留在圖紙使用OpenGL的一個簡單的彩色矩形。它不應該是一個問題,因爲我完成了一個簡單的OpenGL模型加載器(但是這是我的第一個項目,所以我只能做參考一切一次)。反正除了藍色背景之外什麼都沒有顯示出來。Opengl的矩形慣於渲染

香港專業教育學院試圖使簡單的類基本形狀。這裏是矩形類:

class AVRect{ 
private: 
    AVcolor Color; 
    GLuint VBO, VAO, EBO; 
    GLuint indices[6] = { // Note that we start from 0! 
     0, 1, 3, // First Triangle 
     1, 2, 3 // Second Triangle 
    }; 
public: 
    GLfloat geometry[12]; 
    AVRect(int Drawmode, GLfloat x, GLfloat y, GLfloat x2, GLfloat y2,GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4,GLfloat r,GLfloat b,GLfloat g,GLfloat a); 
    void Draw(Shader shader); 
}; 

不要擔心顏色。我只在源文件中設置它,但不在其他地方使用。一旦我的矩形以固定顏色工作,我將盡快實現它。

的RECT類的來源:

#include "stdafx.h" 
#include "Shapes.h" 

AVRect::AVRect(int Drawmode,GLfloat x, GLfloat y, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4, GLfloat r, GLfloat b, GLfloat g, GLfloat a) { 
    geometry[0] = x; 
    geometry[1] = y; 
    geometry[2] = 0.0f; 
    geometry[3] = x2; 
    geometry[4] = y2; 
    geometry[5] = 0.0f; 
    geometry[6] = x3; 
    geometry[7] = y3; 
    geometry[8] = 0.0f; 
    geometry[9] = x4; 
    geometry[10] = y4; 
    geometry[11] = 0.0f; 
    Color.r = r; 
    Color.b = b; 
    Color.g = g; 
    Color.a = a; 
    glGenVertexArrays(1, &VAO); 
    glGenBuffers(1, &VBO); 
    glGenBuffers(1, &EBO); 
    glBindVertexArray(VAO); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(geometry), geometry, Drawmode); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, Drawmode); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); 
    glEnableVertexAttribArray(0); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 

} 

void AVRect::Draw(Shader shader) { 
    glUseProgram(shader.Program); 
    glBindVertexArray(VAO); 
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); 
    glBindVertexArray(0); 
} 

主要是非常自我解釋:

// Bullethell.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <GL/glew.h> 
#include <SDL.h> 
#include <SDL_opengl.h> 
#include <GLTextures.h> 
#include <Shaders.h> 
#include <ctime> 
#include "Shapes.h" 
#define HEIGHT 600 
#define WIDTH 600 

int main(int argc, char* args[]) 
{ 
    SDL_Window *window; 
    SDL_Renderer *renderer; 
    window = SDL_CreateWindow("BulletHell", 100, 100, HEIGHT, WIDTH, SDL_WINDOW_OPENGL); 
    SDL_GLContext glcontext = SDL_GL_CreateContext(window); 

    glewExperimental = GL_TRUE; 
    if (GLEW_OK != glewInit()) 
    { 
     // GLEW failed! 
     printf("GLEW INIT FAILED!"); 
    } 
    Shader basic("./shaders/colorTest/colorTest.vs", "./shaders/colorTest/colorTest.frag"); 
    AVRect test(GL_STATIC_DRAW,0.5f,0.5f,0.5f,-0.5f,-0.5f,-0.5f,0.5f,-0.5f,0.5f,0.9f,0.2f,0.5f); 
    int error = 0; 
    while (SDL_PollEvent(NULL)) 
    { 
     clock_t start = clock(); 
     glClearColor(0.2, 0.2, 0.5, 1); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     test.Draw(basic); 
     SDL_GL_SwapWindow(window); 
     error = glGetError(); 
     if (error != GL_NO_ERROR) 
      printf("GL ERROR:%d ", error); 
     while (((clock() - start) * 1000/CLOCKS_PER_SEC) <16) 
      ; 

     float MS = (float)((clock() - start) * 1000/CLOCKS_PER_SEC); 
     // Frames per seconds 
     float FPS = 1000.0f/MS; 
     //printf("%f \n",FPS); 
     // printf("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); 
    } 
    return 1; 
} 

着色器類進行了測試和正在工作。着色器是簡單且也應該工作(只傳遞值,並分配一個固定的顏色)。

我可能只是盲目的,因爲我看不到的錯誤。請幫我解決這個問題。


下面是完整起見,着色代碼:

#include "stdafx.h" 
#include "Shaders.h" 

Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath) 
{ 
    // 1. Retrieve the vertex/fragment source code from filePath 
    std::string vertexCode; 
    std::string fragmentCode; 
    std::ifstream vShaderFile; 
    std::ifstream fShaderFile; 

    // ensures ifstream objects can throw exceptions: 
    vShaderFile.exceptions (std::ifstream::badbit); 
    fShaderFile.exceptions (std::ifstream::badbit); 
    try 
    { 
     // Open files 
     vShaderFile.open(vertexPath); 
     fShaderFile.open(fragmentPath); 
     std::stringstream vShaderStream, fShaderStream; 
     // Read file's buffer contents into streams 
     vShaderStream << vShaderFile.rdbuf(); 
     fShaderStream << fShaderFile.rdbuf(); 
     // close file handlers 
     vShaderFile.close(); 
     fShaderFile.close(); 
     // Convert stream into string 
     vertexCode = vShaderStream.str(); 
     fragmentCode = fShaderStream.str(); 
    } 
    catch (std::ifstream::failure e) 
    { 
     std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; 
    } 
    const GLchar* vShaderCode = vertexCode.c_str(); 
    const GLchar * fShaderCode = fragmentCode.c_str(); 
    // 2. Compile shaders 
    GLuint vertex, fragment; 
    GLint success; 
    GLchar infoLog[512]; 
    // Vertex Shader 
    vertex = glCreateShader(GL_VERTEX_SHADER); 
    glShaderSource(vertex, 1, &vShaderCode, NULL); 
    glCompileShader(vertex); 
    // Print compile errors if any 
    glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); 
    if (!success) 
    { 
     glGetShaderInfoLog(vertex, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; 
    } 
    // Fragment Shader 
    fragment = glCreateShader(GL_FRAGMENT_SHADER); 
    glShaderSource(fragment, 1, &fShaderCode, NULL); 
    glCompileShader(fragment); 
    // Print compile errors if any 
    glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); 
    if (!success) 
    { 
     glGetShaderInfoLog(fragment, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; 
    } 
    // Shader Program 
    this->Program = glCreateProgram(); 
    glAttachShader(this->Program, vertex); 
    glAttachShader(this->Program, fragment); 
    glLinkProgram(this->Program); 
    // Print linking errors if any 
    glGetProgramiv(this->Program, GL_LINK_STATUS, &success); 
    if (!success) 
    { 
     glGetProgramInfoLog(this->Program, 512, NULL, infoLog); 
     std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; 
    } 
    // Delete the shaders as they're linked into our program now and no longer necessery 
    glDeleteShader(vertex); 
    glDeleteShader(fragment); 

} 
// Uses the current shader 
void Shader::Use() 
{ 
    glUseProgram(this->Program); 
} 
+0

我添加源到主帖。我用我的其他項目(一個簡單的模型加載器與紋理和基本照明)相同的來源,它也工作。 – MoustacheSpy

回答

3

您正在繪製退化三角形,所以沒有像素將被渲染。如果你看一下你的「矩形」的座標,間隔,方便易讀:

0.5f, 0.5f, 
0.5f, -0.5f, 
-0.5f, -0.5f, 
0.5f, -0.5f 

你可以看到,第2和第4頂點具有相同的座標。您頂點的安排是這樣的:

 0 

2  1/3 

進行比較,以你的指標:

0, 1, 3, // First Triangle 
1, 2, 3 // Second Triangle 

,你會看到兩個三角形是退化的,因爲他們的兩個頂點的是相同的。

座標你可能想用的是:

0.5f, 0.5f, 
0.5f, -0.5f, 
-0.5f, -0.5f, 
-0.5f, 0.5f 

我真的建議使用反時針方向的幾何體,因爲這是默認在OpenGL纏繞順序。儘管除非您打算啓用剔除,否則它並不重要。

+0

謝謝你!工作很棒! – MoustacheSpy