glewInit() 失败,OpenGL 应用程序

10

我试图使用glew/glfw构建一个OpenGL应用程序。我已经下载了二进制文件,将它们放在我的文件夹根目录中,并将路径添加到包含和lib目录,并告诉我的项目需要 glew32.lib、GLFW.lib和opengl32.lib。

我甚至将glew32.lib复制到根目录,因为我的项目看不见它。

由于我将要分发这个项目,所以我必须将所有的依赖项都保留在项目目录中。但我现在无法解决问题。

现在当我运行我的程序时,它在 glewInit() 处失败。

这是我目前的实现:

#include "Common.h"

GameEngine::GameEngine()
{
    InitWithSize(1024, 768);
    InitInput();
}

void GameEngine::InitWithSize(int _width, int _height)
{
    try {
        // Initialise GLFW
        if( !glfwInit() )
            throw std::exception("Failed to initialize GLFW\n");

        //glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); 
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
        glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

        // Open a window and create its OpenGL context
        if( !glfwOpenWindow( _width, _height, 0,0,0,0, 32,0, GLFW_WINDOW ) ) 
            throw std::exception("Failed to initialize GLFW\n");


        glfwSetWindowTitle( "Tutorial 01" );

        // Initialize GLEW
        if (glewInit() != GLEW_OK)
            throw std::exception("Failed to initialize GLEW\n");


    } catch ( std::system_error const& err) {
        fprintf(stdout, "System Error: %s", err.what());
        glfwTerminate(); // Free glfw if it has been allocated
        // Try Again
        this->InitWithSize(_width, _height);
    } catch( std::exception const& err) {
        fprintf(stdout, "Exception Found: %s", err.what());
    } catch ( ... ) {
        fprintf(stdout,"Unknown Exception Occurred\n");
    }
}

void GameEngine::InitInput()
{
        // Ensure we can capture the escape key being pressed below
        glfwEnable( GLFW_STICKY_KEYS );
}

void GameEngine::StartGameLoop()
{
    do{
            // Draw nothing, see you in tutorial 2 !

            // Swap buffers
            glfwSwapBuffers();

        } // Check if the ESC key was pressed or the window was closed
        while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
            glfwGetWindowParam( GLFW_OPENED ) );
}


void GameEngine::InitTestData()
{
    // An array of 3 vectors which represents 3 vertices
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f,
    };

}

使用我的通用标题:

#ifndef _COMMON_H
#define _COMMON_H

// OpenGL Libraries
#define GLEW_STATIC
//#pragma comment(lib, "glew32.lib")
#include <GL\glew.h>
#include <GL\glfw.h>

// Core Libraries
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <fstream>

// C++ 11 Libraries
#include <memory>
#include <exception>
#include <thread>
#include <chrono>

// Manager Classes
#include "ThreadManager.h"
#include "GameEngine.h"
#include "ShaderManager.h"

// Lesser Classes
#include "Shader.h"

#endif

也许可以放上你的代码?我在日志中没有看到任何真正的错误,只是一个通知程序已经执行完毕。你可以忽略所有的PDB符号行。 - Tim
4
glewInit() 实际上会返回一个错误代码,你可以将其与 glewGetErrorString 一起使用。参见 http://glew.sourceforge.net/basic.html - Grimmy
5个回答

14

我知道这个答案来得有点晚,但我发现你没有调用 glfwMakeContextCurrent(window) 使OpenGL上下文成为当前上下文。在调用 glewInit() 之前必须先执行该操作。


7

如果glewInit()函数没有返回错误代码,它将返回0。由于某种原因,返回值与GLEW_OK的比较不够准确,但是如果返回值不为0,就意味着出现了错误。

if(glewInit()) {

  //Handle Error

}

6

您正在使用核心OpenGL 3.3版本,因此必须指定您正在使用“新”的GLEW术语和“实验性”的API。在调用glewInit()之前添加此行:

glewExperimental=GL_TRUE;

这里是我引擎的完整初始化代码。虽然是针对4.2版本编写的,但你也可以尝试使用:

 void Engine::InitWithGLFW(){
    if(!glfwInit()){
        exit(EXIT_FAILURE);
    }
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    //glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES,4);
    glfwDisable(GLFW_AUTO_POLL_EVENTS);

 #ifdef DEBUG
    glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
 #endif

    if(!glfwOpenWindow(_width,_height,8, 8, 8, 8, 24, 8,GLFW_WINDOW)){
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetWindowTitle("XDEngine V-1.0");
    InitGlew();
 }

 void Engine::InitGlew(){
    glewExperimental=GL_TRUE;
    GLenum err = glewInit(); 

    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));

    }

    fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_MULTISAMPLE);
    glEnable(GL_DEPTH_CLAMP);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glfwSetWindowSizeCallback(Reshape);
 }

2

我曾经遇到类似的问题,最终在opengl.org上找到了解决方案。 看起来GLEW开发者还没有修复核心上下文的问题。 不过有一个非常简单的解决方案:

  glewExperimental=TRUE;
  GLenum err=glewInit();
  if(err!=GLEW_OK)
  {
    //Problem: glewInit failed, something is seriously wrong.
    cout<<"glewInit failed, aborting."<<endl;
  }

HTH


1

glewInit()的返回类型与GLenum进行比较。

if (glewInit() != GLEW_OK) {
    printf("glew initialization failed!");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接