在vc下用glut写窗口程序的问题

elated 2009-11-29 11:23:09
要建立哪种类型的程序呢,win32控制台程序还是窗口程序?
要写main还是WinMain呢?
比如下面的代码,用控制台编译运行时出错,用窗口程序会有链接错误,提示“无法解析的外部符号 _WinMain@16”。

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(0.4f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480);
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);

glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);

glEnd();
glFlush();
}

int main (int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
...全文
160 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
elated 2009-12-02
  • 打赏
  • 举报
回复
多谢楼上
我忘记调用glutCreateWindow了
张赐 2009-12-02
  • 打赏
  • 举报
回复
不知道你代码有什么问题,很简单的





#include <gl/glut.h> // OpenGL toolkit

///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);


glutSolidCube(1.0);
// Flush drawing commands
glFlush();
}

///////////////////////////////////////////////////////////
// Setup the rendering state
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

///////////////////////////////////////////////////////////
// Main program entry point
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);

SetupRC();

glutMainLoop();

return 0;
}


elated 2009-12-02
  • 打赏
  • 举报
回复
up
mhmnz2 2009-11-30
  • 打赏
  • 举报
回复
VC++6.0:
安装:
1.找到其所在文件夹(一般是:C:\Program Files\Microsoft Visual Studio\VC98\Include\GL),把解压得到的glut.h放到这个文件夹。

2.把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(一般是:C:\Program Files\Microsoft Visual Studio\VC98\Lib)。

3.把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\System32,也可以直接放在windows文件夹下)
配置:
This will show you how to start up an opengl project and setup the setting so that you will be able to compile and run the program. This is assuming that you have already downloaded the appropriate files and installed them in the directories that there documentation tell you to. If you have not done that you need to do it before you can run or write an opengl program.
1. Start VC++ and create a new project.
2. The project has to be a "Win32 Console Application"
3. Type in the name of the project and where it will be on your hard drive.
4. Chose an empty project and click next or finish
5. First thing you need to do when the project opens up is to click on the "Project" menu item from the top.
6. Chose "Settings" (a window will come up)
7. On the left side of the window there are tabs, chose the "Link" tab
8. The string field labeled "Object/library modules" has a few lib files already set in it
9. Go to the end of this string field and enter:
opengl32.lib glut32.lib glu32.lib
10. Chose "OK" and that will include all the opengl libraries that you need
11. Now all you need to do is include <gl\glut.h> and you are ready to go

VS2005中:
安装同上。
配置:
1.创建一个win32控制台工程(创建空项目)。
2.选择菜单项目->属性,选择配置->所有配置,然后配置属性->C/C++->预处理器,在预处理定义中添加GLUT_BUILDING_LIB(用“;”分隔)。
3.然后项目->属性->配置属性->链接器->输入,在附加依赖项中添加opengl32.lib glu32.lib glut32.lib(用空格分开)
4、最后开始编写OpenGL程序了,记住在程序的前面加入#include <gl\glut.h>和<gl\gl.h>,然后 #pragma comment (lib, "glut.lib").(最后这个我不知道是什么东东)

测试程序:

最简单的程序是弹出一个窗口。

#include <gl/glut.h>
void display()
{
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); //设置显示模式:单缓冲区, RGBA颜色模式
glutInitWindowSize (200, 200); //设置窗口宽度、高度
glutCreateWindow (argv[0]); //弹出窗口
glutDisplayFunc (display); //设置窗口刷新的回调函数其中display 是函数指针,是屏幕刷新是会调用的函数 所以我的大部分工作将在这个函数里完成
glutMainLoop(); //开始主循环
return 0;
}

在这个程序中 我们的display 函数为空 也就是什么都不干那么windows 将不会去更新窗口的区域 所以我们这里看到的是桌面背景,如果我们要在窗口中填充自己喜欢的颜色 我们可以在display 中添加如下语句:

void display()
{
glClearColor(1,1,1,1); //设置刷新背景色
glClear(GL_COLOR_BUFFER_BIT); //刷新背景
glFlush(); //更新窗口
}

测试通过。
张赐 2009-11-30
  • 打赏
  • 举报
回复
把myInit放到glutDisplayFunc(myDisplay); 这个前面试试
elated 2009-11-30
  • 打赏
  • 举报
回复
用控制台程序运时出错,提示
“MyGl.exe 中的 0x1000bbae 处未处理的异常: 0xC0000005: 写入位置 0x000000a8 时发生访问冲突”
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutDisplayFunc(myDisplay);
myInit(); //到这步会出错
glutMainLoop();
return 0;
}

是我程序写的有问题还是怎么的?
elated 2009-11-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhangci226 的回复:]
把myInit放到glutDisplayFunc(myDisplay); 这个前面试试
[/Quote]
问题依旧,是glutDisplayFunc出错
张赐 2009-11-30
  • 打赏
  • 举报
回复
应该建立控制台应用程序
linglongyouzhi 2009-11-30
  • 打赏
  • 举报
回复
win32程序需要定义winmain函数
console程序需要定义main函数

看你的错,应该是生成了一个win32的工程

4,504

社区成员

发帖
与我相关
我的任务
社区描述
图形图像/机器视觉
社区管理员
  • 机器视觉
  • 迪菲赫尔曼
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧