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

Author Topic: I want to Get Some OpenGL stuff strait...  (Read 2048 times)

0 Members and 1 Guest are viewing this topic.

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
I want to Get Some OpenGL stuff strait...
« on: December 11, 2010, 04:34:55 pm »
Ok well Here is the example code from the repository and I have some questions about it after the code.

Code: [Select]

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>

////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL");

    // Create a sprite for the background
    sf::Image backgroundImage;
    if (!backgroundImage.LoadFromFile("resources/background.jpg"))
        return EXIT_FAILURE;
    sf::Sprite background(backgroundImage);

    // Load an OpenGL texture.
    // We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
    // but here we want more control on it (generate mipmaps, ...) so we create a new one from the image pixels
    GLuint texture = 0;
    {
        sf::Image image;
        if (!image.LoadFromFile("resources/texture.jpg"))
            return EXIT_FAILURE;
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, image.GetWidth(), image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, image.GetPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    }

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glClearDepth(1.f);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);

    // Bind our texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glColor4f(1.f, 1.f, 1.f, 1.f);

    // Create a clock for measuring the time elapsed
    sf::Clock clock;

    // Start game loop
    while (window.IsOpened())
    {
window.ShowMouseCursor(false);
        // Process events
        sf::Event event;
        while (window.GetEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
{
                window.Close();
return 0;
}

            // Escape key : exit
            if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
                window.Close();
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Up))
{
}
if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Down))
{

}

            // Adjust the viewport when the window is resized
            if (event.Type == sf::Event::Resized)
                glViewport(0, 0, event.Size.Width, event.Size.Height);
        }

        // Draw the background
        window.SaveGLStates();
        window.Draw(background);
        window.RestoreGLStates();

        // Activate the window before using OpenGL commands.
        // This is useless here because we have only one window which is
        // always the active one, but don't forget it if you use multiple windows
        window.SetActive();

        // Clear the depth buffer
        glClear(GL_DEPTH_BUFFER_BIT);

        // We get the position of the mouse cursor, so that we can move the box accordingly
        float x =  window.GetInput().GetMouseX() * 200.f / window.GetWidth()  - 100.f;
        float y = -window.GetInput().GetMouseY() * 200.f / window.GetHeight() + 100.f;

        // Apply some transformations
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(x, y, -100.f);
        //glRotatef(clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
        glRotatef(clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
        //glRotatef(clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);

        // Draw a cube
        float size = 20.f;
        glBegin(GL_QUADS);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, -size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, size);
            glTexCoord2f(1, 1); glVertex3f( size,  size, size);
            glTexCoord2f(1, 0); glVertex3f( size, -size, size);

            glTexCoord2f(0, 0); glVertex3f(-size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(-size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(-size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(-size, -size,  size);

            glTexCoord2f(0, 0); glVertex3f(size, -size, -size);
            glTexCoord2f(0, 1); glVertex3f(size,  size, -size);
            glTexCoord2f(1, 1); glVertex3f(size,  size,  size);
            glTexCoord2f(1, 0); glVertex3f(size, -size,  size);

            glTexCoord2f(0, 1); glVertex3f(-size, -size,  size);
            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);
            glTexCoord2f(0, 0); glVertex3f(-size, size, -size);
            glTexCoord2f(1, 0); glVertex3f( size, size, -size);
            glTexCoord2f(1, 1); glVertex3f( size, size,  size);

        glEnd();

        // Draw some text on top of our OpenGL object
        window.SaveGLStates();
        sf::Text text("SFML / OpenGL demo");
        text.SetPosition(250.f, 450.f);
        text.SetColor(sf::Color(255, 255, 255, 170));
        window.Draw(text);
        window.RestoreGLStates();

        // Finally, display the rendered frame on screen
        window.Display();
    }

    // Don't forget to destroy our texture
    glDeleteTextures(1, &texture);

    return EXIT_SUCCESS;
}


So right off the bat you set up a texture with gl called texture right?
Then the Opengl initiative stuff and then you bind the texture corrrect?
After that you start the main loop and get the events and then You save the gl states? what does that do? But after that you restore it? Does it do Opengl stuff like popping and pushing matixes?
What does set active do?

Also when you call all of the tranformations how do you make it only apply to one shape? Like make it more object oriented?

SOrry I have more questions but I gtg..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
I want to Get Some OpenGL stuff strait...
« Reply #1 on: December 11, 2010, 04:57:09 pm »
Quote
So right off the bat you set up a texture with gl called texture right?
Then the Opengl initiative stuff and then you bind the texture corrrect?

Yes.

Quote
then You save the gl states? what does that do? But after that you restore it? Does it do Opengl stuff like popping and pushing matixes?

Yes, it saves every OpenGL state that SFML may change for its own rendering, so that when you restore them you get your OpenGL states exactly as they were before.

Quote
What does set active do?

It activates the window's OpenGL context, so that the OpenGL calls that follow will be made in this context (every context has its own set of states).

Quote
Also when you call all of the tranformations how do you make it only apply to one shape? Like make it more object oriented?

Code: [Select]
glPushMatrix(...);
glLoadMatrixf(...);
// render object here
glPopMatrix(...);
Laurent Gomila - SFML developer

Fierce_Dutch

  • Full Member
  • ***
  • Posts: 138
    • View Profile
I want to Get Some OpenGL stuff strait...
« Reply #2 on: December 11, 2010, 09:34:55 pm »
Quote from: "Laurent"
Quote
So right off the bat you set up a texture with gl called texture right?
Then the Opengl initiative stuff and then you bind the texture corrrect?

Yes.

Quote
then You save the gl states? what does that do? But after that you restore it? Does it do Opengl stuff like popping and pushing matixes?

Yes, it saves every OpenGL state that SFML may change for its own rendering, so that when you restore them you get your OpenGL states exactly as they were before.

Quote
What does set active do?

It activates the window's OpenGL context, so that the OpenGL calls that follow will be made in this context (every context has its own set of states).

Quote
Also when you call all of the tranformations how do you make it only apply to one shape? Like make it more object oriented?

Code: [Select]
glPushMatrix(...);
glLoadMatrixf(...);
// render object here
glPopMatrix(...);


Ok thanks! But I am still a little confused on the last part. Because am in algebra 1 and we are just starting to learn matrixes. So, I am going to guess that I have to create an martix of data and then push it to hold something's certain data. And then pop it when I am done? So if I wanted to make a class called grass block and I just want to move and rotate the block and eveything as a whole (like this, grassblock.Rotate) I would have to switch matrix data to for each part of the bloack that would be affected?

Also, shapes arent like lights huh? like light0? They are just shapes that have their values to a matrix (if set to a custom matrix, otherwise just put on stack) that can hold the data for the verts and rotation and texture and color?

Sorry for the confusing response



Finally, how does OpenGL load matrices? Am I limited to 4x4 matrices? Because I want to create a class called cube and it will have rotate, translate, update, and the type of block but I want to store all this in a matrix that the class owns. So I want a custom martix to be loaded that holds the vert data.. Because i dont want to just clear the stack and write  to it every function. So how would I go about doing all this???

 

anything