Friday, August 5, 2016

glEnableVertexAttribArray(GLuint attrib_index) .. GLVertexAttribPointer .. demo

DataSourceHereGoes

Data block



  • extern glm::vec3 im_cubeVertex[36];
  • extern glm::vec2 im_cubeTexCoord[6];
  • extern glm::vec3 im_cubeNormal[6];
  • extern glm::vec3 im_groundVertex[6];



[Arrangement_0_]

if upload to GPU as the way followed below:


  • Cube                           Ground                      Torus
  • Pos_Tex_Normals       Pos_Tex_Normals     Pos_Tex_Normals
  • Pos_Tex_Normals       Pos_Tex_Normals     Pos_Tex_Normals
  • Pos_Tex_Normals       Pos_Tex_Normals     Pos_Tex_Normals
  • ...
  • Pos_Tex_Normals       Pos_Tex_Normals     Pos_Tex_Normals

then a specification of the data uploaded should be something like this:
glEnableVertexAttribArray(0);
glVertexAttribPointer(
         0, 3
          , GL_FLOAT, GL_FALSE
          , (3 + 2 + 3) * 3 * sizeof(GLfloat) /// stride
          , (GLvoid*)(0) /// offset
);

glEnableVertexAttribArray(1);
glVertexAttribPointer(
          1, 2 
           , GL_FLOAT, GL_FALSE
           , (3 + 2 + 3) * 3 * sizeof(GLfloat)
           , (GLvoid*)(3 * sizeof(GLfloat)));

glEnableVertexAttribArray(2);
glVertexAttribPointer( 
           2, 3 
           , GL_FLOAT, GL_FALSE 
           , (3 + 2 + 3) * 3 * sizeof(GLfloat) 
           , (GLvoid*)((3 + 2) * sizeof(GLfloat))
);,,,

[Arrangement_1_]

if upload to GPU as the way followed below:
                 Position__TextureCoord__Normals
Cube         f3_______f2___________f3
                  f3_______f2___________f3
                  ...
                  f3_______f2___________f3     ->End Cube Vertex Data


Ground      f3_______f2___________f3

                  f3_______f2___________f3
                  ...
                  f3_______f2___________f3     ->End Ground Vertex Data

Torus         f3_______f2___________f3
                  f3_______f2___________f3
                  ...
                  f3_______f2___________f3     ->End Torus Vertex Data

then a specification of the data uploaded should be something like this:



glEnableVertexAttribArray(0);
glVertexAttribPointer( 
           0, 3 
           , GL_FLOAT, GL_FALSE 
           , (3 + 2 + 3) * sizeof(GLfloat) /// stride 
           , (GLvoid*)(0)                         /// offset
);

glEnableVertexAttribArray(1)
;glVertexAttribPointer(
           1, 2 
           , GL_FLOAT, GL_FALSE 
           , (3 + 2 + 3) * sizeof(GLfloat)
           , (GLvoid*)(3 * sizeof(GLfloat)));

glEnableVertexAttribArray(2);
glVertexAttribPointer( 
           2, 3 
           , GL_FLOAT, GL_FALSE 
           , (3 + 2 + 3) * sizeof(GLfloat) 
           , (GLvoid*)((3 + 2) * sizeof(GLfloat))
);


____________________________________________________________
#include "common_header.h"

#include "static_geometry.h"


int TorusVertexAdded(CVertexBufferObject &vboDest

                  , float mIsRadiusTotal
                  , float mIsRadiusTube
                  , int iSubDivAround
                  , int iSubDivTube)
{
float fAddAngleAround = 360.0f/(float)iSubDivAround;
float fAddAngleTube = 360.0f/(float)iSubDivTube;

float fCurAngleAround = 0.0f;

int iStepsAround = 1;
const float PI = float(atan(1.0)*4.0);

int iFacesAdded = 0;


while(iStepsAround <= iSubDivAround)

{
float fSineAround = sin(fCurAngleAround/180.0f*PI);
float fCosineAround = cos(fCurAngleAround/180.0f*PI);
glm::vec3 vDir1(fSineAround, fCosineAround, 0.0f);
float fNextAngleAround = fCurAngleAround+fAddAngleAround;
float fNextSineAround = sin(fNextAngleAround/180.0f*PI);
float fNextCosineAround = cos(fNextAngleAround/180.0f*PI);
glm::vec3 vDir2(fNextSineAround, fNextCosineAround, 0.0f);
float fCurAngleTube = 0.0f;
int iStepsTube = 1;
while(iStepsTube <= iSubDivTube)
{
float fSineTube         = sin(fCurAngleTube/180.0f*PI);
float fCosineTube       = cos(fCurAngleTube/180.0f*PI);
float fNextAngleTube    = fCurAngleTube+fAddAngleTube;
float fNextSineTube     = sin(fNextAngleTube/180.0f*PI);
float fNextCosineTube   = cos(fNextAngleTube/180.0f*PI);
glm::vec3 vMid1 = vDir1*(mIsRadiusTotal-mIsRadiusTube/2), vMid2 = vDir2*(mIsRadiusTotal-mIsRadiusTube/2);
glm::vec3 isQuadPoints[] = 
{
vMid1 + glm::vec3(0.0f, 0.0f, -fNextSineTube*mIsRadiusTube) + vDir1*fNextCosineTube*mIsRadiusTube,
vMid1 + glm::vec3(0.0f, 0.0f, -fSineTube*mIsRadiusTube) + vDir1*fCosineTube*mIsRadiusTube,
vMid2 + glm::vec3(0.0f, 0.0f, -fSineTube*mIsRadiusTube) + vDir2*fCosineTube*mIsRadiusTube,
vMid2 + glm::vec3(0.0f, 0.0f, -fNextSineTube*mIsRadiusTube) + vDir2*fNextCosineTube*mIsRadiusTube
};

glm::vec3 isNormals[] = 

{
glm::vec3(0.0f, 0.0f, -fNextSineTube) + vDir1*fNextCosineTube,
glm::vec3(0.0f, 0.0f, -fSineTube) + vDir1*fCosineTube,
glm::vec3(0.0f, 0.0f, -fSineTube) + vDir2*fCosineTube,
glm::vec3(0.0f, 0.0f, -fNextSineTube) + vDir2*fNextCosineTube
};

glm::vec2 isTexCoords[] = 

{
glm::vec2(fCurAngleAround/360.0f, fNextAngleTube/360.0f),
glm::vec2(fCurAngleAround/360.0f, fCurAngleTube/360.0f),
glm::vec2(fNextAngleAround/360.0f, fCurAngleTube/360.0f),
glm::vec2(fNextAngleAround/360.0f, fNextAngleTube/360.0f)
};

int iIndices[] = {0, 1, 2, 2, 3, 0};


FOR(i, 6)

{
int index = iIndices[i];
vboDest.addData(&isQuadPoints[index], sizeof(glm::vec3));
vboDest.addData(&isTexCoords[index], sizeof(glm::vec2));
vboDest.addData(&isNormals[index], sizeof(glm::vec3));
}
iFacesAdded += 2; // Keep count of added faces
fCurAngleTube += fAddAngleTube;
iStepsTube++;
}
fCurAngleAround += fAddAngleAround;
iStepsAround++;
}
return iFacesAdded;
}

glm::vec3 im_cubeVertex[36] = 

{
// Front face
glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), 
    glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f),
// Back face
glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), 
    glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f),
// Left face
glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), 
    glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, -0.5f),
// Right face
glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), 
    glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f),
// Top face
glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, 0.5f), 
    glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, -0.5f),
// Bottom face
glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, -0.5f), 
    glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, 0.5f),
};
glm::vec2 im_cubeTexCoord[6] = 
{
    glm::vec2(0.0f, 1.0f), 
    glm::vec2(1.0f, 1.0f), 
    glm::vec2(1.0f, 0.0f), 
    glm::vec2(1.0f, 0.0f), 
    glm::vec2(0.0f, 0.0f), 
    glm::vec2(0.0f, 1.0f)
};

glm::vec3 im_cubeNormal[6] = 

{
    glm::vec3(+0.0f, +0.0f, +1.0f),
glm::vec3(+0.0f, +0.0f, -1.0f),
    glm::vec3(-1.0f, +0.0f, +0.0f),
    glm::vec3(+1.0f, +0.0f, +0.0f),
    glm::vec3(+0.0f, +1.0f, +0.0f),
    glm::vec3(+0.0f, -1.0f, +0.0f)
};

glm::vec3 im_groundVertex[6] = 

{
glm::vec3(-100, -10, -100), 
    glm::vec3( +50, -10, -100), 
    glm::vec3(+100, -10, +100), 
    glm::vec3(+100, -10, +100), 
    glm::vec3(-100, -10, +100),
    glm::vec3(-100, -10, -100)
};

No comments:

Post a Comment