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

Author Topic: [Solved]Using openGL and sfml to create a realistic perspective  (Read 2856 times)

0 Members and 1 Guest are viewing this topic.

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Hey everyone, I need to create some perspective, I tried convex shapes, vertex arrays and shaders... no avail, that is why I found myself looking at raw openGL.

I'm pretty well versed in DirectX but this is the first time I've looked into openGL, so far so good and I have created the perspective without problems. However I'm not trying to texture my square, this will probably be a simple fix but I have no idea so please help me.

#include "stdafx.h"
#include <iostream>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#include <SFML/Graphics.hpp>
#include <gl/GLU.h>

int main()
{
        // Create window
        sf::RenderWindow window(sf::VideoMode(0,0,1024, 768), "SFML Perspective");

        //load the texture
        sf::Texture texture;
        if (!texture.loadFromFile("images\\images.png"))
        {
                return EXIT_SUCCESS;
        }
        texture.setRepeated(true);

        //initialise OPENGL
        glClearDepth(1.0f);
        glClearColor(1.f, 1.0f, 1.0f, 0.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);

        //// Setup the camera
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90.0f, 1.0f, 1.0f, 500.0f);

        // Game loop
        while (window.isOpen())
        {
                // Process events
                sf::Event Event;
                while (window.pollEvent(Event))
                {
                        if (Event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }

                // Clear the screen
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                // Preform the appropriate transformations
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                glTranslatef(0.f, 0.f, -150.f);

                glRotatef(30.0f, 0.f, 1.f, 0.f);

                glEnable(GL_TEXTURE_2D);
                sf::Texture::bind(&texture);
                //Draw a face
                glBegin(GL_QUADS);
                {
                        glColor3f(0, 0, 1);
                        glVertex3f(-50.f, -50.f, 50.f);
                        glVertex3f(-50.f, 50.f, 50.f);
                        glVertex3f(50.f, 50.f, 50.f);
                        glVertex3f(50.f, -50.f, 50.f);
                }
                glEnd();
                sf::Texture::bind(NULL);

                window.display();
        }
        return EXIT_SUCCESS;
}
 

That is my code nice and simple.
When I comment out all of the code to do with texturing namely:
sf::Texture texture;
if (!texture.loadFromFile("images\\images.png"))
{
        return EXIT_SUCCESS;
}
texture.setRepeated(true);

...

glEnable(GL_TEXTURE_2D);
sf::Texture::bind(&texture);

...

sf::Texture::bind(NULL);
 

It works fine and I get (see attachment). When I put the lines of code back in I just get a black screen... what am I doing wrong, any help would be appreciated.
« Last Edit: July 29, 2015, 04:42:56 pm by Synyster_Coder »

maly

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Using openGL and sfml to create a realistic perspective
« Reply #1 on: July 22, 2015, 12:45:50 pm »
glTexCoord2f

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Using openGL and sfml to create a realistic perspective
« Reply #2 on: July 22, 2015, 01:07:16 pm »
Thanks for that I looked into and changed to the fallowing code:
glTranslatef(0.f, 0.f, -5.f);

...

glNormal3f(0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);      glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);      glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);      glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);      glVertex3f(1.0f, 1.0f, 1.0f);
 

I like 1's better than 50's... So still the same problem whole screen goes black, still not sure, any more suggestions?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Using openGL and sfml to create a realistic perspective
« Reply #3 on: July 24, 2015, 06:04:37 pm »
If you're not using textures, wouldn't you be better off using:
glDisable(GL_TEXTURE_2D); // nevermind
Nevermind, I just realised that you meant "I'm now trying to texture my square" rather than "I'm not trying to texture my square"
« Last Edit: July 24, 2015, 06:07:18 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Using openGL and sfml to create a realistic perspective
« Reply #4 on: July 29, 2015, 04:41:05 pm »
Thanks for the suggestions, I actually fixed this maybe 2 days ago, the solution was to add the code:
window.setActive();
 

Just after loading the texture.

 

anything