繁体   English   中英

始终获得空白的黑色窗口,而不会显示OPENGL编程指南第8版中显示的三角形

[英]Always get a blank black window without showing triangles shown in the OPENGL programming guide 8th edition

我一直在尝试构建Opengl编程指南(第8版)中第一个示例的程序,但是我所得到的只是一个空白的黑色窗口,没有显示两个三角形。 我检查了所有汇编,似乎一切正常。 有人可以给我些帮助吗?

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <glew.h>
#include <FreeGL\freeglut.h>

using namespace std;

GLint attribute_v=-1; // global variables
GLuint vertexbuffer;
GLuint VertexArrayID;
GLuint program;
int width=500; int height=700;

struct ShaderInfo
{
    GLenum vshader;
    const char* vertexfile;
    GLenum fshader;
    const char* fragmentfile;
};

const char* getshaderprogram(const char* filename, string& shader)
{
    fstream shaderFile( filename, ios::in );
    if ( shaderFile.is_open() )
    {
        std::stringstream buffer;
        buffer << shaderFile.rdbuf();
        shader = buffer.str();
        buffer.clear();
    }
    shaderFile.close();
    return shader.c_str();
}

GLuint LoadShaders(ShaderInfo shaderinfo)
{
    GLuint program;
    //load and compile vertex shader
    GLuint vertexshader=glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentshader=glCreateShader(GL_FRAGMENT_SHADER);
    string shaderprogramtext;
    const char*     Vertexshadersource=getshaderprogram(shaderinfo.vertexfile,shaderprogramtext);
    const GLint vlength=shaderprogramtext.size();
    glShaderSource(vertexshader,1,&Vertexshadersource,&vlength);

    glCompileShader(vertexshader);
    for(unsigned int i=0;i<shaderprogramtext.size();++i)
    {
        cout<<Vertexshadersource[i];
    }
    cout<<endl;
    GLint status;
    glGetShaderiv( vertexshader, GL_COMPILE_STATUS, &status );

    if( status!=GL_TRUE )
    {
        std::cerr<<"unable to compile the vertex shader..."<<endl;
    }
    char* infoLog=new char[100];
    GLsizei buffsize=0;
    glGetShaderInfoLog(vertexshader,buffsize,NULL,infoLog);
    for(int i=0;i<buffsize;++i)
    {
        cout<<infoLog[i];
    }
    cout<<endl;
    delete[] infoLog;

    //load and compile fragment shader
    string fragmentshadertext;
    const char* Fragmentshadersource=getshaderprogram(shaderinfo.fragmentfile,fragmentshadertext    );
    const GLint flength=fragmentshadertext.size();
    for(unsigned int i=0;i<fragmentshadertext.size();++i)
    {
        std::cout<<Fragmentshadersource[i];
    }
    cout<<endl;
    glShaderSource(fragmentshader,1,&Fragmentshadersource,&flength);
    glCompileShader(fragmentshader);

    glGetShaderiv( fragmentshader, GL_COMPILE_STATUS, &status );
    if ( status != GL_TRUE )
    {
        cerr << "\nFragment Shader compilation failed..." << '\n';
    }
    infoLog = new char[];
    buffsize = 0;
    glGetShaderInfoLog( fragmentshader, buffsize, NULL, infoLog );
    for ( int i = 0; i < buffsize; ++i )
    {
        cout << infoLog[ i ] << endl;
    }
    delete [] infoLog;

    // create the shader program
    program = glCreateProgram();
    // attach the vertex and fragment shaders to the program
    glAttachShader( program, vertexshader );
    glAttachShader( program, fragmentshader );

    // link the objects for an executable program
    glLinkProgram( program );
    GLint compiled;
    glGetProgramiv( program, GL_LINK_STATUS, &compiled );
    if ( !compiled )
    {
        cout << "Link failed..." << endl;
    }

    cout<<"Program:"<<program<<endl;

    // return the program
    return program;
}

void init(void)
{
    glGenVertexArrays(1, &VertexArrayID);

    glBindVertexArray(VertexArrayID);
    GLdouble vertices[6][2] = {
    { -0.90, -0.90 }, // Triangle 1
    { 0.85, -0.90 },
    { -0.90, 0.85 },
    { 0.90, -0.85 }, // Triangle 2
    { 0.90, 0.90 },
    { -0.85, 0.90 }
    };
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    ShaderInfo shaders = { GL_VERTEX_SHADER, "vertex_shader.txt" , GL_FRAGMENT_SHADER, "fragment_shader.txt" };
    program = LoadShaders(shaders);

    glUseProgram(program);
    glVertexAttribPointer(attribute_v, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(attribute_v);
    const GLchar* attribute_name = "attribute_v";  // this name has to be the same variable name in the vertex_shader file. 
    attribute_v = glGetAttribLocation(program, attribute_name);   // check attribute activated. 
    if (attribute_v == -1)
    {
        cout<<"could not bind attribute for vertex..."<<endl; 
    }
}

void display(void)
{
    //glClearColor(1.0f,1.0f,1.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VertexArrayID);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glFlush();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(200,200);
    glutInitContextVersion(4,2);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow("Triangle Visualization");

    glewExperimental = GL_TRUE; 
    if (glewInit())
    {
        cerr << "Unable to initialize GLEW ... exiting" << endl;
        exit(EXIT_FAILURE);
    }
    cout << "The version of opengl is : "<<glGetString(GL_VERSION)<<endl;

    init();
    glutDisplayFunc(display);
    glutMainLoop();
}

此处的语句顺序错误:

glVertexAttribPointer(attribute_v, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attribute_v);
const GLchar* attribute_name = "attribute_v"; 
attribute_v = glGetAttribLocation(program, attribute_name); 

您正在使用attribute_v然后为其分配值。 由于将其初始化为-1,因此glVertexAttribPointer()glEnableVertexAttribArray()将导致GL_INVALID_VALUE错误。

const GLchar* attribute_name = "attribute_v"; 
attribute_v = glGetAttribLocation(program, attribute_name);
glVertexAttribPointer(attribute_v, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(attribute_v);

如果OpenGL渲染有任何问题,最好调用glGetError()来检查是否触发了任何错误情况。

这很可能是视频卡驱动程序问题。 不知道您的环境是什么,但这对我有用。

环境:Dell Inspiron 15 7559,Nvidia GeForce GTX 960M,Ubuntu 15.10

安装顺序很重要,特别是最后安装视频卡包。 请注意,您要先将操作系统更新到最新版本。 另外,如果您的操作系统是32位的,则在所有软件包名称的末尾附加“:386i”。 如果不存在32位版本,则apt-get会让您知道。 但是,如果使用OpenGL 4.x,大多数人都在运行64位Linux。

关于显卡驱动程序的一个说明,apt储存库可能无法为您提供最新的驱动程序,对于我来说,“ nvidia-current”软件包就是这种情况。 我必须通过打开Ubuntu软件和更新实用程序来找到最新的Nvidia软件包的名称:

Ubuntu软件和更新实用程序(屏幕抓图)

这是OpenGL Redbook 8th Edition所需的软件包,以及安装它们的顺序:

sudo apt-get install linux-headers-generic build-essential
sudo apt-get install mesa-utils
sudo apt-get install libxmu-dev libxmu-headers
sudo apt-get install freeglut3-dev
sudo apt-get install libglew1.10
sudo apt-get install libglew-dev

# Bad!
#sudo apt-get install nvidia-current
# Good :)
sudo apt-get install nvidia-352-updates

暂无
暂无

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

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