/ 中存储网

告诉你OpenGL在Windows和Ubuntu下的方法及命令

2015-02-06 00:00:00 来源:中存储网

感觉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 0xa082b78
OpenGL Warning: Retry with 0x8002 returned 0 visuals
Segmentation fault
网上搜了一下,得到解决方案
$ LIBGL_ALWAYS_INDIRECT=1 ./test.exe