Hello,
I’ve tried a lot but i still can’t handle it. In one moment after opening windows, i get another windows which tells: “program has stopped working”. I am programming in CodeBlocks. Here is code:
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glu.h>
#include <GL/glut.h>
struct punkt {
float x, y, z;
};
struct brylaOtaczajaca {
punkt max;
punkt min;
};
typedef float wSkalar;
class Wektor {
/*private:
wSkalar x;
wSkalar y;
wSkalar z;
*/
public:
wSkalar x;
wSkalar y;
wSkalar z;
//konstruktory
Wektor ();
Wektor (wSkalar _x, wSkalar _y, wSkalar _z);
Wektor (const Wektor &v);
wSkalar X();
wSkalar Y();
wSkalar Z();
//realizacja podstawowych dzia³añ na wektorach:
//podstawienie wektora
const Wektor &operator=(const Wektor &v);
//równoœæ wektorów
const bool operator==(const Wektor &v) const;
//nierównoœæ wektorów
const bool operator!=(const Wektor &v) const;
//dodawanie wektorów
const Wektor operator+(const Wektor &v) const;
//dodawanie wektorów ncch einmal
const Wektor operator+() const;
//i jeszcze jeden sposób na dodanie
const Wektor& operator+=(const Wektor &v);
//odejmowanie
const Wektor operator-() const;
const Wektor operator-(const Wektor &v) const;
const Wektor& operator-=(const Wektor &v);
//mno¿enie przez skalar
const Wektor& operator*=(const wSkalar &s);
const Wektor operator*(const wSkalar &s) const;
//dzielenie przez skalar
const Wektor &operator/=(const wSkalar &s);
const Wektor operator/(const wSkalar &s) const;
//iloczyn wektorowy
const Wektor IlWektorow (const Wektor &v) const;
const Wektor operator^(const Wektor &v) const;
//iloczyn skalarny
const wSkalar IlSklarny (const Wektor &v) const;
const wSkalar operator%(const Wektor &v) const;
//d³ugoœæ wektora
const wSkalar Dlugosc() const;
//wektor jednostkowy
const Wektor jednostWektor() const;
//normalizacja wektora
void Normalizacja();
//operator modu³u (d³ugoœci) wektora - przyda mi sie to?
const wSkalar operator!() const;
//zmienia d³ugoœæ wektora
const Wektor operator | (const wSkalar dl) const;
const Wektor& operator |= (const float dl);
//zwraca k¹t, który tworz¹ wektory
const float inline Kat(const Wektor &v) const;
//tworzy odbicie wektora wzglêdem powierzchni zdefiniowanej przez normaln¹
const Wektor inline Odbicie(const Wektor &N) const;
};
class obiektWalec {
public:
float masa;
Wektor predkosc;
Wektor przyspieszenie;
Wektor polozenie;
brylaOtaczajaca Bryla;
};
class Rownia {
public:
float w[6][3];
float nachylenie;
Rownia();
void Rysuj(GLfloat rx, GLfloat ry);
void Laduj();
};
class Okno {
private:
int k_wysokosc;
int k_szerokosc;
int k_poz_x;
int k_poz_y;
public:
Okno (int u_wys, int u_szer, int u_x, int u_y);
void petla (int argc, char *argv[]);
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
GLuint listaW;
Rownia::Rownia() : nachylenie(45.0) {}
void Rownia::Laduj() {
GLfloat w[6][3];
/*GLfloat tmp[6][3] = {{0.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 1.0, 1.0},
{1.0, 0.0, 1.0},
{0.0, 0.0, 1.0}
};
w = tmp;*/
printf("Laduje wierzcholki walca
");
w[0][0] = 0.0;
w[0][1] = 0.0;
w[0][2] = 0.0;
w[1][0] = 1.0;
w[1][1] = 0.0;
w[1][2] = 0.0;
w[2][0] = 0.0;
w[2][1] = 1.0;
w[2][2] = 0.0;
w[3][0] = 0.0;
w[3][1] = 1.0;
w[3][2] = 1.0;
w[4][0] = 1.0;
w[4][1] = 0.0;
w[4][2] = 1.0;
w[5][0] = 0.0;
w[5][1] = 0.0;
w[5][2] = 1.0;
printf("I zaladowane
");
}
void Rownia::Rysuj(GLfloat rx, GLfloat ry) {
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef (rx,1.0,0.0,0.0);
glRotatef (ry,0.0,1.0,0.0);
glScalef(6.0, 6.0, 6.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(w[0][0], w[0][1], w[0][2]);
glVertex3f(w[1][0], w[1][1], w[1][2]);
glVertex3f(w[2][0], w[2][1], w[2][2]);
/*//lewa strona
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);*/
glColor3f(0.0f, 1.0f, 0.0f);
//trójk¹ty tworz¹ce spad
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 1.0);
glColor3f(1.0f, 0.0f, 0.0f);
//prawa strona
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(1.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 1.0);
glColor3f(0.0f, 0.0f, 1.0f);
//ty³
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glEnd();
glPopMatrix();
}
Wektor::Wektor() : x(0.0), y(0.0), z(0.0) {}
Wektor::Wektor(wSkalar _x, wSkalar _y, wSkalar _z) : x(_x), y(_y), z(_z){}
Wektor::Wektor(const Wektor &v) : x(v.x), y(v.y), z(v.z) {}
wSkalar Wektor::X() { return x; }
wSkalar Wektor::Y() { return y; }
wSkalar Wektor::Z() { return z; }
const Wektor& Wektor::operator=(const Wektor &v) {
x = v.x;
y = v.y;
z = v.z;
return *this;
}
const bool Wektor::operator==(const Wektor &v) const{
return ((x == v.x) && (y == v.y) && (z == v.z));
}
const bool Wektor::operator!=(const Wektor &v) const {
return !(*this == v);
}
const Wektor Wektor::operator+(const Wektor &v) const {
return Wektor(x + v.x, y + v.y, z + v.z);
}
const Wektor Wektor::operator+() const {
return Wektor(*this);
}
const Wektor& Wektor::operator+=(const Wektor& v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
const Wektor Wektor::operator-(const Wektor &v) const {
return Wektor (x - v.x, y - v.y, z - v.z);
}
const Wektor Wektor::operator-() const {
return Wektor(-x, -y, -z);
}
const Wektor& Wektor::operator-=(const Wektor &v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
const Wektor& Wektor::operator*=(const wSkalar &s) {
x *= s;
y *= s;
z *= s;
return *this;
}
const Wektor& Wektor::operator/=(const wSkalar &s) {
//dla efektywnoœci - tak radz¹ w ksi¹¿ce
const float odwr = 1/s;
x *= odwr;
y *= odwr;
z *= odwr;
return *this;
}
const Wektor Wektor::operator*(const wSkalar &s) const {
return Wektor (x*s, y*s, z*s);
}
const Wektor Wektor::operator/(const wSkalar &s) const {
wSkalar w = 1/s;
return Wektor(x*w, y*w, z*w);
}
//zakrêcona funkcyjka
const Wektor Wektor::IlWektorow(const Wektor &v) const {
return Wektor(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
}
const wSkalar Wektor::IlSklarny(const Wektor &v) const {
return x*v.x + y*v.y + z*v.z;
}
const wSkalar Wektor::operator%(const Wektor &v) const {
return x*v.x + y*v.y + z*v.z;
}
const wSkalar Wektor::Dlugosc() const {
return (wSkalar)sqrt((double)(x*x + y*y + z*z));
}
const Wektor Wektor::jednostWektor() const {
return (*this) / Dlugosc();
}
void Wektor::Normalizacja() {
(*this) /= Dlugosc();
}
const wSkalar Wektor::operator!() const {
return sqrtf(x*x + y*y + z*z);
}
const Wektor Wektor::operator | (const wSkalar dl) const{
return *this * (dl / !(*this));
}
const Wektor& Wektor::operator |= (const float dl) {
return *this = *this | dl;
}
const float inline Wektor::Kat(const Wektor &v) const {
return acosf(*this % v);
}
//stanowczo zakrêcone!
const Wektor inline Wektor::Odbicie(const Wektor &N) const {
const Wektor wek(*this | 1); //normalizuje wektor
return (wek - N * 2.0 * (wek % N)) * !*this;
}
bool wek = true;
GLint k_szerokosc = 640;
GLint k_wysokosc = 480;
Rownia *r;
// k¹ty obrotu
GLfloat rx = 0.0;
GLfloat ry = 0.0;
//zmienne globalne do manipulacji mysz¹
int przycisk_myszy = GLUT_UP;
int mysz_x, mysz_y;
Okno::Okno(int u_wys, int u_szer, int u_x, int u_y) {
k_wysokosc = u_wys;
k_szerokosc = u_szer;
k_poz_x = u_x;
k_poz_y = u_y;
Rownia *r = new Rownia();
}
void TestWektorow() {
Wektor a(1.0, 2.0, 3.0);
Wektor b(2.0, 2.0, 2.0);
Wektor c(0.0, 0.0, 0.0);
printf("Oto wektor a: [ %f , %f , %f ]
", a.x, a.y, a.z);
printf("Oto wektor b: [ %f , %f , %f ]
", b.x, b.y, b.z);
c = a + b;
printf("a + b = [ %f , %f , %f ]
", c.x, c.y, c.z);
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glViewport(0,0,k_szerokosc,k_wysokosc);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)k_szerokosc/(GLfloat)k_wysokosc,10.0f,10000.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
r->Laduj();
}
// obs³uga przycisków myszki
void MouseButton (int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
// zapamiêtanie stanu lewego przycisku myszki
przycisk_myszy = state;
// zapamiêtanie po³o¿enia kursora myszki
if (state == GLUT_DOWN)
{
mysz_x = x;
mysz_y = y;
}
}
}
// obs³uga ruchu kursora myszki
void MouseMotion (int x, int y) {
if (przycisk_myszy == GLUT_DOWN){
rx += 0.001*(x-mysz_x);
ry += 0.001*(y-mysz_y);
}
}
// obs³uga klawiszy funkcyjnych i klawiszy kursora
void SpecialKeys (int klawisz, int x, int y)
{
switch (klawisz)
{
//klawisz
case GLUT_KEY_LEFT:
ry -= 1;
break;
// kursor w górê
case GLUT_KEY_UP:
rx -= 1;
break;
// kursor w prawo
case GLUT_KEY_RIGHT:
ry += 1;
break;
// kursor w dó³
case GLUT_KEY_DOWN:
rx += 1;
break;
}
//Reshape (glutGet (GLUT_WINDOW_WIDTH),glutGet (GLUT_WINDOW_HEIGHT));
}
void DisplayScene () {
/*const double t = glutGet(GLUT_ELAPSED_TIME) / 10000.0;
const double a = t*90.0;
*/
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -15.0f);
if (wek){
TestWektorow();
wek = false;
}
r->Rysuj(rx, ry);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void Reshape (int width, int height) {
k_szerokosc=width;
k_wysokosc=height;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,k_szerokosc,k_wysokosc);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
int szer = width;
int wys = height;
//co ja z tym pokrêci³am?
gluPerspective(45.0f,(GLfloat)k_szerokosc/(GLfloat)k_wysokosc,10.0f,10000.0f);
glutPostRedisplay ();
}
void Okno::petla (int argc, char *argv[])
{
//inicjalizacja biblioteki GLUT
glutInit(&argc, argv);
//rozmiary g³ównego okna programu
glutInitWindowSize(k_wysokosc, k_szerokosc);
//jego pozycja
glutInitWindowPosition(k_poz_x, k_poz_y);
//inicjalizacja bufora ramki
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//utworzenie g³ównego okna
glutCreateWindow("Fizyka");
//do³¹czenie funkcji generuj¹cej scenê
glutDisplayFunc (DisplayScene);
glutIdleFunc(DisplayScene);
glutReshapeFunc (Reshape);
// do³¹czenie funkcji obs³ugi klawiszy funkcyjnych i klawiszy kursora
glutSpecialFunc (SpecialKeys);
//obs³uga przycisków myszki
glutMouseFunc (MouseButton);
//obs³uga ruchu kursora myszki
glutMotionFunc (MouseMotion);
init();
printf("uruchomienie okna
");
glutMainLoop();
}
//////////
int main (int argc, char *argv[]) {
Okno *okno = new Okno (640, 480, 10, 10);
okno->petla(argc, argv);
return 0;
}