在 OpenGL 中编程(尝试),我将 -lglew 和 -lfreeglut 放入项目>属性>构建>链接器>编译行>附加选项
这是我的代码:
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/freeglut.h>
#include <iostream>
#include <cstdio>
// Local Utilities Includes
#include "vgl.h"
#include "LoadShaders.h"
using std::cerr;
using std::endl;
// Some constants (different from book)
const int Triangles = 0; // The index of the "Triangles" vertex array
const int NumVAOs = 1; // The number of vertex array objects (VAOs)
const int ArrayBuffer = 0; // The index of the array buffer (0)
const int NumBuffers = 1; // The total number of buffers
const int vPosition = 0; // The starting index of the vertex position attribute
GLuint VAOs[NumVAOs]; // An array of vertex array objects
GLuint Buffers[NumBuffers]; // An array of buffers
const GLuint NumVertices = 6; // 6 total vertices in 2 triangles
void init()
{
// Have OpenGL allocate NumVAOs vertex array object names for our use
// The IDs of these objects are returned in the VAOs array
glGenVertexArrays(NumVAOs, VAOs);
// Bind a vertex array object by creating one (with the name provided
// as an argument) and set that object to "active"
// If 0 is passed in, returns vertex objects to default state
// GL_INVALID_OPERATION error generated if array is not a valid value
glBindVertexArray(VAOs[Triangles]);
// Create an array of vertices
GLfloat vertices[NumVertices][2] = {
{ -0.9, -0.9 },
{ 0.85, -0.9 },
{ -0.9, 0.85 },
{ 0.9, -0.85 },
{ 0.9, 0.9 },
{ -0.85, 0.9} };
// Same with glGenVertexArrays, but do so with buffers
glGenBuffers( NumBuffers, Buffers );
// Bind the current one as an array buffer
glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer] );
// Provide data for the bound array buffer from our vertices array
// Could generate a GL_OUT_OF_MEMORY error
glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
// At least two shaders MUST be supplied:
// 1) vertex shader, which will set vertex data
// 2) fragment shader, which will deal with primitives
ShaderInfo shaders[] = {
{GL_VERTEX_SHADER, "triangles.vert"},
{GL_FRAGMENT_SHADER, "triangles.frag"},
{GL_NONE, NULL }
};
// Helper function to load the shaders (in downloadable code in book)
GLuint program = LoadShaders( shaders );
// Runs the shaders
glUseProgram( program );
// Connect shader's 'in' variable with vertex attribute array
glVertexAttribPointer( vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// Enable the vertex attribute array
glEnableVertexAttribArray( vPosition );
}
// Function to draw stuff
void display()
{
// Clears the given framebuffer (in this case, color)
// Could set color to clear to with glClearColor, default is black
// Where should we put a call to clearColor?
glClear(GL_COLOR_BUFFER_BIT);
// Same as in init, we want to use the Triangles vertex array object
glBindVertexArray(VAOs[Triangles]);
// Draw routine, send vertex array to OpenGL rendering pipeline
// Draw triangles, starting at offset = 0, and going to NumVertices
// What happens when mode is changed?
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
// Force OpenGL commands to begin execution
glFlush();
}
int main(int argc, char* argv[])
{
// First, initialize data structures and arguments needed by glut
glutInit(&argc, argv);
// We're displaying in RGBA color space
glutInitDisplayMode(GLUT_RGBA);
// We want a window that 512 by 512 pixels
glutInitWindowSize(512, 512);
// We're using OpenGL 4.4 (you should change this to fit yours, need 3.2)
g
/usr/bin/ld: cannot find -lglew
/usr/bin/ld: cannot find -lfreeglut
lutInitContextVersion(3,2);
glutInitContextProfile(GLUT_CORE_PROFILE);
// Create the window, and call it the name of the first argument)
glutCreateWindow(argv[0]);
// To be safe, we're using glew's experimental stuff.
glewExperimental=GL_TRUE;
// Initialize and error check GLEW
GLenum err = glewInit();
if (GLEW_OK != err)
{
// If something went wrong, print the error message
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
// Initialize the display in our program
init();
// The display callback...function called whenever image is redrawn
glutDisplayFunc(display);
// Infinite looping, calling idle function over and over
glutMainLoop();
return 0;
}
然后当我运行该程序时,调试控制台错误显示:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/connor/NetBeansProjects/HelloWorldOpenGL'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/helloworldopengl
make[2]: Entering directory `/home/connor/NetBeansProjects/HelloWorldOpenGL'
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/helloworldopengl build/Debug/GNU-Linux-x86/main.o -lglew -lfreeglut
/usr/bin/ld: cannot find -lglew
/usr/bin/ld: cannot find -lfreeglut
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/helloworldopengl] Error 1
make[2]: Leaving directory `/home/connor/NetBeansProjects/HelloWorldOpenGL'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/connor/NetBeansProjects/HelloWorldOpenGL'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 111ms)
在目录中,我是否应该执行 make all 和 make install 命令以外的其他操作?在阅读 make 命令之前,我手动将 .h 文件复制到 /usr/include/GL,并将 .a 文件从 opengl-redbook.com 复制到 /usr/bin
答案1
要在 Ubuntu 上正确构建您的项目,您需要安装以下开发(-dev)包,打开终端并输入:
sudo apt-get install libglew1.5-dev libglew-dev libglewmx1.5-dev libglewmx-dev freeglut3-dev build-essential