PDA

View Full Version : centralize a simple 2D curve



sajis997
12-18-2015, 01:39 AM
Hello forum,

I am trying to draw hilbert curve of arbitrary resolution and I want to it to be centralized. The points that are generated to send to the rendering pipeline are as follows:



Total number of points generated: 119
(0, 0, 0)
(0, 1, 0)
(1, 1, 0)
(1, 0, 0)
(2, 0, 0)
(3, 0, 0)
(3, 1, 0)
(2, 1, 0)
(2, 2, 0)
(3, 2, 0)
(3, 3, 0)
(2, 3, 0)
(1, 3, 0)
(1, 2, 0)
(0, 2, 0)
(0, 3, 0)
(0, 4, 0)
(1, 4, 0)
(1, 5, 0)
(0, 5, 0)
(0, 6, 0)
(1, 6, 0)
(2, 6, 0)
(3, 6, 0)
(3, 5, 0)
(2, 5, 0)
(2, 4, 0)
(3, 4, 0)
(4, 4, 0)
(5, 4, 0)
(5, 5, 0)
(4, 5, 0)
(4, 6, 0)
(5, 6, 0)
(6, 6, 0)
(6, 5, 0)
(6, 4, 0)
(6, 3, 0)
(6, 2, 0)
(5, 2, 0)
(5, 3, 0)
(4, 3, 0)
(4, 2, 0)
(4, 1, 0)
(4, 0, 0)
(5, 0, 0)
(5, 1, 0)
(6, 1, 0)
(6, 0, 0)
(7, 0, 0)
(7, 1, 0)
(8, 1, 0)
(8, 0, 0)
(9, 0, 0)
(10, 0, 0)
(10, 1, 0)
(9, 1, 0)
(9, 2, 0)
(10, 2, 0)
(10, 3, 0)
(9, 3, 0)
(8, 3, 0)
(8, 2, 0)
(7, 2, 0)
(7, 3, 0)
(7, 4, 0)
(8, 4, 0)
(8, 5, 0)
(7, 5, 0)
(7, 6, 0)
(8, 6, 0)
(9, 6, 0)
(10, 6, 0)
(10, 5, 0)
(9, 5, 0)
(9, 4, 0)
(10, 4, 0)
(11, 4, 0)
(12, 4, 0)
(12, 5, 0)
(11, 5, 0)
(11, 6, 0)
(12, 6, 0)
(13, 6, 0)
(13, 5, 0)
(13, 4, 0)
(13, 3, 0)
(13, 2, 0)
(12, 2, 0)
(12, 3, 0)
(11, 3, 0)
(11, 2, 0)
(11, 1, 0)
(11, 0, 0)
(12, 0, 0)
(12, 1, 0)
(13, 1, 0)
(13, 0, 0)
(14, 0, 0)
(14, 1, 0)
(15, 1, 0)
(15, 0, 0)
(16, 0, 0)
(16, 1, 0)
(16, 2, 0)
(15, 2, 0)
(14, 2, 0)
(14, 3, 0)
(14, 4, 0)
(15, 4, 0)
(15, 3, 0)
(16, 3, 0)
(16, 4, 0)
(16, 5, 0)
(15, 5, 0)
(14, 5, 0)
(14, 6, 0)
(15, 6, 0)
(16, 6, 0)


The following snippet shows the main rendering command :



...................
...................

//the following function generates the above coordinates
hilbertChung(7,17,HilbertOrientationType::orientat ionA,0,0);

normalizeHilbertCoordinates();


glBufferData(GL_ARRAY_BUFFER,
sizeof(glm::vec3) * mHilbertCoordinates.size(),
&mHilbertCoordinates[0],
GL_STATIC_DRAW);


static const GLfloat color[] = {0.0f,0.0f,0.0f,1.0f};
static const GLfloat depth = 1.0f;

glClearBufferfv(GL_COLOR,0,color);
glClearBufferfv(GL_DEPTH,0,&depth);


//draw stuff

//transfer the scene along the z-axis
glm::mat4 T = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.0f ,mDist));

//centralize the whole scene
glm::mat4 centralizeTranslation = glm::translate(T,glm::vec3(-0.5,-0.5,-0.5));


//the rotation matrix along X concatenated with the translation matrix
glm::mat4 Rx = glm::rotate(centralizeTranslation, glm::radians(static_cast<float>(mRX)),glm::vec3(1.0f,0.0f,0.0f));

//rotation matrix along Y is concatenated with the rotation matrix along X
glm::mat4 MV = glm::rotate(Rx,glm::radians(static_cast<float>(mRY)),glm::vec3(0.0f,1.0f,0.0f));


mModelViewMatrix = mProjectionMatrix * MV;

/*
* Bind the vertex array object and vertex buffer object
* */
glBindVertexArray(mVaoID);
/*
* Now bind it to the context using the GL_ARRAY_BUFFER binding point
* */
glBindBuffer(GL_ARRAY_BUFFER,mVboVerticesID);

/*
* Tell OpenGL to use the data in the buffer to fill the vertex attribute rather than
* using the data we give it using one of the functions as glVertexAttrib*() functions
* */
glEnableVertexAttribArray((GLuint)0);


//set the uniform matrix value that will be passed to the shader
glUniformMatrix4fv(mShader->getUniform("MVP"),1,GL_FALSE,glm::value_ptr(mModelViewMatrix));

glUniform4fv(mShader->getUniform("modelColor"),1,glm::value_ptr(glm::vec4(1.0,1.0,0.0,1.0)));


glDrawArrays(GL_LINE_STRIP,0,mHilbertCoordinates.s ize());

glBindBuffer(GL_ARRAY_BUFFER,0);

glDisableVertexAttribArray(0);
glBindVertexArray(0);

GL_CHECK_ERRORS;

mShader->UnUse();


......................

void Hilbert2DScene::normalizeHilbertCoordinates()
{
for(std::vector<glm::vec3>::iterator it = mHilbertCoordinates.begin(),
itend = mHilbertCoordinates.end();
it != itend;++it)
{
(*it).x /= 7;
(*it).y /= 7;
(*it).z /= 0.5;
}
}


I get the following image

http://i.imgur.com/9oF9UVk.png



As you can see that the generated image is not centralized. Any hint to do the centralize the hilbert curve of arbitrary resolution ?


Thanks