Пример#8 Работа с несколькими текстурами
#define _USE_MATH_DEFINES
#include <string>
#include <windows.h>
#include <sstream>
#include <string>
#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 2
int main_window=0;
int screen_width=640;
int screen_height=480;
using namespace std;
GLuint texture[3];// Место для текстур
float EarthRot = 0.1f;
float Ball_x=-8;
int n=1;
int countLHS=0;
int countRHS=0;
using namespace std;
// A function to draw a line
#define drawOneLine(x1,y1,z1,x2,y2,z2) glBegin(GL_LINES); \
glVertex3f(x1,y1,z1); glVertex3f (x2,y2,z2); glEnd();
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
char *pics[2] = {"ball_.jpg","grass_.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; // Return Success
}
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,100.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 DrawBall(float r) // Window redraw function
{
GLUquadricObj * quadSky;
quadSky = gluNewQuadric();
gluQuadricTexture(quadSky, GL_TRUE);
gluQuadricDrawStyle(quadSky, GLU_FILL);
glColor3d(1,1,1);
glBindTexture(GL_TEXTURE_2D, texture[0]);
gluSphere(quadSky, r, 24, 24);
gluDeleteQuadric(quadSky);
}
void drawString (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, s[i]);
}
void drawBIGString (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24, s[i]);
}
void Draw(void) // Window redraw function
{
glLineWidth(1.0);
//glPushMatrix();
glDisable(GL_TEXTURE_2D);
glColor3f (1.0F, 1.0F, 1.0F);
glRasterPos3f(1.1f, 0.0f, 0.0f);
drawString ("+X");
glRasterPos3f(-1.1f, 0.0f, 0.0f);
drawString ("-X");
glRasterPos3f(0.0f, 1.1f, 0.0f);
drawString ("+Y");
glRasterPos3f(0.0f, -1.1f, 0.0f);
drawString ("-Y");
glRasterPos3f(0.0f, 0.0f, 1.1f);
drawString ("+Z");
glRasterPos3f(0.0f, 0.0f, -1.1f);
drawString ("-Z");
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
glEnable (GL_LINE_STIPPLE);
glLineStipple (1.5, 0x00FF);
glColor3f(1.0f, 0.0f, 0.0f);
drawOneLine (0.0, 0.0, 0.0, -1.0, 0.0, 0.0);
glColor3f(0.0f, 1.0f, 0.0f);
drawOneLine (0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
glColor3f(0.0f, 0.0f, 1.0f);
drawOneLine (0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
glLineWidth (1.0);
glDisable (GL_LINE_STIPPLE);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
}
std::string double_to_string(double f)
{
std::ostringstream s;
s << f;
return s.str();
}
void drawString2 (string s)
{
for (int i = 0; i < s.length(); i++)
glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24, s[i]);
}
void myGlutDisplay( void )
{
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);//glMatrixMode( GL_PROJECTION );
glColor3f(0.0, 0.0, 0.0);
glPushMatrix();
glTranslatef(0,0,-15);
glRotatef(-70,1,0,0);//+Z сверху c наклоном
glRotatef(225,0,0,1);//XY к пользователю
glEnable(GL_TEXTURE_2D);// Разрешение наложение текстуры
//glRotatef(EarthRot,0,0,1);
Draw();
float l=4;
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-2*l, -1.2*l, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 2*l, -1.2*l, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 2*l, 1.2*l, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-2*l, 1.2*l, 0);
glEnd();
glPushMatrix();
glDisable(GL_TEXTURE_2D);// Разрешение наложение текстуры
glColor3f(1,1,1);
glTranslatef(7.1,-0.65,0);
//glutSolidCylinder(0.03,1,16,10);
GLUquadricObj *quadObj1;
quadObj1 = gluNewQuadric();
gluCylinder(quadObj1,0.03, 0.03,1,16,10);
glTranslatef(0, 1.3,0);
//glutSolidCylinder(0.03,1,16,10);
gluCylinder(quadObj1,0.03, 0.03,1,16,10);
glRotatef(90,1,0,0);
//glRotatef(180,0,0,1);
//glTranslatef(0, -1, 0);
glTranslatef(0, 1, 0);
//glutSolidCylinder(0.03,1.3,16,10);
gluCylinder(quadObj1,0.03, 0.03,1.3,16,10);
glPopMatrix();
glPushMatrix();
glDisable(GL_TEXTURE_2D);// Разрешение наложение текстуры
glColor3f(1,1,1);
glTranslatef(-7.1,-0.65,0);
//glutSolidCylinder(0.03,1,16,10);
gluCylinder(quadObj1,0.03, 0.03,1,16,10);
glTranslatef(0, 1.3,0);
//glutSolidCylinder(0.03,1,16,10);
gluCylinder(quadObj1,0.03, 0.03,1,16,10);
glRotatef(90,1,0,0);
//glRotatef(180,0,0,1);
//glTranslatef(0, -1, 0);
glTranslatef(0, 1, 0);
//glutSolidCylinder(0.03,1.3,16,10);
gluCylinder(quadObj1,0.03, 0.03,1.3,16,10);
glPopMatrix();
glEnable(GL_TEXTURE_2D);// Разрешение наложение текстуры
glPushMatrix();
glTranslatef(0,0,0.1f);
glTranslatef(Ball_x,0,fabs(sin(0.5*Ball_x)));
glRotatef( 200*Ball_x,0,1,0);//rand() % 10
DrawBall(0.1f);
glPopMatrix();
if (Ball_x>8) {n=-1;countLHS++;}
if (Ball_x<-8) {n=1;countRHS++;}
Ball_x+=0.04*n;
glRasterPos3f(-8.0f, -1.0f, 3.0f);
drawBIGString ("SCORE");
glRasterPos3f(-8.0f, -1.0f, 2.0f);
drawString2(double_to_string(countRHS));
glRasterPos3f(-8.0f, 0.0f, 2.0f);
drawString2(" : ");
glRasterPos3f(-8.0f, 1.0f, 2.0f);
drawString2(double_to_string(countLHS));
glPopMatrix();
glLoadIdentity();
EarthRot += 0.4;
if (EarthRot > 360) EarthRot = 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. Создайте атрибуты стадиона - трибуны, флажки, игроков.
2. Создайте из примитивов openGL КА Венера-4 и планету Венера.
- Средний радиус планеты Венера = 6051 км
- Расстояние от Солнца = 0.7 а.е.
- КА Венера-4: