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

Author Topic: OpenGL not drawing  (Read 3808 times)

0 Members and 1 Guest are viewing this topic.

Wnt2bsleepin

  • Guest
OpenGL not drawing
« on: February 14, 2013, 03:55:54 am »
Hello, I am new to sfml and I am having a hard time getting OpenGL to render.

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

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

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

    // 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);
            }
        }

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // draw...

                glBegin(GL_TRIANGLES);
                glVertex3f(100.0f, 100.0f, 0.0f);
                glVertex3f(150.0f, 100.0f, 0.0f);
                glVertex3f(125.0f, 50.0f, 0.0f);
                glEnd( );
               
        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}

 

It builds and runs fine, but I get a black screen. Any help is much appreciated.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: OpenGL not drawing
« Reply #1 on: February 14, 2013, 08:00:11 am »
Have you read the tutorial on OpenGL? Your code you posted looks fine to me. A wild guess: you mix sfml and OpenGL draw calls without pushing/poping the states.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: OpenGL not drawing
« Reply #2 on: February 14, 2013, 08:12:24 am »
Does the OpenGL SFML example work for you?
Laurent Gomila - SFML developer

Wnt2bsleepin

  • Guest
Re: OpenGL not drawing
« Reply #3 on: February 14, 2013, 06:51:51 pm »
Where do I find the examples? I am running version 2.0 and I don't believe there is a full SDK download (With the examples?)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: OpenGL not drawing
« Reply #4 on: February 14, 2013, 06:59:24 pm »
Where do I find the examples? I am running version 2.0 and I don't believe there is a full SDK download (With the examples?)
How did you miss this?
And this?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Wnt2bsleepin

  • Guest
Re: OpenGL not drawing
« Reply #5 on: February 14, 2013, 10:21:46 pm »
Didn't think to click on the development snapshot. Seems that there is a good amount of sample code in there. Thanks.


Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: OpenGL not drawing
« Reply #6 on: February 15, 2013, 06:21:08 am »
you draw your trig to a z of 0, this is incorrect, as the camea's clip space won't draw it since its too close :) , at least try a 20 (or -20 depending on your setup) on the z axis
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Weeve

  • Jr. Member
  • **
  • Posts: 87
  • C++ Programmer (Intermediate), 3D Artist (Skilled)
    • View Profile
    • Email
Re: OpenGL not drawing
« Reply #7 on: February 15, 2013, 06:23:15 am »
Oh, something that will save you a ton of headache (OpenGl scales the display if you have a non square viewport given to it)
Long live rapid project development! -- Kestrel3D Game-Engine nearing completion

Wnt2bsleepin

  • Guest
Re: OpenGL not drawing
« Reply #8 on: February 17, 2013, 08:53:30 pm »
you draw your trig to a z of 0, this is incorrect, as the camea's clip space won't draw it since its too close :) , at least try a 20 (or -20 depending on your setup) on the z axis

Thanks for the advice. I have done a little bit of openGL, but things like this are helpful.

I wonder if it's something wrong with my window?

#include <SFML/Graphics.hpp>
#include <iostream>
#include <ios>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include "main.h"

using namespace std;

//Hardcoded in.
bool running = true, runningSecondary = true;
const int XSize = 800, YSize = 600;
double countX = 0;
double countY = 0;

using sf::Thread;


//Draws the ball and returns the circle object back.
sf::CircleShape drawCircle(sf::RenderWindow* window2, sf::CircleShape circle = sf::CircleShape(100.f))
{
                 
                circle.setFillColor(sf::Color::Green);
                window2->clear();
                window2->draw(circle);
                window2->display();  
                return circle;
}

//Changes the position of the ball.
void changePos(bool reverse, char choice)
{
        if(reverse)
        {
                switch (choice)
                {

                case 'b':
                        countX -= .05;
                        countY -= .05;
                        break;
                case 'x':
                        countX -= .05;
                        break;
                case 'y':
                        countY -= .05;
                        break;
                }

        }

        else
        {
                switch (choice)
                {

                case 'b':
                        countX += .05;
                        countY += .05;
                        break;
                case 'x':
                        countX += .05;
                        break;
                case 'y':
                        countY += .05;
                        break;
                }
        }



}

//Animates the moving of the ball.
void animate(animateThread& at)
{
        sf::CircleShape circle = at.circle;
        sf::RenderWindow* window2 = at.renderWindow;  
        int circleDiameter = circle.getRadius() * 2;
       
        while(runningSecondary)
        {
                 
               
                if(countX >= 799 - circleDiameter)
                {
                        cout << circle.getPosition().x << " XXXXXX" << endl;
                        changePos(true, 'x');
                }

                else if(countY >= 599 - circleDiameter)
                {
                        cout << circle.getPosition().y << " YYYYYYYYY" << endl;
                        changePos(true, 'y');
                }

                else if((countX >= 799 - circleDiameter) && (countY >= 599 - circleDiameter))
                {
                        changePos(true, 'b');
                }

                else{changePos(false, 'b'); }

                circle.setPosition(countX,countY);
                drawCircle(window2, circle);
        }
}


int main()
{
   
       
    sf::Window window(sf::VideoMode(XSize, YSize), "OpenGL", sf::Style::Default, sf::ContextSettings(32)); //OpenGl Window
        sf::RenderWindow window2(sf::VideoMode(XSize,YSize), "Graphics"); //SFML Graphics Window
       
       
        window.setVerticalSyncEnabled(true);
        glDisable(GL_DEPTH_TEST);
   
   
   sf::CircleShape circle = drawCircle(&window2); //Draw the circle in the origin.
   
        //Main event loops.
        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);
            }
        }

                sf::Event Event_Window2;
                while(window2.pollEvent(Event_Window2))
                {
                        if(Event_Window2.type == sf::Event::Closed)
                        {
                                running = false;
                        }

                        else if(Event_Window2.type == sf::Event::KeyPressed)
                        {
                                if(Event_Window2.key.code == sf::Keyboard::P)
                                {
                                        runningSecondary = false;
                                }
                       
                        }

                         else if (Event_Window2.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, Event_Window2.size.width, Event_Window2.size.height);
            }

                }

               
               
                 
               
                //Creates a struct containing the initial circle object and the window.  
                animateThread at;
                at.circle = circle;
                at.renderWindow = &window2;
                 
                //Threads the animation so it doesn't interfere with the Event catching.
                //animate is the name of the method, at is the struct object.
                //Thread animationThread(&animate, at);
                //animationThread.launch();
               
                //OpenGL Implementation Attempt.

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // draw...

                glBegin(GL_TRIANGLES);
                glVertex3f(100.0f, 100.0f, 20.0f);
                glVertex3f(150.0f, 100.0f, 20.0f);
                glVertex3f(125.0f, 50.0f, 20.0f);
                glEnd();
               
               
        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}