OpenGL视频学习资料:https://pan.baidu.com/s/1muWuuuo1_89AijQRNOcJmg
提取码:xcwn
图元
图元primitive,即图形元素,是可以编辑的最小图形单位。图元是图形软件用于操作和组织画面的最基本的素材。一幅画面由图元组成,图元是一组最简单的、最通用的几何图形或字符。
在OpenGL中最基本的三种图元类型:点Point、线Line、三角形Triangle。多边形Polygon可以看成是三角形带GL_TRIANGLE_TRIPE组成
顶点数组
glVertexAttribPointer可以用来装载一个顶点数组
glEnableVertexAttribArray启用顶点数组
缓存buffer
glGenBuffers分配bufID
glBindBuffer绑定当前操作buf
glBufferData往当前绑定buf拷贝数据
绘制
glDrawArrays绘制启用的顶点数组
代码示例
#include <gl/glew.h>)
#include <gl/freeglut.h>
#ifdef WIN32
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "freeglut.lib")
#endif
#include <math.h>
const GLfloat Pi = 3.1415926536f;
int glut_loop_continue = 1;
enum EVA{EVA_triangle, EVA_Num};
GLuint aVAs[EVA_Num];
enum EB{EB_triangle, EB_NUM};
GLuint aBufs[EB_NUM];
void reshape(int w, int h){
glViewport(0, 0, w, h);
}
void display(){
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 0.0f);
// 画顶点数组
//glDrawArrays(GL_TRIANGLES, 0, 6);
// 画矩形
//glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
// 画圆(多边形趋近)
glBegin(GL_LINE_LOOP);
float r = 0.5f;
int n = 360;
for(int i=0; i < n; ++i)
glVertex2f(r *cos(2*Pi/n*i), r*sin(2*Pi/n*i));
glEnd();
画五角星
按照A->C->E->B->D->A的顺序,可以一笔将五角星画出
GLfloat a = 1 / (2-2*cos(72*Pi/180));
GLfloat bx = a * cos(18 * Pi/180);
GLfloat by = a * sin(18 * Pi/180);
GLfloat cy = -a * cos(18 * Pi/180);
GLfloat
PointA[2] = { 0, a },
PointB[2] = { bx, by },
PointC[2] = { 0.5, cy },
PointD[2] = { -0.5, cy },
PointE[2] = { -bx, by };
glBegin(GL_LINE_LOOP);
glVertex2fv(PointA);
glVertex2fv(PointC);
glVertex2fv(PointE);
glVertex2fv(PointB);
glVertex2fv(PointD);
glEnd();
// 画坐标系
//glColor3f(0.0f, 1.0f, 1.0f);
//glBegin(GL_LINES);
//glVertex2f(-1.0f, 0.0f);
//glVertex2f(1.0f, 0.0f); // 以上两个点可以画x轴
//glVertex2f(0.0f, -1.0f);
//glVertex2f(0.0f, 1.0f); // 以上两个点可以画y轴
//glEnd();
画正弦函数
//glColor3f(1.0f, 1.0f, 0.0f);
//glBegin(GL_LINE_STRIP);
//float factor = 0.1f;
//for(float x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
//{
// glVertex2f(x*factor, sin(x)*factor);
//}
//glEnd();
glutSwapBuffers();
}
void key(unsigned char key, int x, int y){
switch(key)
{
case 27: // Esc
//glutLeaveMainLoop();
glut_loop_continue = 0;
break;
}
}
void idle(){
// 添加你自己的判断条件,比如从缓存中获取到新的图像数据引起窗口重绘
if (1){
glutPostRedisplay();
}
}
void initRC(){
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glPointSize(2.0f);
glLineWidth(5.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glGenVertexArrays(EVA_Num, aVAs);
glBindVertexArray(aVAs[EVA_triangle]);
GLfloat triangles[][2] = {
{ -0.90, -0.90 },
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 },
{ 0.90, 0.90 },
{ -0.85, 0.90 },
};
glGenBuffers(EB_NUM, aBufs);
glBindBuffer(GL_ARRAY_BUFFER, aBufs[EB_triangle]);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
}
void releaseRC(){
glDeleteVertexArrays(EVA_Num, aVAs);
glDeleteBuffers(EB_NUM, aBufs);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
int mainwnd = glutCreateWindow("glut window");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(key);
if (glewInit() != GLEW_OK){
return -1;
}
initRC();
// 设置窗口关闭后继续执行程序,而不是exit直接退出了程序
//glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
while (glut_loop_continue){
if (glutGetWindow() == 0)
break; // 窗口关闭时退出事件循环
glutMainLoopEvent();
idle();
Sleep(1); // 循环休眠,防止占用CPU过高
}
releaseRC();
return 0;
}
效果图:
这里是画了个圆和五角星,你可以尝试放开display里面的画顶点数组、画矩形、画圆、画五角星、画坐标系、画正弦函数,看看各种效果。
————————————————
版权声明:本文为CSDN博主「ithewei」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/GG_SiMiDa/article/details/73186817