简体   繁体   English

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

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

I've been trying to build the program of the first example in the Opengl Programming guide book(8th edition), but all I got is a blank black window without showing two triangles. 我一直在尝试构建Opengl编程指南(第8版)中第一个示例的程序,但是我所得到的只是一个空白的黑色窗口,没有显示两个三角形。 I've checked all the compilations, and it seems to be all fine. 我检查了所有汇编,似乎一切正常。 Could anyone give me some help please? 有人可以给我些帮助吗?

#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();
}

Wrong order of statements here: 此处的语句顺序错误:

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); 

You're using attribute_v before assigning a value to it. 您正在使用attribute_v然后为其分配值。 Since it was initialized to -1, the glVertexAttribPointer() and glEnableVertexAttribArray() will result in GL_INVALID_VALUE errors. 由于将其初始化为-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);

If you have any kind of issue with OpenGL rendering, it's always a good idea to call glGetError() to check if any error conditions were triggered. 如果OpenGL渲染有任何问题,最好调用glGetError()来检查是否触发了任何错误情况。

This may very well be a video card driver issue. 这很可能是视频卡驱动程序问题。 Not sure what your environment is but this is what worked for me. 不知道您的环境是什么,但这对我有用。

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

Install order matters, specifically install the video card package last. 安装顺序很重要,特别是最后安装视频卡包。 Note that you want to update your OS to the latest version first. 请注意,您要先将操作系统更新到最新版本。 Also, if your OS is 32bit then append ":386i" to the end of all the package names. 另外,如果您的操作系统是32位的,则在所有软件包名称的末尾附加“:386i”。 If a 32bit version doesn't exist then apt-get will let you know. 如果不存在32位版本,则apt-get会让您知道。 However most people are running 64bit Linux if they're working with OpenGL 4.x. 但是,如果使用OpenGL 4.x,大多数人都在运行64位Linux。

One note on the video card drivers, the apt repositories may not give you the latest driver, which was the case for me with the "nvidia-current" package. 关于显卡驱动程序的一个说明,apt储存库可能无法为您提供最新的驱动程序,对于我来说,“ nvidia-current”软件包就是这种情况。 I had to find the name of the most recent Nvidia package by opening the Ubuntu Software & Updates utility: 我必须通过打开Ubuntu软件和更新实用程序来找到最新的Nvidia软件包的名称:

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

Here are the packages that you need for the OpenGL Redbook 8th Edition, and the order in which to install them: 这是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