Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <GL/SOIL.h>
- #include <cmath>
- #include "materials.h"
- GLfloat material[] = { 0.5, 0.5, 0.5, 1.0};
- using namespace std;
- GLfloat spot_direction[ 3 ] =
- {
- 0.0, 0.0, - 1.0
- };
- // kąt odcięcia reflektora
- GLfloat spot_cutoff = 180.0;
- // wykładnik tłumienia kątowego reflektora
- GLfloat spot_exponent = 128.0;
- // stały współczynnik tłumienia światła
- GLfloat constant_attenuation = 1.0;
- // liniowy współczynnik tłumienia światła
- GLfloat linear_attenuation = 0.0;
- // kwadratowy współczynnik tłumienia światła
- GLfloat quadratic_attenuation = 0.0;
- // funkcja rysująca napis w wybranym miejscu
- // (wersja korzystająca z funkcji glWindowPos2i)
- float swiatlo_otoczenia[] = {-0.2f, -0.2f, -0.2f, 1.0f};
- float swiatlo_rozproszone[] = {0.5f, 0.5f, 0.5f, 1.0f};
- float swiatlo_odbite[] = { 0.9, 0.9, 0.9, 1.0};
- float swiatlo_pozycja[] = { 0.0, 0.0, 0.0, 1.0};
- const GLfloat * ambient = BrassAmbient;
- const GLfloat * diffuse = BrassDiffuse;
- const GLfloat * specular = BrassSpecular;
- GLfloat shininess = BrassShininess;
- static float p_na_orbicie = 0.0f, obrot = 0.0f, autom=0.0f;
- GLuint _textureId;
- GLuint _textureId2;
- GLuint _textureIdmerkury;
- GLuint _textureIdvenus;
- GLuint _textureIdmars;
- GLuint _textureIdjowisz;
- GLuint _textureIdsaturn;
- GLuint _textureIduran;
- GLuint _textureIdneptun;
- GLuint img;
- GLUquadricObj *sphere = NULL;
- GLfloat obrr[16]={1,0,1,0,0,1,0,0,1,0,1,0,0,0,0,1};
- GLfloat rotatex = 0.0;
- GLfloat rotatey = 0.0;
- GLfloat translatex = 0.0;
- GLfloat translatey = 0.0;
- int czas=1;
- int button_state = GLUT_UP;
- // położenie kursora myszki
- int button_x, button_y;
- GLfloat skala = 1.0;
- void obrotY(){
- obrr[0]=cos(obrot);
- obrr[2]=-sin(obrot);
- obrr[8]=sin(obrot);
- obrr[10]=cos(obrot);
- }
- void initRendering() {
- glEnable(GL_DEPTH_TEST);
- // glEnable(GL_LIGHTING);
- // glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- glEnable(GL_COLOR_MATERIAL);
- glDisable(GL_CULL_FACE);
- glEnable(GL_TEXTURE_2D);
- //Image* img = loadBMP("sky.bmp");
- //_textureId = loadTextureFromImage(img);
- //delete img;
- _textureId = SOIL_load_OGL_texture
- (
- "test.bmp",
- 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( 0 == _textureId )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureId2 = SOIL_load_OGL_texture
- (
- "test2.bmp",
- 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( 0 == _textureId2 )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdmerkury = SOIL_load_OGL_texture
- (
- "merkury1.bmp",
- 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( 0 == _textureIdmerkury )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdvenus = SOIL_load_OGL_texture
- (
- "venus.bmp",
- 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( 0 == _textureIdvenus )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdmars = SOIL_load_OGL_texture
- (
- "mars.bmp",
- 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( 0 == _textureIdjowisz)
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdmerkury = SOIL_load_OGL_texture
- (
- "jowisz.bmp",
- 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( 0 == _textureIdjowisz)
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdsaturn = SOIL_load_OGL_texture
- (
- "saturn.bmp",
- 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( 0 == _textureIdsaturn )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIduran = SOIL_load_OGL_texture
- (
- "uran.bmp",
- 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( 0 == _textureIduran )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- _textureIdneptun = SOIL_load_OGL_texture
- (
- "neptun.bmp",
- 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( 0 == _textureIdneptun )
- {
- printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
- }
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- sphere = gluNewQuadric();
- gluQuadricDrawStyle(sphere, GLU_FILL);
- gluQuadricTexture(sphere, GL_TRUE);
- gluQuadricNormals(sphere, GLU_SMOOTH);
- }
- void handleResize(int w, int h) {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
- }
- void rysuj(){ // 1
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //glClear(GL_COLOR_BUFFER_BIT);
- glEnable( GL_DEPTH_TEST );
- // włączenie oświetlenia
- glEnable( GL_LIGHTING );
- // włączenie światła GL_LIGHT0
- // włączenie automatycznej normalizacji wektorów normalnych
- glEnable( GL_NORMALIZE );
- glLightf( GL_LIGHT1, GL_SPOT_CUTOFF, 180 );
- // wykładnik tłumienia kątowego reflektora
- // glLightf( GL_LIGHT1, GL_SPOT_EXPONENT, spot_exponent );
- // stały współczynnik tłumienia światła,
- // glLightf( GL_LIGHT0, GL_CONSTANT_ATTENUATION, constant_attenuation );
- // liniowy współczynnik tłumienia światła
- // glLightf( GL_LIGHT0, GL_LINEAR_ATTENUATION, linear_attenuation );
- // kwadratowy współczynnik tłumienia światła
- // glLightf( GL_LIGHT0, GL_QUADRATIC_ATTENUATION, quadratic_attenuation );
- glLightfv( GL_LIGHT1, GL_AMBIENT, swiatlo_otoczenia);
- glLightfv( GL_LIGHT1, GL_DIFFUSE, swiatlo_rozproszone);
- glLightfv( GL_LIGHT1, GL_SPECULAR, swiatlo_odbite);
- glLightfv( GL_LIGHT1, GL_POSITION, swiatlo_pozycja);
- glEnable( GL_LIGHT0 );
- glDisable( GL_LIGHT1 );
- glPushMatrix();
- glBindTexture(GL_TEXTURE_2D, _textureId2);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef( 0, 0, -5); //
- //glutWireSphere(1.0, 20, 16);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- gluSphere(sphere, 1.0, 20, 20);
- glDisable( GL_LIGHT0 );
- //glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glEnable( GL_LIGHT1 );
- glBindTexture(GL_TEXTURE_2D, _textureId);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (2.4, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- //obrotY();
- gluSphere(sphere, 0.2, 15, 15);
- glTranslatef (-2.4, 0.0, 0.0);
- glPopMatrix() ;
- // glutWireSphere(0.2, 10, 8);
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdmerkury);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (1.2, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*0.4, 15, 15);
- glTranslatef (-1.2, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdvenus);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (1.8, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*0.7, 15, 15);
- glTranslatef (-1.8, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdmars);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (2.8, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*0.5, 15, 15);
- glTranslatef (-2.8, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdjowisz);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (3.6, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*2, 15, 15);
- glTranslatef (-3.6, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdsaturn);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (4.5, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*1.5, 15, 15);
- glTranslatef (-4.5, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIduran);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (5.3, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*1.3, 15, 15);
- glTranslatef (-5.3, 0.0, 0.0);
- glPopMatrix() ;
- glPushMatrix();
- glRotatef ( 90, 1.0, 0.0, 0.0);
- glTranslatef( translatex, translatey, 0.0 );
- glScalef( skala, skala, skala );
- //glRotatef ( 90, 1.0, 0.0, 0.0);
- glRotatef( rotatex, 1.0, 0, 0 );
- glRotatef( rotatey, 0, 1.0, 0 );
- glBindTexture(GL_TEXTURE_2D, _textureIdneptun);
- glRotatef (p_na_orbicie, 0.0, 1.0, 0.0);
- glTranslatef (6.0, 0.0, 0.0);
- // //glRotatef ( -90, 1.0, 0.0, 0.0);
- obrotY();
- glMultMatrixf( obrr );
- glRotatef ( -90, 1.0, 0.0, 0.0);
- gluSphere(sphere, 0.2*1.2, 15, 15);
- glPopMatrix() ;
- glutPostRedisplay();
- glutSwapBuffers();
- }
- void keyboard (unsigned char key, int x, int y)
- {
- switch (key) {
- case 'd':
- if(obrot>350.0) obrot=0.0f;
- obrot = obrot + 10.0f;
- glutPostRedisplay();
- break;
- case 'D':
- if(obrot<10.0) obrot=360.0f;
- obrot = obrot - 10.0 ;
- glutPostRedisplay();
- break;
- case 's':
- if(p_na_orbicie>350.0) p_na_orbicie=0.0f;
- p_na_orbicie = p_na_orbicie + 5.0;
- glutPostRedisplay();
- break;
- case 'S':
- if(p_na_orbicie<10.0) p_na_orbicie=360.0f;
- p_na_orbicie = p_na_orbicie - 5.0 ;
- glutPostRedisplay();
- break;
- case 'a':
- if(autom){
- autom=0;
- }else{
- autom=1;
- }
- break;
- case '+':
- skala+=0.1;
- break;
- case '-':
- if(skala>0.1)
- skala-=0.1;
- break;
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- void SpecialKeys( int key, int x, int y )
- {
- switch( key )
- {
- // kursor w lewo
- case GLUT_KEY_LEFT:
- rotatey -= 1;
- break;
- // kursor w górę
- case GLUT_KEY_UP:
- rotatex -= 1;
- break;
- // kursor w prawo
- case GLUT_KEY_RIGHT:
- rotatey += 1;
- break;
- // kursor w dół
- case GLUT_KEY_DOWN:
- rotatex += 1;
- break;
- }
- }
- void MouseButton( int button, int state, int x, int y )
- {
- if( button == GLUT_LEFT_BUTTON )
- {
- // zapamiętanie stanu lewego przycisku myszki
- button_state = state;
- // zapamiętanie położenia kursora myszki
- if( state == GLUT_DOWN )
- {
- button_x = x;
- button_y = y;
- }
- }
- }
- // odrysowanie okna
- // Reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
- void MouseMotion( int x, int y )
- {
- if( button_state == GLUT_DOWN )
- {
- translatex += 1.1 *( 5 ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
- swiatlo_pozycja[0]+=1.1 *( 5 ) / glutGet( GLUT_WINDOW_WIDTH ) *( x - button_x );
- button_x = x;
- translatey += 1.1 *( 5 ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y );
- swiatlo_pozycja[1]+= 1.1 *( 5 ) / glutGet( GLUT_WINDOW_HEIGHT ) *( button_y - y );
- button_y = y;
- glutPostRedisplay();
- }
- }
- void obracanie(int value) {
- if(autom){
- p_na_orbicie+=0.1f;
- obrot+=0.1f;
- }
- if(p_na_orbicie>=360.0) p_na_orbicie-=360.0;
- if(obrot>=360.0) obrot-=360.0;
- glutPostRedisplay();
- glutTimerFunc(50, obracanie, 0);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
- glutInitWindowSize (1000, 500);
- glutCreateWindow("Hello World");
- initRendering();
- glutDisplayFunc(rysuj);
- glutReshapeFunc(handleResize);
- glutKeyboardFunc(keyboard);
- glutSpecialFunc( SpecialKeys );
- glutMouseFunc( MouseButton );
- // obsługa ruchu kursora myszki
- glutMotionFunc( MouseMotion );
- glutTimerFunc(50, obracanie, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement