Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: First application, red book

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    9

    First application, red book

    Hi,
    I tried to compile my first application (red book,chapter1) but there are too much error.
    This is my github: https://github.com/DavideZanin/OpenGL_001_triangolo
    This is the structure:https://dl.dropboxusercontent.com/u/...2000.39.24.png
    This is the code:
    #ifdef WIN32
    #include <windows.h>
    #endif

    #include <iostream>
    using namespace std;
    #include <fstream>
    #include <GLUT/glut.h>
    #include <OpenGL/glu.h>
    #include <OpenGL/gl.h>


    enum VAO_IDs { Triangles, NumVAOs };
    enum Buffer_IDs { ArrayBuffer, NumBuffers };
    enum Attrib_IDs { vPosition = 0 };


    GLuint VAOs[NumVAOs];
    GLuint Buffers[NumBuffers];
    const GLuint NumVertices = 6;

    void init(void)
    {
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);
    GLfloat vertices[NumVertices][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(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    ShaderInfo shaders[] = {
    { GL_VERTEX_SHADER, "triangles.vert" },
    { GL_FRAGMENT_SHADER, "triangles.frag" },
    { GL_NONE, NULL }
    };
    GLuint program = LoadShaders(shaders);
    glUseProgram(program);
    glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
    glEnableVertexAttribArray(vPosition);
    }


    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);
    glFlush();

    }




    int
    main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);

    if (glewInit()) {
    cerr << "Unable to initialize GLEW ... exiting" << endl; exit(EXIT_FAILURE);
    }


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

    }
    Errors:https://dl.dropboxusercontent.com/u/...2001.43.11.png
    What is the problem?
    Last edited by onlyzanna; 03-31-2014 at 04:45 PM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    You are missing includes. Try to find how to setup a basic program setup for OpenGL on a Apple system.

  3. #3
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    From the error messages, it looks like you're building on Mac OS X 10.9? They seem pretty serious about deprecating GLUT. I was successful using it with GL 3.2 the other day, but I'm not sure if anything newer is still possible. What I did was:
    • Include gl3.h before glut.h.
    • Afterwards, and still before including glut.h, define __gl_h_. This prevents glut.h from including the old GL header, and then complaining about conflicts.
    • Add GLUT_3_2_CORE_PROFILE to the flags passed to glutInitDisplayMode().
    • Add a compiler flag to suppress the warnings about the deprecated GLUT entry points. Forgot the exact name, something like -Wno-deprecated-...

    I don't see the glutInitContext*() calls defined in glut.h, so you'll have to disable them for Mac OS.

    ShaderInfo and BUFFER_OFFSET need to be defined somewhere in your code. I don't think they are defined anywhere in a standard header.

    Another possibility might be to use an alternate GLUT implementation. I believe there are open source implementations around.

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    9
    Yes, I use osx Mavericks
    If I include gl3.h: warning gl.h and gl3.h are both included. Compiler will not invoke errors if using removed OpenGL functionality. (I also tried to remove gl.h).
    What should i write to prevents glut.h from including the old GL header?
    And why the book used GL and not gl3? (It's the bible of opengl!)
    I used the last red book, openGL 4.3, and this is the first example, It's so frustrating!! (If the example is from openGL 4.3 why something is deprecated???)
    In addition, I'm a beginner so I can't understand all the errors!
    Last edited by onlyzanna; 04-22-2014 at 12:05 PM.

  5. #5
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    Quote Originally Posted by onlyzanna View Post
    If I include gl3.h: warning gl.h and gl3.h are both included. Compiler will not invoke errors if using removed OpenGL functionality.
    That's where the second bullet item in my previous post comes in. I had success with the following sequence:
    Code :
    #include <OpenGL/gl3.h>
    #define __gl_h_
    #include <GLUT/glut.h>
    The compiler option I used to make it shut up about the deprecated functions is -Wno-deprecated-declarations.

    It's not OpenGL itself that you're having problems with, it's the toolkit (GLUT) that they use for the examples. Certainly frustrating that the toolkit they use is not really supported anymore on your platform of choice. As I suggested in my previous post, there are open source implementations of GLUT, which might be worth trying. I know there's something called FreeGLUT, but have no personal experience with it. This question goes in the same direction, and has some answers:

    http://stackoverflow.com/questions/1...os-x-mavericks

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    9
    Quote Originally Posted by reto.koradi View Post
    That's where the second bullet item in my previous post comes in. I had success with the following sequence:
    Code :
    #include <OpenGL/gl3.h>
    #define __gl_h_
    #include <GLUT/glut.h>
    The compiler option I used to make it shut up about the deprecated functions is -Wno-deprecated-declarations.

    It's not OpenGL itself that you're having problems with, it's the toolkit (GLUT) that they use for the examples. Certainly frustrating that the toolkit they use is not really supported anymore on your platform of choice. As I suggested in my previous post, there are open source implementations of GLUT, which might be worth trying. I know there's something called FreeGLUT, but have no personal experience with it. This question goes in the same direction, and has some answers:

    http://stackoverflow.com/questions/1...os-x-mavericks
    These are my errors/warnings now:
    Click image for larger version. 

Name:	Schermata 2014-04-23 alle 01.02.49.jpg 
Views:	78 
Size:	13.7 KB 
ID:	1271
    What should I do?
    My question now is: Is possible to use xcode and OpenGL 4.3?

  7. #7
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    Well, of course you can always use OpenGL the way Apple wants you to: Using Cocoa instead of GLUT. The initial learning curve will be higher, though, because you'll have to pick up another non-trivial framework. An alternative is FreeGLUT. Then there's also GLFW (http://www.glfw.org), which I haven't used, but seems to become popular as a GLUT replacement.

    Looking at your Xcode structure linked in the original post, you seem to have a pretty wild mix of different things. You have references to the OpenGL frameworks, but then you also have your own OpenGL headers. Similar for GLUT, where you have a reference to the standard framework, but you have FreeGLUT stuff contained in your project. Your code has a call to glewInit(), which is for a toolkit that is typically used under Windows.

    I only use Xcode when I absolutely have to, so I'm not the right person to help you with that.

    I grabbed the code from github link in your original post, and got most of it building on my Mac without much trouble. I don't have the LoadShaders() code, so I temporarily disabled those calls. Which of course means that it wouldn't render anything. But the code below builds without warnings or errors with this command line:

    Code :
    clang -Wno-deprecated-declarations glut.cpp -framework GLUT -framework OpenGL

    This was with the following variation of your original code in a source file named glut.cpp. Sorry, I couldn't resist cleaning up some formatting while I was editing it.
    Code :
    #include <OpenGL/gl3.h>
    #define __gl_h_
    #include <GLUT/glut.h>
     
    enum VAO_IDs { Triangles, NumVAOs };
    enum Buffer_IDs { ArrayBuffer, NumBuffers };
    enum Attrib_IDs { vPosition = 0 };
     
    GLuint  VAOs[NumVAOs];
    GLuint  Buffers[NumBuffers];
    const GLuint NumVertices = 6;
     
    void init()
    {
        glGenVertexArrays(NumVAOs, VAOs);
        glBindVertexArray(VAOs[Triangles]);
        GLfloat vertices[NumVertices][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(NumBuffers, Buffers);
        glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
    #if 0
        ShaderInfo shaders[] = {
            { GL_VERTEX_SHADER, "triangles.vert" },
            { GL_FRAGMENT_SHADER, "triangles.frag" },
            { GL_NONE, NULL }
        };
     
        GLuint program = LoadShaders(shaders);
    #else
        GLuint program = 0;
    #endif
     
        glUseProgram(program);
        glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(vPosition);
    }
     
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(VAOs[Triangles]);
        glDrawArrays(GL_TRIANGLES, 0, NumVertices);
        glutSwapBuffers();
    }
     
    int main(int argc, char* argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_3_2_CORE_PROFILE);
        glutInitWindowSize(512, 512);
        glutCreateWindow(argv[0]);
     
        init();
        glutDisplayFunc(display);
        glutMainLoop();
    }

  8. #8
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    9
    Quote Originally Posted by reto.koradi View Post
    Well, of course you can always use OpenGL the way Apple wants you to: Using Cocoa instead of GLUT. The initial learning curve will be higher, though, because you'll have to pick up another non-trivial framework. An alternative is FreeGLUT. Then there's also GLFW (http://www.glfw.org), which I haven't used, but seems to become popular as a GLUT replacement.

    Looking at your Xcode structure linked in the original post, you seem to have a pretty wild mix of different things. You have references to the OpenGL frameworks, but then you also have your own OpenGL headers. Similar for GLUT, where you have a reference to the standard framework, but you have FreeGLUT stuff contained in your project. Your code has a call to glewInit(), which is for a toolkit that is typically used under Windows.

    I only use Xcode when I absolutely have to, so I'm not the right person to help you with that.

    I grabbed the code from github link in your original post, and got most of it building on my Mac without much trouble. I don't have the LoadShaders() code, so I temporarily disabled those calls. Which of course means that it wouldn't render anything. But the code below builds without warnings or errors with this command line:

    Code :
    clang -Wno-deprecated-declarations glut.cpp -framework GLUT -framework OpenGL

    This was with the following variation of your original code in a source file named glut.cpp. Sorry, I couldn't resist cleaning up some formatting while I was editing it.
    Code :
    #include <OpenGL/gl3.h>
    #define __gl_h_
    #include <GLUT/glut.h>
     
    enum VAO_IDs { Triangles, NumVAOs };
    enum Buffer_IDs { ArrayBuffer, NumBuffers };
    enum Attrib_IDs { vPosition = 0 };
     
    GLuint  VAOs[NumVAOs];
    GLuint  Buffers[NumBuffers];
    const GLuint NumVertices = 6;
     
    void init()
    {
        glGenVertexArrays(NumVAOs, VAOs);
        glBindVertexArray(VAOs[Triangles]);
        GLfloat vertices[NumVertices][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(NumBuffers, Buffers);
        glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
    #if 0
        ShaderInfo shaders[] = {
            { GL_VERTEX_SHADER, "triangles.vert" },
            { GL_FRAGMENT_SHADER, "triangles.frag" },
            { GL_NONE, NULL }
        };
     
        GLuint program = LoadShaders(shaders);
    #else
        GLuint program = 0;
    #endif
     
        glUseProgram(program);
        glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(vPosition);
    }
     
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glBindVertexArray(VAOs[Triangles]);
        glDrawArrays(GL_TRIANGLES, 0, NumVertices);
        glutSwapBuffers();
    }
     
    int main(int argc, char* argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_3_2_CORE_PROFILE);
        glutInitWindowSize(512, 512);
        glutCreateWindow(argv[0]);
     
        init();
        glutDisplayFunc(display);
        glutMainLoop();
    }
    I tried freeglut and there are also problem in xcode.
    No problem with freeglut in Windows+Visual Studio!
    Sorry for your time! Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •