Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
main.cpp
Package: CG_exp1.rar [view]
Upload User: guot24
Upload Date: 2022-06-16
Package Size: 20k
Code Size: 7k
Category:
OpenGL program
Development Platform:
C/C++
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- #include <GLglut.h>
- using namespace std;
- int Width=800;
- int Height=600;
- const float Pi=3.1415;
- int Mystate=0;
- int oldx=0;
- int oldy=0;
- float rate=1;
- float x1=1,x2=1,x3=1,x4=1;
- int color1=0,color2=0,color3=1;
- void MyInit()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- }
- void graph1()
- {
- /***************** area graph1 ****************************/
- //draw the blue line
- glLineWidth(1);
- glColor3f(0.0,0.0,1.0);
- glBegin(GL_LINES);
- glVertex2f(-1.0,1.0);
- glVertex2f(1.0,-1.0);
- glEnd();
- //draw the red broken line
- glLineWidth(1);
- glColor3f(1.0,0.0,0.0);
- glEnable(GL_LINE_STIPPLE);
- glLineStipple(1, 0x00FF);
- glBegin(GL_LINES);
- glVertex2f(-1.0,-1.0);
- glVertex2f(1.0,1.0);
- glEnd();
- //draw the yellow strip line
- glColor3f(1.0,1.0,0.0);
- glLineStipple(1, 0x67E6);
- glBegin(GL_LINE_STRIP);
- glVertex2f(-1.0,-1.0);
- glVertex2f(-0.8,-0.7);
- glVertex2f(0.7,0.8);
- glVertex2f(1.0,1.0);
- glEnd();
- }
- void graph2()
- {
- /************ area graph2 ************************/
- glDisable(GL_LINE_STIPPLE);
- glColor3f(0.0,1.0,0.0);
- glBegin(GL_TRIANGLES);
- glVertex2f(-0.8,-0.8);
- glVertex2f(0.5,0.5);
- glVertex2f(-0.2,0.3);
- glEnd();
- }
- void graph3()
- {
- /*********** area graph3 **********************/
- glShadeModel(GL_SMOOTH);
- glBegin(GL_QUADS);
- glColor3f(0.9,0.0,0.0);
- glVertex2f(-1.0,0);
- glColor3f(0.0,0.8,0.0);
- glVertex2f(1.0,0.5);
- glColor3f(0.9,0.9,0.9);
- glVertex2f(0.6,-0.7);
- glColor3f(0.0,0.0,0.8);
- glVertex2f(-1.0,-0.5);
- glEnd();
- }
- void graph4()
- {
- /********** area graph4 ***********************/
- int blocknum=300;
- glColor3f(1,0,0);
- glBegin(GL_POLYGON);
- for(int i=0;i<blocknum;i++)
- {
- GLfloat Loc[2];
- Loc[0]=0.4*cos(2*Pi*i/blocknum);
- Loc[1]=0.5*sin(2*Pi*i/blocknum);
- glVertex2fv(Loc);
- }
- glEnd();
- }
- void MyDisplay() //绘制要显示的场景
- {
- if (Mystate==0)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glViewport( 0, Height/2, Width/2, Height/2 );
- glScalef(x1,x1,x1);
- graph1();
- glViewport( Width/2, Height/2, Width/2, Height/2 );
- glScalef(x2/x1,x2/x1,x2/x1);
- graph2();
- glViewport( 0, 0, Width/2, Height/2 );
- glScalef(x3/x2,x3/x2,x3/x2);
- graph3();
- glViewport( Width/2,0, Width/2, Height/2 );
- glScalef(x4/x3,x4/x3,x4/x3);
- graph4();
- glViewport(0,0,Width,Height);
- glFlush();
- }
- else if (Mystate==1)
- {
- glViewport(0,0,Width,Height);
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x1,x1,x1);
- graph1();
- glFlush();
- }
- else if (Mystate==2)
- {
- glViewport(0,0,Width,Height);
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x2,x2,x2);
- graph2();
- glFlush();
- }
- else if (Mystate==3)
- {
- glViewport(0,0,Width,Height);
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x3,x3,x3);
- graph3();
- glFlush();
- }
- else
- {
- glViewport(0,0,Width,Height);
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x4,x4,x4);
- graph4();
- glFlush();
- }
- }
- void MyReshape(GLint cx,GLint cy) //窗口大小发生变化时的操作,cx为窗口宽,cy为窗口高
- {
- // 防止除数即高度为0
- // (你可以设置窗口宽度为0).
- if(cy == 0)
- cy = 1;
- Width=cx;
- Height=cy;
- }
- void MyMouse(GLint button,GLint state,GLint cx,GLint cy) //鼠标点击操作
- {
- //button为鼠标键,有左键GLUT_LEFT_BUTTON,右键GLUT_RIGHT_BUTTON,滚轮向上GLUT_WHEEL_UP,滚轮向下GLUT_WHEEL_DOWN
- //state为鼠标按键状态,有按下GLUT_DOWN,弹起GLUT_UP
- //cx,cy为鼠标当前坐标
- //对于滚轮的操作,需要较新的GLUT版本才能支持,可以使用主页上的glut2004
- //对滚轮的判定,使用state == GLUT_UP && button == GLUT_WHEEL_UP来判断
- if (button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
- {
- glLoadIdentity();
- rate=1;
- if (Mystate==0)
- {
- if (cx<=Width/2&&cy<=Height/2)
- {
- Mystate=1;
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x1,x1,x1);
- graph1();
- glFlush();
- }
- else if (cx>Width/2&&cy<=Height/2)
- {
- Mystate=2;
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x2,x2,x2);
- graph2();
- glFlush();
- }
- else if (cx<=Width/2&&cy>Height/2)
- {
- Mystate=3;
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x3,x3,x3);
- graph3();
- glFlush();
- }
- else
- {
- Mystate=4;
- glClear(GL_COLOR_BUFFER_BIT);
- glScalef(x4,x4,x4);
- graph4();
- glFlush();
- }
- }
- else
- {
- Mystate=0;
- glClear(GL_COLOR_BUFFER_BIT);
- MyDisplay();
- }
- }
- else if (button==GLUT_WHEEL_UP&&state==GLUT_UP&&Mystate>=1)
- {
- if (Mystate==1)
- {
- rate*=1.1;
- glScalef(1.1,1.1,1.1);
- x1*=1.1;
- glClear(GL_COLOR_BUFFER_BIT);
- graph1();
- glFlush();
- }
- else if (Mystate==2)
- {
- rate*=1.1;
- glScalef(1.1,1.1,1.1);
- x2*=1.1;
- glClear(GL_COLOR_BUFFER_BIT);
- graph2();
- glFlush();
- }
- else if (Mystate==3)
- {
- rate*=1.1;
- glScalef(1.1,1.1,1.1);
- x3*=1.1;
- glClear(GL_COLOR_BUFFER_BIT);
- graph3();
- glFlush();
- }
- else
- {
- rate*=1.1;
- x4*=1.1;
- glScalef(1.1,1.1,1.1);
- glClear(GL_COLOR_BUFFER_BIT);
- graph4();
- glFlush();
- }
- }
- else if (button==GLUT_WHEEL_DOWN&&state==GLUT_UP&&Mystate>=1)
- {
- if (Mystate==1)
- {
- glScalef(0.9,0.9,0.1);
- rate*=0.9;
- x1*=0.9;
- glClear(GL_COLOR_BUFFER_BIT);
- graph1();
- glFlush();
- }
- else if (Mystate==2)
- {
- glScalef(0.9,0.9,0.1);
- rate*=0.9;
- x2*=0.9;
- glClear(GL_COLOR_BUFFER_BIT);
- graph2();
- glFlush();
- }
- else if (Mystate==3)
- {
- glScalef(0.9,0.9,0.1);
- rate*=0.9;
- x3*=0.9;
- glClear(GL_COLOR_BUFFER_BIT);
- graph3();
- glFlush();
- }
- else
- {
- glScalef(0.9,0.9,0.1);
- rate*=0.9;
- x4*=0.9;
- glClear(GL_COLOR_BUFFER_BIT);
- graph4();
- glFlush();
- }
- }
- else if (button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
- {
- oldx=cx;
- oldy=cy;
- }
- }
- void MyMotion(GLint cx,GLint cy) //处理鼠标移动时的操作,cx,cy为鼠标当前位置
- {
- cout << cx <<" "<<cy<<endl;
- if (Mystate>=1)
- {
- if (Mystate==1)
- {
- glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
- oldx=cx;
- oldy=cy;
- glClear(GL_COLOR_BUFFER_BIT);
- graph1();
- glFlush();
- }
- else if (Mystate==2)
- {
- glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
- oldx=cx;
- oldy=cy;
- glClear(GL_COLOR_BUFFER_BIT);
- graph2();
- glFlush();
- }
- else if (Mystate==3)
- {
- glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
- oldx=cx;
- oldy=cy;
- glClear(GL_COLOR_BUFFER_BIT);
- graph3();
- glFlush();
- }
- else
- {
- glTranslatef(2.0*(cx-oldx)/Width/rate,2.0*(oldy-cy)/Height/rate,0);
- oldx=cx;
- oldy=cy;
- glClear(GL_COLOR_BUFFER_BIT);
- graph4();
- glFlush();
- }
- }
- }
- int main(int argc,char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(Width,Height);
- glutCreateWindow("rjy_Exp1");
- MyInit(); //自定义OpenGL初始化函数
- glutDisplayFunc(MyDisplay); //显示
- glutReshapeFunc(MyReshape); //窗口改变
- glutMouseFunc(MyMouse); //鼠标点击
- glutMotionFunc(MyMotion); //鼠标移动
- glutMainLoop();
- return 0;
- }