Jump to content

OpenGL Too many Quadrics crashes program.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Hi,
I have assignment to make solar system in opengl. So far I have made Sun + Mercury, Venus, Earth(+ moon), Mars. When I'm trying to add Jupiter, everything crashes. I can't find problem. Can any one help me ?
I want to add that I didn't write this program from the beginning I'm editing program which was given by our teacher. That's why there is so many comments.

scene.cpp
//---------------------------------------------------------------------------


#include <windows.h>

#include "ogl_scene.h"

#include <gl\gl.h>

#include <gl\glu.h> 

#include <math.h>



//---------------------------------------------------------------------------


OGL_Scene::OGL_Scene()

  : hdc_(NULL)

  , hrc_(NULL)

  , w_(1)

  , h_(1)

  , rotx_(-60.0)    

  , roty_(0.0)

  , rotz_(0.0)

  , tr_(-10.0)

  , light_(true)

  , l0_(0)

  , std_material_(9)

  , earth_(0.3, 100, 100, 0, true)

  , sun_(2.0, 30, 30, 0, false)

  , moon_(0.05, 30, 30, 0, true)

  , venus_(0.2, 30, 30, 0, true)

  , mercury_(0.1, 30, 30, 0, true)

  , mars_(0.15,30,30,0,true )

  , jupiter_(0.7,100,100,0,true)

  , animate_(true)

{

}


//---------------------------------------------------------------------------


void OGL_Scene::Create(HWND hwnd)

{

  hdc_ = ::GetDC(hwnd);

  // Pobiera kontekst urządzenia danego okna.


  PIXELFORMATDESCRIPTOR pfd = {

    sizeof(PIXELFORMATDESCRIPTOR)  // Rozmiar struktury

    , 1                            // Wersja struktury

    , PFD_DRAW_TO_WINDOW           // Rysowanie w oknie (nie w bitmapie)

    | PFD_SUPPORT_OPENGL           // Obsługa wywołań OpenGL w tym oknie

    | PFD_DOUBLEBUFFER             // Tryb podwójnego buforowania

    , PFD_TYPE_RGBA                // Tryb kolorów RGB

    , 24                           // Ilość bitów na kolor

    , 0,0,0,0,0,0                  // Nie używane

    , 0,0                          // Nie używane

    , 0,0,0,0,0                    // Nie używane

    , 32                           // Rozmiar bufora głębokości (max. 32, 16-ok)

    , 0                            // Nie używane

    , 0                            // Nie używane

    , PFD_MAIN_PLANE               // Rysowanie na głównym planie

    , 0                            // Nie używane

    , 0,0                          // Nie używane

    };


  int index = ::ChoosePixelFormat(hdc_, &pfd);

  // Wybiera, dostępny dla danego kontekstu urządzenia,

  // format pikseli najbardziej zbliżony do informacji

  // otrzymanych w strukturze pfd.


  ::SetPixelFormat(hdc_, index, &pfd);

  // Ustawienie formatu pikseli dla kontekstu urządzenia.

  // Po ustaleniu nie można go już modyfikować.


  hrc_ = wglCreateContext(hdc_); if(!hrc_) throw "ERROR: wglCreateContext()";

  // Tworzy kontekst renderowania odpowiedni do rysowania

  // w podanym kontekście urządzenia Windows.


  if(wglMakeCurrent(hdc_, hrc_) == false) throw "ERROR: MakeCurrent()";

  // Wskazany kontekst renderowania czyni bieżącym dla danego wątku

  // i wiąże go z podanym kontekstem urządzenia.


  textures_[0] = tex_.LoadRAWTexture("earth.raw", 1024, 512, true);

  textures_[1] = tex_.LoadRAWTexture("sun.raw", 1024, 512, true);

  textures_[2] = tex_.LoadRAWTexture("moon.raw", 512, 256, true);

  textures_[3] = tex_.LoadRAWTexture("venus.raw", 1024, 512, true);

  textures_[4] = tex_.LoadRAWTexture("mercury.raw", 1024, 512, true);

  textures_[5] = tex_.LoadRAWTexture("mars.raw", 1000, 500, true);

  textures_[6] = tex_.LoadRAWTexture("jupiter.raw", 1024, 512, true);

}


//---------------------------------------------------------------------------


void OGL_Scene::Destroy(HWND hwnd)

{

  tex_.DeleteTexture(textures_[0]);

  tex_.DeleteTexture(textures_[1]);

  tex_.DeleteTexture(textures_[2]);

  tex_.DeleteTexture(textures_[3]);

  tex_.DeleteTexture(textures_[4]);

  tex_.DeleteTexture(textures_[5]);

  tex_.DeleteTexture(textures_[6]);

                                    

  if(hrc_==NULL) return;


  wglMakeCurrent(hdc_, NULL);

  // Zeruje bieżący kontekst renderowania.

    

  wglDeleteContext(hrc_);

  // Usuwa niepotrzebny już kontekst renderowania.


  ::ReleaseDC( hwnd, hdc_ );

  // Zwalnia kontekst urządzenia danego okna.


  hdc_ = NULL;

  hrc_ = NULL;

  // Unieważnia uchwyty.

}


//---------------------------------------------------------------------------


void OGL_Scene::Resize(float w, float h)

{

  w_ = (w==0) ? 1 : w;

  h_ = (h==0) ? 1 : h;

  // 1. Zabezpieczenie przed dzieleniem przez zero.



  glViewport(0, 0, (int)w_, (int)h_);

  // ----- Macierz okna -----

  // 1. Ustala obszar okna do wykorzystania przez OpenGL.

  // 2. Ustawienie widoku na wymiary okna.


  Redraw();

  // Wywołanie procedury renderującej.

}


//---------------------------------------------------------------------------


void OGL_Scene::Redraw()

{

  // 1. Przygotowanie...


  // Określenie koloru tła

  glClearColor(0.0, 0.0, 0.0, 1.0);


  // Wybór macierzy rzutowania

  glMatrixMode(GL_PROJECTION);

  // Ustawienie macierzy jednostkowej (!!!)

  glLoadIdentity();

  

  // Włącza ukrywanie niewidocznych powierzchni dla danego ustawienia 

  // obserwatora (kamery) 

  glEnable(GL_DEPTH_TEST);

  // Określenie, jakim porównaniem ma się posługiwać bufor głębokości, 

  // czyli które piksele mają być wyświetlone na ekranie

  glDepthFunc(GL_LEQUAL);

  // Określenie jakości interpolacji dla koloru i tekstur, czyli dokładności 

  // z jaką będą obliczane

  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


  // Ustala lub modyfikuje zakres bryły obcinania	

  gluPerspective(45.0, w_/h_, 0.1, 80.0);


  // Wybór macierzy modelowania

  glMatrixMode(GL_MODELVIEW);

  // Ustawienie macierzy jednostkowej (!!!)

  glLoadIdentity();


  // Wyczyszczenie bufora koloru oraz bufora głębokości (!!!)

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



  // 2. Rendering...


    // Przesunięcie obiektów od okna

	glTranslatef(0.0, 0.0, tr_);

	

	// obrót obiektów dookoła osi x

	glRotatef(rotx_, 1.0, 0.0, 0.0);

  	// obrót obiektów dookoła osi y

	glRotatef(roty_, 0.0, 1.0, 0.0);

	// obrót obiektów dookoła osi z

	glRotatef(rotz_, 0.0, 0.0, 1.0);


  // Włączenie/wyłączenie oświetlenia          

  if(light_) 

    {

      glEnable(GL_LIGHTING);

      l0_.Set_Pos(0.0, 0.0, 0.0, 1.0);

      l0_.Set_Amb(1.0, 1.0, 1.0, 1.0);

      l0_.Set_Dif(1.0, 1.0, 1.0, 1.0);

      l0_.Set_Spe(1.0, 1.0, 1.0, 1.0);

      l0_.On();               

      m0_.SetMaterial(std_material_);

      m0_.Material();

    }

    else

    {

      glDisable(GL_LIGHTING);

    }  

                          

    glEnable(GL_TEXTURE_2D);

    // wybór tekstury

    glBindTexture(GL_TEXTURE_2D, textures_[1]);

    sun_.Redraw();


    // wybór tekstury

    glBindTexture(GL_TEXTURE_2D, textures_[0]);

    earth_.Redraw();


    // wybór tekstury

    glBindTexture(GL_TEXTURE_2D, textures_[2]);

    moon_.Redraw();


    // wybór tekstury

    glBindTexture(GL_TEXTURE_2D, textures_[3]);

    venus_.Redraw();


    // wybór tekstury

    glBindTexture(GL_TEXTURE_2D, textures_[4]);

    mercury_.Redraw();



    glBindTexture(GL_TEXTURE_2D, textures_[5]);

    mars_.Redraw();     

    

    glBindTexture(GL_TEXTURE_2D, textures_[6]);

    jupiter_.Redraw();     		   			



  // 3. Zakończenie...


  // Wykonanie poleceń OpenGL oczekujących w kolejce

  glFlush();


  // Kopiuje zawartość tylnego bufora do wyspecyfikowanego hdc_

  ::SwapBuffers(hdc_);

}


//---------------------------------------------------------------------------


void OGL_Scene::Animate()

{

  static int a=0;     // kąt obrotu Ziemi dookoła własnej osi

  static int d=0;     // licznik dni dla Ziemi

  static int m=0;     // licznik dni dla Księżyca

  static int w=50;    // licznik dni dla Wenus

  static int me=30;   // licznik dni dla Merkurego

  static int ma=6;   // licznik dni dla Marsa

  static int ju=12;    // licznik startowy dni Jowisza


  if(animate_) 

  {

    a=a+10;

    if(a>=360) 

    {

      a=0;

      d++;

      m++;

      w++;

      me++;

      ma++;

      ju++;

    }

    // okres obrotu Ziemi dookoła Słońca około 360 dni

    if(d>=360)

    {

      d=0;

      m=0;

    }

    

    if ((d%30)==0) m=0;      // okres obrotu Księżyca dookoła Ziemi około 30 dni

    if ((w%225)==0) w=0;     // okres obrotu Wenus dookoła Słońca około 225 dni

    if ((me%88)==0) me=0;    // okres obrotu Merkurego dookoła Słońca około 88 dni

    if ((ma%687)==0) ma=0;   // okres obrotu Marsa dokola Slonca okolo 687 dni

    if ((ju%4332)==0) ju=0;   // okres obrotu Jowisza dokola slonca okolo 4 332 dni


    // okres obrotu Ziemi dookoła Słońca około 360 dni        

    float earthx = 5.0*sin(((float)d+((float)a/360.0))*PI/180.0);

    float earthy = 5.0*cos(((float)d+((float)a/360.0))*PI/180.0);

    earth_.TranslateToX(earthx);

    earth_.TranslateToY(earthy);

    // niewielki ruch oscylacyjny w kierunku osi z

    earth_.TranslateToZ(0.2*cos(((float)d+((float)a/360.0))*PI/180.0));

    // okres obrotu Ziemi dookoła własnej osi 1 dzień

    earth_.RotateToZ(a);


    // okres obrotu Księżyca dookoła Ziemi około 30 dni

    moon_.TranslateToX(earthx + 0.5*sin(((float)m*12.0+((float)a/360.0)*12.0)*PI/180.0));

    moon_.TranslateToY(earthy + 0.5*cos(((float)m*12.0+((float)a/360.0)*12.0)*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z

    moon_.TranslateToZ(0.04*cos(((float)m*12.0+((float)a/360.0)*12.0)*PI/180));


    // okres obrotu Wenus dookoła Słońca około 225 dni   

    venus_.TranslateToX(4.0*sin(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    venus_.TranslateToY(4.0*cos(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z    

    venus_.TranslateToZ(0.15*cos(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    // okres obrotu Wenus dookoła własnej osi około 243 dni

    venus_.RotateToZ(a * 1.0/243.0);


    // okres obrotu Merkurego dookoła Słońca około 88 dni

    mercury_.TranslateToX(3.0*sin(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    mercury_.TranslateToY(3.0*cos(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z    

    mercury_.TranslateToZ(0.1*cos(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    // okres obrotu Merkurego dookoła własnej osi około 59 dni

    mercury_.RotateToZ(a * 1.0/59.0);

    

    // Okres obiegu Marsa dookola slonca wynosi okolo 687 dni

    mars_.TranslateToX(6.5*sin(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

    mars_.TranslateToY(6.5*cos(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

     //ruch oscylacyjny marsa w kierunku osi z

    mars_.TranslateToZ(0.1*cos(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

     //okres obrotu Marsa dookola wlasnej osi wynosi okolo 24h

    mars_.RotateToZ(a*1.0/1.02);


        // Okres obiegu Jowisza dookola slonca wynosi okolo 4 332 dni

    jupiter_.TranslateToX(8.5*sin(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

    jupiter_.TranslateToY(8.5*cos(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

      // ruch oscylacyjny jowisza w kierunku osi z

    jupiter_.TranslateToZ(0.1*cos(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

      // okres obrotu Jowisza dookola wlasnej osi wynosi okolo 10h

    jupiter_.RotateToZ(a*1.0/0.4);

  }

  Redraw();

}

scene.h


#ifndef OGL_SCENE

#define OGL_SCENE


#include "ogl_light.h"

#include "ogl_material.h"

#include "ogl_texture.h"

#include "ogl_quadric.h"


#define PI 3.14


class OGL_Scene

{

public:


  OGL_Scene();

  // Default constructor


  void Create(HWND hwnd);

  // Application should call this method when

  // window is created


  void Destroy(HWND hwnd);

  // Application should call this method when

  // window is destroyed


  void Resize(float w, float h);

  // Application should call this method when

  // window size is changed


  void Redraw();

  // Application should call this method when

  // scene needs to be redrawn


  float tr_;

  float rotx_;

  float roty_;

  float rotz_;

  bool animate_;

  

  void LightOnOff() { light_ = !light_; }

  unsigned int std_material_;   

  void Animate();

  


private:


  HDC   hdc_;

  HGLRC hrc_;


  float w_;

  float h_;

  

  bool light_;

  OGL_Light l0_;

  OGL_Material m0_;

  unsigned int textures_[7];

  OGL_Texture tex_;

  OGL_Quadric earth_;

  OGL_Quadric sun_;

  OGL_Quadric moon_;

  OGL_Quadric venus_;  

  OGL_Quadric mercury_;

  OGL_Quadric mars_;

  OGL_Quadric jupiter_;

};


//---------------------------------------------------------------------------


#endif


Also full code with textures :
Download solarsystem.rar from Sendspace.com - send big files the easy way

Edited by notes, 09 January 2012 - 12:11 PM.

Remebre about KISS & DRY

#2
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Nevemind, seems that Great Dev c++ don't cope with large project's, I had to rebuild whole project then compile it. Program is working fine now. Anyways, thanks.
Remebre about KISS & DRY

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
Glad you got it working! Although Dev C++ isn't great. :P

#4
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Don't tell... I have another project in MS Visual c# :/ I have never worked with it and then all of a sudden i have to write whole project in this IDE...
Remebre about KISS & DRY

#5
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
I have another problem, I want to send msg when user clicks on planet. The code is working only when quadric is not moving ( in this case sun )
Look at code :

Main : api.cpp

#include <windows.h>

#include "ogl_scene.h"

#include <stdio.h>


//----------------------------------------------------------------------


OGL_Scene scene_;

int sleep = 1;


//----------------------------------------------------------------------


long pascal Window_Procedure (HWND h, unsigned int m, unsigned int w, long p);


//----------------------------------------------------------------------


int pascal WinMain (HINSTANCE hinst, HINSTANCE, char*, int)

{

  WNDCLASS wc;

  wc.hInstance = hinst;

  wc.lpszClassName = "WINDOW";

  wc.lpfnWndProc = Window_Procedure;

  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

  wc.hIcon = NULL;

  wc.hCursor = LoadCursor(NULL, IDC_ARROW);

  wc.lpszMenuName = NULL;

  wc.cbClsExtra = 0;

  wc.cbWndExtra = 0;

  wc.hbrBackground = 0;

  if(!::RegisterClass(&wc)) return 0;


  HWND hwnd = ::CreateWindow(

      "WINDOW"                    // Registered class name

    , "Solar System App v 1.00b"   // Window title

    , WS_OVERLAPPEDWINDOW         // Window style

    | WS_VISIBLE

    , 100                         // X position

    , 100                         // Y position

    , 1024                         // width

    , 720                         // height

    , NULL                        // Parent window handler

    , NULL                        // id or menu

    , hinst                       // Application instance handler

    , NULL                        // No window creation data

    );


  MSG msg;

  while(true)

  {

    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

    {

      if(msg.message==WM_QUIT) break;

      ::TranslateMessage(&msg);

      ::DispatchMessage(&msg);

    }

    else

    {

      scene_.Animate();

      Sleep(sleep);

    }

  }

  return msg.wParam;

}


//----------------------------------------------------------------------


long pascal Window_Procedure (HWND hwnd, unsigned int m, unsigned int w, long p)

{

  switch(m)

  {    

    case WM_NCDESTROY:

      ::PostQuitMessage(0);

      break;


    case WM_DESTROY:

      scene_.Destroy(hwnd);

      break;


    case WM_CREATE:

      scene_.Create(hwnd);

      break;


    case WM_SIZE:

      scene_.Resize(LOWORD(p),HIWORD(p));

      break;


    case WM_PAINT: {

      PAINTSTRUCT ps;

      HDC dc = ::BeginPaint(hwnd, &ps);

      scene_.Redraw();

      ::EndPaint(hwnd, &ps);

      break;

    }


    case WM_LBUTTONDOWN: //Clicking method here

      scene_.Select(LOWORD(p),HIWORD(p));

      break;  


    case WM_KEYDOWN:

    {

      //bool end_Universe = false;

      char buff [255];

      sprintf(buff,"kod znaku = %d",w);

      SendMessage(hwnd,WM_SETTEXT,0,(LPARAM)buff);

      switch(w)

      {

        case 27: // Esc

          SendMessage(hwnd,WM_CLOSE,0,0);

          break;

        case 32: // space

          scene_.animate_=!scene_.animate_;

          break;

        case 35: // End

          if (scene_.tr_>(-40))

             scene_.tr_-=0.5;    

          else 

              MessageBox(NULL,"Reached end of universe!\nYou can't go any further.","Error!",MB_OK|MB_ICONSTOP);      

          break;

        case 36: // Home

          if (scene_.tr_<35)

             scene_.tr_+=0.5;  

          else

              MessageBox(NULL,"Reached end of universe!\nYou can't go any further.","Error!",MB_OK|MB_ICONSTOP);        

          break;

        case 37: // left

          scene_.roty_-=1.0;

          break;

        case 39: // right

          scene_.roty_+=1.0;

          break;

        case 38: // up

          scene_.rotx_-=1.0;

          break;

        case 40: // down

          scene_.rotx_+=1.0;

          break;

        case 90: // z

          scene_.rotz_-=1.0;

          break;

        case 88: // x

          scene_.rotz_+=1.0;

          break; 

        case 76: // l

          scene_.LightOnOff();

          break; 

        case 187: // +

          if(sleep<8000) sleep++;

          break;

        case 189: // -

          if(sleep>0) sleep--;

          break;

      }         

    }   

  }

  return ::DefWindowProc(hwnd, m, w, p);

}


//----------------------------------------------------------------------





ogl_scene.h

#ifndef OGL_SCENE

#define OGL_SCENE


#include "ogl_light.h"

#include "ogl_material.h"

#include "ogl_texture.h"

#include "ogl_quadric.h"

#include <string>

#define PI 3.14


#define SELECT_TOL 4             //Pixel precision

#define SELECT_BUFFER_SIZE 16    // Select Buffor size


//Names of Quadrics

#define SUN_NAME 1                

#define EARTH_NAME 2                

#define VENUS_NAME 3

#define MERCURY_NAME 4

#define MARS_NAME 5

#define JUPITER_NAME 6

#define SATURN_NAME 7

#define URANUS_NAME 8

#define PLUTO_NAME 9 


class OGL_Scene

{

public:


  OGL_Scene();

  // Default constructor


  void Create(HWND hwnd);

  // Application should call this method when

  // window is created


  void Destroy(HWND hwnd);

  // Application should call this method when

  // window is destroyed


  void Resize(float w, float h);

  // Application should call this method when

  // window size is changed


  void Redraw();

  // Application should call this method when

  // scene needs to be redrawn

  

  void Select(int x, int y);

 


  float tr_;

  float rotx_;

  float roty_;

  float rotz_;

  bool animate_;

  

  void LightOnOff() { light_ = !light_; }

  unsigned int std_material_;   

  void Animate();

  


private:


  HDC   hdc_;

  HGLRC hrc_;


  float w_;

  float h_;


  bool light_;

  OGL_Light l0_;

  OGL_Material m0_;

  unsigned int textures_[12];

  OGL_Texture tex_;

  OGL_Quadric earth_;

  OGL_Quadric sun_;

  OGL_Quadric moon_;

  OGL_Quadric venus_;  

  OGL_Quadric mercury_;

  OGL_Quadric mars_;

  OGL_Quadric jupiter_;

  OGL_Quadric saturn_;

  OGL_Quadric uranus_;

  OGL_Quadric pluto_;

  OGL_Quadric back_;

  OGL_Quadric sat_ring1_, sat_ring2_, sat_ring3_;

  bool earth_sel, sun_sel, venus_sel, mercury_sel,

       mars_sel, jupiter_sel, saturn_sel, uranus_sel, pluto_sel;


   void LoadText(char *planet_name[]); // Function to load text about selected planet

};


//---------------------------------------------------------------------------


#endif



The Select function is at the bottom I think Animate has to do something with it ;s

ogl_scene.cpp
//---------------------------------------------------------------------------


#include <windows.h>

#include "ogl_scene.h"

#include <gl\gl.h>

#include <gl\glu.h> 

#include <math.h>

#include <string>

#include <stdio.h>


//---------------------------------------------------------------------------


OGL_Scene::OGL_Scene()

  : hdc_(NULL)

  , hrc_(NULL)

  , w_(1)

  , h_(1)

  , rotx_(-53.0)    

  , roty_(12.0)

  , rotz_(23.0)

  , tr_(-35.0)

  , light_(true)

  , l0_(0)

  , std_material_(9)

  , earth_(0.134, 100, 100, 2, true)

  , sun_(2.0, 100, 100, 1, false)

  , moon_(0.02, 30, 30, 0, true)

  , venus_(0.112, 30, 30, 3, true)

  , mercury_(0.048, 30, 30, 4, true)

  , mars_(0.064,30,30,5,true )

  , jupiter_(0.73,100,100,6,true)

  , saturn_(0.442, 100, 100, 7, true)

  , uranus_(0.332,100,100,8,true)

  , pluto_(0.298,100,100,9,true)

  , back_(40,9000,9000,0,true)

  , sat_ring1_(0.56,0.74,0,60,60,0,true,1)

  , sat_ring2_(0.77,0.82,0,60,60,0,true,1)

  , sat_ring3_(0.91,0.94,0,60,60,0,true,1)

  , animate_(true)

{// TODO : 


earth_sel = false; sun_sel= false; venus_sel=false; mercury_sel=false;

mars_sel=false; jupiter_sel=false; saturn_sel=false; uranus_sel=false; pluto_sel=false;

}


//---------------------------------------------------------------------------


void OGL_Scene::Create(HWND hwnd)

{

  hdc_ = ::GetDC(hwnd);

  // Pobiera kontekst urządzenia danego okna.


  PIXELFORMATDESCRIPTOR pfd = {

    sizeof(PIXELFORMATDESCRIPTOR)  // Rozmiar struktury

    , 1                            // Wersja struktury

    , PFD_DRAW_TO_WINDOW           // Rysowanie w oknie (nie w bitmapie)

    | PFD_SUPPORT_OPENGL           // Obsługa wywołań OpenGL w tym oknie

    | PFD_DOUBLEBUFFER             // Tryb podwójnego buforowania

    , PFD_TYPE_RGBA                // Tryb kolorów RGB

    , 24                           // Ilość bitów na kolor

    , 0,0,0,0,0,0                  // Nie używane

    , 0,0                          // Nie używane

    , 0,0,0,0,0                    // Nie używane

    , 32                           // Rozmiar bufora głębokości (max. 32, 16-ok)

    , 0                            // Nie używane

    , 0                            // Nie używane

    , PFD_MAIN_PLANE               // Rysowanie na głównym planie

    , 0                            // Nie używane

    , 0,0                          // Nie używane

    };


  int index = ::ChoosePixelFormat(hdc_, &pfd);

  


  ::SetPixelFormat(hdc_, index, &pfd);

 


  hrc_ = wglCreateContext(hdc_); if(!hrc_) throw "ERROR: wglCreateContext()";

 


  if(wglMakeCurrent(hdc_, hrc_) == false) throw "ERROR: MakeCurrent()";

  


  textures_[0] = tex_.LoadRAWTexture("textures/earth.raw", 1024, 512, true);

  textures_[1] = tex_.LoadRAWTexture("textures/sun.raw", 1024, 512, true);

  textures_[2] = tex_.LoadRAWTexture("textures/moon.raw", 512, 256, true);

  textures_[3] = tex_.LoadRAWTexture("textures/venus.raw", 1024, 512, true);

  textures_[4] = tex_.LoadRAWTexture("textures/mercury.raw", 1024, 512, true);

  textures_[5] = tex_.LoadRAWTexture("textures/mars.raw", 1000, 500, true);

  textures_[6] = tex_.LoadRAWTexture("textures/jupiter.raw", 1024, 512, true);

  textures_[7] = tex_.LoadRAWTexture("textures/saturn.raw", 1800, 900, true);

  textures_[8] = tex_.LoadRAWTexture("textures/uranus.raw",1024,512,true);

  textures_[9] = tex_.LoadRAWTexture("textures/pluto.raw",2000,1000,true);

  textures_[10] = tex_.LoadRAWTexture("textures/bg1_stars.raw",5000,3750,true);

  textures_[11] = tex_.LoadRAWTexture("textures/saturnring.raw",915,64,true);


  MessageBox(NULL,"Welcome to solar system aplication!","Welcome",MB_OK|MB_ICONINFORMATION);

  

  

}


//---------------------------------------------------------------------------


void OGL_Scene::Destroy(HWND hwnd)

{

  tex_.DeleteTexture(textures_[0]);

  tex_.DeleteTexture(textures_[1]);

  tex_.DeleteTexture(textures_[2]);

  tex_.DeleteTexture(textures_[3]);

  tex_.DeleteTexture(textures_[4]);

  tex_.DeleteTexture(textures_[5]);

  tex_.DeleteTexture(textures_[6]);

  tex_.DeleteTexture(textures_[7]);

  tex_.DeleteTexture(textures_[8]);

  tex_.DeleteTexture(textures_[9]);

  tex_.DeleteTexture(textures_[10]);

  tex_.DeleteTexture(textures_[11]);


                                    

  if(hrc_==NULL) return;


  wglMakeCurrent(hdc_, NULL);

  

    

  wglDeleteContext(hrc_);

 


  ::ReleaseDC( hwnd, hdc_ );

  


  hdc_ = NULL;

  hrc_ = NULL;

  

}


//---------------------------------------------------------------------------


void OGL_Scene::Resize(float w, float h)

{

  w_ = (w==0) ? 1 : w;

  h_ = (h==0) ? 1 : h;

  



  glViewport(0, 0, (int)w_, (int)h_);

  


  Redraw();

  

}


//---------------------------------------------------------------------------


void OGL_Scene::Redraw()

{

  // 1. Przygotowanie...


  

  glClearColor(0.0, 0.0, 0.0, 1.0);


  

  glMatrixMode(GL_PROJECTION);

  

	

	

  //  (!!!)

  glLoadIdentity();

  

 

  glEnable(GL_DEPTH_TEST);

  

  glDepthFunc(GL_LEQUAL);

  

  

  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


  

  gluPerspective(45.0, w_/h_, 0.1, 80.0);


  

  glMatrixMode(GL_MODELVIEW);

  //  (!!!)

  glLoadIdentity();


  //  (!!!)

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



  // 2. Rendering...


    

	glTranslatef(0.0, 0.0, tr_);

	

	

	glRotatef(rotx_, 1.0, 0.0, 0.0);

  	

	glRotatef(roty_, 0.0, 1.0, 0.0);

	

	glRotatef(rotz_, 0.0, 0.0, 1.0);


         

  if(light_) 

    {

      glEnable(GL_LIGHTING);

      l0_.Set_Pos(0.0, 0.0, 0.0, 1.0);

      l0_.Set_Amb(1.0, 1.0, 1.0, 1.0);

      l0_.Set_Dif(1.0, 1.0, 1.0, 1.0);

      l0_.Set_Spe(1.0, 1.0, 1.0, 1.0);

      l0_.On();               

      m0_.SetMaterial(std_material_);

      m0_.Material();

    }

    else

    {

      glDisable(GL_LIGHTING);

    }  

                          

    glEnable(GL_TEXTURE_2D);

    

    glBindTexture(GL_TEXTURE_2D, textures_[1]);

    sun_.Redraw();

    

    glBindTexture(GL_TEXTURE_2D, textures_[10]);

    back_.Redraw();

    

    glBindTexture(GL_TEXTURE_2D, textures_[0]);

    earth_.Redraw();


    

    glBindTexture(GL_TEXTURE_2D, textures_[2]);

    moon_.Redraw();


    

    glBindTexture(GL_TEXTURE_2D, textures_[3]);

    venus_.Redraw();


    

    glBindTexture(GL_TEXTURE_2D, textures_[4]);

    mercury_.Redraw();



    glBindTexture(GL_TEXTURE_2D, textures_[5]);

    mars_.Redraw();     

    

    glBindTexture(GL_TEXTURE_2D, textures_[6]);

    jupiter_.Redraw();  


    glBindTexture(GL_TEXTURE_2D, textures_[7]);

    saturn_.Redraw();     		   			

    

    glBindTexture(GL_TEXTURE_2D, textures_[11]);

    sat_ring1_.Redraw(); 

    sat_ring2_.Redraw();

    sat_ring3_.Redraw();  

   		   			

    glBindTexture(GL_TEXTURE_2D, textures_[8]);

    uranus_.Redraw();


    glBindTexture(GL_TEXTURE_2D, textures_[9]);

    pluto_.Redraw();


  // 3. Zakończenie...


  

  glFlush();


 

  ::SwapBuffers(hdc_);

}


//---------------------------------------------------------------------------


void OGL_Scene::Animate()

{

  static int a=0;     // kąt obrotu Ziemi dookoła własnej osi

  static int d=180;     // licznik dni dla Ziemi

  static int m=0;     // licznik dni dla Księżyca

  static int w=210;    // licznik dni dla Wenus

  static int me=0;   // licznik dni dla Merkurego

  static int ma=600;   // licznik dni dla Marsa

  static int ju=2300;    // licznik startowy dni Jowisza

  static int sa = 10000;

  static int ur = 0;

  static int pl = 20012;


  if(animate_) 

  {

    a=a+10;

    if(a>=360) 

    {

      a=0;

      d++;

      m++;

      w++;

      me++;

      ma++;

      ju++;

      sa++;

      ur++;

      pl++;

    }

    // okres obrotu Ziemi dookoła Słońca około 360 dni

    if(d>=360)

    {

      d=0;

      m=0;

    }

    

    if ((d%30)==0) m=0;      // okres obrotu Księżyca dookoła Ziemi około 30 dni

    if ((w%225)==0) w=0;     // okres obrotu Wenus dookoła Słońca około 225 dni

    if ((me%88)==0) me=0;    // okres obrotu Merkurego dookoła Słońca około 88 dni

    if ((ma%687)==0) ma=0;   // okres obrotu Marsa dokola Slonca okolo 687 dni

    if ((ju%4332)==0) ju=0;   // okres obrotu Jowisza dokola slonca okolo 4 332 dni

    if ((sa%10755)==0) sa =0; // Okres obrotu Saturana wynosi okolo 10 755 dni

    if ((ur%30706)==0) ur = 0; // Okres obiegu Uranu dokola slonca wynosi okolo 30 706 dni

    if ((pl%60222)==0) pl=0; // Okres obiegu Plutona dookola slonca wynosi okolo 60 222 dni

    // okres obrotu Ziemi dookoła Słońca około 360 dni        

    float earthx = 6.0*sin(((float)d+((float)a/360.0))*PI/180.0);

    float earthy = 6.0*cos(((float)d+((float)a/360.0))*PI/180.0);

    earth_.TranslateToX(earthx);

    earth_.TranslateToY(earthy);

    // niewielki ruch oscylacyjny w kierunku osi z

    earth_.TranslateToZ(0.2*cos(((float)d+((float)a/360.0))*PI/180.0));

    // okres obrotu Ziemi dookoła własnej osi 1 dzień

    earth_.RotateToZ(a);


    // okres obrotu Księżyca dookoła Ziemi około 30 dni

    moon_.TranslateToX(earthx + 0.5*sin(((float)m*12.0+((float)a/360.0)*12.0)*PI/180.0));

    moon_.TranslateToY(earthy + 0.5*cos(((float)m*12.0+((float)a/360.0)*12.0)*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z

    moon_.TranslateToZ(0.04*cos(((float)m*12.0+((float)a/360.0)*12.0)*PI/180));


    // okres obrotu Wenus dookoła Słońca około 225 dni   

    venus_.TranslateToX(4.5*sin(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    venus_.TranslateToY(4.5*cos(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z    

    venus_.TranslateToZ(0.15*cos(((float)w*(360.0/225.0)+((float)a/225.0))*PI/180.0));

    // okres obrotu Wenus dookoła własnej osi około 243 dni

    venus_.RotateToZ(-(a * 1.0/243.0));


    // okres obrotu Merkurego dookoła Słońca około 88 dni

    mercury_.TranslateToX(3.5*sin(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    mercury_.TranslateToY(3.5*cos(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    // niewielki ruch oscylacyjny w kierunku osi z    

    mercury_.TranslateToZ(0.1*cos(((float)me*(360.0/88.0)+((float)a/88.0))*PI/180.0));

    // okres obrotu Merkurego dookoła własnej osi około 59 dni

    mercury_.RotateToZ(a * 1.0/59.0);

    

    // Okres obiegu Marsa dookola slonca wynosi okolo 687 dni

    

    mars_.TranslateToX(7.0*sin(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

    mars_.TranslateToY(7.0*cos(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

     //ruch oscylacyjny marsa w kierunku osi z

    mars_.TranslateToZ(0.1*cos(((float)ma*(360.0/687.0)+((float)a/687.0))*PI/180.0));

     //okres obrotu Marsa dookola wlasnej osi wynosi okolo 24h

    mars_.RotateToZ(a*1.0/1.02);


        // Okres obiegu Jowisza dookola slonca wynosi okolo 4 332 dni

    jupiter_.TranslateToX(10.6*sin(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

    jupiter_.TranslateToY(10.6*cos(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

      // ruch oscylacyjny jowisza w kierunku osi z

    jupiter_.TranslateToZ(0.1*cos(((float)ju*(360.0/4332.0)+((float)a/4332.0))*PI/180.0));

      // okres obrotu Jowisza dookola wlasnej osi wynosi okolo 10h

    jupiter_.RotateToZ(a*1.0/0.4);


            // Okres obiegu Saturna dookola slonca wynosi okolo 10 755 dni

    float saturnX=14.6*sin(((float)sa*(360.0/10755.0)+((float)a/10755.0))*PI/180.0);

    float saturnY=14.6*cos(((float)sa*(360.0/10755.0)+((float)a/10755.0))*PI/180.0);

    float saturnZ=0.1*cos(((float)sa*(360.0/10755.0)+((float)a/10755.0))*PI/180.0);

    saturn_.TranslateToX(saturnX);

    saturn_.TranslateToY(saturnY);

      // ruch oscylacyjny saturna w kierunku osi z

    saturn_.TranslateToZ(saturnZ);

      // okres obrotu saturna dookola wlasnej osi wynosi okolo 11h

    saturn_.RotateToZ(a*1.0/0.3);

    //-----------------------

    sat_ring1_.TranslateToX(saturnX);

    sat_ring1_.TranslateToY(saturnY);

    sat_ring1_.TranslateToZ(saturnZ);

    //sat_ring1_.RotateToZ(a*1.0/10.9);

    sat_ring2_.TranslateToX(saturnX);

    sat_ring2_.TranslateToY(saturnY);

    sat_ring2_.TranslateToZ(saturnZ);

    //sat_ring2_.RotateToZ(a*1.0/10.9);

    sat_ring3_.TranslateToX(saturnX);

    sat_ring3_.TranslateToY(saturnY);

    sat_ring3_.TranslateToZ(saturnZ);

    //sat_ring3_.RotateToZ(a*1.0/10.9);


                // Okres obiegu Uranu dookola slonca wynosi okolo 30 706 dni

    uranus_.TranslateToX(16.0*sin(((float)ur*(360.0/30706.0)+((float)a/30706.0))*PI/180.0));

    uranus_.TranslateToY(16.0*cos(((float)ur*(360.0/30706.0)+((float)a/30706.0))*PI/180.0));

      // ruch oscylacyjny uranu w kierunku osi z

    uranus_.TranslateToZ(0.1*cos(((float)ur*(360.0/30706.0)+((float)a/30706.0))*PI/180.0));

      // okres obrotu uranu dookola wlasnej osi wynosi okolo 17h

    uranus_.RotateToZ(a*1.0/0.2);


                    // Okres obiegu Plutona dookola slonca wynosi okolo 60 222 dni

    pluto_.TranslateToX(18.1*sin(((float)pl*(360.0/60222.0)+((float)a/60222.0))*PI/180.0));

    pluto_.TranslateToY(18.1*cos(((float)pl*(360.0/60222.0)+((float)a/60222.0))*PI/180.0));

      // ruch oscylacyjny Plutona w kierunku osi z

    pluto_.TranslateToZ(0.1*cos(((float)pl*(360.0/60222.0)+((float)a/60222.0))*PI/180.0));

      // okres obrotu Plutona dookola wlasnej osi wynosi okolo 16h

    pluto_.RotateToZ(a*1.0/0.18);

  }

  Redraw();

}


void OGL_Scene :: Select (int x, int y) {


    unsigned int selectBuf[SELECT_BUFFER_SIZE];

    int hits;

    int viewport[4];


    

    glGetIntegerv (GL_VIEWPORT, viewport);

    

    glSelectBuffer (SELECT_BUFFER_SIZE, selectBuf);


    

    glRenderMode(GL_SELECT);

  

    glInitNames();

 

    glPushName(0);


    

    glMatrixMode(GL_PROJECTION);

 

    glPushMatrix();


        

        glLoadIdentity();

        gluPickMatrix(x, y, SELECT_TOL, SELECT_TOL, viewport);

        gluPerspective(45.0, (GLfloat)w_/(GLfloat)h_, 0.1, 80.0);

        

		glMatrixMode(GL_MODELVIEW);

        

        glLoadIdentity();


        

    	glTranslatef(0.0, 0.0, tr_);

    	

    	glRotatef(rotx_, 1.0, 0.0, 0.0);

      	

    	glRotatef(roty_, 0.0, 1.0, 0.0);

    	

    	glRotatef(rotz_, 0.0, 0.0, 1.0);

	    

        earth_.Redraw();

        sun_.Redraw();

        venus_.Redraw();


    

    glPopMatrix();

    

    glFlush();


   

    hits = glRenderMode(GL_RENDER);


    earth_sel= false;

    sun_sel= false;

    venus_sel =false; 

    

  

   // For chosen object in buffer there are assigned 4 bytes

   // name of chosen object is returned in fourth byte

    if(hits > 0)

    {

      for (int i = 0; i < hits; i++)

      {

        if(selectBuf[i * 4 + 3] == 1)    // Object 1 was selected

        {

          sun_sel=true;

          

        } 

        if(selectBuf[i * 4 + 3] == 2)    // Object  2 was selected

        {

           earth_sel=true;

        }

        if(selectBuf[i * 4 + 3] == 3)    // Object 3

        {

          venus_sel=true; 

        }

      }

     if (sun_sel) {

       MessageBox(NULL,"Reached end of universe!\nYou can't go any further.","Error!",MB_OK|MB_ICONSTOP);

           sun_sel=false;

       }

     if (earth_sel) {

       MessageBox(NULL,"EARTHHHH\n","Error!",MB_OK|MB_ICONSTOP);

           earth_sel=false;

       }

     if (venus_sel) {

       MessageBox(NULL,"Reached end of universe!\nYou can't go any further.","Error!",MB_OK|MB_ICONSTOP);

           venus_sel=false;

       }

    }    




}


When I click on the sun, everything is working fine, when I click any other(earth or venus) planet nothing happens

If any one want's whole program please write. I have more classes but I think they are irrelevant except constructor in Quadric.h looks like :

OGL_Quadric(float radius, int slices, int stacks, int name, bool orientation);

Its overloaded for rings but as I said its seems to be irrelevant.

Can any one help me with this one ? Any suggestions even how you would make it would be good. I'm stuck .

Thanks i advance.
Remebre about KISS & DRY




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users