tag:blogger.com,1999:blog-84549385091511270042024-02-20T03:24:00.211-08:00Programmingganjahttp://www.blogger.com/profile/02869748348436863447noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8454938509151127004.post-12733358369790704982011-02-03T04:19:00.000-08:002011-02-03T04:19:11.452-08:00Bezier surface without using Evaluators OpenGlHi all, <br />
This is my first post that contains some actual code. I wont provide the entire code for any of the posts, as it will be a spoon feeding. I will off-course give substantial part of it. (And to much extent with any optimizations, leaving them to the imagination of the user :))<br />
<br />
We will use <a href="http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm">DeCasteljau Algorithm</a> algorithm to draw the surface.The similar code using evaluators is available in Red Book Chapter 13.<br />
<br />
Before going onto the code please learn a bit of opengl, as i will only explain how to calculate the points.<br />
<br />
<pre class="brush: cpp">
GLfloat ctrlpoints[4][4][3];
//set the control points as u wish, 4 sets, 4 points each, x, y, z
int LOD=20;
//increase or decrease this (may be using +/-) to increase
number of curves making the surface
/* simple linear interpolation between two points
in three dimension*/
void lerp (GLfloat dest[], GLfloat a[], GLfloat b[], float t)
{
dest[0] = a[0] + (b[0]-a[0])*t;
dest[1] = a[1] + (b[1]-a[1])*t;
dest[2] = a[2] + (b[2]-a[2])*t;
}
// evaluate a point on a bezier-curve. t goes from 0 to 1.0
void bezier (GLfloat dest[],GLfloat a[],GLfloat b[],
GLfloat c[],GLfloat d[], float t)
{
GLfloat ab[3],bc[3],cd[3],abbc[3],bccd[3];
lerp (ab, a,b,t); // point between a and b
lerp (bc, b,c,t); // point between b and c
lerp (cd, c,d,t); // point between c and d
lerp (abbc, ab,bc,t); // point between ab and bc
lerp (bccd, bc,cd,t); // point between bc and cd
lerp (dest, abbc,bccd,t); // point on the bezier-curve
}
/*
Given u and v, compute a point on the surface
*/
void eval(GLfloat dest[],float u,float v,GLfloat pnts[4][4][3])
{
GLfloat Q[4][3];
for(int i=0;i<4;i++)
{
bezier(Q[i],pnts[i][0],pnts[i][1],pnts[i][2],pnts[i][3],u);
}
bezier(dest,Q[0],Q[1],Q[2],Q[3],v);
}
/*
For orthogonal lines, change we need column vise points
This function find transpose of the matrix
*/
void get_vertical(GLfloat dest[4][4][3])
{
for(int i=0;i<4;i++)//ith column
{
for(int j=0;j<4;j++)//row
{
for(int k=0;k<3;k++)//elem
{
dest[i][j][k]=ctrlpoints[j][i][k];
}
}
}
}
void display()
{
/*
Intialization etc code here
*/
/*
Compute using algo
Plot them using GL_LINE_STRIP to get a smooth line, instead of discrete points
*/
for(int i=0;i<LOD;i++)
{
float u = (float)i/(LOD-1);
glBegin(GL_LINE_STRIP);
for(int j=0;j<LOD;j++)
{
float v = (float)j/(LOD-1);
GLfloat p[3];
eval(p,u,v,ctrlpoints);
glVertex3fv(p);
}
glEnd();
}
GLfloat dest[4][4][3];
get_vertical(dest);
for(int i=0;i<LOD;i++)
{
float u = (float)i/(LOD-1);
glBegin(GL_LINE_STRIP);
for(int j=0;j<LOD;j++)
{
float v = (float)j/(LOD-1);
GLfloat p[3];
eval(p,u,v,dest);
glVertex3fv(p);
}
glEnd();
}
/*
Swapping buffer etc code goes here
*/
}
</pre>ganjahttp://www.blogger.com/profile/02869748348436863447noreply@blogger.com8tag:blogger.com,1999:blog-8454938509151127004.post-59405609591944251912011-01-29T23:17:00.000-08:002011-01-30T11:33:56.700-08:00How to Add code snippets to blogger ??This is my first post with something interesting. Although many such posts exist out there, i made a collection of those to make things easy for myself and the guys out there.<br />
<br />
I prefer <a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighlighter</a>, a open source project hosted at <a href="http://code.google.com/">code.google.com</a>.Or now hosted at <a href="http://alexgorbatchev.com/SyntaxHighlighter/">http://alexgorbatchev.com/SyntaxHighlighter/</a><br />
<br />
After you've <a href="http://code.google.com/p/syntaxhighlighter/source/checkout">checked out</a> the the source code, or <a href="http://alexgorbatchev.com/SyntaxHighlighter/download/">downloaded</a> a package (Might be outdated, as compared to trunk) from somewhere, its time to add them to blogger. <br />
Its better if you checkout the latest trunk. <br />
<br />
Now you need to host the source files somewhere, I am using <a href="https://sites.google.com/site/ganjasyntaxhighlighter/">my google sites</a> for this job. We can as well use the code.google.com itself, but the directory structure might change in the future, leaving your blog in a bad look. <br />
<br />
Anyways getting to the point, there are some javascript (.js) files in the code (See Scripts Folder e.g. shBrushCpp.js etc). Each of this script is for a particular language(Cpp for c++ , vb for visual basic etc), so u might want all of them or just few depending on your needs. Use as minimum as required, as javascript can take some time to load. <br />
<br />
Apart from these language specific files, you need to use shCore.js . Also You need the <span id="sites-admin-attach-title-wuid:gx:5a7383880068aed5">SyntaxHighlighter.css which defines all the classes for our use. and other file </span><span id="sites-admin-attach-title-wuid:gx:3b935fd2cae1356">clipboard.swf . <br />
</span> <br />
Upload these files to your webhost (or google sites for that matter).<br />
<br />
<br />
Now add the <a href="http://code.google.com/p/syntaxhighlighter/wiki/Usage">following snippet</a> before </body> tag. <br />
Add you domain name to the to the links before moving on. <br />
<br />
Example program in c++ , <br />
<br />
<br />
<pre class="brush: cpp">int main()
{
printf("Hello World !");
return 0;
}
</pre>ganjahttp://www.blogger.com/profile/02869748348436863447noreply@blogger.com9tag:blogger.com,1999:blog-8454938509151127004.post-11260017959429845402011-01-28T06:00:00.000-08:002011-01-28T06:00:56.562-08:00IntroductionHi All,<br />
<br />
Some of you might be returning visitors from <a href="http://blog.gunjanbansal.in/">http://blog.gunjanbansal.in</a> (Computers and Technology). This is my new blog were i will "try" to hone my programming skills as well as give hints to people. I am not the best programmer out there, but sure I'll try my very best to be. Hope I succeed. Please add a comment to a post if you feel something is not as its supposed to be. I will update it within a day or two. Drop me a mail if i miss.ganjahttp://www.blogger.com/profile/02869748348436863447noreply@blogger.com5