GLFW 和代码块
我在使用代码块 10.05 识别机器上的 GLFW 库时遇到一些困难。当我创建一个空项目时,复制粘贴从 GLFW 教程中找到的代码>> http://content.gpwiki.org/index.php/GLFW:教程:基础知识
#include <stdlib.h>
#include <GL/glfw.h>
void Init(void);
void Shut_Down(int return_code);
void Main_Loop(void);
void Draw_Square(float red, float green, float blue);
void Draw(void);
float rotate_y = 0,
rotate_z = 0;
const float rotations_per_tick = .2;
int main(void)
{
Init();
Main_Loop();
Shut_Down(0);
}
void Init(void)
{
const int window_width = 800,
window_height = 600;
if (glfwInit() != GL_TRUE)
Shut_Down(1);
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
0, 0, 0, GLFW_WINDOW) != GL_TRUE)
Shut_Down(1);
glfwSetWindowTitle("The GLFW Window");
// set the projection matrix to a normal frustum with a max depth of 50
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect_ratio = ((float)window_height) / window_width;
glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
glMatrixMode(GL_MODELVIEW);
}
void Shut_Down(int return_code)
{
glfwTerminate();
exit(return_code);
}
void Main_Loop(void)
{
// the time of the previous frame
double old_time = glfwGetTime();
// this just loops as long as the program runs
while(1)
{
// calculate time elapsed, and the amount by which stuff rotates
double current_time = glfwGetTime(),
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
old_time = current_time;
// escape to quit, arrow keys to rotate view
if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
break;
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
rotate_y += delta_rotate;
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
rotate_y -= delta_rotate;
// z axis always rotates
rotate_z += delta_rotate;
// clear the buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the figure
Draw();
// swap back and front buffers
glfwSwapBuffers();
}
}
void Draw_Square(float red, float green, float blue)
{
// Draws a square with a gradient color at coordinates 0, 10
glBegin(GL_QUADS);
{
glColor3f(red, green, blue);
glVertex2i(1, 11);
glColor3f(red * .8, green * .8, blue * .8);
glVertex2i(-1, 11);
glColor3f(red * .5, green * .5, blue * .5);
glVertex2i(-1, 9);
glColor3f(red * .8, green * .8, blue * .8);
glVertex2i(1, 9);
}
glEnd();
}
void Draw(void)
{
// reset view matrix
glLoadIdentity();
// move view back a bit
glTranslatef(0, 0, -30);
// apply the current rotation
glRotatef(rotate_y, 0, 1, 0);
glRotatef(rotate_z, 0, 0, 1);
// by repeatedly rotating the view matrix during drawing, the
// squares end up in a circle
int i = 0, squares = 15;
float red = 0, blue = 1;
for (; i < squares; ++i){
glRotatef(360.0/squares, 0, 0, 1);
// colors change for each square
red += 1.0/12;
blue -= 1.0/12;
Draw_Square(red, .6, blue);
}
}
并使用代码块编译它,它返回给我这个错误:
/home/user/Graphics/Project_2/test.c|27|undefined reference to `glfwInit'|
/home/user/Graphics/Project_2/test.c|30|undefined reference to `glfwOpenWindow'|
......
.....
....
...
但是当我创建一个简单的 *.c 文件并使用以下命令编译它时:
gcc myprog.c -o myprog -lglfw -lGL -lpthread
它可以工作..如何使代码块与 GLFW 一起使用? 谢谢
I am having some difficulties with codeblocks 10.05 recognizing the GLFW libraries on my machine. When I create an empty project, and copy paste this code found from this GLFW tutorial >> http://content.gpwiki.org/index.php/GLFW:Tutorials:Basics
#include <stdlib.h>
#include <GL/glfw.h>
void Init(void);
void Shut_Down(int return_code);
void Main_Loop(void);
void Draw_Square(float red, float green, float blue);
void Draw(void);
float rotate_y = 0,
rotate_z = 0;
const float rotations_per_tick = .2;
int main(void)
{
Init();
Main_Loop();
Shut_Down(0);
}
void Init(void)
{
const int window_width = 800,
window_height = 600;
if (glfwInit() != GL_TRUE)
Shut_Down(1);
// 800 x 600, 16 bit color, no depth, alpha or stencil buffers, windowed
if (glfwOpenWindow(window_width, window_height, 5, 6, 5,
0, 0, 0, GLFW_WINDOW) != GL_TRUE)
Shut_Down(1);
glfwSetWindowTitle("The GLFW Window");
// set the projection matrix to a normal frustum with a max depth of 50
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect_ratio = ((float)window_height) / window_width;
glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
glMatrixMode(GL_MODELVIEW);
}
void Shut_Down(int return_code)
{
glfwTerminate();
exit(return_code);
}
void Main_Loop(void)
{
// the time of the previous frame
double old_time = glfwGetTime();
// this just loops as long as the program runs
while(1)
{
// calculate time elapsed, and the amount by which stuff rotates
double current_time = glfwGetTime(),
delta_rotate = (current_time - old_time) * rotations_per_tick * 360;
old_time = current_time;
// escape to quit, arrow keys to rotate view
if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
break;
if (glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS)
rotate_y += delta_rotate;
if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
rotate_y -= delta_rotate;
// z axis always rotates
rotate_z += delta_rotate;
// clear the buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the figure
Draw();
// swap back and front buffers
glfwSwapBuffers();
}
}
void Draw_Square(float red, float green, float blue)
{
// Draws a square with a gradient color at coordinates 0, 10
glBegin(GL_QUADS);
{
glColor3f(red, green, blue);
glVertex2i(1, 11);
glColor3f(red * .8, green * .8, blue * .8);
glVertex2i(-1, 11);
glColor3f(red * .5, green * .5, blue * .5);
glVertex2i(-1, 9);
glColor3f(red * .8, green * .8, blue * .8);
glVertex2i(1, 9);
}
glEnd();
}
void Draw(void)
{
// reset view matrix
glLoadIdentity();
// move view back a bit
glTranslatef(0, 0, -30);
// apply the current rotation
glRotatef(rotate_y, 0, 1, 0);
glRotatef(rotate_z, 0, 0, 1);
// by repeatedly rotating the view matrix during drawing, the
// squares end up in a circle
int i = 0, squares = 15;
float red = 0, blue = 1;
for (; i < squares; ++i){
glRotatef(360.0/squares, 0, 0, 1);
// colors change for each square
red += 1.0/12;
blue -= 1.0/12;
Draw_Square(red, .6, blue);
}
}
and compile it using codeblocks it returns me this error:
/home/user/Graphics/Project_2/test.c|27|undefined reference to `glfwInit'|
/home/user/Graphics/Project_2/test.c|30|undefined reference to `glfwOpenWindow'|
......
.....
....
...
but when I create a simple *.c file and compile it with :
gcc myprog.c -o myprog -lglfw -lGL -lpthread
it works.. How can I make codeblocks work with GLFW??
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请按照以下步骤操作。
并将解压文件的 lib_mingw 文件夹中的 glfw.dll 文件粘贴到文件夹“C:\Program Files\CodeBlocks\MinGW\lib”
在“C:\Windows\System32”文件夹中。如果您不想将其保留在 system32 中,则可以将其与项目 exe 文件一起保留。
Follow the following procedure.
and paste to the folder "C:\Program Files\CodeBlocks\MinGW\lib"
inside "C:\Windows\System32" floder. If you don't like to keep it in system32 then you can keep it with your project exe file.
确保您拥有正确类型的 glfw 库。即 mingw 编译器的 x86 或 x64。 8 小时的时间搜索未定义的引用错误,即 glfw 的链接器问题。
Make sure that you have the correct type of glfw libs. ie x86 or x64 for your mingw compiler. 8 Hours of time searching for undefined reference errors ie linker problems to glfw.
请遵循以下说明:
http://codeincodeblock.blogspot.com/2011/02 /setup-glfw-project-in-codeblock.html
Follow these instrucions:
http://codeincodeblock.blogspot.com/2011/02/setup-glfw-project-in-codeblock.html
我之前在将 GLFW 导入代码块时遇到过类似的问题,最近我发现了一些适合我的东西。
我可以看到您已经正确完成了标头安装,但我会将其包含在此响应中,以便您可以仔细检查一切是否处于最佳状态。
我还建议您使用 glfw.org 的“文档”选项卡中的示例代码。它对我来说非常有效,所以我想它也可能适合你。
由于您使用的是 GLFW,我假设您希望在应用程序中使用 OpenGL,因此我将在教程
A 中包含 OpenGL 的安装。 创建相关文件夹
您需要做的第一件事是设置库的位置系统上的文件和头文件。您可以在驱动器上的指定位置创建这些文件夹,也可以在 code::blocks 项目中创建这些文件夹。
就我而言,我在 code::blocks 项目文件夹中创建了“lib”和“head”文件夹。
B.下载&安装必要的介质
GLFW:
GLEW (OpenGL):
此时,您的“head”文件夹应包含“GLFW”和“GL”文件夹,“lib”文件夹应包含“glew32”、“glew32s”以及来自 GLFW 的编译器的所有库文件。
C. 配置您的 code::blocks 项目
D. 构建并运行您的代码:)!希望这有帮助!
I was having similar problems earlier with importing GLFW into codeblocks, and I recently found something that works for me.
I can see that you have already done the header installation correctly, but I will include that in this response so that you can double-check that everything is in tip-top condition.
I also recommend that you use the sample code within the 'Documentation' tab of glfw.org. It works really well for me, so I think it might for you as well.
Since you are using GLFW, I will assume that you want to use OpenGL in your application, so I will include the installation of OpenGL in the tutorial
A. Creating relevant folders
The first thing you will need to do is set up a location for library files and header files on your system. You can either creates these folders within a specified location on your drive, or you can create these folders within your code::blocks project.
In my case, I created a 'lib' and 'head' folder in my code::blocks project folder.
B. Downloading & Installing necessary media
GLFW:
GLEW (OpenGL):
At this point, your 'head' folder should contain 'GLFW' and 'GL' folders, and your 'lib' folder should contain 'glew32', 'glew32s', and all of the library files from GLFW for your compiler.
C. Configuring your code::blocks project
D. Build and Run your code :)! Hope this helps!