SDL和OpenGL程序编译失败

我正在尝试构建一个简单的OpenGL和SDL程序,但它拒绝工作。

我正在使用最新的NVIDIA驱动程序(如果我记得,有些东西是285.),我用mingw编译。 我使用的机器运行Windows7。 我对mingw的了解有限,所以这可能是一个非常简单的链接错误。

SDL im使用的版本是1.2(我应该升级到1.3吗?)

起初,一切正常,但我只是通过SDL打开一个opengl窗口。 这里是原始makefile的代码

all:
g++ -o sdlGL.exe esg.cpp tiletest.cpp -lmingw32 -lSDLmain -lSDL -lopengl32 -lglu32

这些是被调用的2个函数

    void init_sdl(int width, int height) {
        //Start SDL
        if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
            fDebug << "Failed to init SDL" << std::endl;
            quit(1);
        }

        //Set SDL attrib.
        SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
        SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

        //Set SDL video mode
        if( SDL_SetVideoMode(width, height, 0, SDL_OPENGL) == 0 ) {
            fDebug << "Failed to set SDL video mode" << std::endl;
            quit(1);
        }
    }

    //Initializes OpenGL
    void init_opengl(int width, int height, bool alpha) {
        // Set the OpenGL state after creating the context with SDL_SetVideoMode
        glClearColor( 0, 0, 0, 0 );
        glClear(GL_COLOR_BUFFER_BIT);
        glEnable( GL_TEXTURE_2D ); // Need this to display a texture

        if(alpha) {
            glEnable (GL_BLEND);
            glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        }

        glViewport( 0, 0, width, height );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( 0, width, height, 0, -1, 1 );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    }

现在的内含物:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

有了这些代码,一切正常。 然后我添加了一些功能来照顾着色器和着色器程序

    void CreateShader(GLenum eShaderType, const std::string &strShaderFile, std::vector<GLuint> &shaderList) {
        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);


        shaderList.push_back(shader);
    }

    void ClearShader(std::vector<GLuint> &shaderList) {
        std::for_each(shaderList.begin(), shaderList.end(), glDeleteShader);
    }

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

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

        return program;
    }

现在,编译器抱怨glCreateShader,glShaderSource,glCompileShader,glGetShaderiv,glDeleteShader,glCreateProgram,glAttachShader,glLinkProgram,glGetProgramiv和glDetachShader未在此范围内声明。

我想我需要包括欢乐合唱团或者专线(对吧?),所以我去了。 然后,我将-glew32添加到我的生成文件中,并将glew.h添加到我的包含文件中

#include "GL/glew.h"

现在,编译器列出了很多glew.h的错误,例如----不命名类型,----未在此范围内声明。

我不知道该如何解决这个问题。 我应该补充一点,我对openGL没有太多的经验。


GLEW处理GL头的平台特定的#include ,所以你不再需要SDL_opengl.h 。 它也#include包括合适的GLU标题,所以你也不需要那个。

正如Xavier在其他任何事情之前指出#include <GL/glew.h> 。 如果我记得正确的话,它往往会与windows.h冲突。


然后,我将-glew32添加到我的生成文件中,并将glew.h添加到我的包含文件中

-glew32不对,它是-lglew32#include <GL/glew.h>标题应该包含在与OpenGL交互的其他所有内容之前。

链接地址: http://www.djcxy.com/p/38447.html

上一篇: SDL and OpenGL program compilation fails

下一篇: Windows 8 and SDL/GLEW