GLFW 和代码块

发布于 2024-12-11 02:16:10 字数 3562 浏览 0 评论 0原文

我在使用代码块 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森林散布 2024-12-18 02:16:10

请按照以下步骤操作。

  1. 首先从 这里
  2. 解压zip文件
  3. 从include文件夹复制glfw.h文件并粘贴到文件夹“C:\Program Files\CodeBlocks\MinGW\include\GL”
  4. 从lib_mingw文件夹复制所有文件(libglfw.a和libglfwdll.a)
    并将解压文件的 lib_mingw 文件夹中的 glfw.dll 文件粘贴到文件夹“C:\Program Files\CodeBlocks\MinGW\lib”
  5. 并粘贴
    在“C:\Windows\System32”文件夹中。如果您不想将其保留在 system32 中,则可以将其与项目 exe 文件一起保留。
  6. 现在,在 code::blocks 中创建 GLFW 项目时,为 glfw 位置提供路径 C:\Program Files\CodeBlocks\MinGW"
  7. 如果您再次感到困惑,请转到 此处了解分步过程以及每个步骤的图像。

Follow the following procedure.

  1. First download the GLFW from here.
  2. unzip the zip file
  3. copy the glfw.h file from the include folder and paste to the folder "C:\Program Files\CodeBlocks\MinGW\include\GL"
  4. copy all file (libglfw.a and libglfwdll.a ) from the lib_mingw folder
    and paste to the folder "C:\Program Files\CodeBlocks\MinGW\lib"
  5. glfw.dll file from the lib_mingw folder of unzip file and paste it
    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.
  6. Now while creating the GLFW project in code::blocks give the path C:\Program Files\CodeBlocks\MinGW" for glfw location
  7. IF you are again confuse then go here for step by step procedure with image of every step.
绿萝 2024-12-18 02:16:10

确保您拥有正确类型的 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.

浅紫色的梦幻 2024-12-18 02:16:10

我之前在将 GLFW 导入代码块时遇到过类似的问题,最近我发现了一些适合我的东西。

我可以看到您已经正确完成了标头安装,但我会将其包含在此响应中,以便您可以仔细检查一切是否处于最佳状态。

我还建议您使用 glfw.org 的“文档”选项卡中的示例代码。它对我来说非常有效,所以我想它也可能适合你。

由于您使用的是 GLFW,我假设您希望在应用程序中使用 OpenGL,因此我将在教程


A 中包含 OpenGL 的安装。 创建相关文件夹

您需要做的第一件事是设置库的位置系统上的文件和头文件。您可以在驱动器上的指定位置创建这些文件夹,也可以在 code::blocks 项目中创建这些文件夹。

就我而言,我在 code::blocks 项目文件夹中创建了“lib”和“head”文件夹。


B.下载&安装必要的介质

GLFW:

  1. 前往 GLFW.org,然后点击“下载”。
  2. 单击“32 位 Windows 二进制文件”,然后打开下载的 zip 文件中的“glfw-version.bin.WIN32”。
  3. 打开“include”文件夹并将随附的 GLFW 文件夹复制到您在 A 部分中创建的“head”文件夹。
  4. 再次打开“glfw-version.bin.WIN32”,然后选择对应的“lib”文件夹你的编译器。因为我使用的是 MinGW GCC 编译器,所以我选择了“lib-mingw”。要查找您的编译器,请转到 code::blocks,单击设置,然后单击“编译器...”。您的编译器可以在出现的窗口的“选定编译器”部分中找到。
  5. 找到要使用的“lib”文件夹后,将其打开,并将其中的所有文件复制到您在 A 部分中创建的“lib”文件夹中。

GLEW (OpenGL):

  1. 转到 链接
  2. 打开其中的文件夹下载的 zip 文件。
  3. 转到“lib”,然后release,然后Win32,并将其中的所有文件复制到您在步骤A 中创建的“lib”文件夹中
  4. 。返回到下载的zip 中的文件夹。
  5. 进入“include”目录,将“GL”文件夹复制到a部分创建的“head”文件夹中。

此时,您的“head”文件夹应包含“GLFW”和“GL”文件夹,“lib”文件夹应包含“glew32”、“glew32s”以及来自 GLFW 的编译器的所有库文件。


C. 配置您的 code::blocks 项目

  1. 打开项目后,右键单击工作区中的项目名称,然后点击“构建选项...”,
  2. 在出现的窗口左侧,确保“选择“调试”。
  3. 接下来单击“链接器设置”选项卡,然后添加以下库:“glfw.3”、“gdi32”和“opengl32”。
  4. 接下来,在“搜索目录”选项卡中,选择“编译器”选项卡。
  5. 添加“head”文件夹的路径。如果头文件夹是在项目文件夹中创建的,则只需键入“head”。
  6. 此外,在“搜索目录”选项卡中,选择“链接器”选项卡。
  7. 添加“lib”文件夹的路径。同样,如果 lib 文件夹是在项目文件夹中创建的,则只需键入“lib”即可。
  8. 点击窗口底部的“确定”。

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:

  1. Head over to GLFW.org, and hit 'Downloads'.
  2. Click '32-bit Windows binaries', and open the 'glfw-version.bin.WIN32' within zip file that was downloaded.
  3. Open the 'include' folder and copy the enclosed GLFW folder to the 'head' folder you created in part A.
  4. Open up 'glfw-version.bin.WIN32' again, and pick the 'lib' folder that corresponds to your compiler. Because I am using the MinGW GCC compiler, I picked 'lib-mingw'. To find your compiler, go to code::blocks, click settings, and then click 'compiler...'. Your compiler is found in the 'selected compiler' section of the window that appears.
  5. Once you've found the 'lib' folder that you will use, open it, and copy all of the files within into the 'lib' folder that you created during part A.

GLEW (OpenGL):

  1. Go to this link
  2. Open the folder that is within the downloaded zip.
  3. Go to 'lib', then release, then Win32, and copy all of the files located there into the 'lib' folder you created in step A.
  4. Go back to the folder within the downloaded zip.
  5. Enter the 'include' directory, and copy the 'GL' folder into the 'head' folder created in part a.

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

  1. Once you've opened your project, right click on the name of your project in your workspace, and hit 'build options...
  2. On the left of the window that has appeared, make sure 'Debug' is selected.
  3. Next click the 'linker settings' tab, and add the following libraries: 'glfw.3', 'gdi32', and 'opengl32'.
  4. Next, in the 'Search Directories' tab, select the 'Compiler' tab.
  5. Add the path to your 'head' folder. If the head folder was created within your project folder, you can just type 'head'
  6. Also in the 'Search Directories' tab, select the 'Linker' tab.
  7. Add the path to your 'lib' folder. Again, if the lib folder was created withing your project folder, you can just type 'lib'.
  8. Hit 'OK' at the bottom of the window.

D. Build and Run your code :)! Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文