Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Opengl core simple example  (Read 3011 times)

0 Members and 1 Guest are viewing this topic.

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Opengl core simple example
« on: August 11, 2013, 12:57:20 am »
Hi,

I am trying to make a simple Opengl 3.X or 4.X example that uses only the core features.

For instance, I am trying to migrate the attached example that uses GLUT.

The problem I had doing that is that when I include the functions CreateShader and CreateProgram to my simple SFML example that includes  <SFML/Window.hpp> and <SFML/OpenGL.hpp> it doesnt finds the opengl functions: glCreateShader and other Shader related...

I know that there is a Shader class at SFML but I am trying to load the shaders as the attached example if possible.

Thanks in advance,
Nicolas

GLuint CreateShader(GLenum eShaderType, const std::string &strShaderFile)
{
        GLuint shader = glCreateShader(eShaderType);
        const char *strFileData = strShaderFile.c_str();
        glShaderSource(shader, 1, &strFileData, NULL);

        glCompileShader(shader);

        GLint status;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
        if (status == GL_FALSE)
        {
                GLint infoLogLength;
                glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);

                GLchar *strInfoLog = new GLchar[infoLogLength + 1];
                glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);

                const char *strShaderType = NULL;
                switch(eShaderType)
                {
                case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
                case GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
                case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
                }

                fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
                delete[] strInfoLog;
        }

        return shader;
}

GLuint CreateProgram(const std::vector<GLuint> &shaderList)
{
        GLuint program = glCreateProgram();

        for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
                glAttachShader(program, shaderList[iLoop]);

        glLinkProgram(program);

        GLint status;
        glGetProgramiv (program, GL_LINK_STATUS, &status);
        if (status == GL_FALSE)
        {
                GLint infoLogLength;
                glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);

                GLchar *strInfoLog = new GLchar[infoLogLength + 1];
                glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
                fprintf(stderr, "Linker failure: %s\n", strInfoLog);
                delete[] strInfoLog;
        }

        for(size_t iLoop = 0; iLoop < shaderList.size(); iLoop++)
                glDetachShader(program, shaderList[iLoop]);

        return program;
}

 

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Opengl core simple example
« Reply #1 on: August 11, 2013, 01:09:26 am »
Did you link the OpenGL libraries?  Including the right headers doesn't automate the linking.

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Opengl core simple example
« Reply #2 on: August 11, 2013, 03:09:57 am »
Part of the question i have is which header files should i include instead of glut.
 And which .lib files should i link?

Currently i could run the opengl examples that come
Wirh sfml.

Thanks!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Opengl core simple example
« Reply #3 on: August 11, 2013, 03:57:44 am »
Did you read http://www.sfml-dev.org/tutorials/2.1/window-opengl.php ?

Quote
#include <SFML/OpenGL.hpp>
This header includes OpenGL and GLU functions, and nothing else. People sometimes think that SFML automatically includes GLEW (a library which manages OpenGL extensions) because it uses it internally, but it's only an implementation detail. From the user's point of view, GLEW must be handled like any other extra library.

You will then need to link your program to the OpenGL library. Unlike what it does with the headers, SFML can't provide a unified way of linking OpenGL. Therefore, you need to know which library to link to according to what OS you're using ("opengl32" on Windows, "GL" on Linux, etc.). Same thing for GLU, in case you use it too: "glu32" on Windows, "GLU" on Linux, etc.

nicoraf

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Opengl core simple example
« Reply #4 on: August 11, 2013, 08:38:03 am »
Thank you!

I should include the Glew library.