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

Author Topic: OpenGL Help  (Read 1666 times)

0 Members and 1 Guest are viewing this topic.

CaptainSeagull

  • Newbie
  • *
  • Posts: 1
    • View Profile
OpenGL Help
« on: November 26, 2014, 11:13:08 pm »
HI! I'm currently working on an OpenGL/SFML project, and I'm having some trouble. I *hope* it's something simple in the code, but me and a friend have looked over it again and again and we can't find the problem. Basically, we should be drawing a textured block, but nothing's appearing! We have no problem clearing the screen to a different colour, but the .obj model we load isn't there. The code has previously been tested with SDL, so we know the model loader and positions should be right. Is there some secret call I'm missing or something? Thansk in advance.

int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        // Request a 32-bits depth buffer when creating the window
    sf::ContextSettings contextSettings;
    contextSettings.depthBits = 32;
        contextSettings.majorVersion = 3;
        contextSettings.minorVersion = 3;

    // Create the main window
    sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL", sf::Style::Default, contextSettings);

    // Make it the active window for OpenGL calls
    window.setActive();

    // Set the color and depth clear values
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 1.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Disable lighting and texturing
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);

    // Configure the viewport (the same size as the window)
    glViewport(0, 0, window.getSize().x, window.getSize().y);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
    glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);


        textures[0].loadFromFile("fabric.bmp");


        vector<GLfloat> verts;
        vector<GLfloat> norms;
        vector<GLfloat> tex_coords;
        vector<GLuint> indices;
        loadObj("cube.obj", verts, norms, tex_coords, indices);
        GLuint size = indices.size();
        meshIndexCount = size;
        meshObjects[0] = createMesh(verts.size()/3, verts.data(), nullptr, norms.data(), tex_coords.data(), size, indices.data());

        shaderProgram = initShaders("phong-tex.vert","phong-tex.frag");
        setLight(shaderProgram, light0);
        setMaterial(shaderProgram, material0);


    // Enable position and color vertex components
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    // Disable normal and texture coordinates vertex components
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

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

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                window.close();

            // Escape key: exit
            if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
                window.close();

            // Resize event: adjust the viewport
            if (event.type == sf::Event::Resized)
                glViewport(0, 0, event.size.width, event.size.height);
        }

                // clear the screen
                glEnable(GL_CULL_FACE);
                glClearColor(0.5f,0.5f,0.5f,1.0f);
                glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);

                glm::mat4 projection(1.0);
                projection = glm::perspective(60.0f,800.0f/600.0f,1.0f,150.0f);
                rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
       
                GLfloat scale(1.0f); // just to allow easy scaling of complete scene
       
                glm::mat4 modelview(1.0); // set base position for scene
                mvStack.push(modelview);
               
                //glBindTexture(GL_TEXTURE_2D, textures[0]);
                sf::Texture::bind(&textures[0]);
                mvStack.push(mvStack.top());
                mvStack.top() = glm::translate(mvStack.top(), glm::vec3(0.0f, 0.0f, -10.0f));
                mvStack.top() = glm::scale(mvStack.top(),glm::vec3(20.0f, 0.1f, 20.0f));
                setUniformMatrix4fv(shaderProgram, "modelview", glm::value_ptr(mvStack.top()));
                setMaterial(shaderProgram, material0);
                drawIndexedMesh(meshObjects[0],meshIndexCount,GL_TRIANGLES);
                mvStack.pop();


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

    return EXIT_SUCCESS;
}

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: OpenGL Help
« Reply #1 on: November 27, 2014, 12:07:15 am »
This is not a problem for SFML, it does not belong on this forum. You are also disabling things that you dont need to disable. Lighting is disabled by default and im not sure what you are trying to achieve by disabling GL_TEXTURE_2D.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: OpenGL Help
« Reply #2 on: November 27, 2014, 12:53:47 am »
You can't have tested this exact code in SDL since you use SFML data structures in it. If you would check for OpenGL errors every now and then, you would probably see that a load of them are being produced... every frame. Because you are mixing sfml-graphics data structures and raw OpenGL code, you will have to heed all the nuances that are associated with using them together. I don't know what version of SFML you are using, but not too long ago, a change had to be made in order to fix another well known bug in sfml-graphics that unfortunately requires you to reactivate the window's OpenGL context after the first time you make use of sf::Texture or sf::Shader if rely on raw OpenGL to do your drawing. Take what I said into account and throw in a few extra window.setActive()s where they are needed and see if it fixes your problem.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: OpenGL Help
« Reply #3 on: November 28, 2014, 01:54:20 am »
You are also disabling things that you dont need to disable. Lighting is disabled by default and im not sure what you are trying to achieve by disabling GL_TEXTURE_2D.
I believe that this is in an SFML example somewhere. I lifted the code from the example (looks like it might have been this one) and adapted it for my test code for Objex and I can see that it also does the two things that you mentioned. I guess disabling lighting when it's already disabled anyway can't harm anything and disabling texture could be if they're not using textures.

CaptainSeagull, you may find looking at my Objex repository useful. It's not a perfect class by any means but I believe it to be in working order. The Objex class is used to load OBJ files by the test.cpp which displays it in SFML.
« Last Edit: November 28, 2014, 02:00:10 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*