0

I am following the 8Th edition and below is the code I modified from book. it does not give me what I want, but a blank window instead. I wonder what mistake(s) did I make during modifications?

below is the code I modified, hope anyone can point out the mistake.

#include <iostream>
#include <OpenGL/glu.h>
#include <OpenGL/gl3.h>
#include <GLUT/GLUT.h>

GLuint vaoNames[1];
GLuint bufferNames[1];
int vPosition = 0;

GLuint createProgram();
GLuint createShader(GLenum type, const char* src);
GLuint program;

void init()
{
    glGenVertexArrays(1, vaoNames);
    glBindVertexArray(vaoNames[0]);

    GLfloat  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, bufferNames);
    glBindBuffer(GL_ARRAY_BUFFER, bufferNames[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glUseProgram(createProgram());
    glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(vPosition);
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(vaoNames[0]);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glFlush();
}

GLuint createProgram()
{
    GLuint program = glCreateProgram();

    const char* vertexShaderSrc =
    "#version 400 core                              \n"
    "layout( location = 0 ) in vec4 vPosition;      \n"
    "void main()                                    \n"
    "{                                              \n"
        "gl_Position = vPosition;                   \n"
    "}";
    GLuint vertexShader = createShader(GL_VERTEX_SHADER, vertexShaderSrc);

    const char* fragmentShaderSrc =
    "#version 400 core                              \n"
    "out vec4 fColor;                               \n"
    "void main()                                    \n"
    "{                                              \n"
    "fColor = vec4( 0.0, 0.0, 1.0, 1.0 );           \n"
    "}";
    GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, fragmentShaderSrc);

    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);

    glLinkProgram(program);
    return program;
}

GLuint createShader(GLenum type, const char* src){
    GLuint shaderID = glCreateShader(type);
    glShaderSource(shaderID, 1, &src, 0);
    glCompileShader(shaderID);
    return shaderID;
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);
    glutCreateWindow("8877");

    init();
    glutDisplayFunc(render);
    glutMainLoop();
}
5
  • Are you trying to do something silly like use this on OSX? Commented Dec 21, 2013 at 5:39
  • Do you get any errors other than a blank screen? Can you post a screen shot of what you get and what you expect (if you can that is)? Commented Dec 21, 2013 at 5:43
  • Why aren't your checking the shader compilation and link logs? Commented Dec 21, 2013 at 5:43
  • @genpfault there is no error during shader compilation and program linking. I am just a beginner this is the hello world example. thanks Commented Dec 21, 2013 at 5:56
  • @rhughes, There is no any error and all i got is just a black blank window,I am expecting a scene with two triangles from the code according to the tutorial <<OpenGL Programming Guide>> first example presented. Commented Dec 21, 2013 at 6:03

1 Answer 1

3

When you say 'there is no error during shader compilation and program linking', do you mean that the the program doesn't stop, or do you mean that you actually ran a different program and checked the results of glGetError() at various points, or fetched the compilation or linking log? Your program as is does no actual error checking, and OpenGL doesn't throw exceptions or produce error output as a matter of course. You have to actually request these things.

Simply because glCompileShader() doesn't generate an error doesn't mean your shader actually compiled. You need to be checking the compilation status of shaders and the link status of programs with code similar to this:

    GLint compiled;
    glGetShaderiv(newShader, GL_COMPILE_STATUS, &compiled);
    if (!compiled) {
        std::string log = getLog(newShader);
        SAY(log.c_str());
        throw std::runtime_error("Failed to compile shader " + log);
    }

where getLog is

static std::string getLog(GLuint shader) {
    std::string log;
    GLint infoLen = 0;
    glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);

    if (infoLen > 1) {
        char* infoLog = new char[infoLen];
        glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
        log = std::string(infoLog);
        delete[] infoLog;
    }
    return log;
}

This is for checking shaders, and there are equivalent functions for checking the results and getting the logs for program linking. Unless you're calling them you have no guarantee that the shaders are compiling. Assuming you haven't made some simple mistake in the shader syntax, the most likely problem you're facing is a lack of support for OpenGL 4.x, which would cause the shaders to fail right off.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Jherico, It seems the problem was Mac os x supports lower version of Opengl cause shader failed to compile.
I should have guessed. The statement #include <OpenGL/gl3.h> is distinctive to the Mac OS, and I've been struggling with OSX's lack of 4.x support myself.
Me too, According to this link developer.apple.com/graphicsimaging/opengl/capabilities/…, my code should support GLSL 4.1. somehow it does not seems to be. I am still working on that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.