📄 earthtex.c
字号:
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#define PI 3.1415926
GLfloat step = 0.0, viewdis = 20., viewrotx = 0., viewroty = 0.;
typedef struct {float x,y,z;} Ppoint3;
Ppoint3 sphere[36][36];
void myinit(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK display(void);
void makeStripeImage(void);
AUX_RGBImageRec *image;
GLubyte *stripeImage;
void myinit(void)
{
GLfloat mat_ambient[]={0.1,0.1,0.1,1.0};
GLfloat mat_diffuse[]={0.7,0.,0.,1.0};
GLfloat mat_specular[] = { .5, 0.5, 0.5, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_ambient[]={0.3,0.3,0.3,1.0};
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
initsphere(1.0,18,36,sphere);
glClearColor (0.0, 0.0, 0.0, 0.0);
makeStripeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image->sizeX,image->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, stripeImage);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_BLEND);
// glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(step,0.0,1.0,0.0);
fillsphere(18,36,sphere);
glPopMatrix();
step+=0.1;
glFlush();
auxSwapBuffers();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 100.0);
else
glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(12,10,12,0,0,0,0,1,0);
}
initsphere(rad,nlat,nlong,point)
float rad;
int nlat,nlong;
Ppoint3 point[][37];
{
int i,j,maxlat=36,maxlong=36;
double pi=3.1415926,inclat,jj,dval,magnitude;
if(nlat>maxlat) nlat=maxlat;
if(nlong>maxlong) nlong=maxlong;
inclat=pi/2.;
for(i=0;i<nlat;i++)
{ /* go around cross section */
magnitude=cos(inclat)*rad;
dval=sin(inclat)*rad;
for(j=0;j<nlong+1;j++)
point[i][j].y=(float) dval;
for(j=0;j<nlong+1;j++)
{ /* go around top view */
jj=(double)j;
dval=cos(2.*pi*jj/nlong)*magnitude;
point[i][j].x=(float)dval;
dval=sin(2.*pi*jj/nlong)*magnitude;
point[i][j].z=(float)dval;
}
inclat=inclat-(pi/(float)(nlat-1));
}
return(0);
}
fillsphere(nlat,nlong,point)
int nlat,nlong;
float point[][37][3];
{
int i,j;
float tx,ty;
ty=1.0/(float)(nlat-1);
tx=1.0/(float)nlong;
for(i=0;i<nlat;i++) {
glBegin(GL_QUAD_STRIP);
for(j=0;j<nlong+1;j++)
{
glTexCoord2f((float)j*tx,(float)(i+1)*ty);
glNormal3fv(point[i+1][j]);
glVertex3fv(point[i+1][j]);
glTexCoord2f((float)j*tx,(float)i*ty);
glNormal3fv(point[i][j]);
glVertex3fv(point[i][j]);
} // end j
glEnd();
} // end i
return(0);
}
void makeStripeImage(void)
{
image=auxDIBImageLoad("earth.bmp");
if(image==NULL) exit(-1);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
stripeImage=(GLubyte *)malloc(3*image->sizeX*image->sizeY*sizeof(GLubyte));
gluScaleImage(GL_RGB,image->sizeX,image->sizeY,GL_UNSIGNED_BYTE,
image->data,image->sizeX,image->sizeY,
GL_UNSIGNED_BYTE,stripeImage);
}
void main(void)
{
auxInitDisplayMode (AUX_DOUBLE | AUX_RGBA);
auxInitPosition (100,100, 800, 600);
auxInitWindow ("地球");
myinit();
auxReshapeFunc (myReshape);
auxIdleFunc (display);
auxMainLoop(display);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -