开始学习opengl
首先借用http://blog.csdn.net/slience_perseverance/article/details/8096233文章中所给的代码开始第一步的学习。
#include<GL/GLUT.H>
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
\\本行填写画图语句
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("openglѧϰ1");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
为画图的架构
下面绘制第一条折现
之后加入本人填写的语句,可以绘制出一条完整的折现
#include<GL/GLUT.H>
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex2f(-0.8f, 0.1f);
glVertex2f(-0.4f, 0.6f);
glVertex2f(0.2f, 0.8f);
glVertex2f(0.7f, 0.2f);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("opengl1");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
下面的工作为实现Berzier曲线
为了分别显示图,这里调用
void glVertex2fv(const GLfloat *v)
语句
这里的参数为指针形式
其中的点坐标为
GLfloat p1[] ={-0.8f, 0.1f};
GLfloat p2[] ={-0.4f, 0.6f};
GLfloat p3[] ={0.2f, 0.8f};
GLfloat p4[] ={0.7f, 0.2f};
重新绘制点和折线
#include<GL/GLUT.H>
#include <windows.h>
#include <math.h>
#include <gl/GL.h>
GLint num = 4;
GLfloat p1[] ={-0.8f, 0.1f};
GLfloat p2[] ={-0.4f, 0.6f};
GLfloat p3[] ={0.2f, 0.8f};
GLfloat p4[] ={0.7f, 0.2f};
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(2);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2fv(p1);
glVertex2fv(p2);
glVertex2fv(p3);
glVertex2fv(p4);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2fv(p1);
glVertex2fv(p2);
glVertex2fv(p3);
glVertex2fv(p4);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("opengl1");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
之后就是进行绘制bezier曲线了
bezier曲线就是根据点的坐标以及伯恩斯坦基进行计算的
我采用了把bezier曲线分为10段的方法
代码如下
#include<GL/GLUT.H>
#include <windows.h>
#include <math.h>
#include <gl/GL.h>
GLfloat p1[] ={-0.8f, 0.1f};
GLfloat p2[] ={-0.4f, 0.6f};
GLfloat p3[] ={0.2f, 0.8f};
GLfloat p4[] ={0.7f, 0.2f};
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(3);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINTS);
glVertex2f(p1[0],p1[1]);
glVertex2fv(p2);
glVertex2fv(p3);
glVertex2fv(p4);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex2fv(p1);
glVertex2fv(p2);
glVertex2fv(p3);
glVertex2fv(p4);
glEnd();
GLfloat ps[11][2];
GLint i = 0;
for (double t = 0.0; t <= 1.0; t += 0.1)
{
double a1 = pow((1 - t), 3);
double a2 = pow((1 - t), 2) * 3 * t;
double a3 = 3 * t*t*(1 - t);
double a4 = t*t*t;
ps[i][0] = a1*p1[0] + a2*p2[0] + a3*p3[0] + a4*p4[0];
ps[i][1] = a1*p1[1] + a2*p2[1] + a3*p3[1] + a4*p4[1];
i=i+1;
}
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < 11; i++)
{
glVertex2fv(ps[i]);
}
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("opengl1");
glutDisplayFunc(&myDisplay);
glutMainLoop();
return 0;
}
我也是综合了几个方法赶紧这样计算最容易,就采用了这种方法
今天的任务就算进行完了。就连这么简单的一段程序也搞了半天,可见我还有很多需要学习的。
对了,主要要总结的就是opengl里的数组也是从0开始的。我说怎么溢出了半天没查到原因呢。这回记住了
未来几天我会探索不同阶数的Bezier曲线以及B样条曲线
之后就要做Bezier曲面和B样条曲面了,至于光照神马的。。慢慢来吧
GLfloat ps[11][2];
GLint i = 0;
for (double t = 0.0; t <= 1.0; t += 0.1)
{
double a1 = pow((1 - t), 3);
double a2 = pow((1 - t), 2) * 3 * t;
double a3 = 3 * t*t*(1 - t);
double a4 = t*t*t;
ps[i][0] = a1*p1[0] + a2*p2[0] + a3*p3[0] + a4*p4[0];
ps[i][1] = a1*p1[1] + a2*p2[1] + a3*p3[1] + a4*p4[1];
i=i+1;
}
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < 11; i++)
{
glVertex2fv(ps[i]);
}
glEnd();
这是最重要的计算公式,mark一下,嘿嘿