OpenGL Voxel Engine

So I have been making a voxel engine using sfml and the opengl that comes with it. Now what I have been aiming for at this step (Which, however easy, has confused me) is to render cube comprised of 27 smaller cubes. However what I have been noticing is, even though all 27 cubes are rendering only one is colored. Since, when I rotate the entire cube structure, when the single colored cube reaches the back it is covered by a mass of black cubes. I have looked over my code for a couple hours and tested many solutions which have all failed, but I still can't seem to locate the problem.
Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdafx.h"
#include "RenderBlock.hpp"
#include "Window.hpp"
#include "SFML/Window.hpp"
#include "SFML/OpenGL.hpp"

 int main()
 {
	 Window Main_Window;
	 Render Main_Render;
	 float x = 0;
	 Main_Window.Start();
	 Main_Render.GlPresets();
	 int Time = 0;
	 while(1){ //Main Loop
Main_Window.Update();
for(int x = 0; x < 3; x++)
{
    for(int y = 0; y < 3; y++)
    {
        for(int z = 0; z < 3; z++)
        {
			Time++;
	     	Main_Render.RenderBlock(x,y,z,Time);
        }
    }
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){return 0;}
Main_Window.Display();
	 }

     return EXIT_SUCCESS;
 }

Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stdafx.h"
#include "SFML\Window.hpp"
#include "SFML\OpenGL.hpp"
#include "Window.hpp"

void Window::Start(){
sf::ContextSettings settings;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 4;
settings.majorVersion = 3;
settings.minorVersion = 0;
  MainWindow.create(sf::VideoMode(1366,768), "TechneQuad!", sf::Style::Default, settings);
}
void Window::Update(){
	sf::Event Update;
	while (MainWindow.pollEvent(Update))
   {
	if (Update.type == sf::Event::Resized)//Resize
	{
		glViewport(0, 0, Update.size.width, Update.size.height);
}
	if (Update.type == sf::Event::Closed)//Close{
		MainWindow.close();
	}}}
void Window::Display(){
	MainWindow.display();
}

Window.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

#include "stdafx.h"

#include "SFML\Window.hpp"

class Window{
	public:
		sf::Window MainWindow;
		void Start();
		void Update();
		void Display();
};


RenderBlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "stdafx.h"
#include "SFML\OpenGL.hpp"
#include "Window.hpp"
#include "RenderBlock.hpp"

int Render::RenderBlock(double x, double y, double z, int Time){

	    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        
        glTranslatef(Time/-250, 50.f, -500.f);
		//glRotatef(Time / 10, 1.f, 0.f, 0.f);
        //glRotatef(Time / 30, 0.f, 1.f, 0.f);
        //glRotatef(Time / 10, 0.f, 0.f, 1.f);
	        glBegin(GL_QUADS);
			
			glRotatef(6, 1.f, 0.f, 0.f);
            glColor3f(1.f, 0.f, 0.f);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
            glVertex3f((-50.f)+50*x,  (50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x,  (50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
			
            glColor3f(1.f, 0.f, 0.f);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y, (50.f)+50*z);
            glVertex3f((-50.f)+50*x,  (50.f)+50*y, (50.f)+50*z);
            glVertex3f(( 50.f)+50*x,  (50.f)+50*y, (50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (-50.f)+50*y, (50.f)+50*z);
			
            glColor3f(0.f, 1.f, 0.f);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
            glVertex3f((-50.f)+50*x,  (50.f)+50*y, (-50.f)+50*z);
            glVertex3f((-50.f)+50*x,  (50.f)+50*y,  (50.f)+50*z);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y,  (50.f)+50*z);
			
            glColor3f(0.f, 1.f, 0.f);
            glVertex3f((50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
            glVertex3f((50.f)+50*x,  (50.f)+50*y, (-50.f)+50*z);
            glVertex3f((50.f)+50*x,  (50.f)+50*y,  (50.f)+50*z);
            glVertex3f((50.f)+50*x, (-50.f)+50*y,  (50.f)+50*z);
			
            glColor3f(0.f, 0.f, 1.f);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y,  (50.f)+50*z);
            glVertex3f((-50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (-50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (-50.f)+50*y,  (50.f)+50*z);
			
            glColor3f(0.f, 0.f, 1.f);
            glVertex3f((-50.f)+50*x, (50.f)+50*y,  (50.f)+50*z);
            glVertex3f((-50.f)+50*x, (50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (50.f)+50*y, (-50.f)+50*z);
            glVertex3f(( 50.f)+50*x, (50.f)+50*y,  (50.f)+50*z);

        glEnd();

		Render_Window.Display();

	return 0;
}

int Render::GlPresets(){
	
	    // Set the color and depth clear values
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);
	return 0;
}

RenderBlock.hpp
1
2
3
4
5
6
7
8
#pragma once
#include "Window.hpp"
class Render{
	public:
	int RenderBlock(double, double, double, int);
	int GlPresets();
	Window Render_Window;
};


-I used #pragma once because of #ifdef's were not working for me strangely-
Last edited on
Your rendering process looks like this:
1
2
3
4
5
for each cube{
    clear buffer
    draw cube
    swap buffers
}
when it should look like this:
1
2
3
4
5
clear buffer
for each cube{
    draw cube
}
swap buffers
Last edited on
I tried moving the clear buffer out before, but it just gave me blackness. Why would I need to swap the buffers, does sfml enable double buffering automatically?
What's with the two windows?

In main:
1
2
 	 Window Main_Window;
	 Render Main_Render;


Where Render is defined as:
1
2
3
4
5
6
class Render{
	public:
	int RenderBlock(double, double, double, int);
	int GlPresets();
	Window Render_Window;
};


One of the windows is never created, but you use it as if it were.


What do you mean? I don't understand what you are trying to say. Those are just objects.
Render_Window Is a object in RenderBlock to the class Window
Main_Window Is a object in Main to the class Window
Main_Render Is a object in Main to the class RenderBlock

I was trying to ask what is the GL command for swapping the buffers?
The buffers are swapped when you call display() on your sf::Window(s) (of which you have two.)

Documented at: http://sfml-dev.org/tutorials/2.0/window-opengl.php under "A typical OpenGL-with-SFML program."

Yeah, I just figured that out, and was writing a post to say never mind :) . Thanks any way cire and helios for the help. I was using this
https://sites.google.com/site/letsmakeavoxelengine/home/basic-block-rendering
just because its written in his own opengl wrapper, so its just like a big page full of psuedo-code.
Topic archived. No new replies allowed.