Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- sf::Clock function = sf::Clock();
- float timeElapsed = function.getElapsedTime().asMilliseconds();
- PFNGLBLENDEQUATIONEXTPROC glBlendEquation = NULL;
- PFNGLBLENDEQUATIONEXTPROC glBlendColor = NULL;
- sf::Texture t1;
- struct
- {
- float theta;
- float fi;
- float r;
- } camera;
- struct
- {
- float val;
- } fov;
- struct
- {
- float x;
- float y;
- float z;
- } translate;
- void translate_init(void)
- {
- translate.x = 0;
- translate.y = 0;
- translate.z = 0;
- }
- struct
- {
- float x;
- float y;
- float z;
- } scale;
- void scale_init(void)
- {
- scale.x = 1;
- scale.y = 1;
- scale.z = 1;
- }
- struct
- {
- float x;
- float y;
- float z;
- } rotate;
- void rotate_init(void)
- {
- rotate.x = 0;
- rotate.y = 0;
- rotate.z = 0;
- }
- void camera_init(void)
- {
- fov.val = 45.0;
- camera.theta = 1.57f;
- camera.fi = -1.57f;
- camera.r = 20.f;
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_TEXTURE_2D);
- glBlendEquation = (PFNGLBLENDEQUATIONEXTPROC)wglGetProcAddress("glBlendEquation");
- glBlendColor = (PFNGLBLENDEQUATIONEXTPROC)wglGetProcAddress("glBlendColor");
- }
- void reshapeScreen(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(fov.val, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- };
- GLUquadric *quad;
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- GLfloat light_theta;
- GLfloat light_fi;
- void change_light_position(void)
- {
- light_position[0] = 15 * cos(light_theta)*cos(light_fi);
- light_position[1] = 15 * cos(light_theta)*sin(light_fi);
- light_position[2] = 15 * sin(light_theta);
- }
- void texture(void)
- {
- t1.loadFromFile("./grass.png");
- sf::Texture::bind(&t1);
- }
- void drawScene()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- float x = camera.r * cos(camera.theta)*cos(camera.fi);
- float xp = camera.r * cos(camera.theta+0.001f)*cos(camera.fi);
- float y = camera.r * cos(camera.theta)*sin(camera.fi);
- float yp = camera.r * cos(camera.theta+0.001f)*sin(camera.fi);
- float z = camera.r * sin(camera.theta);
- float zp = camera.r * sin(camera.theta + 0.001f);
- gluLookAt(x, y, z, 0.0, 0.0, 0.0, xp, yp, zp);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMatrixMode(GL_PROJECTION);
- glDisable(GL_LIGHTING);
- glLineWidth(1.5);
- glBegin(GL_LINES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(5.0f, 0.0f, 0.0f);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 5.0f, 0.0f);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 5.0f);
- glEnd();
- glEnable(GL_LINE_STIPPLE);
- glLineStipple(3, 0xAAAA);
- glBegin(GL_LINES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(-5.0f, 0.0f, 0.0f);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, -5.0f, 0.0f);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(0.0f, 0.0f, 0.0f);
- glVertex3f(0.0f, 0.0f, -5.0f);
- glEnd();
- glEnable(GL_LIGHTING);
- glDisable(GL_LINE_STIPPLE);
- glMatrixMode(GL_MODELVIEW);
- glTranslatef(translate.x, translate.y, translate.z);
- glScalef(scale.x, scale.y, scale.z);
- glRotatef(rotate.x, 1, 0, 0);
- glRotatef(rotate.y, 0, 1, 0);
- glLineWidth(3);
- glBegin(GL_LINES);
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(2.0f, 2.0f, -2.0f);
- glVertex3f(2.0f, 2.0f, 2.0f);
- glVertex3f(2.0f, 2.0f, 2.0f);
- glVertex3f(2.0f, -2.0f, 2.0f);
- glVertex3f(-2.0f, 2.0f, 2.0f);
- glVertex3f(2.0f, 2.0f, 2.0f);
- glVertex3f(-2.0f, -2.0f, -2.0f);
- glVertex3f(-2.0f, -2.0f, 2.0f);
- glVertex3f(-2.0f, -2.0f, 2.0f);
- glVertex3f(2.0f, -2.0f, 2.0f);
- glVertex3f(-2.0f, -2.0f, -2.0f);
- glVertex3f(2.0f, -2.0f, -2.0f);
- glVertex3f(-2.0f, 2.0f, 2.0f);
- glVertex3f(-2.0f, -2.0f, 2.0f);
- glVertex3f(2.0f, -2.0f, -2.0f);
- glVertex3f(2.0f, -2.0f, 2.0f);
- glVertex3f(-2.0f, 2.0f, -2.0f);
- glVertex3f(-2.0f, -2.0f, -2.0f);
- glVertex3f(-2.0f, 2.0f, -2.0f);
- glVertex3f(-2.0f, 2.0f, 2.0f);
- glVertex3f(2.0f, 2.0f, -2.0f);
- glVertex3f(2.0f, -2.0f, -2.0f);
- glVertex3f(-2.0f, 2.0f, -2.0f);
- glVertex3f(2.0f, 2.0f, -2.0f);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(2.0f, 2.0f, -2.0f);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(-2.0f, 2.0f, 2.0f);
- glColor3f(0.0f, .0f, 1.0f);
- glVertex3f(2.0f, -2.0f, 2.0f);
- glEnd();
- quad = gluNewQuadric();
- glDisable(GL_COLOR_MATERIAL);
- glPushMatrix();
- glMaterialfv(GL_FRONT, GL_AMBIENT, PolishedGoldAmbient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, PolishedGoldDiffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, PolishedGoldSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, PolishedGoldShininess);
- //glColor3f(1.0f, 0.84f, 0.0f);
- glTranslatef(7.0, 0, 0);
- gluSphere(quad, 2.0, 15, 15);
- glPopMatrix();
- glRotatef(timeElapsed, 1, 0, 0);
- glPushMatrix();
- //glColor3f(0.31f, 0.78f, 0.47f);
- glTranslatef(-7.0, 0, 0);
- glMaterialfv(GL_FRONT, GL_AMBIENT, EmeraldAmbient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, EmeraldDiffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, EmeraldSpecular);
- glMaterialfv(GL_FRONT, GL_SHININESS, EmeraldShininess);
- gluCylinder(quad, 3.0, 0.0, 5.0, 15, 15);
- glPopMatrix();
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0);
- glVertex3f(-2.0f, 2.0f, 5.0f);
- glTexCoord2f(0, 1);
- glVertex3f(-2.0f, -2.0f, 5.0f);
- glTexCoord2f(1, 0);
- glVertex3f(2.0f, -2.0f, 5.0f);
- glTexCoord2f(1, 1);
- glVertex3f(2.0f, 2.0f, 5.0f);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_BLEND);
- glBlendEquation(GL_ONE_MINUS_SRC_ALPHA);
- glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
- glBegin(GL_QUADS);
- glColor4f(1.0f, 0.0f, 0.0f, 0.2f);
- glVertex3f(5.0f, 5.0f, -7.0f);
- glColor4f(0.0f, 1.0f, 0.0f, 0.2f);
- glVertex3f(-5.0f, 5.0f, -7.0f);
- glColor4f(1.0f, 0.0f, 1.0f, 0.2f);
- glVertex3f(-5.0f, -5.0f, -7.0f);
- glColor4f(0.7f, 0.7f, 1.0f, 0.2f);
- glVertex3f(5.0f, -5.0f, -7.0f);
- glEnd();
- glDisable(GL_BLEND);
- }
- int main()
- {
- sf::ContextSettings context(24, 0, 0, 4, 5);
- sf::RenderWindow window(sf::VideoMode(800, 600), "Open GL", 7U, context);
- bool running = true;
- translate_init();
- camera_init();
- scale_init();
- texture();
- reshapeScreen(window.getSize().x, window.getSize().y);
- while (running)
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- running = false;
- }
- if(event.type == sf::Event::Resized) reshapeScreen(window.getSize().x, window.getSize().y);
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))camera.fi -= 0.0005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))camera.fi += 0.0005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))camera.theta -= 0.0005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))camera.theta += 0.0005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.y += 0.005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && !sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.y -= 0.005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)&& sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.z += 0.005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))translate.z -= 0.005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))translate.x -= 0.005f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))translate.x += 0.005f;
- //if (sf::keyboard::iskeypressed(sf::keyboard::))scale.y += 0.001f;
- //if (sf::keyboard::iskeypressed(sf::keyboard::g))scale.y -= 0.001f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
- {
- fov.val += 0.01f;
- reshapeScreen(window.getSize().x, window.getSize().y);
- // camera.r += 0.001f;
- };
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::G))
- {
- fov.val -= 0.01f;
- reshapeScreen(window.getSize().x, window.getSize().y);
- // camera.r -= 0.001f;
- };
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))scale.x -= 0.001f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::H))scale.x += 0.001f;
- /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::I))rotate.y += 0.05f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::K))rotate.y -= 0.05f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::J))rotate.x -= 0.05f;
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::L))rotate.x += 0.05f;*/
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::I))
- {
- light_theta += 0.0005f;
- change_light_position();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::K))
- {
- light_theta -= 0.0005f;
- change_light_position();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::J))
- {
- light_fi -= 0.0005f;
- change_light_position();
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::L))
- {
- light_fi += 0.0005f;
- change_light_position();
- }
- drawScene();
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement