Jump to content
Sign in to follow this  

The 3D book



After a break of several years the 3D book project is back on.   A few short words now on what this blog is about.  I have to deliver my wife to the bus station in a few minutes, then a week alone so may have the time then to explain things.  But the 3D book is something I started in 014 and put several years into, then the break, now on again.  A win32 app with a text window and an ogl window.

I just remembered I had something written on this so here it is

I write to see if anyone in this community of game developers, programmers, enthusiasts,  may be interested in a project I have been developing[off and on] for several years now.

So follows a short description of this project, which I call the 3D-Book project. 

The 3D-Format Reader: A new format of media. Imagine opening a book, the left page is conventional formatted text - on the right page a 3D animation of the subject of the text on the left hand page.  The text page with user input from mouse and keyboard, the 3D page with user intput from a game pad.

    An anatomy text for a future surgeon, with the a beating heart in 3D animation.
    A childrens story adventure book with a 3D fantasy world to enter on the right page.   

    Currently 3D-Format Reader consists of a C++ Windows program: Two "child" windows in a main window frame. Two windows: a text-2D rendering window and a 3D-rendering window.  The text-2D window, as its' name implies, displays text and 2D graphics; it is programmed using Microsoft's DirectWrite text formatting API and Microsoft's Direct2D API for 2D graphics.  The 3D-rendering window uses the OpenGL API.

    A 3DE-Book page is formatted in one of two possible modes: DW_MODE or GL_MODE.  In GL_MODE both windows are shown; the text-2D rendering window is on the left and the 3D OpenGL window is on the right.  In DW_MODE, only the text-2D rendering window is shown, the OpenGL window is hidden (Logically it is still there, it has just been given zero width).

    The 3D-Format Reader reads text files, which consists of the text of the book, control character for the formatting of text, (bold, underline, ...), display of tables,  loading of images(.jpg .png ...), and control of 2D and 3D routines.      

    3D-Reader programming is based on a Model-View-Controller (MVC) architecture.  The MVC design is modular: The Controller component handles user input from the operating system , the Model component processes the input, and the View component sends output back to the user on the display.  Typical Parent-Child windows programs have multiple "call back" window procedures(winProcs): One for the parent window and one for child window.  The MVC model, simplifies message routing by using a call-back window procedure which receives Windows messages for the main window, the text-2D window and the OGL window.   
    A sample MVC program by Song Ho Ahn was used as a template for the 3DE-Reader.

Rushed for time now, so a hasty sign off and thanks for reading.


8 - 21 -18

I spent the last few days working on procedural mesh generation.  First looking to find a bit of code to do what I had in mind.  Which begs the question: What 
did I have in mind?   I just wanted a cube mesh generator such that...  


Input:    An integer  n = units from origin to cube face.


  • The vertices for a unit cube centered on the origin.                                                                    
  • 8n² triangles per cube face.
  • 3 times 8n² verts in clockwise winding order (from the outside of the cube) ready for the rendering pipeline.

Screenshot of some cubes generated with the procedural cube mesh generator.



That was about it for the output requirements.   I did not want to hand code even a single vertex and did not want to load a mesh file.   I was sure the 
code was out there somewhere, but was not finding it.  So, a bit reluctantly at first, I started coding the mesh generator. I started enjoying creating this thing and stopped searching for the "out-there-somewhere" code; although still curious how others did this.


First question: How do we number the verts?  It would be great to conceive of some concise algorithm to put out the cube face verts all in clockwise
order for the outside faces of the cube directly.  That seemed beyond me so I plodded along step by step. 

I decided to just use a simple nested loop to generate the cube face verts and number them in the order they were produced.  The hope(and the presumption) was: The loop code was in some order, running thru the x y and z coordinates in order, from -n to +n, therefore the output would be a recognizable pattern.

The simple nested loop vert generator did not let us down: It gave us a recognizable pattern, at least for this face. It turned out (as expected now) that all six faces have similar recognizable patterns. Plotting the first row or two of verts you can easily see how to run the rest of the pattern. 

Plot of the first(of six) cube faces verts output by the vert generator:  Input of n:  There are (2n+1)² verts per cube face, or 25 verts for n = 2.


This is looking at the x = -n face from the outside of the cube.  To simplify the math it helps to define s = 2n.  Then there are 

                                                                                     (s + 1)²  verts,  or 25 for s = 4

                                                                                      s²  cells on the face, or 16 for 4 = 2.

We are going divide each cell into 2 triangles, so there are 2s² triangles per face, or 32 for s = 4.

Second question: What pattern for the triangles?  How to number the 2s² = 32 triangles? What we want in the end is a bit of code such that... for triangles T[0] thru T[2s²-1]  or T[0] thru T[31]( for n = 4), we have T[N] = f0(N), f1(N), f2(N).  Where f0(N) gives the first vertex of T[N] as a function of N. and f1 and f2 give the second and third verts, all in CW winding order looking into the cube of course. 

Here the choice is a bit arbitrary, but it would seem to make things easier if we can manage to have the order of triangles follow the order of verts to a degree.


Numbering the triangles.



And now the problem becomes: Look at the triangle vert list,  T0 - T8...T31 in the image, and try to discern some pattern leading us to the sought after functions f0(N), f1(N), f2(N) where N is the number of the triangle, 0 thru 2s²-1. This really is the holy grail of this whole effort; then we have T[N] = f0(N), f1(N), f2(N) and that list of verts can be sent directly to the rendering pipeline.  Of course we want these functions to work for all six faces and all 12s² triangles to cover the cube.  But first let's see if we can just do this one face, 0 thru 2s²-1..

Thru a bit of trial and error the 32 triangles(T0 - T31) were ordered as shown.  

Now we have an ordered list of the triangles and the
verts from our loop.

T0 = 0 5 6
T1 = 6 1 0
T2 = 1 6 7
T3 = 7 2 1
T4 = 2 7 8
T5 = 8 3 2
T6 = 3 8 9
T7 = 9 4 3
T8 = 5 10 11 ... T30 T31.

If we can find a pattern in the verts on the right side
of this list; we can implement it in an algorithm and
the rest is just coding.

Pattern recognition:

It appears

T2 = T0 with 1 added to each component
T3 = T1 with 1 added to each component

In general T[N+2] = T[N] with 1 added to each component, until we come to T8 at least.  Also it is hard to recognize a relation between the even and odd  triangles,To see what is happening here it helps to look at an image of the generalized case where n can take on any integer value n > 0.


Looking for patterns in this generalized(for any n) vert plot we see...

We have defined s = 2n.

The 4 corner coordinates(+-n,+-n) of the x = - n cube face, one at each corner (+-n,+-n). 

There are (s+1)² verts/face numbered (0 thru (s+1)² -1).

There are 2s² triangles/face numbered (0 thru 2s² -1). They are indicated in red.

It's not as bad as it looks iff you break it down.  Let's look at the even triangles only and just the 0th vert of these triangles.  For any row we see the number of that first vert of the even triangles just increases by one going down the row. We can even try a relation such as T[N].0 = N/2.  Here  T[N].0 denotes the 0th vert of th Nth triangle. Which works until we have to jump to the next row. Every time we jump a row we T[N+1].0  = T[N].0 + 2 for the first triangle in the higher row.  So we need a corrective term to the T[N].0 = N/2 relation that adds 1 every time we jump a row. We can use computer integer division to generate such a term and N/2s is such a term.  It only changes value when we jump rows and we get our first function ...

                                                                                   f0(N) = N/2 + N/2s.  (even triangles)

Remember the integer division will discard any remainder from the terms and check this works for the entire cube face, but only for the even triangles. What about the odd triangles?  Going back to the triangle vs vert list for the specific case n = 2, s = 4 for the first row; we see for the odd triangles T[N].0  = T[N-1].0 + s  + 2.  And adding this term, s + 2 to the formula for the even triangle 0th vert we get f0[N] for the odd triangles.

                                                                                   f0(N) = N/2 + N/2s + s  + 2. (odd triangles)

Continuing this somewhat tedious analysis for the remaining functions f1(N), f2(N) we eventually have these relations for the x = -n cube face triangles.

                                                                               for N = 0  thru N = 2s² - 1.

                                                                               defining   m = N/2 + N/2s.

                                                                               T[N] = m,  m + s + 1,  m + s + 2       T[N]   =    f0(N), f1(N), f2(N).  (even N)

                                                                               T[N] = m + s + 2,  m + 1,  m              T[N]   =   f0'(N), f1'(N), f2'(N) (odd N)

So it turns out we have two sets of functions for the verts, fn(N) for the even triangles and fn'(N) for the odd.

To recap here; we now have formulae for all the T[N] verts as functions of N and the input parameter n:

Input:    An integer  n = units from origin to cube face.

But this is only for the first face x = -n, we have five more faces to determine.  So the question is: Do these formulae work for the other faces? And the answer is no they do not, but going through a similar analysis for the remaining face gives similar T[N] = f0(N), f1(N), f2(N)  for them. There is still the choice of how to number the remaining triangles and verts on the remaining five faces, and the f0(N), f1(N), f2(N) will depend on the somewhat arbitrary choice of how we do the numbering.  For the particular choice of a numbering scheme I ended up making, it became clear how to determine the f0(N), f1(N), f2(N) for the remaining faces.  It required making generalized vert plots for the remaining five face similar to the previous image. Then these relation emerged...

For face x = -n         T[N]   N(0 thru 2²-1)  we have the f0(N), f1(N), f2(N), even and odd

For face x =  n         T[N]   N(2s² thru 4s²-1)       add (s+1)² to the x=-n face components and reverse the winding order

For face y = -n         T[N]   N(4s² thru 6s²-1)       add 2(s+1)² to the x=-n face components and reverse the winding order

For face y =  n         T[N]   N(6s² thru 8s²-1)       add 3(s+1)² to the x=-n face components 

For face z = -n         T[N]   N(8s²0 thru 10s²-1)   add 4(s+1)² to the x=-n face components 

For face z =  n         T[N]   N(10s²0 thru 12s²-1)  add 5(s+1)² to the x=-n face components and reverse the winding order

And these are enough to allow us to write explicit expressions for all 12n² triangles for all 6 faces T[N] and what remains to be done is to implement these expression in code.  Which turned out to be a much simpler task than finding the f0(N), f1(N), f2(N) and resulted in a surprisingly short bit of code.


I have attempted to make this C++ snippet of code as generic as possible and have removed any dev-platform specific #includes and the like.  GLM, a C++ mathematics library for graphics developed by Christophe Riccio is used.  It is a header only library.


 That is the only outside dependency.


//  Procedural cube face verticies generator

#include <vector>
#include <glm/gtc/matrix_transform.hpp>

struct Triangle
    glm::vec3 vert[3]; // the three verts of the triangle


std::vector<Triangle> cube_Faces(int n)

Input:        integer 'n'; the units from origin to cube face.

Output:        vector<Triangle> glTriangle; container for the 
            12*(2*n)² triangles covering the 6 cube faces.


std::vector<Triangle> cube_Faces(int n){

    size_t number_of_triangles(12*(2*n )*(2*n));
    size_t number_of_face_verts(6*(2*n +1 )*(2*n+1));
    std::vector<glm::vec3> face_verts(number_of_face_verts);
    std::vector<Triangle> glTriangle(number_of_triangles);

//  Generate the 6*(2n +1 )² face verts -------------------------------

    int l(0);
    for(int i = 0; i < 6; i++){
        for(int j = -n; j <= n; j++){
            for(int k = -n; k <= n; k++){
                // Below "ifS" strip out all interior cube verts.
                if( i == 0){ // do yz faces
                    face_verts[l].x = (float)(-n); //x
                    face_verts[l].y = (float)j; //y
                    face_verts[l].z = (float)k;}//z
                if( i == 1){ // do yz faces
                    face_verts[l].x = (float)(n); //x
                    face_verts[l].y = (float)j; //y
                    face_verts[l].z = (float)k;}//z            
                if( i == 2){ // do zx faces
                    face_verts[l].x = (float)j; //x
                    face_verts[l].y = (float)(-n); //y
                    face_verts[l].z = (float)k;}//z
                if( i == 3){ // do zx faces
                    face_verts[l].x = (float)j; //x
                    face_verts[l].y = (float)(n); //y
                    face_verts[l].z = (float)k;}//z
                if( i == 4){ // do xy faces
                    face_verts[l].x = (float)j; //x
                    face_verts[l].y = (float)k; //y
                    face_verts[l].z = (float)(-n);}//z
                if( i == 5){ // do xy faces
                    face_verts[l].x = (float)j; //x
                    face_verts[l].y = (float)k; //y
                    face_verts[l].z = (float)(n);}//z

//  Generate the 12*(2*n)² triangles from the face verts -------

    int s = 2*n;    int q = 2*s*s;    int a = (s+1)*(s+1);
    int f(0);        int r(0);        int h(0);

    for( int N=0; N < number_of_triangles; ){ 
        // triangles already in CW winding 
        if( N <  q || N <  5*q && N > 3*q - 1  ){ 
            // do the even indicies
            f= q*(N/q); r = a*(N/q); h = (N-f)/2 + (N-f)/(2*s) + r;
            glTriangle[N].vert[0] = face_verts[h];
            glTriangle[N].vert[1] = face_verts[s + 1 + h];
            glTriangle[N].vert[2] = face_verts[s + 2 + h];
            N++; f= q*(N/q); r = a*(N/q); h = (N-f)/2 + (N-f)/(2*s) + r;
            // do the odd indicies
            glTriangle[N].vert[0] = face_verts[s + 2 + h];
            glTriangle[N].vert[1] = face_verts[ 1 + h];
            glTriangle[N].vert[2] = face_verts[h];
            N++; f= q*(N/q); r = a*(N/q); h = (N-f)/2 + (N-f)/(2*s) + r;
        // triangles needing reverse order for CW winding
        if( N >  5*q - 1 || N <  3*q && N > q - 1 ){ 
            // do the even indicies
            glTriangle[N].vert[0] = face_verts[s + 2 + h];
            glTriangle[N].vert[1] = face_verts[s + 1 + h];
            glTriangle[N].vert[2] =  face_verts[h];
            N++; f= q*(N/q); r = a*(N/q); h = (N-f)/2 + (N-f)/(2*s) + r;
            // do the odd indicies
            glTriangle[N].vert[0] = face_verts[h];
            glTriangle[N].vert[1] = face_verts[1 + h];
            glTriangle[N].vert[2] = face_verts[s + 2 + h];
            N++; f= q*(N/q); r = a*(N/q); h = (N-f)/2 + (N-f)/(2*s) + r;

//  Normalize the cube to side = 1 ------------------------------

    for(int i = 0; i < number_of_triangles; i++){
        glTriangle[i].vert[0].x = glTriangle[i].vert[0].x/(2.0*(float)n);
        glTriangle[i].vert[0].y = glTriangle[i].vert[0].y/(2.0*(float)n);
        glTriangle[i].vert[0].z = glTriangle[i].vert[0].z/(2.0*(float)n);
        glTriangle[i].vert[1].x = glTriangle[i].vert[1].x/(2.0*(float)n);
        glTriangle[i].vert[1].y = glTriangle[i].vert[1].y/(2.0*(float)n);
        glTriangle[i].vert[1].z = glTriangle[i].vert[1].z/(2.0*(float)n);
        glTriangle[i].vert[2].x = glTriangle[i].vert[2].x/(2.0*(float)n);
        glTriangle[i].vert[2].y = glTriangle[i].vert[2].y/(2.0*(float)n);
        glTriangle[i].vert[2].z = glTriangle[i].vert[2].z/(2.0*(float)n);

    return glTriangle;


The rendering was done using OpenGl.

//  OGL render call to the cube mesh generator - PSUEDOCODE

int n(2);
int cube_triangle_Count = (12*(2*n)*(2*n));
std::vector<Triangle> cube_Triangles(cube_triangle_Count);
cube_Triangles = cube_Faces(n);
glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
glBufferData(GL_ARRAY_BUFFER, cube_Triangles.size()*sizeof(Triangle), &cube_Triangles[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), 0);


This just gets the position attribute of the cube face triangle verts; for the color and other attributes there are a couple of options:  Use separate GL_ARRAY_BUFFERS for the color and other attributes.  Or add attributes to the Triangle struct...

struct Triangle
	glm::vec3 vert[3]; // the three verts of the triangle

Screenshot of the spherified cube.


What's next?

Now that we have the cube mesh what we can do with with it practically unlimited.  The first thing I did was turn it into a sphere.  Playing with tesselating the cube or sphere or stellating it with different patterns;  might do.  Ended up trying a few matrix transformations on the cube mesh.  These are shown in the image below.



These shapes are result short bits of code like the code for the column shape below.

    for(int i = 0; i < number_of_triangles; i++){
        for(int j = 0; j < 3; j++){
            if( glTriangle[i].vert[j].y < 0.5f  && glTriangle[i].vert[j].y  > -0.5f  ){ 
            float length_of_v = sqrt((glTriangle[i].vert[j].x * glTriangle[i].vert[j].x) + (glTriangle[i].vert[j].z * glTriangle[i].vert[j].z));
            glTriangle[i].vert[j].x = 0.5f*glTriangle[i].vert[j].x/length_of_v;
            glTriangle[i].vert[j].z = 0.5f*glTriangle[i].vert[j].z/length_of_v;


Doing this; the blacksmith at his forge analogy soon presents.  The mesh is the ingot, hammer matricies stretch, round and bend it against the fixed geometry of the anvil - coordinate system. I am the smith.   






Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By sidbhati32
      How do I detect the mouse event of moving my mouse left or right and wheel up or down?
      I have used Get_X_LParam for mouse movement and WParam for wheel movement.
      Like DWORD x = HIWORD(wParam) but both of these events return continuous values.

      for eg. if(x>0)
      //do this

      //do this

      the Wparam only returns the same value every time even if I am moving my wheel downwards. Same with Get_X_LParam
    • By Masterbuiler64
      Good Morning, Afternoon, or Evening,
      My name is Dalton Potter and I am a budding game developer looking to learn skills and develop a beautiful project me and my friend came up with a year ago or so and have refined ever since. The idea is a basically a mix of Final Fantasy and Zelda in terms of exploration and battle, but will throw in its own unique features to switch things up a bit. What we have in place so far is the main story and many connecting character back stories, a map of the over world (still not 100% confirmed however), how some of the main characters look (also not 100% confirmed), a few battle and puzzle mechanic ideas, general story progression, locations, a few beta music tracks, and lore. What we lack however is any solid assets or work done on it as neither of us have any expertise in game development, but have both unanimously agreed that this idea is too good to forget and pass up.
      We are currently looking for people to help us work on the project as time goes on and maybe, just maybe, it may grow into a full blown team of people working on a game and eventually sell it on Steam or other client services. Any replies to this topic will be read as soon as possible depending on my schedule. I have also attached a couple photos and sound files of some design concepts we have. I also have a Pastebin made of the entire story and main character back stories, as well as history into how the idea came to be, though I'll let the Pastebin be requested as needed in the future.
      Hopefully this project turns from being just an idea into something amazingly beautiful and playable......it just needs to be created that's all.....
      Thank you in advance,
      Dalton Potter
      P.S. The sound file, "Power and Prestige" is a song that sounds as though it could be used as a trailer theme, and "Curiosity" sounds as though it could be used on a farm at sunrise.
      Source of music was from YouTube, but the groups official site is as follows: http://floatingcloud.net/

      Floating Cloud - Power and Prestige.mp3

      Floating Cloud - Curiosity.mp3

    • By ryt
      I took a look at this video to see the difference between static and dynamic linking. Basically the author uses __declspec(dllexport) to export a function.
      How could we also export classes from the same file? Do we need to put the same keyword before class definition or maybe something else?
    • By ryt
      Consider the following classes and pInt declaration:
      class A { ... }; class B : public A { void function() { int A::*pInt; // use pInt } }; Where does pInt belongs, is it local to B::function() or it's a member of A?
    • By ggenije
      Important: I am trying to realize in scrtach which is performance very low due to it's "virutal level" scrtach->flashplayer->java...
      Also i'm new to this forum so i'm sorry if I missed group (like last time)
      Like a title is saying:
      I have project ,and I get negative feedback on it because some people need 30 min to complete it (what is the planned time)
      but problem is that some people need EVEN 5 hours…(game is incremental/idle/upgrade type so it's important to keep same time ...)
      Of course people with slower computer will have less fps so game will be slower for them,
      so I have created TimeDelta system for each frame to calculate something to do per second
      for example
        Update(){move(TimeDelta*speed)}  so that mean it will be moving speed number of pixels(or units) per second so it will be same for almost each user.

      But problem is next:
      I have to change ySpeed by jumpPower (#PlayerJump in my project)
      when any jump button is pressed
      then in each frame decrease ySpeed by gravity it is(-10 * TimeDelta)
      but when someone have lower fps it will have higher TimeDelta and will fall faster but with same jump it turns out to jump significantly lower that changes core of game
      BUT even worse if fps suddenly in moment of jump then timeDelta would be 1 so player will jump much much MUCH higher , then fall much slower because timeDelta changed in meanwhile…(and the point of my game is about upgrading jump not complete game in first fps drop)


      Then I got an idea to fix TimeDelta (like in unity for rigibody) so it will be rounded like
      if calculated TimeDelta is 0.01834 it will be 0.02 fixed
      if weaker computer is using it the TImeDelta will be 0.143 so runded to 0.14 and so on…

      I did not manage to realize it… i tried to calculate it before main initialization of game objects
      but I'm afraid to fps will drop in moment that is calculating so it will be much diffirent…
      I was trying with empty loop(400)(in scrtach even this is taking time) to calculate it but i'm not sure is it right

      So is there good way to realize this fixed TimeDelta
      I only have timer function to use and time difference between frames

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!