0

I follow the book "OpenGL SuperBible 5th Edition" to study openGL, the capture 6th demo to create shader by hand all not work. Like the demo of "ShadedTriangle", to create a triangle but only create a black ground window.

The error is:

The shader at ShadedIdentity.vp failed to compile with the following error:
ERROR: 0:5: '' :  version '150' is not supported"

and I change the GLSL version to 330,440, 150 etc, but all not work.

and now my mac version is 10.12.5 (16F73) , and the version of code glGetString(GL_VERSION) is 2.1 INTEL-10.25.13

the follow is the full code:

#include <GLTools.h>            // OpenGL toolkit
#include <GLShaderManager.h>    // Shader Manager Class

#ifdef __APPLE__
#include <glut/glut.h>          // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>            // Windows FreeGlut equivalent
#endif

GLBatch triangleBatch;
GLShaderManager shaderManager;

GLint   myIdentityShader;

///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
    {
    glViewport(0, 0, w, h);
    }


///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context. 
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
    {
    // Blue background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

    shaderManager.InitializeStockShaders();

    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 
                          0.5f, 0.0f, 0.0f,
                          0.0f, 0.5f, 0.0f };

    GLfloat vColors [] = { 1.0f, 0.0f, 0.0f, 1.0f,
                           0.0f, 1.0f, 0.0f, 1.0f,
                           0.0f, 0.0f, 1.0f, 1.0f };

    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.CopyColorData4f(vColors);
    triangleBatch.End();

    myIdentityShader = gltLoadShaderPairWithAttributes("ShadedIdentity.vp", "ShadedIdentity.fp", 2, 
                                    GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_COLOR, "vColor");
    }


///////////////////////////////////////////////////////////////////////////////
// Cleanup
void ShutdownRC()
   {
   glDeleteProgram(myIdentityShader);

   }


///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
    {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glUseProgram(myIdentityShader);
    triangleBatch.Draw();

    // Perform the buffer swap to display back buffer
    glutSwapBuffers();
    }


///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
    {
    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Shaded Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);

    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
        }

    SetupRC();

    const char* version = (const char*)glGetString(GL_VERSION);
    fprintf(stderr, "OpenGLVersion: %s\n", version);

    glutMainLoop();

    ShutdownRC();

    return 0;
    }

and the ShadedIdentity.vp is :

// The ShadedIdentity Shader
// Vertex Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 150

in vec4 vColor;
in vec4 vVertex;

out vec4 vVaryingColor;

void main(void) 
    { 
    vVaryingColor = vColor;
    gl_Position = vVertex;
    }

so ,how to solve this problem

genpfault
  • 51,148
  • 11
  • 85
  • 139
jimbo
  • 42
  • 1
  • 7
  • If your graphic card only supports OpenGL 2.1, then non of the versions you tried can be used. 150 corresponds to OpenGL 3.2. The highest version you can use is 120, but note, that the shader you have is not compatible with this version. – BDL Jun 08 '17 at 08:37
  • I dot understand more about "compatible with this version". – jimbo Jun 08 '17 at 08:49
  • I change the version to 1.2.0,and also have error.like:The shader at ShadedIdentity.vp failed to compile with the following error: ERROR: 0:7: Invalid storage qualifiers 'in' in global variable context ERROR: 0:8: Invalid storage qualifiers 'in' in global variable context ERROR: 0:10: Invalid storage qualifiers 'out' in global variable context ERROR: 0:14: Use of undeclared identifier 'vVaryingColor' ERROR: 0:14: Use of undeclared identifier 'vColor' ERROR: 0:15: Use of undeclared identifier 'vVertex' – jimbo Jun 08 '17 at 08:50
  • That's what I said. `in` is not supported in 120. The way you wrote shaders changed between this versions. – BDL Jun 08 '17 at 09:22
  • @BDL Thanks, I know, I'll try other ways to implement openGL 3.2 – jimbo Jun 08 '17 at 09:39

3 Answers3

2

On Mac you need to tell OpenGL which version to use before creating the window.

This answer explains how to do it with GLFW:

I'm still searching for a way to do this with GLUT.

something like this:

glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800,600,"My OpenGL Window", None, None)
izzy
  • 1,309
  • 1
  • 15
  • 25
0

1.50 requires OpenGL 3.2 support. Unless you can get a newer driver (or a GPU), I think you're out of luck on this one.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
0

macOS' system GLUT does not have the capability of creating an OpenGL 3+ context.

FreeGLUT provides a way to do it, taken from here:

glutInitContextVersion(3,2); /* or later versions, core was introduced only with 3.2 */
glutInitContextProfile(GLUT_CORE_PROFILE);

Or you can take @izzy's advice and use GLFW.

flyx
  • 35,506
  • 7
  • 89
  • 126