I have code:
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <GL/glut.h>
int main()
{
sf::ContextSettings contextSettings;
contextSettings.depthBits = 24;
contextSettings.sRgbCapable = false;
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML graphics with OpenGL", sf::Style::Default, contextSettings);
window.setVerticalSyncEnabled(true);
sf::View view(sf::Vector2f(0, 0), sf::Vector2f(400, 300));
sf::Texture texture;
if (!texture.loadFromFile("texture.jpg"))
return EXIT_FAILURE;
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
glViewport(0, 0, window.getSize().x,window.getSize().y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(view.getTransform().getMatrix());
sf::RectangleShape shape(sf::Vector2f(100, 100));
static const GLfloat cube[] =
{
-20, -20, -20, 0, 0,
-20, 20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
-20, 20, -20, 1, 0,
-20, 20, 20, 1, 1,
20, -20, -20, 0, 0,
20, 20, -20, 1, 0,
20, -20, 20, 0, 1,
20, -20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
20, -20, -20, 1, 0,
20, -20, 20, 1, 1,
-20, 20, -20, 0, 0,
20, 20, -20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, 20, -20, 0, 1,
-20, 20, -20, 0, 1,
20, -20, -20, 1, 0,
20, 20, -20, 1, 1,
-20, -20, 20, 0, 0,
20, -20, 20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, -20, 20, 1, 0,
20, 20, 20, 1, 1
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube);
glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
view.move(0, 5);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
view.move(0, -5);
window.setView(view);
window.clear();
window.pushGLStates();
window.popGLStates();
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(40, 0, -50.f);
glEnable(GL_TEXTURE_2D);
sf::Texture::bind(&texture);
glDrawArrays(GL_TRIANGLES, 0, 36);
window.pushGLStates();
window.draw(shape);
window.popGLStates();
window.display();
}
return EXIT_SUCCESS;
}
and have problem because i want to move view and Opengl stuff move too.
I know that there is a problem
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(view.getTransform().getMatrix());
After add this line:
glLoadMatrixf(view.getTransform().getMatrix());
object OpenGL just fade away.
Can u tell me how exactly i can do this ? 3D effect will be still visible ?
Thanks
PS. sorry for my english ;)