之后有空再补一下具体的细节理论叭(可能正好当作自己复习的资料啦,)
#include
#include
static GLfloat spin=0.0; //旋转量
static GLfloat move=0.0; //平移量
static GLfloat size=1.0; //缩放量
void init(void)
{
glClearColor(0.0,0.0,0.5,0.5); //背景色:蓝黑色
glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //清除所有的像素
glPushMatrix();
glTranslatef(move,0,0); //移动,参数含义(x轴位移,y轴位移,z轴位移)
glRotatef(spin,0,0,1); //旋转,参数含义(旋转量,x轴,y轴,z轴)
glScalef(size,size,1); //缩放,参数含义(x轴倍数,y轴倍数,z轴倍数)
glColor3f(1.0,1.0,0.0); //绘制颜色RGB:黄色
glRectf(-25.0,-25.0,25.0,25.0); //绘制矩形
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
}
void spinAndSizeDisplay()
{
spin>360?spin-=360:spin+=2;
size>2?size-=2:size+=0.003;
glutPostRedisplay(); //标记当前窗口需要重绘,否则不会旋转
Sleep(10);
}
void moveDisplay()
{
move=move>20?move-=20:move+=1;
glutPostRedisplay(); //标记当前窗口需要重绘
Sleep(10);
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv); //初始化GLUT并处理命令行参数
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); //指定模式:双缓存;RGB模式
glutInitWindowSize(250,250); //指定窗口大小(像素)
glutInitWindowPosition(300,200); //指定窗口左上角在屏幕上的位置
glutCreateWindow(argv[0]); //使用OpenGL场景创建一个窗口,参数为窗口名称
init(); //调用初始化函数
glutDisplayFunc(display); //显示
glutReshapeFunc(reshape); //重绘
//glutIdleFunc(spinAndSizeDisplay); //旋转&缩放
glutIdleFunc(moveDisplay); //移动(与上边函数只能有一个有效)
glutMainLoop(); //进入主循环并处理事件,此时创建的所有窗口都会显示出来,被渲染到这些窗口中的内容也将显示出来,程序开始处理事件,注册的显示回调函数被触发
return 0; //ANSI C要求函数main()返回一个int值
}
和上面的代码几乎没有区别,只是主函数改了一个地方(如果你也发现……)
#include
#include
static GLfloat spin=0.0; //旋转量
static GLfloat move=0.0; //平移量
static GLfloat size=1.0; //缩放量
void init(void)
{
glClearColor(0.0,0.0,0.5,0.5); //背景色:蓝黑色
glShadeModel(GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); //清除所有的像素
glPushMatrix();
glTranslatef(move,0,0); //移动,参数含义(x轴位移,y轴位移,z轴位移)
glRotatef(spin,0,0,1); //旋转,参数含义(旋转量,x轴,y轴,z轴)
glScalef(size,size,1); //缩放,参数含义(x轴倍数,y轴倍数,z轴倍数)
glColor3f(1.0,1.0,0.0); //绘制颜色RGB:黄色
glRectf(-25.0,-25.0,25.0,25.0); //绘制矩形
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0);
}
void spinAndSizeDisplay()
{
spin>360?spin-=360:spin+=2;
size>2?size-=2:size+=0.003;
glutPostRedisplay(); //标记当前窗口需要重绘,否则不会旋转
Sleep(10);
}
void moveDisplay()
{
move=move>20?move-=20:move+=1;
glutPostRedisplay(); //标记当前窗口需要重绘
Sleep(10);
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv); //初始化GLUT并处理命令行参数
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); //指定模式:双缓存;RGB模式
glutInitWindowSize(250,250); //指定窗口大小(像素)
glutInitWindowPosition(300,200); //指定窗口左上角在屏幕上的位置
glutCreateWindow(argv[0]); //使用OpenGL场景创建一个窗口,参数为窗口名称
init(); //调用初始化函数
glutDisplayFunc(display); //显示
glutReshapeFunc(reshape); //重绘
glutIdleFunc(spinAndSizeDisplay); //旋转&缩放
// glutIdleFunc(moveDisplay); //移动(与上边函数只能有一个有效)
glutMainLoop(); //进入主循环并处理事件,此时创建的所有窗口都会显示出来,被渲染到这些窗口中的内容也将显示出来,程序开始处理事件,注册的显示回调函数被触发
return 0; //ANSI C要求函数main()返回一个int值
}
void myDisplay(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-500.0, 500.0, -500.0, 500.0);
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(-500.0, 0.0);
glVertex2f(500.0, 0.0);
glVertex2f(0.0, -500.0);
glVertex2f(0.0, 500.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glRecti(50, 100, 200, 150);//initial rectangle
glColor3f(0.0, 1.0, 0.0);
glTranslatef(50.0, 50.0, 0.0);//translation
glRecti(50, 100, 200, 150);
//reset the current modelview matrix
glLoadIdentity();// 重置当前矩阵为单位阵
glColor3f(0.0, 0.0, 1.0);
glRotatef(90.0, 0.0, 0.0, 1.0);//rotation 逆时针
glRecti(50, 100, 200, 150);
//reset the current modelview matrix
glLoadIdentity();
glColor3f(1.0, 1.0, 0.0);
glTranslatef(-100.0, 0.0, 0.0);//translation
glScalef(0.5, 1.0, 1.0);//scale 所有x坐标缩小一半
glRecti(50, 100, 200, 150);
glFlush();
}
#include
#include
#include
#include
#include
// 绕轨道
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// 绘制一个景象
void RenderScene(void)
{
// 旋转角度
static float fElect1 = 0.0f;
// 背景设置:默认为黑色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Translate the whole scene out and into view
// This is the initial viewing transformation
glTranslatef(0.0f, 0.0f, -100.0f);
// 红色球体
glColor3ub(255, 0, 0);
glutSolidSphere(10.0f, 15, 15);
//绘制茶壶
glColor3ub(255, 122, 0);
glutSolidTeapot(20.0);
// 黄色的电子
glColor3ub(255,255,0);
// 保存看到的平移
glPushMatrix();
// 固定角度绕着轨道旋转
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
// 轨道移动
glTranslatef(90.0f, 0.0f, 0.0f);
// 绘制电子
glutSolidSphere(6.0f, 15, 15);
// Restore the viewing transformation
glPopMatrix();
// Second Electron Orbit
glPushMatrix();
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(-70.0f, 0.0f, 0.0f);
glutSolidSphere(6.0f, 15, 15);
glPopMatrix();
// Third Electron Orbit
glPushMatrix();
glRotatef(360.0f-45.0f,0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 60.0f);
glutSolidSphere(6.0f, 15, 15);
glPopMatrix();
// Increment the angle of revolution
fElect1 += 10.0f;
if(fElect1 > 360.0f)
fElect1 = 0.0f;
// Show the image
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet
// 背景色
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(xRot > 356.0f)
xRot = 0.0f;
if(xRot < -1.0f)
xRot = 355.0f;
if(yRot > 356.0f)
yRot = 0.0f;
if(yRot < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1);
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
else
glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("OpenGL Atom");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
glutTimerFunc(500, TimerFunc, 1);
SetupRC();
glutMainLoop();
return 0;
}
/*
绘制三维图形,
实现他们的旋转、缩放和平移
*/
#include
#include
#include
#include
#include
// 旋转参数
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// 绘制函数
void RenderScene(void)
//绘制的思路:梦里一只红色的茶壶周围绕着黄色小茶壶
{
static float fElect1 = 0.0f;
// 默认颜色请空窗口
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 重置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//初始化
//glTranslatef:定义一个平移矩阵,该矩阵与当前矩阵相乘,使后续的图形进行平移变换
glTranslatef(0.0f, 0.0f, -100.0f);
//中心球体
glColor3ub(255, 0, 0);
glutWireSphere(80.0,11,11);
//尝试将该茶壶缩放
//glScalef(1, 1, 1);
// 黄色茶壶
glColor3ub(255,255,0);
glPushMatrix();
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
// Translate out from origin to orbit distance
glTranslatef(90.0f, 0.0f, 0.0f);
glutSolidTeapot(8);
// Restore the viewing transformation
glPopMatrix();
// Second Electron Orbit
glPushMatrix();
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(-70.0f, 0.0f, 0.0f);
glutSolidTeapot(8);
glPopMatrix();
// 绕轨道运行
glPushMatrix();
glRotatef(360.0f-45.0f,0.0f, 0.0f, 1.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 60.0f);
glutSolidTeapot(8);
//glutWireTeapot(8);
glPopMatrix();
// 增加旋转的角度
fElect1 += 10.0f;
if(fElect1 > 360.0f)
fElect1 = 0.0f;
// 展示图片
glutSwapBuffers();
}
void SetupRC()
{
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside of jet
GLfloat LightAmbient[] = { 1.0f, 1.0f, 0.0f, 0.0f }; //环境光参数,半亮白色
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; //漫射光参数,
GLfloat LightPosition[] = { 0.0f, 0.0f, 0.0f, 1.0f }; //光源位置
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); //设置环境光
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); //设置漫射光
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); //设置光源位置
glEnable(GL_LIGHT1); //启用一号光源
glEnable(GL_LIGHTING); //开光
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(xRot > 356.0f)
xRot = 0.0f;
if(xRot < -1.0f)
xRot = 355.0f;
if(yRot > 356.0f)
yRot = 0.0f;
if(yRot < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, 1);
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// 视点位置
glViewport(0, 0, w, h);
// 重置系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
else
glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("高宏艳 第二次实验");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
glutTimerFunc(500, TimerFunc, 1);
SetupRC();
glutMainLoop();
return 0;
}
代码跑出来太不容易啦,人菜瘾大系列
通过上下左右键控制缩放、平移以及旋转
核心代码在于key_board(),
void key_board(int key, int x, int y)
{
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
if(key == GLUT_KEY_UP)//按“上”,放大图形
glScalef(1.5, 1.5, 1.5);
if(key == GLUT_KEY_DOWN)//按“下”,缩小图形
glScalef(0.5, 0.5, 0.5);
if(key == GLUT_KEY_LEFT)//按“左”,旋转图形
glRotated(10, 0, 1, 0);
if(key == GLUT_KEY_RIGHT)//按“右”,右移图形
glTranslated(10, 0, 0);
glutPostRedisplay();
}
#include
#include
GLfloat AngleX, AngleY;
void init()
{
AngleX = 45.0f;
AngleY = 315.0f;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DITHER);
glShadeModel(GL_SMOOTH);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
{
glRotatef(AngleX, 1.0f, 0.0f, 0.0f);
glRotatef(AngleY, 0.0f, 1.0f, 0.0f);
//绘制三维的正方体
glBegin(GL_POLYGON);
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f, 50.0f, 50.0f);
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f, -50.0f, 50.0f);
glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);
glVertex3f(-50.0f, -50.0f, 50.0f);
glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)255);
glVertex3f(-50.0f, 50.0f, 50.0f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(50.0f, 50.0f, -50.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(50.0f, -50.0f, -50.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-50.0f, -50.0f, -50.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f, 50.0f, -50.0f);
glEnd();
glBegin(GL_POLYGON);
glColor3d(0.0, 1.0, 1.0);
glVertex3f(50.0f, 50.0f, -50.0f);
glColor3d(1.0, 1.0, 1.0);
glVertex3f(50.0f, 50.0f, 50.0f);
glColor3d(1.0, 0.0, 1.0);
glVertex3f(-50.0f, 50.0f, 50.0f);
glColor3d(0.0, 0.0, 1.0);
glVertex3f(-50.0f, 50.0f, -50.0f);
glEnd();
glBegin(GL_POLYGON);
glColor3ub(0u, 255u, 0u);
glVertex3f(50.0f, -50.0f, -50.0f);
glColor3ub(255u, 255u, 0u);
glVertex3f(50.0f, -50.0f, 50.0f);
glColor3ub(255u, 0u, 0u);
glVertex3f(-50.0f, -50.0f, 50.0f);
glColor3ub(255u, 255u, 255u);
glVertex3f(-50.0f, -50.0f, -50.0f);
glEnd();
glBegin(GL_POLYGON);
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f, 50.0f, 50.0f);
glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)255);
glVertex3f(50.0f, 50.0f, -50.0f);
glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f, -50.0f, -50.0f);
glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);
glVertex3f(50.0f, -50.0f, 50.0f);
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-50.0f, 50.0f, 50.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-50.0f, 50.0f, -50.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-50.0f, -50.0f, -50.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-50.0f, -50.0f, 50.0f);
glEnd();
}
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
GLfloat aspect = (GLfloat)w / (GLfloat)h;
GLfloat nRange = 100.0f;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
{
glOrtho(-nRange, nRange, -nRange * aspect, nRange * aspect, -nRange, nRange);
}
else
{
glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange);
}
}
void key_board(int key, int x, int y)
{
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
if(key == GLUT_KEY_UP)//按“上”,放大图形
glScalef(1.5, 1.5, 1.5);
if(key == GLUT_KEY_DOWN)//按“下”,缩小图形
glScalef(0.5, 0.5, 0.5);
if(key == GLUT_KEY_LEFT)//按“左”,旋转图形
glRotated(10, 0, 1, 0);
if(key == GLUT_KEY_RIGHT)//按“右”,右移图形
glTranslated(10, 0, 0);
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutCreateWindow("919106840202高宏艳-实验二");
glutReshapeFunc(reshape);
//glutKeyboardFunc(key_board);
glutSpecialFunc(key_board);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}