SFML community forums
Help => General => Topic started by: Paci4416 on September 02, 2009, 07:45:23 pm
-
Hello ,
I'm new for SFML and my goal is to build a small game with 3D models.
I'm also new to OpenGL and never used it.
So I found some tut's about OpenGL but i dont know if i can learn from them because i'm using also the sfml.
For example I want to learn from this site:
http://nehe.gamedev.net/lesson.asp?index=01
But I dont know if I need to write everything written there or SFML does somthing for me , and how to add this code with the SFML main loop and other.
Hope you understand me, Thank you in advance. :)
-
This should help you : http://www.sfml-dev.org/tutorials/1.5/window-opengl.php .
-
I read this tut but still that wasnt what I meant.
Look at this code:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01
it's alott of code just to create a blank window with some error handling.
and on the sfml tut with Ogl it took something like 6 commands to create this window.
So i want to know what SFML already does for me in OpenGL so i know what i need to really learn about OpenGL
-
All SFML does is creating a valid OpenGL context. In other words, all the OS specific stuff (wgl**) is done internally, so that you can focus on the useful/funny part (gl***).
-
I am trying to run 3D and also 2D OpenGL with SFML.
I tried modifying some of the tutorials, and got close. But I can't get the previous 3D screen to clear. As you see in the code, I commented out what I thought would work, and as is-- at least you can see what the problem is.
Thanks for any help!
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// Create main window
sf::RenderWindow App(sf::VideoMode(1024, 768, 32), "SFML OpenGL", sf::Style::Fullscreen);
App.PreserveOpenGLStates(true);
// Create a sprite for the background
sf::Image BackgroundImage;
if (!BackgroundImage.LoadFromFile("data/ks9CubeArt.png"))
return EXIT_FAILURE;
sf::Sprite Background(BackgroundImage);
// Load an OpenGL texture.
// We could directly use a sf::Image as an OpenGL texture (with its Bind() member function),
// but here we want more control on it (generate mipmaps, ...) so we create a new one
GLuint Texture = 0;
{
sf::Image Image;
if (!Image.LoadFromFile("data/ks9CubeArt.png"))
return EXIT_FAILURE;
glGenTextures(1, &Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(), Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE, Image.GetPixelsPtr());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
}
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
// Bind our texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture);
glColor4f(1.f, 1.f, 1.f, 1.f);
// Create a clock for measuring the time elapsed
sf::Clock Clock;
// Draw some text on top of our OpenGL object
sf::String Text("ks9.us - Let's play!");
Text.SetColor(sf::Color(0,255,0));
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
// Adjust the viewport when the window is resized
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
/* When I use (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)************
* the cube does not show up as I would think it should. *
* *
* How can I get rid of the ghosting from the previous *
* cube renders, thanks! *********************************************
*/
//glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
// Draw background
App.Draw(Background);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -150.f);
glRotatef(Clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(Clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// Draw a cube
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.Draw(Text);
// Finally, display the rendered frame on screen
App.Display();
}
// Don't forget to destroy our texture
glDeleteTextures(1, &Texture);
return EXIT_SUCCESS;
}
Code::Blocks Project download of source and running exe file in 7 zip format. CBProject (http://ks9.us/wf/cubeTexturedSFML.7z)
-
What happens if you uncomment this line?
-
I do not see the cube at all. Only the sprite and text.
With the comment, I see the cube, but it doesn't erase the previous draws of the cube, so it leaves a trail of previous cube renders.
Thanks for replying, sorting this out will really help.
-
So, the OpenGL sample from the SDK doesn't work with no modification??
-
I'll figure it out, thanks anyways.