在VS2012下使用glut繪制一系列直線的示例程序,繪制的是圓周上的二十條直徑,效果如下圖所示,按PC的UP,DOWN,LEFT,RIGHT按鍵可以從不同的角度查看該圖形:
源代碼:
[cpp]
// GlutLinesDemo.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
//圓周率宏
#define GL_PI 3.1415f
//獲取屏幕的寬度與高度
GLint SCREEN_WIDTH=0;
GLint SCREEN_HEIGHT=0;
//設置程序的窗口大小
GLint windowWidth=400;
GLint windowHeight=300;
//繞x軸旋轉角度
GLfloat xRotAngle=0.0f;
//繞y軸旋轉角度
GLfloat yRotAngle=0.0f;
//顯示回調函數
void renderScreen(void){
GLfloat x,y,z,angle;
//把整個窗口清理為當前清理顏色:黑色
glClear(GL_COLOR_BUFFER_BIT);
//將當前Matrix狀態入棧
glPushMatrix();
//坐標系繞x軸旋轉xRotAngle
glRotatef(xRotAngle,1.0f,0.0f,0.0f);
//坐標系繞y軸旋轉yRotAngle
glRotatef(yRotAngle,0.0f,1.0f,0.0f);
//開始繪直線
glBegin(GL_LINES);
z=0.0f;
//繪制圓周上的20條直徑
for(angle=0.0f;angle<=GL_PI;angle+=(GL_PI/20)){
x=50.0f*sin(angle);
y=50.0f*cos(angle);
glVertex3f(x,y,z);
x=50.0f*sin(angle+GL_PI);
y=50.0f*cos(angle+GL_PI);
glVertex3f(x,y,z);
}
//結束繪直線
glEnd();
//恢復壓入棧的Matrix
glPopMatrix();
//交換兩個緩沖區的指針
glutSwapBuffers();
}
//設置Redering State
void setupRederingState(void){
//設置清理顏色為黑色
glClearColor(0.0f,0.0,0.0,1.0f);
//設置繪畫顏色為綠色
glColor3f(0.0f,1.0f,0.0f);
}
//窗口大小變化回調函數
void changSize(GLint w,GLint h){
//橫寬比率
GLfloat ratio;
//設置坐標系為x(-100.0f,100.0f)、y(-100.0f,100.0f)、z(-100.0f,100.0f)
GLfloat coordinatesize=100.0f;
//窗口寬高為零直接返回
if((w==0)||(h==0))
return;
//設置視口和窗口大小一致
glViewport(0,0,w,h);
//對投影矩陣應用隨後的矩陣操作
glMatrixMode(GL_PROJECTION);
//重置當前指定的矩陣為單位矩陣
glLoadIdentity();
ratio=(GLfloat)w/(GLfloat)h;
//正交投影
if(w<h)
glOrtho(-coordinatesize,coordinatesize,-coordinatesize/ratio,coordinatesize/ratio,-coordinatesize,coordinatesize);
else
glOrtho(-coordinatesize*ratio,coordinatesize*ratio,-coordinatesize,coordinatesize,-coordinatesize,coordinatesize);
//對模型視圖矩陣堆棧應用隨後的矩陣操作
glMatrixMode(GL_MODELVIEW);
//重置當前指定的矩陣為單位矩陣
glLoadIdentity();
}
//按鍵輸入處理回調函數
void specialKey(int key,int x,int y){
if(key==GLUT_KEY_UP){
xRotAngle-=5.0f;
}
else if(key==GLUT_KEY_DOWN){
xRotAngle+=5.0f;
}
else if(key==GLUT_KEY_LEFT){
yRotAngle-=5.0f;
}
else if(key==GLUT_KEY_RIGHT){
yRotAngle+=5.0f;
}
//重新繪制
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
//初始化glut
glutInit(&argc,argv);
//使用雙緩沖區模式
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
//獲取系統的寬像素
SCREEN_WIDTH=glutGet(GLUT_SCREEN_WIDTH);
//獲取系統的高像素
SCREEN_HEIGHT=glutGet(GLUT_SCREEN_HEIGHT);
//創建窗口,窗口名字為OpenlGL Lines Demo
glutCreateWindow("OpenlGL Lines Demo");
//設置窗口大小
glutReshapeWindow(windowWidth,windowHeight);
//窗口居中顯示
glutPositionWindow((SCREEN_WIDTH-windowWidth)/2,(SCREEN_HEIGHT-windowHeight)/2);
//設置窗口大小變化時的回調函數
glutReshapeFunc(changSize);
//設置顯示回調函數
glutDisplayFunc(renderScreen);
//設置按鍵輸入處理回調函數
glutSpecialFunc(specialKey);
//設置全局渲染參數
setupRederingState();
glutMainLoop();
return 0;
}
// GlutLinesDemo.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
//圓周率宏
#define GL_PI 3.1415f
//獲取屏幕的寬度與高度
GLint SCREEN_WIDTH=0;
GLint SCREEN_HEIGHT=0;
//設置程序的窗口大小
GLint windowWidth=400;
GLint windowHeight=300;
//繞x軸旋轉角度
GLfloat xRotAngle=0.0f;
//繞y軸旋轉角度
GLfloat yRotAngle=0.0f;
//顯示回調函數
void renderScreen(void){
GLfloat x,y,z,angle;
//把整個窗口清理為當前清理顏色:黑色
glClear(GL_COLOR_BUFFER_BIT);
//將當前Matrix狀態入棧
glPushMatrix();
//坐標系繞x軸旋轉xRotAngle
glRotatef(xRotAngle,1.0f,0.0f,0.0f);
//坐標系繞y軸旋轉yRotAngle
glRotatef(yRotAngle,0.0f,1.0f,0.0f);
//開始繪直線
glBegin(GL_LINES);
z=0.0f;
//繪制圓周上的20條直徑
for(angle=0.0f;angle<=GL_PI;angle+=(GL_PI/20)){
x=50.0f*sin(angle);
y=50.0f*cos(angle);
glVertex3f(x,y,z);
x=50.0f*sin(angle+GL_PI);
y=50.0f*cos(angle+GL_PI);
glVertex3f(x,y,z);
}
//結束繪直線
glEnd();
//恢復壓入棧的Matrix
glPopMatrix();
//交換兩個緩沖區的指針
glutSwapBuffers();
}
//設置Redering State
void setupRederingState(void){
//設置清理顏色為黑色
glClearColor(0.0f,0.0,0.0,1.0f);
//設置繪畫顏色為綠色
glColor3f(0.0f,1.0f,0.0f);
}
//窗口大小變化回調函數
void changSize(GLint w,GLint h){
//橫寬比率
GLfloat ratio;
//設置坐標系為x(-100.0f,100.0f)、y(-100.0f,100.0f)、z(-100.0f,100.0f)
GLfloat coordinatesize=100.0f;
//窗口寬高為零直接返回
if((w==0)||(h==0))
return;
//設置視口和窗口大小一致
glViewport(0,0,w,h);
//對投影矩陣應用隨後的矩陣操作
glMatrixMode(GL_PROJECTION);
//重置當前指定的矩陣為單位矩陣
glLoadIdentity();
ratio=(GLfloat)w/(GLfloat)h;
//正交投影
if(w<h)
glOrtho(-coordinatesize,coordinatesize,-coordinatesize/ratio,coordinatesize/ratio,-coordinatesize,coordinatesize);
else
glOrtho(-coordinatesize*ratio,coordinatesize*ratio,-coordinatesize,coordinatesize,-coordinatesize,coordinatesize);
//對模型視圖矩陣堆棧應用隨後的矩陣操作
glMatrixMode(GL_MODELVIEW);
//重置當前指定的矩陣為單位矩陣
glLoadIdentity();
}
//按鍵輸入處理回調函數
void specialKey(int key,int x,int y){
if(key==GLUT_KEY_UP){
xRotAngle-=5.0f;
}
else if(key==GLUT_KEY_DOWN){
xRotAngle+=5.0f;
}
else if(key==GLUT_KEY_LEFT){
yRotAngle-=5.0f;
}
else if(key==GLUT_KEY_RIGHT){
yRotAngle+=5.0f;
}
//重新繪制
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
//初始化glut
glutInit(&argc,argv);
//使用雙緩沖區模式
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
//獲取系統的寬像素
SCREEN_WIDTH=glutGet(GLUT_SCREEN_WIDTH);
//獲取系統的高像素
SCREEN_HEIGHT=glutGet(GLUT_SCREEN_HEIGHT);
//創建窗口,窗口名字為OpenlGL Lines Demo
glutCreateWindow("OpenlGL Lines Demo");
//設置窗口大小
glutReshapeWindow(windowWidth,windowHeight);
//窗口居中顯示
glutPositionWindow((SCREEN_WIDTH-windowWidth)/2,(SCREEN_HEIGHT-windowHeight)/2);
//設置窗口大小變化時的回調函數
glutReshapeFunc(changSize);
//設置顯示回調函數
glutDisplayFunc(renderScreen);
//設置按鍵輸入處理回調函數
glutSpecialFunc(specialKey);
//設置全局渲染參數
setupRederingState();
glutMainLoop();
return 0;
}