我正在运行一个基本的openGL程序,该程序可以使用以下命令在命令提示符下编译并正常运行

g ++ gltest.cpp -std = c ++ 11 -lglew32s -lglfw3 -lgdi32 -lopengl32 -o gltest.exe

但是,当我尝试在netbeans中使用它时,会出现很多“未定义的对_imp____glewCreateShader的引用”错误。对glew函数的所有引用。

我将所需的库放在链接器选项中

c++ - 无法让Netbeans正确链接GLEW-LMLPHP

c++ - 无法让Netbeans正确链接GLEW-LMLPHP

这是代码:

// g++ gldrop.cpp -std=c++11 -lglew32s -lglfw3 -lgdi32 -lopengl32 -o gldrop.exe

#define GLEW_STATIC
#include <iostream>
#include "gl\glew.h"
#include "gl\glfw3.h"
#include "shaderM.cpp"
#include "glm\glm\gtc\matrix_transform.hpp"
#include "glm\glm\glm.hpp"

#include "data.cpp"

GLFWwindow* window;

int main(int argv, char** argc)
{
//initialize GLFW
if (!glfwInit())
{
    std::cout << "Failed to initialize GLFW \n";
    return -1;
}

glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

//create GLFW window
window = glfwCreateWindow(640, 480, "GL", NULL, NULL);

glfwMakeContextCurrent(window);
glewExperimental = true;

//initialize GLEW
if(glewInit() != GLEW_OK)
{
    std::cout << "failed to initialize glew";
    return -1;
}

glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

//background
glClearColor(0.4f, 0.3f, 0.7f, 0.0f);

//depth test
glEnable(GL_DEPTH_TEST);
//takes fragments closer to the camera
glDepthFunc(GL_LESS);

//Vertex array object
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

//Vertex buffer
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

//color buffer
GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

//create shaders and attach them to a program object
GLuint program = rigShadersToProgram();

GLuint matrixID = glGetUniformLocation(program, "MVP");
//projection matrix 45 degree FoV, 4:3 ratio, display range 0.1 - 100
glm::mat4 projection = glm::perspective(70.0f, 4.0f/3.0f, 0.1f, 100.0f);
//camera matrix
glm::mat4 view = glm::lookAt(
                    glm::vec3(4,3,-3), //camera is at (4,3,-3)
                    glm::vec3(0,0, 0), //looks at origin
                    glm::vec3(0,1, 0)  //head is up
                    );
//model matrix identity matrix
glm::mat4 model = glm::mat4(1.0f);
//model-view-projection
glm::mat4 MVP   = projection * view * model;

while (!glfwWindowShouldClose(window))
{
    //clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //use the compiled shaders
    glUseProgram(program);
    //send transformation matrix to currently bound shader
    glUniformMatrix4fv(matrixID, 1, GL_FALSE, &MVP[0][0]);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

    glVertexAttribPointer(
        0,          //index
        3,          //size
        GL_FLOAT,   //type
        GL_FALSE,   //normalized?
        0,          //stride
        0           //array buffer offset
    );

    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);

    glVertexAttribPointer(
        1,          //index
        3,          //size
        GL_FLOAT,   //type
        GL_FALSE,   //normalized?
        0,          //stride
        0           //array buffer offset
    );

    //draw triangle
    glDrawArrays(GL_TRIANGLES, 0, 12*3); //start @ vertex 0, 3 verticies

    glfwSwapBuffers(window);
    glfwPollEvents();
}

glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &colorbuffer);
glDeleteProgram(program);
glDeleteVertexArrays(1, &vao);

glfwTerminate();

return 0;


}

不知道为什么它不能识别库。

最佳答案

您在哪里下载glew32?
也许您下载了Visual C版本,否则您可能会解压缩错误的版本(64位)。

我听到的另一件事(请先尝试)
我在mingw上遇到glew32s的问题,由于某种原因它不起作用,请使用常规的glew32。它仍应与预处理器一起使用,并且可以静态编译而没有任何问题。

10-08 05:06