12

我想开始使用 OpenGL 3+ 和 4,但我在让 Glew 工作时遇到了问题。我试图将 glew32.lib 包含在 Additional Dependencies 中,并将库和 .dll 移动到主文件夹中,因此不应该有任何路径问题。我得到的错误是:

Error   5   error LNK2019: unresolved external symbol __imp__glewInit referenced in function "void __cdecl init(void)" (?init@@YAXXZ)   C:\Users\Mike\Desktop\Test Folder\ModelLoader through VBO\ModelLoader\main.obj  ModelLoader
Error   4   error LNK2019: unresolved external symbol __imp__glewGetErrorString referenced in function "void __cdecl init(void)" (?init@@YAXXZ) C:\Users\Mike\Desktop\Test Folder\ModelLoader through VBO\ModelLoader\main.obj  ModelLoader
Error   3   error LNK2001: unresolved external symbol __imp____glewGenBuffers   C:\Users\Mike\Desktop\Test Folder\ModelLoader through VBO\ModelLoader\main.obj  ModelLoader
Error   1   error LNK2001: unresolved external symbol __imp____glewBufferData   C:\Users\Mike\Desktop\Test Folder\ModelLoader through VBO\ModelLoader\main.obj  ModelLoader
Error   2   error LNK2001: unresolved external symbol __imp____glewBindBuffer   C:\Users\Mike\Desktop\Test Folder\ModelLoader through VBO\ModelLoader\main.obj  ModelLoader

这是我的大部分代码:

#define NOMINMAX

#include <vector>
#include <memory>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <Windows.h>
#include <cstdio>
#include <time.h>
#include "GL\glew.h"
#include "glut.h"

#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")

using namespace std;

GLsizei screen_width, screen_height;

float camera[3] = {0.0f, 10.0f, -15.0f};

float xPos = 0;
float yPos = 10;
float zPos = -15;
float orbitDegrees = 0;

clock_t sTime;
float fPS;
int fCount;

GLdouble* modelV;
GLdouble* projM;
GLint* vPort;

//Lights settings
GLfloat light_ambient[]= { 0.1f, 0.1f, 0.1f, 0.1f };
GLfloat light_diffuse[]= { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat light_specular[]= { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat light_position[]= { 100.0f, 0.0f, -10.0f, 1.0f };

//Materials settings
GLfloat mat_ambient[]= { 0.5f, 0.5f, 0.0f, 0.0f };
GLfloat mat_diffuse[]= { 0.5f, 0.5f, 0.0f, 0.0f };
GLfloat mat_specular[]= { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat mat_shininess[]= { 1.0f };

typedef struct Vectors {
    float x;
    float y;
    float z;
}Vector;

typedef struct Polys {
    Vector v;
    Vector vt;
    Vector vn;
    int texture;
} Poly;

vector<Vector> vecs;
vector<Vector> normVecs;
vector<Vector> textVecs;

vector<Poly> polyList;


void loadModel(string filepath);
void createTex(string ref);
void render();

// 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,
};

void render()
{
}

void createTex(string ref)
{
}

void loadModel(string filepath)
{
}

void resize (int p_width, int p_height)
{
    if(screen_width==0 && screen_height==0) exit(0);
    screen_width=p_width; // Obtain the new screen width values and store it
    screen_height=p_height; // Height value

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear both the color and the depth buffer so to draw the next frame
    glViewport(0,0,screen_width,screen_height); // Viewport transformation

    glMatrixMode(GL_PROJECTION); // Projection transformation
    glLoadIdentity(); // Initialize the projection matrix as identity
    gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,10000.0f);

    glutPostRedisplay(); // This command redraw the scene (it calls the same routine of glutDisplayFunc)
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This clear the background color to dark blue
    glMatrixMode(GL_MODELVIEW); // Modeling transformation
    glPushMatrix();
    glLoadIdentity(); // Initialize the model matrix as identity

    gluLookAt(xPos, yPos, zPos, /* look from camera XYZ */
               0, yPos, 0, /* look at the origin */
               0, 1, 0); /* positive Y up vector */

    glRotatef(orbitDegrees, 0.f, 1.0f, 0.0f);
    //glTranslatef(0.0,0.0,-20); // We move the object forward (the model matrix is multiplied by the translation matrix)
    //rotation_x = 30;
    //rotation_x = rotation_x + rotation_x_increment;
   //rotation_y = rotation_y + rotation_y_increment;
       //rotation_z = rotation_z + rotation_z_increment;

   //if (rotation_x > 359) rotation_x = 0;
   //if (rotation_y > 359) rotation_y = 0;
   //if (rotation_z > 359) rotation_z = 0;

   //  glRotatef(rotation_x,1.0,0.0,0.0); // Rotations of the object (the model matrix is multiplied by the rotation matrices)
   //glRotatef(rotation_y,0.0,1.0,0.0);
   //    glRotatef(rotation_z,0.0,0.0,1.0);

    //if (objarray[0]->id_texture!=-1) 
    //{
    //  glBindTexture(GL_TEXTURE_2D, objarray[0]->id_texture); // We set the active texture 
    //    glEnable(GL_TEXTURE_2D); // Texture mapping ON
    //  printf("Txt map ON");
    //}
    //else
    //    glDisable(GL_TEXTURE_2D); // Texture mapping OFF

    glGetDoublev(GL_PROJECTION_MATRIX, modelV);

    glGetDoublev(GL_PROJECTION_MATRIX, projM);

    glGetIntegerv(GL_VIEWPORT, vPort);

    if(clock() > sTime)
    {
        fPS = fCount;
        fCount = 0;
        sTime = clock() + CLOCKS_PER_SEC;
    }

    render();

    glDisable(GL_LIGHTING);

    GLdouble pos[3];

    gluUnProject(100, yPos, -14, modelV, projM, vPort, &pos[0], &pos[1], &pos[2]);

    char buffer2[255];

    int pAmmount = sprintf(buffer2,"FPS: %.2f", fPS);

    //glRasterPos3f(pos[0], pos[1], pos[2]);

    for(int i = 0; i < pAmmount; i++)
    {
        glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, buffer2[i]);
    }

    glEnable(GL_LIGHTING);

    /*glPopMatrix();
    glPushMatrix();
    glTranslatef(5.0,0.0,-20.0);
    objarray[1]->render();*/
    glPopMatrix();
    glFlush(); // This force the execution of OpenGL commands
    glutSwapBuffers(); // In double buffered mode we invert the positions of the visible buffer and the writing buffer
    fCount++;
}

void keyboard(unsigned char k, int x, int y)
{
    switch(k)
    {
        case 'w':
                yPos++;
        break;
        case 's':
            yPos--;
        break;
        case 'a':
            xPos--;
        break;
        case 'd':
            xPos++;
        break;
        case 'q':
        orbitDegrees--;
        break;
        case 'e':
            orbitDegrees++;
        break;
        case 'z':
            zPos--;
        break;
        case 'x':
            zPos++;
        break;
    }
}

void initWindow(GLsizei screen_width, GLsizei screen_height)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); // Clear background color to black

    // Viewport transformation
    glViewport(0,0,screen_width,screen_height);

    // Projection transformation
    glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations 
    glLoadIdentity(); // We initialize the projection matrix as identity
   gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f,10000.0f); // We define the "viewing volume"

    gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */
               0, 0, 0, /* look at the origin */
               0, 1, 0); /* positive Y up vector */

    try
    {
        //loadModel("Goku habit dechiré.obj");
        loadModel("Flooring.obj");;
    }
    catch(string& filepath)
    {
        cerr << "Model could not be loaded: " << filepath << endl;

        filepath = "Model could not be loaded: " + filepath;

        wostringstream sString;

        sString << filepath.c_str();

        MessageBox(HWND_DESKTOP, sString.str().c_str(), L"Error: loadModel(string filepath)", MB_OK);
    }

    //Lights initialization and activation
    glLightfv (GL_LIGHT1, GL_AMBIENT, light_ambient);
    glLightfv (GL_LIGHT1, GL_DIFFUSE, light_diffuse);
    glLightfv (GL_LIGHT1, GL_DIFFUSE, light_specular);
    glLightfv (GL_LIGHT1, GL_POSITION, light_position);    
    glEnable (GL_LIGHT1);
    glEnable (GL_LIGHTING);

    //Materials initialization and activation
    glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_specular);
    glMaterialfv (GL_FRONT, GL_POSITION, mat_shininess);

    //Other initializations
    glShadeModel(GL_SMOOTH); // Type of shading for the polygons
    //glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Texture mapping perspective correction
    //glEnable(GL_TEXTURE_2D); // Texture mapping ON
    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
    glEnable(GL_CULL_FACE); // Enable the back face culling
    glEnable(GL_DEPTH_TEST); // Enable the depth test 
    glEnable(GL_NORMALIZE);

    /*float* matrix = new float[16];

    glGetFloatv(GL_PROJECTION_MATRIX, matrix);

    for(int i = 0; i < 4; i++)
    {
        cout << matrix[0] << " " << matrix[1] << " " << matrix[2] << " " << matrix[3] << endl;
        matrix += 3;
    }*/

    modelV = new GLdouble[16];

    projM = new GLdouble[16];

    vPort = new GLint[4];

    sTime = clock() + CLOCKS_PER_SEC;
}

void init()
{
    GLenum GlewInitResult;

    GlewInitResult = glewInit();

    if (GLEW_OK != GlewInitResult) {
        fprintf(
            stderr,
            "ERROR: %s\n",
            glewGetErrorString(GlewInitResult)
        );
        exit(EXIT_FAILURE);
    }

    // This will identify our vertex buffer
    GLuint vertexbuffer;

    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);

    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
}

int main(int argc, char **argv)
{
    screen_width = 800;
    screen_height = 800;

    glutInit(&argc, argv);    
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screen_width,screen_height);
    glutInitWindowPosition(0,0);
    glutCreateWindow("ModelLoader");    
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc (resize);
    glutKeyboardFunc(keyboard);

    //glutKeyboardFunc(keyboard);
    //glutSpecialFunc(keyboard_s);
    initWindow(screen_width, screen_height);
    init();
    glutMainLoop();

    return 0;
}
4

3 回答 3

11

你有一个链接问题。您没有向链接器提供 glew lib 文件的正确路径。因此,链接器无法找到您正在调用的函数的编译代码。

从您的日志看来,您正在使用 Windows。如果您使用的是 Visual Studio,请右键单击您的项目。选择链接器,然后选择输入。验证 Additional dependencies 是否包含 glew lib 的路径。

请注意,链接时不需要 dll。这将仅在运行时加载(请记住将其放在与可执行文件相同的文件夹中或系统路径中列出的路径中)。

于 2013-02-12T12:47:15.403 回答
5

这已得到修复。这个问题只出现在我在大学使用的机器上。问题是机器有一个较旧的 .dll 与我在解决方案文件中的 .dll 冲突。它安装在机器的系统文件中。因此,它产生了这些错误。

下次我应该检查一下这样的事情。

于 2014-02-03T11:49:55.430 回答
2

不确定它将如何帮助您,但您可以尝试将您的项目静态链接到“.a”库。或者它是 Windows 上的 .lib。如果有未解析的符号,则意味着它试图链接到库而不是真正的 dll。

如果它使用 dll,它可能会在运行时失败。

于 2012-06-23T12:50:52.827 回答