- 浏览: 1445514 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (691)
- linux (207)
- shell (33)
- java (42)
- 其他 (22)
- javascript (33)
- cloud (16)
- python (33)
- c (48)
- sql (12)
- 工具 (6)
- 缓存 (16)
- ubuntu (7)
- perl (3)
- lua (2)
- 超级有用 (2)
- 服务器 (2)
- mac (22)
- nginx (34)
- php (2)
- 内核 (2)
- gdb (13)
- ICTCLAS (2)
- mac android (0)
- unix (1)
- android (1)
- vim (1)
- epoll (1)
- ios (21)
- mysql (3)
- systemtap (1)
- 算法 (2)
- 汇编 (2)
- arm (3)
- 我的数据结构 (8)
- websocket (12)
- hadoop (5)
- thrift (2)
- hbase (1)
- graphviz (1)
- redis (1)
- raspberry (2)
- qemu (31)
- opencv (4)
- socket (1)
- opengl (1)
- ibeacons (1)
- emacs (6)
- openstack (24)
- docker (1)
- webrtc (11)
- angularjs (2)
- neutron (23)
- jslinux (18)
- 网络 (13)
- tap (9)
- tensorflow (8)
- nlu (4)
- asm.js (5)
- sip (3)
- xl2tp (5)
- conda (1)
- emscripten (6)
- ffmpeg (10)
- srt (1)
- wasm (5)
- bert (3)
- kaldi (4)
- 知识图谱 (1)
最新评论
-
wahahachuang8:
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下go ...
websocket的helloworld -
q114687576:
http://www.blue-zero.com/WebSoc ...
websocket的helloworld -
zhaoyanzimm:
感谢您的分享,给我提供了很大的帮助,在使用过程中发现了一个问题 ...
nginx的helloworld模块的helloworld -
haoningabc:
leebyte 写道太NB了,期待早日用上Killinux!么 ...
qemu+emacs+gdb调试内核 -
leebyte:
太NB了,期待早日用上Killinux!
qemu+emacs+gdb调试内核
1.我提供一个不需要配置环境就可运行的源码。
glut.h放在项目上一层include/gl目录。
glut.lib和glut32.lib放在上一层lib目录。
glut.dll和glut32.dll放exe同目录。
2.不需要配置环境变量。
3.头文件glut.h和库文件glut.lib与glut32.lib
这是include和lib
设计
#include <gl/glut.h> #include "Shell.h" #include "Box.h" #include "Matrix.h" void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); glRectf(-0.5f, -0.5f, 0.5f, 0.5f); glFlush(); } int main(int argc, char *argv[]) { //炮弹发射 Shell(argc,argv); //方块世界 //Box(argc,argv); //矩阵变换 //Matrix(argc,argv); return 0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(400, 400); glutCreateWindow("第一个OpenGL程序"); glutDisplayFunc(&myDisplay); glutMainLoop(); return 0; }
Shell.h
#include "Shell.h" GLfloat f_Speed = 80.0f; GLfloat f_X = -70; GLfloat f_Y = -70; GLfloat f_Agle = 45; void Shell_Reshape(GLsizei w,GLsizei h)//重绘回调 { GLfloat aspectRatio; //防止被0所除 if (h==0) { h=1; } //把视口设置为窗口的大小 glViewport(0,0,w,h); //重置坐标系统 glMatrixMode(GL_PROJECTION); glLoadIdentity(); //建立裁剪区域(左,右,底,顶,近,远) aspectRatio = (GLfloat)w / (GLfloat)h; if(w <= h) glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho(-100.0 * aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0); // glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //设置渲染状态 void Shell_SetupRC(void) { glClearColor(0.0f,0.0f,0.0f,1.0f); } //绘制场景 void Shell_RenderScene(void) { //用当前清除颜色清除窗口 glClear(GL_COLOR_BUFFER_BIT); //画速度指示条 //绿色 glColor3f(0.0f,1.0f,0.0f); //平移 glPushMatrix(); glTranslatef(0.0f,80.0f,0.0f); glTranslatef(-90.0f,0.0f,0.0f); //指示条 glRectf(0.0f, 0.0f, f_Speed, 10.0f); //还原 glPopMatrix(); //画炮台 //灰色 glColor3f(1.0f,1.0f,1.0f); //平移 glPushMatrix(); glTranslatef(-77.0f,-86.0f,0.0f); //炮台 glBegin(GL_POLYGON); glVertex2f(0.0f, 0.0f); glVertex2f(13.0f, 0.0f); glVertex2f(10.0f, 16.0f); glVertex2f(3.0f, 16.0f); glVertex2f(0.0f, 0.0f); glEnd(); //还原 glPopMatrix(); //画炮管 //灰色 glColor3f(1.0f,0.0f,1.0f); //平移 glPushMatrix(); glTranslatef(-70.0f,-70.0f,0.0f); glRotatef(f_Agle,0.0,0.0,1.0); //炮管 glBegin(GL_POLYGON); glVertex2f(-5.0f, 5.0f); glVertex2f(45.0f, 5.0f); glVertex2f(45.0f, -5.0f); glVertex2f(-5.0f, -5.0f); glVertex2f(-5.0f, 5.0f); glEnd(); //还原 glPopMatrix(); //画炮弹 //红色 glColor3f(1.0f,0.0f,0.0f); //平移 glPushMatrix(); glTranslatef(f_X,0,0.0f); glTranslatef(0,f_Y,0.0f); glRotatef(f_Agle-45,0.0,0.0,1.0); //炮弹 glBegin(GL_TRIANGLES); glVertex3f(-5.0f,5.0f, 0.0f); glVertex3f(5.0f,5.0f, 0.0f); glVertex3f(5.0f,-5.0f, 0.0f); glEnd(); //还原 glPopMatrix(); //刷新绘图命令 glFlush(); } void Shell_SpecialKeys(int key, int x, int y)//特殊按键F1之类 { GLfloat fOld = f_Speed; switch(key) { case GLUT_KEY_DOWN: f_Agle -= 5; break; case GLUT_KEY_UP: f_Agle += 5; break; case GLUT_KEY_LEFT: f_Speed -= 10.0f; break; case GLUT_KEY_RIGHT: f_Speed += 10.0f; break; } if (f_Speed < 10.0f || f_Speed > 180.0f) { f_Speed = fOld; return; } if (f_Agle < 0) { f_Agle = 0; return; } if (f_Agle > 90) { f_Agle = 90; return; } glutPostRedisplay();//重绘消息 } void Shell_timer(int id)//定时器 { f_X += 5.0f * cos((f_Agle/180)*Pi); f_Y += 5.0f * sin((f_Agle/180)*Pi); glutPostRedisplay(); glutTimerFunc((190-f_Speed)*5,Shell_timer,5);//需要在函数中再次调用,才能完成循环。 } void Shell_key(unsigned char key,int x,int y)//键盘鼠标事件 { if (key == ' ') { f_X = -70; f_Y = -70; } glutPostRedisplay();//重绘消息 } //main调用 int Shell(int argc, char* argv[]) { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500);//窗体大小 glutInitWindowPosition(500,100);//起始位置 glutCreateWindow("炮弹发射");//创建窗体 glutDisplayFunc(Shell_RenderScene);//类OnDraw() glutReshapeFunc(Shell_Reshape);//重绘回调 glutKeyboardFunc(Shell_key);//键盘事件 glutSpecialFunc(Shell_SpecialKeys);//特殊按键F1之类 glutTimerFunc((190-f_Speed)*5,Shell_timer,5); Shell_SetupRC();//类Init() cout<<"上下键控制角度,左右键控制发射速度。"<<endl; glutMainLoop();//消息循环 return 0; }
Box.cpp
#include "Box.h" double rotatex=0,rotatey=0,rotatez=0; double movex=0,movey=0,movez=0; double zoomx=0.5,zoomy=0.5,zoomz=0.5; vector<structCube> vecCube; void Box_Init(void) { glClearColor(0.0,0.0,0.0,0.0);//黑色清屏 AddCube(1,1,1,0,0,1); AddCube(0,0,0,1,0,1); AddCube(-1,-1,0,0,1,1); } void AddCube(int x,int y,GLfloat fR,GLfloat fG,GLfloat fB,GLfloat fWidth) { structCube cube; cube.x = x; cube.y = y; cube.fR = fR; cube.fG = fG; cube.fB = fB; cube.fWidth = fWidth; vecCube.push_back(cube); } void Box_Display(void) { // glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-2.0*64/48.0,2.0*64/48.0,-2.0,2.0,0.1,100); // glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2.0,2.0,2.0,0.7,0.7,0.0,0.0,1.0,0.0);//设置相机 glClear(GL_COLOR_BUFFER_BIT); //变换 glPushMatrix(); //平移 glTranslatef (movex + 2,0.0, 0.0);//x轴平移 glTranslatef (0.0, movey + 2, 0.0);//y轴平移 //旋转 glRotatef (rotatez, 0.0, 0.0, 1.0);//z轴旋转 glRotatef (rotatex, 1.0, 0.0, 0.0);//x轴旋转 glRotatef (rotatey, 0.0, 1.0, 0.0);//y轴旋转 //缩放 glScalef(zoomx,zoomy,zoomz);//缩放 for(vector<structCube>::iterator iter=vecCube.begin();iter!=vecCube.end();iter++) { DrawCube(iter->x,iter->y,iter->fR,iter->fG,iter->fB,iter->fWidth); } //还原 glPopMatrix(); glFlush();//绘图 } void DrawCube(int x,int y,GLfloat fR,GLfloat fG,GLfloat fB,GLfloat fWidth) { glColor3d(fR,fG,fB);//设置立方体边颜色 GLfloat fX = (GLfloat)x; GLfloat fY = (GLfloat)y; fX = (fX - 400) / 400; fY = (fY - 400) / 400; //cout<<fX<<","<<fY<<endl; glTranslatef (fX,0.0, 0.0);//x轴平移 glTranslatef (0.0, fY, 0.0);//y轴平移 glutWireCube(fWidth);//绘制立方体 } void Box_specialKey(GLint key,GLint x,GLint y) { switch(key) { case GLUT_KEY_UP: movey+=0.1; break; case GLUT_KEY_DOWN: movey+=-0.1; break; case GLUT_KEY_LEFT: movex+=-0.1; movey+=-0.03; break; case GLUT_KEY_RIGHT: movex+=0.1; movey+=0.03; break; case GLUT_KEY_PAGE_UP: zoomx+=0.2; zoomy+=0.2; zoomz+=0.2; break; case GLUT_KEY_PAGE_DOWN: zoomx+=-0.2; zoomy+=-0.2; zoomz+=-0.2; break; } glutPostRedisplay(); } void Box_Keyboard(unsigned char key,GLint x,GLint y) { switch(key) { case 'w': movey+=0.1; break; case 'a': movex+=-0.1; movey+=-0.03; break; case 'd': movex+=0.1; movey+=0.03; break; case 's': movey+=-0.1; break; case 'x': rotatex+=0.5; break; case 'y': rotatey+=0.5; break; case 'z': rotatez+=0.5; break; } glutPostRedisplay(); } void Box_Mouse(int button,int state,int x,int y) { if(state==GLUT_DOWN) { if(button==GLUT_LEFT_BUTTON) { AddCube(x,y,0,1,0,1); } else if(button==GLUT_RIGHT_BUTTON) { AddCube(x,y,0,0,1,0.5); } } glutPostRedisplay(); } //main调用 void Box(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(800,600); glutInitWindowPosition(500,150); glutCreateWindow("鼠标左键放大,右键缩小."); glutDisplayFunc(Box_Display); glutKeyboardFunc(Box_Keyboard); glutSpecialFunc(Box_specialKey); glutMouseFunc(Box_Mouse); Box_Init(); cout<<"鼠标点击添加。"<<endl; cout<<"Page Up放大,Page Down缩小。"<<endl; cout<<"上下左右键平移。"<<endl; cout<<"x,y,z键旋转。"<<endl; glViewport(0,0,640,480); glutMainLoop(); }
Matrix.cpp
#include "Matrix.h" void Matrix_Reshape(GLsizei w,GLsizei h)//重绘回调 { GLfloat aspectRatio; //防止被0所除 if (h==0) { h=1; } //把视口设置为窗口的大小 glViewport(0,0,w,h); //重置坐标系统 glMatrixMode(GL_PROJECTION); glLoadIdentity(); //建立裁剪区域(左,右,底,顶,近,远) aspectRatio = (GLfloat)w / (GLfloat)h; if(w <= h) glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0); else glOrtho(-100.0 * aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0); // glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //设置渲染状态 void Matrix_Init(void) { glClearColor(0.0f,0.0f,0.0f,1.0f); } //绘制场景 void Matrix_Display(void) { //用当前清除颜色清除窗口 glClear(GL_COLOR_BUFFER_BIT); //把当前矩阵设置为模型视力矩阵,并进行重置。 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //画球体 glutSolidSphere(10,25,25); /* //沿y轴正方向移动10个单位 glTranslatef(0,50,0); //绘制第一个球体 glutSolidSphere(10,25,25); //再次重置模型视力矩阵 glLoadIdentity(); //沿x轴正方向移动10个单位 glTranslatef(50,0,0); //绘制第二个球体 glutSolidSphere(10,25,25); */ glPushMatrix(); //沿y轴正方向移动10个单位 glTranslatef(0,50,0); //绘制第一个球体 glutSolidSphere(10,25,25); glPopMatrix(); // glPushMatrix(); //沿x轴正方向移动10个单位 glTranslatef(50,0,0); //绘制第二个球体 glutSolidSphere(10,25,25); glPopMatrix(); //刷新绘图命令 glFlush(); } //main调用 int Matrix(int argc, char* argv[]) { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500);//窗体大小 glutInitWindowPosition(500,100);//起始位置 glutCreateWindow("矩形转换");//创建窗体 glutDisplayFunc(Matrix_Display);//类OnDraw() glutReshapeFunc(Matrix_Reshape);//重绘回调 Matrix_Init();//类Init() cout<<"提示信息。"<<endl; glutMainLoop();//消息循环 return 0; }
参考
http://www.cnblogs.com/greatverve/archive/2013/01/02/openGL-HelloWorld.html
发表评论
-
weak_ptr解决循环引用问题
2021-03-08 21:12 1073C++11引入的三种智能指 ... -
gcc链接顺序
2019-10-12 18:25 520代码在 https://github.com/killinux ... -
c++11的function和bind
2019-09-10 16:12 486参考:https://www.cnblogs.co ... -
画图板用c++实现和用js实现的websocket版本
2014-10-17 13:02 2099画图板 opencv的c++ #include <o ... -
c语言内存
2014-07-02 10:26 6631、C中内存分为五个区 栈:用来存放函数的形参和函数内的局部变 ... -
重定向stdout到文件
2014-03-05 18:37 5418把stdout重定向到文件 两种方法: 第一种方法没有恢复 ... -
通过nginx远程执行shell
2014-03-03 10:26 4987saltstack远程执行shell,远程管理等返回json已 ... -
c的urldecode
2014-02-28 18:22 1318#include <stdio.h> #in ... -
pthread的pthread_mutex_lock 的使用
2014-02-25 16:54 26061参考http://haoningabc.iteye.com/b ... -
c调用c++
2013-10-12 15:24 1131参考 http://www.cppblog.com/frank ... -
用C语言,实现接收管道输出的结果,并显示
2013-04-23 21:35 1890在shell里利用“|”管道干的事情就是io重定向,把“|”命 ... -
关于char * 与 char[]
2013-04-22 21:56 921问题引入: 在实习过程中发现了一个以前一直默认的错误,同样ch ... -
单向链表翻转
2012-12-25 23:41 984临时笔记,创建一个链表 #include <stdl ... -
trie 树 的代码
2012-12-14 23:20 1099想起搜狐老大的一句话 看代码先看h文件,擦,当初感觉他这句话很 ... -
指针函数与函数指针的区别
2012-12-14 22:44 1157一、 1、指针函数是指带指针的函数,即本质是一个函数。函数返回 ... -
指针和数组
2012-11-14 22:40 1026转载http://kan.weibo.com/con/3512 ... -
js备份
2012-10-31 23:56 1684<!DOCTYPE HTML PUBLIC " ... -
线程的helloworld
2012-10-30 21:51 1555#include<stdio.h> #inc ... -
c的书籍
2012-10-30 10:56 1092http://www.acm.uiuc.edu/webmonk ... -
深入理解计算机系统第三章笔记 gcc
2012-10-24 12:11 1488随便写个最简单程序 然后gcc -S 看汇编 在gcc -C ...
相关推荐
openGL红宝书的第一个程序,入门学习之用
个人写的helloworld,需要的亲可以使用。入门级。
简单啊速度快回来就爱上了了解爽肤水时代感爱上对方士大夫vadfv
NULL 博文链接:https://justsee.iteye.com/blog/2336218
true opengl hello world using QT c++
安卓 OpenGL ES 2.0 完全入门(一):基本概念和 hello world
本代码是 “OpenGL 第一个窗口”的入门学习代码, 代码几乎每行都进行了注释, 还有每一个函数的功能,与使用方法都进行了注释
一个用opengl着色语言(glsl)编写的犹他壶,并且可以通过鼠标点动沿三个坐标轴转动
一个简单的OpenGL窗体hello代码,hello,world可以随鼠标的敲击移动。
// Hello World_Code // #import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }...
Hello-GLUT:一个非常简单的“ Hello World!” GLUT应用程序演示了如何使用MinGW和MSVC用C编写OpenGL应用程序
hello-gl:OpenGL 2.0中的“ Hello World”
opengl编程入门,glsl语言的hello world,着色器,glsl语言简介
helloworld-sdl2-opengl-emscripten:使用SDL2 + OpenGL的基本程序,在本地和通过emscripten进行编译
因为OpenGL是一个图形API,而不是一个自有平台,它需要...不过,我会尝试解释大多数用到的概念,包括高级C++主题,所以你不必是一个C++专家,但是你应该不能只会写’Hello World’程序。本教程将带你进入opengl的旅程!
opengl编程入门,glsl语言的hello world,着色器,glsl语言简介
该实例从NEHE第一章改写而来,精简了必要的代码,工程从Win32 HelloWorld 而来,只添加了initgl函数和改写了initinstance函数,添加了几个全局变量,其他均未改变(改写wm_paint消息)。使用于第一使用opengl
opengl编程入门,glsl语言的hello world,你好三角形,绘制一个三角形