Пример#7 Текстурирование сферы
#define _USE_MATH_DEFINES
#include <string.h>
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <glut.h>
#include <soil.h>
#include <cmath>
#pragma comment(lib,"soil.lib")
#define obj 1//количество текстурируемых объектов
int main_window=0;
int screen_width=640;
int screen_height=480;
using namespace std;
GLuint texture[1];// массив для текстур
float Rotate = 0.1f;
#define drawOneLine(x1,y1,z1,x2,y2,z2) glBegin(GL_LINES); \
glVertex3f(x1,y1,z1); glVertex3f (x2,y2,z2); glEnd();
void drawString (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, s[i]);
}
int LoadGLTextures()
{
char *pics[1] = {"planet3d.jpg"};
for (int i=0;i<obj;++i)
{
texture[i] = SOIL_load_OGL_texture(
pics[i],
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB
| SOIL_FLAG_COMPRESS_TO_DXT );
if(texture[i] == 0)
return false;
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
}
return true;
}
void init()
{
LoadGLTextures(); // Загрузка текстур
glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
glShadeModel(GL_SMOOTH); // Type of shading for the polygons
// Viewport transformation
glViewport(0,0,screen_width,screen_height);
// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(60.0f,(GLfloat)screen_width/(GLfloat)screen_height,1.0f, 20000.0f); // We define the "viewing volume"
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DrawEarth(void)
{
GLUquadricObj * quadSky;
quadSky = gluNewQuadric();
gluQuadricTexture(quadSky, GL_TRUE);
gluQuadricDrawStyle(quadSky, GLU_FILL);
glColor3d(1,1,1);
glBindTexture(GL_TEXTURE_2D, texture[0]);
gluSphere(quadSky, 6371, 24, 24);
gluDeleteQuadric(quadSky);
}
void myGlutDisplay( void )
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
glPushMatrix();
glTranslatef(0,0,-15000);
glRotatef(-70,1,0,0);//+Z сверху c наклоном
glRotatef(225,0,0,1);//XY к пользователю
glRotatef(Rotate,0,0,1);
DrawEarth();
glPopMatrix();
glLoadIdentity();
Rotate += 0.4;
if (Rotate > 360) Rotate = 0;
glutSwapBuffers();
glutPostRedisplay();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
switch(button)
{
case GLUT_MIDDLE_BUTTON:
exit(0);
}
}
int main(int argc, char* argv[])
{
setlocale(0,"RUS");
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE|GLUT_RGBA |GLUT_DEPTH );
glutInitWindowPosition( 200, 200 );
glutInitWindowSize( 800, 600 );
main_window = glutCreateWindow( "texture" );
init();
glutDisplayFunc( myGlutDisplay );
glutMouseFunc(mouse);
glutMainLoop();
}
Для самостоятельной работы:
1. Создайте модель движения Земля-Луна с соответствующими текстурами.
С учетом следующих параметров и положений:
- Экваториальный радиус Луны = 1738 км
- наклонение орбиты Луны ~5 градусов
- орбита Луны круговая с радиусом = 384000 км
2. Смоделируйте движение первого спутника Земли, нанесите текстуры на Землю и спутник.
Параметры орбиты Спутника 1:
- Большая полуось 6955.2 км
- Эксцентриситет 0.052
- Период 96.7 минут