|
|
发表于 2006-1-22 15:32:00
|
显示全部楼层
Re:Opengl画动态直线问题,请高手看看程序,在线等
/// 不知道你满意了没- -
#include<GL/glaux.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
static float rotate=0;
int clrBufferFlag=1;
float view_rotx=0.0;
float view_roty=0.0;
int count = 0;
int pos_x1=20,pos_y1=30,pos_z1=5;
int pos_x=20,pos_y=30,pos_z=5;
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);//&Eacute;è&para;¨&raquo;&ordm;&acute;&aelig;&micro;&Auml;&Ccedil;&aring;&sup3;&yacute;&Ouml;&micro;
glShadeModel(GL_FLAT);//&Ntilde;&iexcl;&Ocirc;&ntilde;&AElig;&frac12;&Atilde;&aelig;&Atilde;÷°&micro;&Auml;&pound;&Ecirc;&frac12;
}
static void special(int k, int x, int y)
{
switch (k)
{
case GLUT_KEY_UP:
view_rotx += 5.0;
break;
case GLUT_KEY_DOWN:
view_rotx -= 5.0;
break;
case GLUT_KEY_LEFT:
view_roty += 5.0;
break;
case GLUT_KEY_RIGHT:
view_roty -= 5.0;
break;
default:
return;
}
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);//&Ccedil;&aring;&sup3;&yacute;&raquo;&ordm;&acute;&aelig;&Icirc;&ordf;&Eacute;è&para;¨&Ouml;&micro;
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//&Ecirc;&Oacute;&micro;&atilde;&Eacute;è&Ouml;&Atilde;
glPushMatrix();
//glLoadIdentity();//&frac12;&laquo;&Otilde;&raquo;&para;&yen;&Eacute;è&Icirc;&ordf;&micro;&yen;&Icirc;&raquo;&frac34;&Oslash;&Otilde;ó
glLoadIdentity();
glRotatef((GLfloat) rotate,0.0,1.0,0.0);
glRotatef((GLfloat)view_rotx, 1.0, 0.0, 0.0);
glRotatef((GLfloat)view_roty, 0.0, 0.0, 1.0);
glColor3f(1.0,0.0,0.0);//&ordm;ì&Eacute;&laquo;
//glScalef(600.0,600.0,500.0);
glutWireCube(600.0);//&raquo;&shy;&Iuml;&szlig;&iquest;ò
//glFlush();
//glEnable(GL_LINE_SMOOTH);
glTranslatef(-300.0,-300.0,-300.0);
//glTranslatef(-300.0,-300.0,0.0);
glBegin(GL_LINES);
glColor3f(0.0,1.0,1.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(800.0,0.0,0.0);
glColor3f(1.0,1.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,800.0,0.0);
glColor3f(1.0,0.0,1.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(0.0,0.0,800.0);
glEnd();
//****************************************//&raquo;&shy;&para;&macr;&Igrave;&not;&Ouml;±&Iuml;&szlig;&sup2;&iquest;·&Ouml;
//*
//int clrBufferFlag=1;
if (clrBufferFlag==1)
{
//glClear(GL_COLOR_BUFFER_BIT);
clrBufferFlag=0;
}
//glLoadIdentity();
//gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
//glRotatef((GLfloat) rotate,0.0,1.0,0.0);
//glEnable(GL_LINE_SMOOTH);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glColor3f(1.0,1.0,1.0);
glVertex3f(pos_x1,pos_y1,pos_z1);
glVertex3f(pos_x,pos_y,pos_z);
glEnd();
//glPopMatrix();
//glDisable(GL_LINE_SMOOTH);
//*/
//****************************************&raquo;&shy;&para;&macr;&Igrave;&not;&Ouml;±&Iuml;&szlig;&frac12;á&Ecirc;&oslash;
glPopMatrix();
glFlush();
glutSwapBuffers();//&frac12;&raquo;&raquo;&raquo;&micro;±&Ccedil;°&acute;°&iquest;&Uacute;&micro;&Auml;&raquo;&ordm;&acute;&aelig;
}
void update()
{
count++;
pos_x+=10;pos_y+=15;pos_z+=20;
if (count == 15)
{
pos_x=pos_x1;
pos_y=pos_y1;
pos_z=pos_z1;
count = 0;
}
Sleep(100);
glutPostRedisplay();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-550,550,-550,550,1300,-700);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key){
case 'r':
rotate += 1;
if (rotate > 360)
rotate -= 360;
glutPostRedisplay();
//rotate+=1.0f;
break;
case 'R':
rotate -= 1;
if (rotate < 0)
rotate += 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glutKeyboardFunc(keyboard);
glutIdleFunc(update);
glutMainLoop();
return 0;
}
|
|