Скачиваний:
12
Добавлен:
01.05.2014
Размер:
10.04 Кб
Скачать


#include <windows.h>
#include<Math.h>
#include "resource.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>

GLfloat light_diffuse[] = {15.0, 5.0, 15.0, 10.0};
float sp[4] = {1,1,1,1};


AUX_RGBImageRec* photo_image;


#define M_PI        3.14159265358979323846
float delta = 0.1;
float x = 0, y = 0, z = 5;

float m = 1;				//?????????? ???????? ??????????????? ?????
float p = 1.05;				//?????????? ???????? ??????????????? ????? ????????? ?????

int alpha = 0, beta = 0;	//?????????? ???? ????????


void CALLBACK resize(int width,int height)
{
	glViewport(0,0,width,height);
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho(-5,5, -5,5, 2,12);   
	gluLookAt(x, y, z, 0,0,0, 0,1,0 );
	glMatrixMode( GL_MODELVIEW );
}

//??????? ???????? ???????? ????????? ? ??????????
void Polar2Normal(float a, float b, float r, float nor[3])
 {
	nor[0] = r*cos(a)*cos(b);
	nor[1] = r*sin(a);
	nor[2] = -r*cos(a)*sin(b);
 }

//??????? ???????? ?????????? ????????? ? ????????
void Normal2Polar(float x, float y, float z, float pol[3])
 {
 	pol[2] = sqrt(x*x+y*y+z*z);
	//pol[0] = asin(y/sqrt(x*x+y*y+z*z));
	pol[0] = acos(sqrt(x*x+z*z)/sqrt(x*x+y*y+z*z));
	pol[1] = acos(x/sqrt(x*x+z*z));
	if(z>0) pol[1] = 2*M_PI - pol[1];
			
 }

//??????? ???????? ????????? ? ??????????
void Polar2Normal2(float a, float b, float r, float* x, float* y, float* z)
{	*x = r * cos(a) * cos(b);
	*y = r*sin(a);
	*z = -r*cos(a)*sin(b);
}

//???????? ????? - ? ??????? ????
void CALLBACK mouse(AUX_EVENTREC *event)	//?????????? ????????? ?? ????
{	
	static int x0,y0=-12345;

	if(y0!=-12345)
	{
	  alpha += event->data[AUX_MOUSEX] - x0;
	  beta += event->data[AUX_MOUSEY] - y0;
	}
	
	//????????? ????????? ????
	x0 = event->data[AUX_MOUSEX];
	y0 = event->data[AUX_MOUSEY];
}

/*
//???????? ?????? - ? ??????? ????
void CALLBACK mouse(AUX_EVENTREC *event)	//?????????? ????????? ?? ????
{	
	float pol[3];
	static int x0,y0=-12345;

	if(y0!=-12345)
	{
		Normal2Polar(x, y, z, pol);
		pol[1] += event->data[AUX_MOUSEX] - x0;
		Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
		resize(400,400);
		
	   	Normal2Polar(x, y, z, pol);
		pol[0] += event->data[AUX_MOUSEY] - y0;
		Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
		resize(400,400);
	}
		//????????? ????????? ????
	x0 = event->data[AUX_MOUSEX];
	y0 = event->data[AUX_MOUSEY];
}
*/

//???????? ????? - ?????????? ????????? ?? ??????????
void CALLBACK Key_LEFT(void)		//?????? ?????? "??????" (???????? ????? - ??????????)
{	alpha -= 2;}
void CALLBACK Key_RIGHT(void)		//?????? ?????? "???????" (???????? ????? - ??????????)
{	alpha += 2;}
void CALLBACK Key_UP(void)			//?????? ?????? "?????" (???????? ????? - ??????????)
{	beta += 2;}
void CALLBACK Key_DOWN(void)		//?????? ?????? "????" (???????? ????? - ??????????)
{	beta -= 2 ;}


//???????? ?????? - ?????????? ????????? ?? ??????????
void CALLBACK Key_LEFT_KAM(void)		//?????? ?????? "H" - ?????? (???????? ?????? - ??????????)
{	float pol[3];
		
	Normal2Polar(x, y, z, pol);
	pol[1] -= delta;
	Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
	resize(400,400);
}

void CALLBACK Key_RIGHT_KAM(void)		//?????? ?????? "K" - ??????? (???????? ?????? - ??????????)
{	float pol[3];
	
	Normal2Polar(x, y, z, pol);
	pol[1] += delta;
	Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
	resize(400,400);
}

void CALLBACK Key_UP_KAM(void)			//?????? ?????? "U" - ????? (???????? ?????? - ??????????)
{	float pol[3];
	
	Normal2Polar(x, y, z, pol);
	pol[0] += delta;
	Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
	resize(400,400);
}

void CALLBACK Key_DOWN_KAM(void)		//?????? ?????? "J" - ???? (???????? ?????? - ??????????)
{	float pol[3];
	
	Normal2Polar(x, y, z, pol);
	pol[0] -= delta;
	Polar2Normal2(pol[0], pol[1], pol[2], &x, &y, &z);
	resize(400,400);
}

//???????? ????????? ?????????
void CALLBACK Key_LEFT_A(void)		//?????? ?????? "A" - ?????? (???????? ????????? ?????????)
{
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[1] -= delta;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
 }

void CALLBACK Key_RIGHT_D(void)		//?????? ?????? "D" - ??????? (???????? ????????? ?????????)
 {
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[1] += delta;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
 }

 void CALLBACK Key_DOWN_S(void)		//?????? ?????? "S" - ???? (???????? ????????? ?????????)
 {
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[0] -= delta;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
 }

 void CALLBACK Key_UP_W(void)		//?????? ?????? "W" - ????? (???????? ????????? ?????????)
 {
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[0] += delta;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
 }


//??????????????? ?????
void CALLBACK Key_1(void)			//?????? ?????? "1" - ?????????? ???????? ??????????? ? 1.05 ???
{	
	m *= 1.2;
}

void CALLBACK Key_2(void)			//?????? ?????? "2" - ?????????? ???????? ??????????? ? 1.05 ???
{	
	m /= 1.2;
}

//??????????????? ??????? ????? ????????? ?????
void CALLBACK Key_3(void)			//?????? ?????? "3" - ?????????? ??????? ????? ????????? ????? ? 1.05 ???
{	
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[2] *= p+1;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
}

void CALLBACK Key_4(void)			//?????? ?????? "4" - ?????????? ??????? ????? ????????? ????? ? 1.05 ???
{	
	float nor[4];
	float pol[3];
	
	glGetLightfv(GL_LIGHT0, GL_POSITION, nor);
	Normal2Polar(nor[0], nor[1], nor[2], pol);
	pol[2] /= p;
	Polar2Normal(pol[0], pol[1], pol[2], nor);
	glLightfv(GL_LIGHT0, GL_POSITION, nor);
}

//????????? ?????? 
void torus(int numc,int numt)
{
   int i,j,k;
   double s,t,x,y,z,twopi;
   twopi=2*(double) 3.1415;
   for(i=0;i<numc;i++)
   {
      glBegin(GL_QUAD_STRIP);
         for(j=0;j<=numt;j++)
         {
            for(k=1;k>=0;k--)
            {
               s=(i+k)%numc+0.5;
               t=j%numt;
               x=(2+.2*cos(s*twopi/numc))*cos(t*twopi/numt);
               y=(2+.2*cos(s*twopi/numc))*sin(t*twopi/numt);
               z=.2*sin(s*twopi/numc);
               glVertex3f(x,y,z);
            }
         }
      glEnd();
   }
}

void CALLBACK display(void)
{

	float ambient[4] = {0.3, 0.4, 0.3, 1};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, sp);
	GLUquadricObj *quadObj; 
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	
	quadObj = gluNewQuadric(); 
    gluQuadricTexture(quadObj, GL_TRUE);
    gluQuadricDrawStyle(quadObj, GLU_FILL); 
    glColor3d(1,1,1);

	glPushMatrix();

	glTranslated(0.0,0.0,0.0);
	glRotated(alpha, 0,1,0);
	glRotated(beta, -1,0,0);
	glScaled(m,m,m);

	glColor3d(1.0,1.0,1.0);
	auxSolidCube(3);
	glPopMatrix();
  

gluDeleteQuadric(quadObj);
auxSwapBuffers();


}

void main()
{
	float pos[4] = {3,3,3,1};
	float dir[3] = {-2,-2,-2};
	GLfloat mat_specular[] = {1,1,1,1};



    auxInitPosition( 50, 10, 400, 400);
    auxInitDisplayMode( AUX_RGB | AUX_DEPTH | AUX_DOUBLE );
    auxInitWindow( "Трехмерный объект OpenGL" );
    auxIdleFunc(display);
    auxReshapeFunc(resize);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

	glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
  
   	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   	glMaterialf(GL_FRONT, GL_SHININESS, 128.0);

	//????????????? ??????????????? ????? - ??????????? ??????????
	 auxKeyFunc(AUX_1, Key_1);
     auxKeyFunc(AUX_2, Key_2);
     
	//????????????? ???????? ????? - ??????????? ??????????
	 auxKeyFunc(AUX_LEFT, Key_LEFT);
     auxKeyFunc(AUX_RIGHT, Key_RIGHT);
     auxKeyFunc(AUX_UP, Key_UP);
     auxKeyFunc(AUX_DOWN, Key_DOWN);
 
	//????????????? ???????? ?????? - ??????????? ??????????
	 auxKeyFunc(AUX_H, Key_LEFT_KAM);	auxKeyFunc(AUX_h, Key_LEFT_KAM);
     auxKeyFunc(AUX_K, Key_RIGHT_KAM);	auxKeyFunc(AUX_k, Key_RIGHT_KAM);
     auxKeyFunc(AUX_U, Key_UP_KAM);		auxKeyFunc(AUX_u, Key_UP_KAM); 
     auxKeyFunc(AUX_J, Key_DOWN_KAM);	auxKeyFunc(AUX_j, Key_DOWN_KAM);
 	 
	//????????????? ???????? ????????? - ??????????? ??????????
	 auxKeyFunc(AUX_a, Key_LEFT_A);		auxKeyFunc(AUX_A, Key_LEFT_A);
     auxKeyFunc(AUX_d, Key_RIGHT_D);	auxKeyFunc(AUX_D, Key_RIGHT_D);
     auxKeyFunc(AUX_w, Key_UP_W);		auxKeyFunc(AUX_W, Key_UP_W);
     auxKeyFunc(AUX_s, Key_DOWN_S);		auxKeyFunc(AUX_S, Key_DOWN_S);

 	//????????????? ??????????????? ??????? ????????? ????? - ??????????? ??????????
	 auxKeyFunc(AUX_3, Key_3);
     auxKeyFunc(AUX_4, Key_4);

	//????????????? ???????? ????? - ?????????? ????
	auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSELOC, mouse);


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);

	photo_image = auxDIBImageLoad("photo.bmp");

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, 
                 photo_image->sizeX,
                 photo_image->sizeY,
                 0, GL_RGB, GL_UNSIGNED_BYTE,
                 photo_image->data);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    auxMainLoop(display);

   
}


Соседние файлы в папке Лабораторная работа №36