Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdlib.h>
- #if defined(__APPLE__)
- #include <OpenGL/gl.h>
- #include <OpenGL/glu.h>
- #include <GLUT/glut.h>
- #else
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
- #include <windows.h>
- #endif
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #endif
- #ifndef M_PI
- #define M_PI 3.14159265359
- #endif
- struct Vector {
- float x, y, z;
- Vector(float v = 0) : x(v), y(v), z(v) { }
- Vector(float x, float y, float z) : x(x), y(y), z(z) { }
- Vector operator+(const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }
- Vector operator-(const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); }
- friend Vector operator-(float f, const Vector& v) { return Vector(f) - v; }
- Vector operator*(const Vector& v) const { return Vector(x * v.x, y * v.y, z * v.z); }
- friend Vector operator*(float f, const Vector& v) { return v*f; }
- Vector operator/(const Vector& v) const { return Vector(x / v.x, y / v.y, z / v.z); }
- Vector& operator+=(const Vector& v) { x += v.x, y += v.y, z += v.z; return *this; }
- Vector& operator-=(const Vector& v) { x -= v.x, y -= v.y, z -= v.z; return *this; }
- Vector& operator*=(const Vector& v) { x *= v.x, y *= v.y, z *= v.z; return *this; }
- Vector& operator/=(const Vector& v) { x /= v.x, y /= v.y, z /= v.z; return *this; }
- Vector operator-() const { return Vector(-x, -y, -z); }
- float dot(const Vector& v) const { return x*v.x + y*v.y + z*v.z; }
- Vector cross(const Vector& v) const { return Vector(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }
- friend Vector cross(const Vector& a, const Vector& b) { return a.cross(b); }
- float length() const { return sqrt(x*x + y*y + z*z); }
- Vector normalize() const { float l = length(); if(l > 1e-3) { return (*this/l); } else { return Vector(); } }
- bool isNull() const { return length() < 1e-3; }
- };
- GLuint tex[6];
- void CreateTextures() {
- glGenTextures(6, tex);
- const char* ascii_textures[6] = {
- "........"
- "...**..."
- "....*..."
- "....*..."
- "....*..."
- "....*..."
- "...***.."
- "........",
- "........"
- "..***+.."
- "....+*.."
- "....+*.."
- "...+*+.."
- "..+*+..."
- "..****.."
- "........",
- "........"
- "..***+.."
- "....+*.."
- "....+*.."
- "...**+.."
- "....+*.."
- "..***+.."
- "........",
- "........"
- "....*..."
- "...+*..."
- "..+**..."
- "..+.*..."
- "..****.."
- "....*..."
- "........",
- "........"
- "..****.."
- "..*....."
- "..***+.."
- "....+*.."
- "....+*.."
- "..***+.."
- "........",
- "........"
- "........"
- "..++**.."
- "..*....."
- "..*+*+.."
- "..*..*.."
- "..+**+.."
- "........"
- };
- GLubyte texture_data[6][64][3];
- for(int t = 0; t < 6; t++) {
- for(int i = 0; i < 64; i++) {
- switch(ascii_textures[t][i]) {
- case '*':
- for(int j = 0; j < 3; j++) {
- texture_data[t][i][j] = 0;
- }
- break;
- case '+':
- for(int j = 0; j < 3; j++) {
- texture_data[t][i][j] = 127;
- }
- break;
- default:
- for(int j = 0; j < 3; j++) {
- texture_data[t][i][j] = 255;
- }
- break;
- }
- }
- glBindTexture(GL_TEXTURE_2D, tex[t]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_data[t]);
- }
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- void glVertex3f(const Vector& v) {
- glVertex3f(v.x, v.y, v.z);
- }
- void glQuad(const Vector& a, const Vector& b, const Vector& c, const Vector& d) {
- Vector normal = cross(b-a, c-a).normalize();
- glNormal3f(normal.x, normal.y, normal.z);
- int res = 8; // resolution
- for(int i = 0; i < res; i++) {
- for(int j = 0; j < res; j++) {
- glTexCoord2f(float(i)/res, float(j)/res);
- glVertex3f(a + i*(b-a)/res + j*(d-a)/res);
- glTexCoord2f(float(i+1)/res, float(j)/res);
- glVertex3f(a + (i+1)*(b-a)/res + j*(d-a)/res);
- glTexCoord2f(float(i+1)/res, float(j+1)/res);
- glVertex3f(a + (i+1)*(b-a)/res + (j+1)*(d-a)/res);
- glTexCoord2f(float(i)/res, float(j+1)/res);
- glVertex3f(a + i*(b-a)/res + (j+1)*(d-a)/res);
- }
- }
- }
- void drawCube(const Vector& size) {
- /* (E)-----(A)
- /| /|
- / | / |
- (F)-----(B) |
- | (H)---|-(D)
- | / | /
- |/ |/
- (G)-----(C) */
- Vector s = size / 2;
- Vector A(+s.x, +s.y, -s.z), B(+s.x, +s.y, +s.z), C(+s.x, -s.y, +s.z), D(+s.x, -s.y, -s.z),
- E(-s.x, +s.y, -s.z), F(-s.x, +s.y, +s.z), G(-s.x, -s.y, +s.z), H(-s.x, -s.y, -s.z);
- Vector vertices[6][4] = {
- {A, B, C, D}, {G, F, E, H}, {A, E, F, B},
- {D, C, G, H}, {B, F, G, C}, {E, A, D, H}
- };
- glEnable(GL_TEXTURE_2D);
- for(int i = 0; i < 6; i++) {
- glBindTexture(GL_TEXTURE_2D, tex[i]); // Ezt semmiképpen se rakd a glBegin - glEnd blokk közé
- glBegin(GL_QUADS); {
- glQuad(vertices[i][0], vertices[i][1], vertices[i][2], vertices[i][3]);
- } glEnd();
- }
- glBindTexture(GL_TEXTURE_2D, 0);
- glDisable(GL_TEXTURE_2D);
- }
- void onDisplay() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- CreateTextures();
- drawCube(2.0f);
- glutSwapBuffers();
- }
- void onIdle() {
- static bool first_call = true;
- if(first_call) {
- glutPostRedisplay();
- first_call = false;
- }
- }
- void onInitialization() {
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- gluPerspective(60, 1, 0.1, 10);
- glMatrixMode(GL_MODELVIEW);
- gluLookAt(-3, 2, -2, 0, 0, 0, 0, 1, 0);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- float a[4] = {0.3f, 0.3f, 0.3f, 1.0f};
- glLightfv(GL_LIGHT0, GL_AMBIENT, a);
- float p[4] = {-1.2f, 1.7f, -1.3f, 1};
- glLightfv(GL_LIGHT0, GL_POSITION, p);
- float p2[4] = {-1.2f, 1.7f, -1.3f, -1};
- glLightfv(GL_LIGHT1, GL_POSITION, p2);
- float c[4] = {1.0f, 1.0f, 1.0f, 1.0f};
- glLightfv(GL_LIGHT0, GL_DIFFUSE, c);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, c);
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
- glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.0f);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5f);
- glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.5f);
- glFrontFace(GL_CCW);
- glCullFace(GL_BACK);
- glEnable(GL_CULL_FACE);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
- void onKeyboard(unsigned char key, int, int) {}
- void onKeyboardUp(unsigned char key, int, int) {}
- void onMouse(int, int, int, int) {}
- void onMouseMotion(int, int) {}
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(100, 100);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Grafika pelda program");
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- onInitialization();
- glutDisplayFunc(onDisplay);
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment