Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What else do I need to call besides sf::Texture::Bind() ?  (Read 2020 times)

0 Members and 1 Guest are viewing this topic.

BruceJohnJennerLawso

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • My Code on Github
What else do I need to call besides sf::Texture::Bind() ?
« on: March 05, 2015, 12:32:34 am »
Okay, so I am trying to create a program with a textured openGL cube,

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

#define cameraMove 6


void Draw_cube(float size, sf::Image &sideFace);
void Draw_cube_texed(float size, sf::Texture &sideFace);
void Draw_point(float x, float y, float z);
void Draw_pyramid();

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    // load resources, initialize the OpenGL states, ...

        glClearDepth(1.0f);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        // ummm just transparent apparently
       
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_TEXTURE_2D);  
        glDepthMask(GL_TRUE);
       
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90.0f, 1.0f, 1.0f, 500.0f);

        float cubeSize = 20;

        float cameraDistance = -200;

        sf::RectangleShape rect(sf::Vector2f(1500, 50));
        rect.setFillColor(sf::Color(0, 255, 0, 128));
       
        sf::Texture cubeFace;
        if(!cubeFace.loadFromFile("./crate.png"))
        {       std::cout << "Unable to load image from file" << std::endl;
                return -1;
        }
       

        sf::Clock clock;
    // run the main loop
    bool running = true;
    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
                // this would be better done with forcing the window size back
                // to some constant values
           
                                // or at least forced to a specific scale, as if you change the
                                // width/height ratio, the image ends up being distorted
            }
            if(event.type == sf::Event::KeyPressed)
                        {       if(event.key.code = sf::Keyboard::Up)
                                {       cubeSize += 1;
                                }
                                if(event.key.code = sf::Keyboard::Down)
                                {       cubeSize -= 1;
                                }
                        }
                        if(event.type == sf::Event::MouseWheelMoved)
                        {       cameraDistance += event.mouseWheel.delta*cameraMove;
                        }
                       
                       
        }

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // erases any previously drawn stuff...
       
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        // beats me...
        glTranslatef(0.0f, 0.0f, cameraDistance);
        // what the, why does it translate every frame
               
                // ohhh, so we can be 200 units back from wherever the
               
                glRotatef(20*cos((clock.getElapsedTime().asSeconds()*0.5)), 1.f, 0.f, 0.f);
                glRotatef((clock.getElapsedTime().asSeconds()*30), 0.f, 1.f, 0.f);
                //glRotatef((clock.getElapsedTime().asSeconds()*90), 0.f, 0.f, 1.f);
                // rotate the camera view a wee bit

                // lets just try with the z axis rotate
                // okay, no
               
                // that does not work

        // draw...
                Draw_cube_texed(cubeSize, cubeFace);
                //Draw_pyramid();
               

               
               
                Draw_point(25, 25, 25);
                window.pushGLStates();

                window.draw(rect);

                window.popGLStates();
               
                int points = std::rand()%10 + 1;
                for(int cy = 0; cy != points; ++cy)
                {       Draw_point((20 + std::rand()%10),(20 + std::rand()%10),(20 + std::rand()%10));
                }
                // kewl

        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}

void Draw_cube(float size, sf::Texture &sideFace)
{       glBegin(GL_QUADS);

        glColor3f(0,0,255);
        // lancelots favourite cube
       
        // I dont care if the joke is old by now dammit!

        glVertex3f(-size, -size, -size);
        glVertex3f(-size,  size, -size);
        glVertex3f( size,  size, -size);
        glVertex3f( size, -size, -size);
        // I guess we define them one at a time in sets of four, hence the quads
        // part...
               
        // and each set of four defines a face...


        sf::Texture::bind(&sideFace);  
        //glTexture
        //glColor3f(255,255,255);
        glNormal3f(0.0, 0.0, 1.0f);
        glTexCoord2f(0,0);
        glVertex3f(-size, -size, size);
        glTexCoord2f(1,0);
        glVertex3f(-size,  size, size);
        glTexCoord2f(1, 1);
        glVertex3f( size,  size, size);
        glTexCoord2f(0,1);
        glVertex3f( size, -size, size);
        //sf::Texture::bind(NULL);     
       
       
        glColor3f(128,128,0);
       
        glVertex3f(-size, -size, -size);
        glVertex3f(-size,  size, -size);
        glVertex3f(-size,  size,  size);
        glVertex3f(-size, -size,  size);

        glColor3f(255,0,0);

        glVertex3f(size, -size, -size);
        glVertex3f(size,  size, -size);
        glVertex3f(size,  size,  size);
        glVertex3f(size, -size,  size);

        glColor3f(128,0,128);

        glVertex3f(-size, -size,  size);
        glVertex3f(-size, -size, -size);
        glVertex3f( size, -size, -size);
        glVertex3f( size, -size,  size);

        glColor3f(0,0,255);

        glVertex3f(-size, size,  size);
        glVertex3f(-size, size, -size);
        glVertex3f( size, size, -size);
        glVertex3f( size, size,  size);



        glEnd();
}

void Draw_point(float x, float y, float z)
{       glBegin(GL_POINTS);
        glColor3f(255,0,0);
        // aha, that works nicely
        glVertex3f(x, y, z);
        glEnd();
}

void Draw_pyramid()
{
       
                glBegin(GL_TRIANGLES);                                                          // Start Drawing A Triangle
                glColor3f(10.0f,0.0f,0.0f);                                             // Red
                glVertex3f( 0.0f, 10.0f, 0.0f);                                 // Top Of Triangle (Front)
                glColor3f(0.0f,10.0f,0.0f);                                             // Green
                glVertex3f(-10.0f,-10.0f, 10.0f);                                       // Left Of Triangle (Front)
                glColor3f(0.0f,0.0f,10.0f);                                             // Blue
                glVertex3f( 10.0f,-10.0f, 10.0f);                                       // Right Of Triangle (Front)
                glColor3f(10.0f,0.0f,0.0f);                                             // Red
                glVertex3f( 0.0f, 10.0f, 0.0f);                                 // Top Of Triangle (Right)
                glColor3f(0.0f,0.0f,10.0f);                                             // Blue
                glVertex3f( 10.0f,-10.0f, 10.0f);                                       // Left Of Triangle (Right)
                glColor3f(0.0f,10.0f,0.0f);                                             // Green
                glVertex3f( 10.0f,-10.0f, -10.0f);                                      // Right Of Triangle (Right)
                glColor3f(10.0f,0.0f,0.0f);                                             // Red
                glVertex3f( 0.0f, 10.0f, 0.0f);                                 // Top Of Triangle (Back)
                glColor3f(0.0f,10.0f,0.0f);                                             // Green
                glVertex3f( 10.0f,-10.0f, -10.0f);                                      // Left Of Triangle (Back)
                glColor3f(0.0f,0.0f,10.0f);                                             // Blue
                glVertex3f(-10.0f,-10.0f, -10.0f);                                      // Right Of Triangle (Back)
                glColor3f(10.0f,0.0f,0.0f);                                             // Red
                glVertex3f( 0.0f, 10.0f, 0.0f);                                 // Top Of Triangle (Left)
                glColor3f(0.0f,0.0f,10.0f);                                             // Blue
                glVertex3f(-10.0f,-10.0f,-10.0f);                                       // Left Of Triangle (Left)
                glColor3f(0.0f,10.0f,0.0f);                                             // Green
                glVertex3f(-10.0f,-10.0f, 10.0f);                                       // Right Of Triangle (Left)
        glEnd();                                                                                        // Done Drawing The Pyramid

}


void Draw_cube_texed(float size, sf::Texture &sideFace)
{       glBegin(GL_QUADS);                          // Start Drawing Quads
    // Front Face
   
        sf::Texture::bind(&sideFace);  
   
    glNormal3f( 0.0f, 0.0f, 1.0f);                  // Normal Pointing Towards Viewer
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Point 1 (Front)
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Point 2 (Front)
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Point 3 (Front)
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Point 4 (Front)
    // Back Face
    glNormal3f( 0.0f, 0.0f,-1.0f);                  // Normal Pointing Away From Viewer
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Point 1 (Back)
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Point 2 (Back)
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Point 3 (Back)
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Point 4 (Back)
    // Top Face
    glNormal3f( 0.0f, 1.0f, 0.0f);                  // Normal Pointing Up
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Point 1 (Top)
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Point 2 (Top)
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Point 3 (Top)
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Point 4 (Top)
    // Bottom Face
    glNormal3f( 0.0f,-1.0f, 0.0f);                  // Normal Pointing Down
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Point 1 (Bottom)
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Point 2 (Bottom)
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Point 3 (Bottom)
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Point 4 (Bottom)
    // Right face
    glNormal3f( 1.0f, 0.0f, 0.0f);                  // Normal Pointing Right
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);  // Point 1 (Right)
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);  // Point 2 (Right)
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);  // Point 3 (Right)
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);  // Point 4 (Right)
    // Left Face
    glNormal3f(-1.0f, 0.0f, 0.0f);                  // Normal Pointing Left
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);  // Point 1 (Left)
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);  // Point 2 (Left)
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);  // Point 3 (Left)
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);  // Point 4 (Left)
        glEnd();                                // Done Drawing Quads
}

But when I run the function void Draw_cube_texed(float size, sf::Texture &sideFace) in my event loop with a properly loaded texture the cube appears without being textured. What else do I need to include in the function call for the texture to be mapped to the sides of the cube?
« Last Edit: March 05, 2015, 02:44:27 am by BruceJohnJennerLawso »
Quote
The computer is mightier than the pen, the sword, and usually the programmer.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: What else do I need to call besides sf::Texture::Bind() ?
« Reply #1 on: March 05, 2015, 01:49:49 am »
Is this a joke or something? The code you posted doesn't even compile. And yes... I added some includes that I assume are in that header file you didn't provide either.

Please... if you are going to post such a long piece of code (even containing a lot of unused stuff), at least make sure people can build it, because they sure aren't going to read it to try and figure out why things don't work. That aside, your code still won't compile, simply because a certain part doesn't make any sense. That isn't the only part that doesn't make sense by the way, but the others don't result in a compile error.

I have to ask you, have you actually read any documentation? SFML or OpenGL? Because from your code it seems that you haven't. If you're the trial and error kind of guy, then that's great, but don't leave half of the work for others to do.

I can't stress this enough, but OpenGL is a very advanced topic. And if you want to have even the slightest chance of making any use of it, you need to learn to read documentation on your own. There might be people who end up posting in this thread giving you a tutorial on what you did wrong in this exact scenario, but if you don't change your learning habits now, you will end up starting many more threads like this one in the future.

If you actually got that code to compile, I am really interested in what compiler you are using, because it would have to be very intelligent to make sense of what you are giving to it.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

BruceJohnJennerLawso

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • My Code on Github
Re: What else do I need to call besides sf::Texture::Bind() ?
« Reply #2 on: March 05, 2015, 02:49:13 am »
Fixed the code above, it should at least compile, I am using g++ with SFML 2.1. My apologies, I was editing the code before I posted, and I should have checked before putting it in the post.

Yes, I have read some of the documentation on OpenGL, and I have read all of the tutorials for SFML, if not the entire API documentation top-to-bottom. The problem I am finding is that most OpenGL tutorials are written for glut or other windowing systems, perhaps I would be better off working through a tutorial written for a different windowing system?
Quote
The computer is mightier than the pen, the sword, and usually the programmer.