Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/System.hpp>
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- #include <iostream>
- void processEvents(sf::Window *app);
- void processInput(sf::Window *app, const sf::Input &input);
- void renderCube(sf::Window *app, sf::Clock *clock);
- void renderGlScene(sf::Window *app);
- int loadResources();
- void init();
- void update();
- GLuint texture_handle;
- float xRot = 0.0f;
- float yRot = 0.0f;
- float zRot = 0.0f;
- int main() {
- sf::Window app(sf::VideoMode(800, 600, 32), "Lesson 6");
- app.UseVerticalSync(false);
- init();
- if (loadResources() == -1) {
- return EXIT_FAILURE;
- }
- while (app.IsOpened()) {
- update();
- processEvents(&app);
- //renderCube(&app, &clock);
- renderGlScene(&app);
- app.Display() ;
- }
- return EXIT_SUCCESS;
- }
- int loadResources() {
- sf::Image img_data;
- // Load Texture
- if (!img_data.LoadFromFile("data/images/crate.jpg")) {
- std::cout << "Could not load data/images/crate.jpg";
- return -1;
- }
- // Give the texture an id
- glGenTextures(1, &texture_handle);
- // Bind the id to a texture (i.e: associate the id with a texture)
- glBindTexture(GL_TEXTURE_2D, texture_handle);
- //
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_data.GetWidth(), img_data.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data.GetPixelsPtr());
- //
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- return 0;
- }
- void update() {
- xRot += 0.003f;
- yRot += 0.002f;
- zRot += 0.004f;
- }
- void init() {
- glClearDepth(1.f);
- glClearColor(0.f, 0.f, 0.f, 0.f);
- // Enable z-buffer and read and write
- glEnable(GL_DEPTH_TEST);
- glDepthMask(GL_TRUE);
- // Setup a perpective projection
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.f, 1.f, 1.f, 500.f);
- }
- void processEvents(sf::Window *app) {
- sf::Event event;
- while (app->GetEvent(event)) {
- if (event.Type == sf::Event::Closed) {
- app->Close();
- }
- if (event.Type == sf::Event::KeyPressed &&
- event.Key.Code == sf::Key::Escape) {
- app->Close();
- }
- }
- }
- void renderGlScene(sf::Window *app) {
- app->SetActive();
- // Clear color depth buffer
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Apply some transformations
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.0f,0.0f,-5.0f);
- glRotatef(xRot,1.0f,0.0f,0.0f); // Rotate On The X Axis
- glRotatef(yRot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
- glRotatef(zRot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
- glBindTexture(GL_TEXTURE_2D, texture_handle);
- glBegin(GL_QUADS);
- // Front Face
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
- // Back Face
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
- // Top Face
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- // Bottom Face
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- // Right face
- glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
- glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
- // Left Face
- glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
- glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
- glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
- glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
- glEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement