2013-07-11 64 views
0

我想使用openGL在藍色背景上繪製一個簡單的三角形,但是當我編譯並運行代碼時,只會出現一個帶有藍色背景的窗口(沒有假定的白色三角形要繪製),使用的Xcode在這段代碼中不能繪製使用Opengl的三角形

我的代碼

#include <iostream> 
#include <string> 
#include <GLUT/glut.h> 
#include <OpenGL/gl3.h> 
#include <fstream> 

using namespace std; 

// VAO & VBO objects 
GLuint VBO; 
GLuint VAO; 

void display(); 

// vertex Data (position) 
float vertex[] = {-1.0, 0.0 , 1.0, 
        0.0, 1.0 , 0.0, 
        0.0, 0.0 , 0.0 }; 

GLuint Program; 

GLuint Vshader; 
GLuint Fshader; 

// main program 

int main (int argc, char *argv[]) 

{  

    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE); 
    glutInitWindowSize(1000, 400); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("My First GLUT/OpenGL Window"); 
    glutDisplayFunc(display); 
    glutMainLoop(); 


    return 0; 
} 



// main display function 

void display() 

{ 
// reading the vertex shader 

fstream VertexS; 
VertexS.open("/Users/hawbashali/Desktop/Project X/BwlbWl/BwlbWl/vertexShader"); 

if(VertexS.good() == false) 
    cout << "Error openning the file \n"; 
if(VertexS.bad() == true) 
    cout << "Read/writing error on i/o operation \n"; 
if (VertexS.fail() == true) 
    cout <<"Logical error on i/o operation \n"; 

VertexS.seekg(0,ios::end); 
int size = (int)VertexS.tellg(); 
VertexS.clear(); 
VertexS.seekg(0,ios::beg); 

char* vBuffer = new (nothrow) char[size]; 

VertexS.read(vBuffer,size); 

VertexS.close(); 



// reading fragment shader 

fstream FragS; 
FragS.open("/Users/hawbashali/Desktop/Project X/BwlbWl/BwlbWl/fragmentShader"); 

if(FragS.good() == false) 
    cout << "Error openning the file \n"; 
if(FragS.bad() == true) 
    cout << "Read/writing error on i/o operation \n"; 
if (FragS.fail() == true) 
    cout <<"Logical error on i/o operation \n"; 

FragS.seekg(0,ios::end); 
int size2 = (int)FragS.tellg(); 
FragS.clear(); 
FragS.seekg(0,ios::beg); 

char* fBuffer = new (nothrow) char[size2]; 

FragS.read(fBuffer,size2); 

FragS.close(); 


// creating shaders 

Vshader = glCreateShader(GL_VERTEX_SHADER); 
Fshader = glCreateShader(GL_FRAGMENT_SHADER); 

GLint x = size; 
GLint y = size2; 


glShaderSource(Vshader, 1,(const char**)&vBuffer, &x); 
glShaderSource(Fshader, 1, (const char**)&fBuffer, &y); 

glCompileShader(Vshader); 
glCompileShader(Fshader); 

Program = glCreateProgram(); 

glAttachShader(Program, Vshader); 
glAttachShader(Program, Fshader); 

glLinkProgram(Program); 

glUseProgram(Program); 



glClearColor(0, 0, 1,1); 
glClear(GL_COLOR_BUFFER_BIT); 

glGenBuffers(1,&VBO); 
glBindBuffer(GL_ARRAY_BUFFER, VBO); 
glBufferData(GL_ARRAY_BUFFER,9 *sizeof(vertex),vertex, GL_STATIC_DRAW); 

glEnableVertexAttribArray(0); 

glVertexAttribPointer(0, 3,GL_FLOAT, GL_TRUE, 0, 0); 

// Drawing the triangle 
glDrawArrays(GL_TRIANGLES, 0, 3); 

glDisableVertexAttribArray(0); 

glutSwapBuffers(); 

glDeleteShader(Vshader); 
glDeleteShader(Fshader); 

delete [] vBuffer; 
delete [] fBuffer; 

}; 

頂點着色器IAM:

#version 320 core 
layout(location = 0) in vec4 vPosition; 
void 
main() 
{ 
    gl_Position = vPosition; 
} 

片段着色器

#version 320 core 
out vec4 fColor; 
void 
main() 
{ 
    fColor = vec4(0.0, 0.0, 1.0, 1.0); 
} 
+1

你到底打算如何在OSX上使用'#version 430 core'? – genpfault

+0

@genpfault好吧,現在我知道了它的困難的方式,即使我將它更改回#version 320結果是相同的.. – BulBul

回答

4
glClearColor(0, 0, 1,1); 
... 
fColor = vec4(0.0, 0.0, 1.0, 1.0); 

你試着畫在藍色背景上一個藍色三角形。你必須撥打你的對比pretty high看到:)

使其中一個不同的顏色,如紅色。

你也:

試試這個:

#include <GL/glew.h> 
#include <GL/freeglut.h> 
#include <vector> 
#include <iostream> 

struct Program 
{ 
    static GLuint Load(const char* vert, const char* geom, const char* frag) 
    { 
     GLuint prog = glCreateProgram(); 
     if(vert) AttachShader(prog, GL_VERTEX_SHADER, vert); 
     if(geom) AttachShader(prog, GL_GEOMETRY_SHADER, geom); 
     if(frag) AttachShader(prog, GL_FRAGMENT_SHADER, frag); 
     glLinkProgram(prog); 
     CheckStatus(prog); 
     return prog; 
    } 

private: 
    static void CheckStatus(GLuint obj) 
    { 
     GLint status = GL_FALSE, len = 10; 
     if(glIsShader(obj)) glGetShaderiv(obj, GL_COMPILE_STATUS, &status); 
     if(glIsProgram(obj)) glGetProgramiv(obj, GL_LINK_STATUS, &status); 
     if(status == GL_TRUE) return; 
     if(glIsShader(obj)) glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &len); 
     if(glIsProgram(obj)) glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &len); 
     std::vector<char> log(len, 'X'); 
     if(glIsShader(obj)) glGetShaderInfoLog(obj, len, NULL, &log[0]); 
     if(glIsProgram(obj)) glGetProgramInfoLog(obj, len, NULL, &log[0]); 
     std::cerr << &log[0] << std::endl; 
     exit(-1); 
    } 

    static void AttachShader(GLuint program, GLenum type, const char* src) 
    { 
     GLuint shader = glCreateShader(type); 
     glShaderSource(shader, 1, &src, NULL); 
     glCompileShader(shader); 
     CheckStatus(shader); 
     glAttachShader(program, shader); 
     glDeleteShader(shader); 
    } 
}; 

#define GLSL(version, shader) "#version " #version "\n" #shader 

const char* vert = GLSL 
( 
    150 core, 
    layout(location = 0) in vec4 vPosition; 
    void 
    main() 
    { 
     gl_Position = vPosition; 
    } 
); 

const char* frag = GLSL 
( 
    150 core, 
    out vec4 fColor; 
    void 
    main() 
    { 
     fColor = vec4(1.0, 0.0, 0.0, 1.0); 
    } 
); 

// VAO & VBO objects 
GLuint VAO; 
GLuint VBO; 
GLuint prog; 
void init() 
{ 
    // vertex Data (position) 
    float vertex[] = { -1.0, -1.0 , 0.0, 
         1.0, -1.0 , 0.0, 
         0.0, 1.0 , 0.0 }; 

    glGenVertexArrays(1, &VAO); 
    glBindVertexArray(VAO); 

    glGenBuffers(1,&VBO); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(vertex), vertex, GL_STATIC_DRAW); 

    prog = Program::Load(vert, NULL, frag); 
    glUseProgram(prog); 

    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); 
} 

void display() 
{ 
    glClearColor(0, 0, 1,1); 
    glClear(GL_COLOR_BUFFER_BIT); 

    // Drawing the triangle 
    glDrawArrays(GL_TRIANGLES, 0, 3); 
    glutSwapBuffers(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 
    glutInitWindowSize(1000, 400); 
    glutInitWindowPosition(100, 100); 
    glutInitContextVersion(3, 2); 
    glutInitContextProfile(GLUT_CORE_PROFILE); 
    glutCreateWindow("My First GLUT/OpenGL Window"); 

    glewExperimental = GL_TRUE; 
    if(GLEW_OK != glewInit()) 
     return -1; 

    init(); 

    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 

Ë DIT:移植到GL 2.1:

#include <GL/glew.h> 
#include <GL/glut.h> 
#include <vector> 
#include <iostream> 

struct Program 
{ 
    static GLuint Load(const char* vert, const char* geom, const char* frag) 
    { 
     GLuint prog = glCreateProgram(); 
     if(vert) AttachShader(prog, GL_VERTEX_SHADER, vert); 
     if(geom) AttachShader(prog, GL_GEOMETRY_SHADER, geom); 
     if(frag) AttachShader(prog, GL_FRAGMENT_SHADER, frag); 
     glLinkProgram(prog); 
     CheckStatus(prog); 
     return prog; 
    } 

private: 
    static void CheckStatus(GLuint obj) 
    { 
     GLint status = GL_FALSE, len = 10; 
     if(glIsShader(obj)) glGetShaderiv(obj, GL_COMPILE_STATUS, &status); 
     if(glIsProgram(obj)) glGetProgramiv(obj, GL_LINK_STATUS, &status); 
     if(status == GL_TRUE) return; 
     if(glIsShader(obj)) glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &len); 
     if(glIsProgram(obj)) glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &len); 
     std::vector<char> log(len, 'X'); 
     if(glIsShader(obj)) glGetShaderInfoLog(obj, len, NULL, &log[0]); 
     if(glIsProgram(obj)) glGetProgramInfoLog(obj, len, NULL, &log[0]); 
     std::cerr << &log[0] << std::endl; 
     exit(-1); 
    } 

    static void AttachShader(GLuint program, GLenum type, const char* src) 
    { 
     GLuint shader = glCreateShader(type); 
     glShaderSource(shader, 1, &src, NULL); 
     glCompileShader(shader); 
     CheckStatus(shader); 
     glAttachShader(program, shader); 
     glDeleteShader(shader); 
    } 
}; 

#define GLSL(version, shader) "#version " #version "\n" #shader 

const char* vert = GLSL 
( 
    120, 
    attribute vec4 vPosition; 
    void main() 
    { 
     gl_Position = vPosition; 
    } 
); 

const char* frag = GLSL 
( 
    120, 
    void main() 
    { 
     gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 
    } 
); 

// VBO objects 
GLuint VBO; 
GLuint prog; 
void init() 
{ 
    // vertex Data (position) 
    float vertex[] = { -1.0, -1.0 , 0.0, 
         1.0, -1.0 , 0.0, 
         0.0, 1.0 , 0.0 }; 

    glGenBuffers(1,&VBO); 
    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(vertex), vertex, GL_STATIC_DRAW); 

    prog = Program::Load(vert, NULL, frag); 
    glUseProgram(prog); 

    int posLoc = glGetAttribLocation(prog, "vPosition"); 
    glEnableVertexAttribArray(posLoc); 
    glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0); 
} 

void display() 
{ 
    glClearColor(0, 0, 1,1); 
    glClear(GL_COLOR_BUFFER_BIT); 

    // Drawing the triangle 
    glDrawArrays(GL_TRIANGLES, 0, 3); 
    glutSwapBuffers(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 
    glutInitWindowSize(1000, 400); 
    glutInitWindowPosition(100, 100); 
    glutCreateWindow("My First GLUT/OpenGL Window"); 

    if(GLEW_OK != glewInit()) 
     return -1; 

    init(); 

    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 
+0

感謝您的回覆,但是這並沒有解決問題,我嘗試了代碼[Here ](http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction)它工作得很好,但事實是,它不使用VBO或VAO,我想使用它們,所以我熟悉不同的方式在opengl中繪製,雖然我修改了代碼,所以它使用VBO,但它仍然與背景顏色卡住,你有任何進一步的建議?我真的需要幫助。 – BulBul

+0

我發佈的程序是如何在您的系統上發佈失敗的?像以前一樣缺少三角形? – genpfault

+0

iam使用xcode進行編碼,它返回使用glut庫不freeglut(Windows實現),我的代碼給'glutInitContextVersion(3,2);'glutInitContextProfile(GLUT_CORE_PROFILE)錯誤**錯誤消息**使用未聲明的標識符**我認爲這是特定於freeglut不過度本身,當我註釋掉這些行我得到以下錯誤_ERROR:0:1:'':版本'150'不支持_ _ERROR:0:1:'':語法錯誤#version_ _ERROR:0:2:'layout':語法錯誤語法error_ – BulBul

0

您已在下面的行提供無效的緩衝區大小:

glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(vertex), vertex, GL_STATIC_DRAW); 

由於sizeof(vertex)應返回陣列的總尺寸(例如36)而不是基本的類型,因此沒有必要提高9乘以如果你還是想用乘法嘗試以下操作:

9 * sizeof(float) 

我已經寫下來...你應該SEPA評估你的初始化和繪製週期,就像genpfault的答案一樣。

相關問題