Jump to content
  • Advertisement
Sign in to follow this  
Matthewj234

OpenGL SFML Depth Buffer not behaving as expected

This topic is 950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have had some experience using OpenGL with C# and OpenTk, and I ported some of the code to draw a rotating cube to the screen. However, my depth buffer seems to be doing some really strange things.

 

It seems to remove the front face, and sometimes one of the side faces. Any help you could offer to setting up the depth buffer correctly would be greatly appreciated.

 

Im32s61.png

 

Below is my code:

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML\OpenGL.hpp>


void cube(float x, float y, float z, float size)
{
	float sizeX2 = size * 2;

	glBegin(GL_TRIANGLES);


	glColor3f(0, 1, 1);
	glVertex3f(x, y + size, z);
	glVertex3f(x, y + size, z + size);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y + size, z);
	glVertex3f(x, y + size, z);

	glColor3f(1, 0, 0);
	glVertex3f(x, y, z);
	glVertex3f(x, y + size, z + size);
	glVertex3f(x, y, z + size);
	glVertex3f(x, y + size, z);
	glVertex3f(x, y + size, z + size);
	glVertex3f(x, y, z);

	glColor3f(1, 1, 0);
	glVertex3f(x, y, z);
	glVertex3f(x, y, z + size);
	glVertex3f(x + size, y, z + size);
	glVertex3f(x + size, y, z + size);
	glVertex3f(x + size, y, z);
	glVertex3f(x, y, z);

	glColor3f(0, 1, 0);
	glVertex3f(x, y, z);
	glVertex3f(x, y + size, z);
	glVertex3f(x + size, y + size, z);
	glVertex3f(x + size, y + size, z);
	glVertex3f(x + size, y, z);
	glVertex3f(x, y, z);


	glColor3f(0, 0, 1);
	glVertex3f(x, y, z + size);
	glVertex3f(x, y + size, z + size);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y, z + size);
	glVertex3f(x, y, z + size);

	glColor3f(0.5f, 0.5f, 0.5f);
	glVertex3f(x + size, y, z);
	glVertex3f(x + size, y + size, z);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y + size, z + size);
	glVertex3f(x + size, y, z + size);
	glVertex3f(x + size, y, z);


	glEnd();
}

int main()
{
	sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
	sf::CircleShape shape(100.f);
	shape.setFillColor(sf::Color::Green);

	std::cout << "SFML Loading";

	// Create a clock for measuring time elapsed
	sf::Clock Clock;

	//prepare OpenGL surface for HSR
	glClearDepth(10.0f);
	glClearColor(0.3f, 0.3f, 0.3f, 0.f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glDepthMask(GL_TRUE);
	glDisable(GL_CULL_FACE);
	//// Setup a perspective projection & Camera position
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();


	bool rotate = true;
	float angle;

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		//Prepare for drawing
		// Clear color and depth buffer
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Apply some transformations for the cube
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0, 0, 0);

		if (rotate){
			angle = Clock.getElapsedTime().asSeconds();
		}
		glRotatef(angle * 50, 1.f, 0.f, 0.f);
		glRotatef(angle * 30, 0.f, 1.f, 0.f);
		glRotatef(angle * 90, 0.f, 0.f, 1.f);
		cube(0.0f, 0.0f, 0.0f, 0.5f);

		GLenum err = glGetError();

		//window.clear();
		//window.draw(shape);
		window.display();
	}

	return 0;
}

Edited by Matthewj234

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!