Home >
Back-end > Opengl for help to add texture, short of a program
Opengl for help to add texture, short of a program
This is I wrote the original program, it is necessary to add texture to the earth the sun the moon, but didn't come out, you great god help me,
# include & lt; GL/glut. H>
# include & lt; Math. H>
Float seta=200;
Float mx=0, my=0, mz=50, rx=0, ry=0, rz=0;//translation and rotation
Float mspeed=5, rspeed=1;
Static int oldrx=0, oldry=0;//rotate
Static int oldmx=0, oldmy=0;//translation
Static int oldpx=0, oldpy=0;
Bool a=false, b=false;
Int specialKey;
Void myDisplay (void);
Void Mouse (int button, int state, int x, int y)//deal with the Mouse to click
{
If (state==GLUT_DOWN)//the mouse to press for the first time, record the initial coordinates of the mouse in the window
{
If (button==GLUT_RIGHT_BUTTON) {
, oldry oldrx=x=y;
B=true;
}
Else if (button==GLUT_LEFT_BUTTON) {
, oldmy oldmx=x=y;
A=true;
}
}
Else if (state==GLUT_UP)/release/mouse
{
If (button==GLUT_LEFT_BUTTON)
{
A=false;
}
Else if (button==GLUT_RIGHT_BUTTON)
{
B=false;
}
}
}
Void onm ouseMove (int x, int y)//handle the mouse drag
{
If (a==true) & amp; & (b==true))//about key
{
Mz +=(y - oldmy) * mspeed/10;
, oldmy oldmx=x=y;
}
Else if (a==true)//click
{
My +=- (y - oldmy) * mspeed;
Mx +=(x - oldmx) * mspeed;
, oldmy oldmx=x=y;
}
Else if (b==true)//right
{
Ry +=- (x - oldrx);
Rx +=- (y - oldry);
, oldry oldrx=x=y;
}
}
Void myTimerFunc (int val)
{
+ + seta;
If (seta & gt;
=360)Seta=0;//is 360 days a year, more than 360 from zero to start
MyDisplay ();
MyTimerFunc glutTimerFunc (1, 0).
}
Void myKeyboardFunc (unsigned char key, int x, int y)
{
The switch (key)
{
Case 'W' :
My +=mspeed;
break;
In case the 'S' :
My -=mspeed;
break;
Case 'A' :
Mx -=mspeed;
break;
Case 'D' :
Mx +=mspeed;
break;
Case 'Q:
Mz +=mspeed;
break;
Case 'E' :
Mz -=mspeed;
break;
In case the 'I' :
Rx +=rspeed;
break;
Case 'K' :
Rx -=rspeed;
break;
Case 'J' :
Ry +=rspeed;
break;
Case 'L' :
Ry -=rspeed;
break;
Case '1' :
Mspeed +=2;
break;
Case '2' :
Mspeed -=2;
Break;
Case '3' :
Rspeed +=0.5;
break;
Case '4' :
Rspeed -=0.5;
break;
}
}
Void SetRC ()
{
GlShadeModel (GL_FLAT);
GlFrontFace (GL_CCW);
GlEnable (GL_DEPTH_TEST);
GlEnable (GL_LIGHTING);
GlEnable (GL_LIGHT0);
Float amb [4]={0.8, 0.0, 0.0, 1};//red ambient light
Float dif [4]={1.0, 1.0, 1.0, 1};//white diffuse light and reflected light
Float the spe [4]={1.0, 1.0, 1.0, 1};//black mirror
GlLightfv (GL_LIGHT0 GL_AMBIENT, amb);
GlLightfv (GL_LIGHT0 GL_DIFFUSE, dif);
GlLightfv (GL_LIGHT0 GL_SPECULAR, amb);
GlEnable (GL_COLOR_MATERIAL);
GlColorMaterial (GL_FRONT GL_AMBIENT_AND_DIFFUSE);
GlPolygonMode (GL_BACK GL_LINE);
}
Void SetView ()
{
GlRotatef (- ry, 0, 0);//h
GlRotatef (- rx, 0, 1);//p
GlRotatef (- rz, 0, 1);//r
GlTranslatef (mx, - my - mz);
}
Void RenderWorld ()
{
GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GlPushMatrix ();
GlTranslatef (0, 5, 25);
GlRotatef (seta, 0, 0);
GlPopMatrix ();
//the sun
GlPushMatrix ();
GlTranslatef (0-100-600);
GlPushMatrix ();
GlPolygonMode (GL_FRONT_AND_BACK GL_LINE);
GlColor3f (0, 1);
GlRotatef (seta * 2,0,1,0);
,1,0,0 glRotatef (90);
GlutSolidSphere (70100100);
Float pos [4]=,0,0,1 {0};//the sun as the light source,
GlLightfv (GL_LIGHT0 GL_POSITION, pos);//set the light source position
GlPopMatrix ();
//the earth
GlPolygonMode (GL_FRONT_AND_BACK GL_LINE);
GlColor3f (0, 1);
GlRotatef (seta, 0, 0);//revolution
GlTranslatef (0, 175);
GlRotatef (seta, 0, 0);//rotation
,1,0,0 glRotatef (90);
GlutSolidSphere (16200200);//the earth
GlPolygonMode (GL_FRONT_AND_BACK GL_LINE);
GlColor3f (1, 0);
GlRotatef (seta * 6, 0, 0, 1);
GlTranslatef (0, 35);
GlRotatef (seta * 1,0,1,0);//the moon's rotation
GlutSolidSphere (4.5, 60, 60);
GlPopMatrix ();//the moon
}
Void myDisplay (void)
{
GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GlPushMatrix ();
SetView ();
RenderWorld ();
//model control
GlPopMatrix ();
GlutSwapBuffers ();
}
Void myReshape (int w, int h)
{
GLfloat nRange=100.0 f;
GlViewport (0, 0, w, h);
GlMatrixMode (GL_PROJECTION);
GlLoadIdentity ();
GluPerspective (60 and GLfloat (w)/h, 1100);
GlMatrixMode (GL_MODELVIEW);
GlLoadIdentity ();
}
Int main (int arg c, char * argv [])
{
GlutInit (& amp; Arg c, argv);
GlutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
GlutInitWindowPosition (100, 100);
GlutInitWindowSize (800, 800);
GlutCreateWindow (" the sun the earth moon system ");
GlutDisplayFunc (& amp; MyDisplay);
GlutMouseFunc (Mouse);
GlutMotionFunc (onMouseMove);
MyTimerFunc glutTimerFunc (1, 0).
GlutReshapeFunc (& amp; MyReshape);
GlutKeyboardFunc (& amp; MyKeyboardFunc);
SetRC ();
GlutMainLoop ();
return 0;
}
This is three texture map, please help the
nullnullnullnullnullnullnullnullnullnullnullnull