感觉OpenGL开窍啦~移动、缩放、旋转效果
代码如下,略有不同:Windows:
- #include "StdAfx.h"
- #include "windows.h"
- #include "GLgl.h"
- #include "GLglu.h"
- #include "GLglut.h"
- #pragma comment( lib, "glu32.lib" )
- #pragma comment( lib, "OpenGL32.lib" )
- void myinit(void);
- void myReshape(int w,int h);
- void display();
- static int color = 0;
- static float size = 1.0;
- //set current color according to "color"
- void setColor()
- {
- //Change current color
- switch(color){
- case 0:
- glColor3f(1.0,1.0,0);
- break;
- case 1:
- glColor3f(0,1.0,1.0);
- break;
- case 2:
- glColor3f(1.0,0,1.0);
- break;
- default:
- glColor3f(1.0,1.0,1.0);
- }
- }
- //set current color according to next "color"
- void nextColor()
- {
- if (color < 2)
- color += 1;
- else
- color = 0;
- setColor();
- }
- void init(void)
- {
- glClearColor(0.0, 0.0 ,0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- }
- void display()
- {
- init();
- //show coordinate lines
- glColor3f(1.0,1.0,1.0);
- glBegin(GL_LINES);
- //x
- glColor3f(1.0f,0,0);
- glVertex3f(0.9f,0,0);
- glVertex3f(-0.9f,0,0);
- //y
- glColor3f(0,1.0f,0);
- glVertex3f(0,0.9f,0);
- glVertex3f(0,-0.9f,0);
- //z
- glColor3f(0,0,1.0f);
- glVertex3f(0,0,0.9f);
- glVertex3f(0,0,-0.9f);
- glEnd();
- //show image
- setColor();
- glutWireCube(size);
- glFlush();
- }
- //Call when resize
- void reshape(int w,int h)
- {
- glViewport(0,0,w,h);
- }
- //Use Space to change color
- void CALLBACK changeColor()
- {
- nextColor();
- display();
- }
- //Use Right to move right
- void CALLBACK moveRight()
- {
- glTranslatef(0.05f, 0.0, 0.0);
- display();
- }
- //Use Left to move left
- void CALLBACK moveLeft()
- {
- glTranslatef(-0.05f, 0.0, 0.0);
- display();
- }
- //Use Up to move up
- void CALLBACK moveUp()
- {
- glTranslatef(0.0, 0.05f, 0.0);
- display();
- }
- //Use Down to move down
- void CALLBACK moveDown()
- {
- glTranslatef(0.0, -0.05f, 0.0);
- display();
- }
- //Use s to make bigger
- void CALLBACK bigger()
- {
- size /= 0.9f;
- display();
- }
- //Use a to make smaller
- void CALLBACK smaller()
- {
- size *= 0.9f;
- display();
- }
- //Use q to rotate by x
- void CALLBACK rotateX()
- {
- glRotatef(5.0, 1.0, 0, 0);
- display();
- }
- //Use w to rotate by y
- void CALLBACK rotateY()
- {
- glRotatef(5.0, 0, 1.0, 0);
- display();
- }
- //Use e to rotate by z
- void CALLBACK rotateZ()
- {
- glRotatef(5.0, 0, 0, 1.0);
- display();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27: // ESCAPE key
- exit (0);
- break;
- case "s":
- bigger();
- break;
- case "a":
- smaller();
- break;
- case "q":
- rotateX();
- break;
- case "w":
- rotateY();
- break;
- case "e":
- rotateZ();
- break;
- case " ":
- changeColor();
- break;
- case "j":
- moveLeft();
- break;
- case "k":
- moveDown();
- break;
- case "l":
- moveRight();
- break;
- }
- }
- void special(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_LEFT:
- moveLeft();
- break;
- case GLUT_KEY_RIGHT:
- moveRight();
- break;
- case GLUT_KEY_UP:
- moveUp();
- break;
- case GLUT_KEY_DOWN:
- moveDown();
- break;
- }
- }
- void main(int argc, char** argv)
- {
- //Init a window
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (100, 100);
- glutCreateWindow("Test");
- //clear window and set init color
- setColor();
- //listener of keyboard
- glutKeyboardFunc(keyboard);
- glutSpecialFunc(special);
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- }
Linux:- #include "GL/gl.h"
- #include "GL/glu.h"
- #include "GL/glut.h"
- #include <iostream>
- using namespace std;
- void myinit(void);
- void myReshape(int w,int h);
- void display();
- static int color = 0;
- static float size = 1.0;
- //set current color according to "color"
- void setColor()
- {
- //Change current color
- switch(color){
- case 0:
- glColor3f(1.0,1.0,0);
- break;
- case 1:
- glColor3f(0,1.0,1.0);
- break;
- case 2:
- glColor3f(1.0,0,1.0);
- break;
- default:
- glColor3f(1.0,1.0,1.0);
- }
- }
- //set current color according to next "color"
- void nextColor()
- {
- if (color < 2)
- color += 1;
- else
- color = 0;
- setColor();
- }
- void init(void)
- {
- glClearColor(0.0, 0.0 ,0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- }
- void display()
- {
- init();
- //show coordinate lines
- glColor3f(1.0,1.0,1.0);
- glBegin(GL_LINES);
- //x
- glColor3f(1.0f,0,0);
- glVertex3f(0.9f,0,0);
- glVertex3f(-0.9f,0,0);
- //y
- glColor3f(0,1.0f,0);
- glVertex3f(0,0.9f,0);
- glVertex3f(0,-0.9f,0);
- //z
- glColor3f(0,0,1.0f);
- glVertex3f(0,0,0.9f);
- glVertex3f(0,0,-0.9f);
- glEnd();
- //show image
- setColor();
- glutWireCube(size);
- glFlush();
- }
- //Call when resize
- void reshape(int w,int h)
- {
- glViewport(0,0,w,h);
- }
- //Use Space to change color
- void changeColor()
- {
- nextColor();
- display();
- }
- //Use Right to move right
- void moveRight()
- {
- glTranslatef(0.05f, 0.0, 0.0);
- display();
- }
- //Use Left to move left
- void moveLeft()
- {
- glTranslatef(-0.05f, 0.0, 0.0);
- display();
- }
- //Use Up to move up
- void moveUp()
- {
- glTranslatef(0.0, 0.05f, 0.0);
- display();
- }
- //Use Down to move down
- void moveDown()
- {
- glTranslatef(0.0, -0.05f, 0.0);
- display();
- }
- //Use s to make bigger
- void bigger()
- {
- size /= 0.9f;
- display();
- }
- //Use a to make smaller
- void smaller()
- {
- size *= 0.9f;
- display();
- }
- //Use q to rotate by x
- void rotateX()
- {
- glRotatef(5.0, 1.0, 0, 0);
- display();
- }
- //Use w to rotate by y
- void rotateY()
- {
- glRotatef(5.0, 0, 1.0, 0);
- display();
- }
- //Use e to rotate by z
- void rotateZ()
- {
- glRotatef(5.0, 0, 0, 1.0);
- display();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27: // ESCAPE key
- exit (0);
- break;
- case "s":
- bigger();
- break;
- case "a":
- smaller();
- break;
- case "q":
- rotateX();
- break;
- case "w":
- rotateY();
- break;
- case "e":
- rotateZ();
- break;
- case " ":
- changeColor();
- break;
- case "j":
- moveLeft();
- break;
- case "k":
- moveDown();
- break;
- case "l":
- moveRight();
- break;
- case "i":
- moveUp();
- break;
- }
- }
- void special(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_LEFT:
- moveLeft();
- break;
- case GLUT_KEY_RIGHT:
- moveRight();
- break;
- case GLUT_KEY_UP:
- moveUp();
- break;
- case GLUT_KEY_DOWN:
- moveDown();
- break;
- }
- }
- int main(int argc, char** argv)
- {
- //Init a window
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize (600, 600);
- glutInitWindowPosition (100, 100);
- glutCreateWindow("Test");
- //clear window and set init color
- setColor();
- //listener of keyboard
- glutKeyboardFunc(keyboard);
- glutSpecialFunc(special);
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
编译:$ g++ -IGL -lglut test2.cpp -o test.exe就生成了test.exe$ ./test.exe显示:OpenGL Warning: XGetVisualInfo returned 0 visuals for 0xa082b78OpenGL Warning: Retry with 0x8002 returned 0 visualsSegmentation fault网上搜了一下,得到解决方案$ LIBGL_ALWAYS_INDIRECT=1 ./test.exe运行结果:按qwe进行xyz轴旋转,as放大缩小,上下左右移动,space换色,得到以下效果——RedHat Server 下安装VMWare Workstation 7.1.4 虚拟win xp sp3系统在CentOS 5.5上安装FireFox 4相关资讯 OpenGL
- OpenGL显示图片 (08月07日)
- OS X的CAOpenGLLayer中如何启用 (12/13/2015 16:26:18)
- OpenGL编程指南(原书第8版) 中英 (08/25/2015 08:34:15)
| - OpenGL中旋转平移缩放等变换的顺序 (03月10日)
- Fedora和Ubuntu下安装OpenGL开发环 (10/23/2015 15:14:17)
- OpenGL中投影矩阵的推导 (08/25/2015 08:33:52)
|
本文评论 查看全部评论 (0)