2012-12-09 28 views
6

我試圖用glew/glfw構建一個OpenGL應用程序。我已經下載了二進制文件,將它們放在我的文件夾的根目錄下,將路徑添加到include和lib目錄,並告訴我的項目需要glew32.lib,GLFW.lib和opengl32.lib。glewInit()失敗,OpenGL應用程序

我甚至將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 
+0

之前這樣做也許把你的代碼?我沒有在該日誌中看到任何實際的錯誤,只是一個程序完成執行的通知。您可以忽略所有的PDB符號行。 – Tim

+3

'glewInit()'實際上會返回一個錯誤代碼,您可以使用'glewGetErrorString'。請參閱http://glew.sourceforge.net/basic.html – Grimmy

回答

5

如果從glewInit()返回任何錯誤則返回0。某種原因,返回的結果與GLEW_OK沒有很好的比較,但如果該值不是0,則出現錯誤。

if(glewInit()) { 

    //Handle Error 

} 
3

您正在使用核心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); 
} 
1

我有類似的問題,最後得到了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

5

我知道這個答案來得有點晚了,但我沒有看到你通過調用glfwMakeContextCurrent(window)使得OpenGL上下文電流。您必須在致電glewInit()