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.
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;
}