SFML community forums
Help => Graphics => Topic started by: short_name on January 29, 2012, 05:43:31 am
-
I am trying to create a cube based on the "Using OpenGL" tutorial. I am using sfml 2.0. The problem is the cube shows up as plain white no matter if I bind the texture or not. I am not an expert on OpenGL so I am geussing that I am making a silly mistake. I found another post at http://www.sfml-dev.org/forum/viewtopic.php?t=2041&sid=1397d9c455d32225fdcdc4bed5542c29 that says I need to set the texture coordinates for each quad which I think I am doing correctly. Any suggestions would be greatly appreciated!
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
sf::Clock Clock;
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
sf::Texture Texture;
if( Texture.LoadFromFile( "test.png" ) )
{
cout << "image loaded!" << endl;
}
else
{
cout << "image not found!" << endl;
}
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
double seconds = 0;
while (App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
App.Close();
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
seconds = Clock.GetElapsedTime() / 1000.0;
glRotatef( seconds * 50 , 1.f, 0.f, 0.f);
glRotatef( seconds * 30 , 0.f, 1.f, 0.f);
glRotatef( seconds * 90 , 0.f, 0.f, 1.f);
//Uncommenting the next line does not affect the cube at all
//Texture.Bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(50.f, -50.f, -50.f);
glTexCoord2f(0, 1); glVertex3f(50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f(50.f, 50.f, 50.f);
glTexCoord2f(1, 0); glVertex3f(50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, -50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, -50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, -50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, -50.f, 50.f);
glTexCoord2f(0, 1); glVertex3f(-50.f, 50.f, 50.f);
glTexCoord2f(0, 0); glVertex3f(-50.f, 50.f, -50.f);
glTexCoord2f(1, 0); glVertex3f( 50.f, 50.f, -50.f);
glTexCoord2f(1, 1); glVertex3f( 50.f, 50.f, 50.f);
glEnd();
App.Display();
}
return EXIT_SUCCESS;
}
-
I changed sf::Window to sf::RenderWindow and kablamo it works now.
-
You should be able to do it with just a sf::Window, you problable need to enable a state in order to use textures that sf::RenderWindow does for you. I think there is a flag just for using texturss. glEnable( GL_TEXTURE ) or something like that. Not 100% certain. Look up Nehe's tutorial.