当然,这些只是我7天来业余时间的学习,我觉得这个网址不错,大家如果也想学习opengl,并且具有一定的C语言C++基础,入门课程推荐大家去学习这个网址OpenGL入门学习 - CrazyXiaoM - 博客园
我的这些代码等都是从这个网址学习的,推荐你还是去这个网址学习,那更全更准确。
PS:“今天”(发表文章的今天)把我入门学习的资料送给一块儿学习的同学了,看着那厚厚的一叠折折状状的资料,我穿越了,我很清晰的看到了我接下来的学习生活--将会更加投入,将会有更厚更厚的资料进入我的大脑!
10.14
今天写了15个程序。这些是画二维图形的基础。
开始的时候犯了一个错误,以为坐标范围是像素点呢,后来才知道坐标范围是-1~1;(0,0)在中心。
第三个程序到第15个程序都是在练习glBegin()的使用,最后可以画一个近似圆的多边形。
GL_POINTS,GL_LINES,GL_LINE_STRIP,GL_LINE_LOOP,GL_TRIANGLES,GL_TRIANGLE_STRIP,GL_TRIANGLE_FAN
GL_POLYGON
#include<GL/GLUT.H>
void myDisplay(void){
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f, -0.4f, 0.5f, 0.5f);
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;
}
2.
#include <GL/glut.h>
void myDisplay(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRectf(-0.5f,-0.5f,0.5f,0.5f);
glFlush();
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(400, 400);
glutInitWindowSize(200, 200);
glutCreateWindow("独立");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
3.
4.
- #include<GL/glut.h>
- void myDisplay(){
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_LINES);
- //glVertex2i(100, 100);
- //glVertex2i(50, 50);
- //glVertex2i(70, 70);
- glVertex2f(0.3f, 0.3f);
- glVertex2f(-0.3f, -0.3f);
- glVertex2f(0.5f, 0.5f);
- glVertex2d(0.2,0.5);
- glVertex2d(-0.4, -0.6);
- glVertex2d(0.3,0.6);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study03");
- glutDisplayFunc(&myDisplay);
- glutMainLoop();
- return 0;
- }
5.
- #include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_LINE_STRIP);
- glVertex2f(0.1f, 0.8f);
- glVertex2f(-0.1f, -0.8f);
- glVertex2f(0.1f, -0.8f);
- glVertex2f(-0.1f, 0.8f);
- glVertex2f(0.1f, 0.9f);
- glVertex2f(0.4f, 0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }
6.1
- #include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_LINE_LOOP);
- glVertex2f(0.2f, 0.2f);
- glVertex2f(-0.2f, 0.2f);
- glVertex2f(-0.2f, -0.2f);
- glVertex2f(0.2f, -0.2f);
- //glVertex2f(0.1f, 0.9f);
- //glVertex2f(0.4f, 0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }
7
- #include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLES);
- //glVertex2f(0.2f, 0.2f);
- //glVertex2f(-0.2f, 0.2f);
- //glVertex2f(-0.2f, -0.2f);
- glVertex2f(0.8f, 0.4f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.0f, 0.0f);
- //glVertex2f(0.1f, 0.9f);
- //glVertex2f(0.4f, 0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }
7.1
- #include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLES);
- glVertex2f(0.2f, 0.2f);
- glVertex2f(-0.2f, 0.2f);
- glVertex2f(-0.2f, -0.2f);
- glEnd();
- glBegin(GL_TRIANGLES);
- glVertex2f(0.8f, 0.4f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.0f, 0.0f);
- //glVertex2f(0.1f, 0.9f);
- //glVertex2f(0.4f, 0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }
- #include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLES);
- glVertex2f(0.2f, 0.2f);
- glVertex2f(-0.2f, 0.2f);
- glVertex2f(-0.2f, -0.2f);
- glVertex2f(0.8f, 0.4f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.0f, 0.0f);
- //glVertex2f(0.1f, 0.9f);
- //glVertex2f(0.4f, 0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }
7.3
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLES);
- glVertex2f(0.2f, 0.2f);
- glVertex2f(-0.2f, 0.2f);
- glVertex2f(-0.2f, -0.2f);
- glVertex2f(0.8f, 0.4f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.1f, 0.9f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.9f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLE_STRIP);
- glVertex2f(0.0f,0.0f);
- glVertex2f(0.4f,0.8f);
- glVertex2f(0.8f,0.4f);
- glVertex2f(0.8f,0.5f);
- glVertex2f(-0.1f,0.9f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
9.
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_TRIANGLE_FAN);
- glVertex2f(0.0f,0.0f);
- glVertex2f(0.4f,0.8f);
- glVertex2f(0.8f,0.4f);
- glVertex2f(0.8f,-0.5f);
- glVertex2f(0.1f,-0.9f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
10.
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_POLYGON);
- glVertex2f(0.0f,0.0f);
- glVertex2f(0.8f,0.4f);
- glVertex2f(0.4f,0.8f);
- glVertex2f(0.8f,-0.5f);
- glVertex2f(0.1f,-0.9f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
11.
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_QUADS);
- glVertex2f(0.2f, 0.2f);
- glVertex2f(0.2f, -0.2f);
- glVertex2f(-0.2f,-0.2f);
- glVertex2f(-0.2f,0.2f);
- glVertex2f(0.3f, 0.3f);
- glVertex2f(0.3f, 0.7f);
- glVertex2f(0.7f,1.0f);
- glVertex2f(0.7f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
12
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const int n = 20;
- const GLfloat R = 0.5f;
- const GLfloat Pi = 3.14159265358979f;
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_POLYGON);
- for(int i=0;i<n;++i)
- {
- glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
- }
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
12.1
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const int n = 30;
- const GLfloat R = 0.5f;
- const GLfloat Pi = 3.14159265358979f;
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_POLYGON);
- for(int i=0;i<n;++i)
- {
- glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
- }
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
12.2
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const int n = 50;
- const GLfloat R = 0.5f;
- const GLfloat Pi = 3.14159265358979f;
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_POLYGON);
- for(int i=0;i<n;++i)
- {
- glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
- }
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
13
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat R = 0.5f;
- const GLfloat Pi = 3.14159265358979f;
- void myDisplay()
- {
- GLfloat PointA[2] = {0,R};
- GLfloat PointB[2] = {R*cos(162*Pi/180),R*sin(162*Pi/180)};
- GLfloat PointC[2] = {R*cos(234*Pi/180),R*sin(234*Pi/180)};
- GLfloat PointD[2] = {R*cos(306*Pi/180),R*sin(306*Pi/180)};
- GLfloat PointE[2] = {R*cos(18*Pi/180),R*sin(18*Pi/180)};
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_LINE_LOOP);
- glVertex2fv(PointA);
- glVertex2fv(PointC);
- glVertex2fv(PointE);
- glVertex2fv(PointB);
- glVertex2fv(PointD);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
10.15
以下程序诸个描述
14.用很多线段的连线去模拟一个正玄函数线。用多条短的线段去模拟复杂的曲线或弧线似乎是一个很好的思维,在这思维下可以画出很多复杂的函数曲线
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- void myDisplay()
- {
- GLfloat x;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glBegin(GL_LINES);
- glVertex2f(-1.0f, 0.0f);
- glVertex2f(1.0f, 0.0f);
- glVertex2f(0.0f, -1.0f);
- glVertex2f(0.0f, 1.0f);
- glEnd();
- glBegin(GL_LINE_STRIP);
- for(x=-1.0f/factor;x<1.0f/factor;x+=0.01)
- {
- glVertex2f(x*factor, sin(x)*factor);
- }
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
15.上周上机实验时设置点的大小总是不通过,今天发现设置点的大小不是那么难。
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- void myDisplay()
- {
- GLfloat x;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPointSize(5.0f);
- glBegin(GL_POINTS);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.4f, 0.4f);
- glVertex2f(-0.2f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- void myDisplay()
- {
- GLfloat x;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPointSize(50.0f);
- glBegin(GL_POINTS);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.4f, 0.4f);
- glVertex2f(-0.2f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- void myDisplay()
- {
- GLfloat x;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glLineWidth(5.0f);
- glBegin(GL_LINE_STRIP);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.4f, 0.4f);
- glVertex2f(-0.2f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
16.1 设置了夸张的线宽
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- void myDisplay()
- {
- GLfloat x;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glLineWidth(50.0f);
- glBegin(GL_LINE_STRIP);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.4f, 0.4f);
- glVertex2f(-0.2f,0.3f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
17. 画圆。圆心和周长上的点的连线,线加粗。
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- class screenPt
- {
- private :
- GLint x,y;
- public:
- screenPt()
- {
- x=y=0;
- }
- void setCoords(GLint xCoordValue,GLint yCoordValue)
- {
- x=xCoordValue;
- y=yCoordValue;
- }
- GLint getx() const
- {
- return x;
- }
- GLint gety() const
- {
- return y;
- }
- void incrementx()
- {
- x++;
- }
- void decrementy()
- {
- y--;
- }
- };
- void line(GLint x1, GLint y1, GLint x2, GLint y2){
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0, 0.0, 0.0);
- glLineWidth(5.0f);
- glBegin(GL_LINES);
- glVertex2f(x1/200.0f, y1/200.0f);
- glVertex2f(x2/200.0f, y2/200.0f);
- //glEnd();
- glFlush();
- }
- void circleMidpoint(GLint xc,GLint yc,GLint radius)
- {
- screenPt circPt;
- GLint p=1-radius;
- circPt.setCoords(0,radius);
- void circlePlotPoints (GLint,GLint,screenPt);
- circlePlotPoints(xc, yc, circPt);
- while(circPt.getx()<circPt.gety())
- {
- circPt.incrementx();
- if(p<0)
- p+=2*circPt.getx() +1;
- else
- {
- circPt.decrementy();
- p+=2*(circPt.getx()-circPt.gety())+1;
- }
- circlePlotPoints(xc,yc,circPt);
- }
- }
- void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)
- {
- line(xc, yc, xc+circPt.getx(),yc+circPt.gety());
- line(xc, yc, xc-circPt.getx(),yc+circPt.gety());
- line(xc, yc, xc+circPt.getx(),yc-circPt.gety());
- line(xc, yc, xc-circPt.getx(),yc-circPt.gety());
- line(xc, yc, xc+circPt.gety(),yc+circPt.getx());
- line(xc, yc, xc-circPt.gety(),yc+circPt.getx());
- line(xc, yc, xc+circPt.gety(),yc-circPt.getx());
- line(xc, yc, xc-circPt.gety(),yc-circPt.getx());
- }
- void Mycircle(void)
- {
- circleMidpoint(100,80,50);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(&Mycircle);
- glutMainLoop();
- return 0;
- }
- </strong></span>
17.1 画圆。圆心和周长上的点的连线,线稍细。
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- const GLfloat factor = 0.1f;
- class screenPt
- {
- private :
- GLint x,y;
- public:
- screenPt()
- {
- x=y=0;
- }
- void setCoords(GLint xCoordValue,GLint yCoordValue)
- {
- x=xCoordValue;
- y=yCoordValue;
- }
- GLint getx() const
- {
- return x;
- }
- GLint gety() const
- {
- return y;
- }
- void incrementx()
- {
- x++;
- }
- void decrementy()
- {
- y--;
- }
- };
- void line(GLint x1, GLint y1, GLint x2, GLint y2){
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0, 0.0, 0.0);
- glLineWidth(1.0f);
- glBegin(GL_LINES);
- glVertex2f(x1/200.0f, y1/200.0f);
- glVertex2f(x2/200.0f, y2/200.0f);
- //glEnd();
- glFlush();
- }
- void circleMidpoint(GLint xc,GLint yc,GLint radius)
- {
- screenPt circPt;
- GLint p=1-radius;
- circPt.setCoords(0,radius);
- void circlePlotPoints (GLint,GLint,screenPt);
- circlePlotPoints(xc, yc, circPt);
- while(circPt.getx()<circPt.gety())
- {
- circPt.incrementx();
- if(p<0)
- p+=2*circPt.getx() +1;
- else
- {
- circPt.decrementy();
- p+=2*(circPt.getx()-circPt.gety())+1;
- }
- circlePlotPoints(xc,yc,circPt);
- }
- }
- void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)
- {
- line(xc, yc, xc+circPt.getx(),yc+circPt.gety());
- line(xc, yc, xc-circPt.getx(),yc+circPt.gety());
- line(xc, yc, xc+circPt.getx(),yc-circPt.gety());
- line(xc, yc, xc-circPt.getx(),yc-circPt.gety());
- line(xc, yc, xc+circPt.gety(),yc+circPt.getx());
- line(xc, yc, xc-circPt.gety(),yc+circPt.getx());
- line(xc, yc, xc+circPt.gety(),yc-circPt.getx());
- line(xc, yc, xc-circPt.gety(),yc-circPt.getx());
- }
- void Mycircle(void)
- {
- circleMidpoint(100,80,50);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(&Mycircle);
- glutMainLoop();
- return 0;
- }
- </strong></span>
18. 设置线的模式
- <span style="font-size:18px;"><strong><span style="white-space:pre"> </span>glEnable(GL_LINE_STIPPLE);//激活模式选择
- glLineStipple(2,0x3333);//单位线,虚实匹配</strong></span>
- <span style="font-size:18px;"><strong>#include <GL/glut.h>
- #include<math.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glEnable(GL_LINE_STIPPLE);
- glLineStipple(2,0x3333);
- glLineWidth(3.0f);
- glBegin(GL_LINES);
- glVertex2f(-1.0f, 0.0f);
- glVertex2f(1.0f, 0.0f);
- glVertex2f(0.0f, -1.0f);
- glVertex2f(0.0f, 1.0f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
19.练习一下程序的基本流程。不能看书,自己敲完。
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glLineWidth(5.0f);
- glBegin(GL_LINES);
- glVertex2f(0.0f, 0.0f);
- glVertex2f(0.6f,0.8f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式
- glPolygonMode(GL_BACK,GL_LINE); //设置反面为线性模式
- glFrontFace(GL_CCW);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
20.
- <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);</strong></span>
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式
- glPolygonMode(GL_BACK,GL_LINE); //设置反面为线性模式
- glFrontFace(GL_CW);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
21 面具有两个面,可以剔除一个面,当面被挡着时可以剔除一个面。
glEnable(GL_CULL_FACE);//opengl是一个状态机,需要激活才能使用一些功能
glCullFace(GL_FRONT);//剔除正面
- <span style="font-size:18px;"><strong>//glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);</strong></span>
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式
- glPolygonMode(GL_BACK,GL_LINE); //设置反面为线性模式
- glFrontFace(GL_CCW);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glColor3f(1.0,0.0,0.0);
- //glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glDisable(GL_CULL_FACE);
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
21.1剔除了一个面后,被挡的面出现
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- void myDisplay()
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glPolygonMode(GL_FRONT,GL_FILL); //设置正面为填充模式
- glPolygonMode(GL_BACK,GL_LINE); //设置反面为线性模式
- glFrontFace(GL_CCW);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glColor3f(1.0,0.0,0.0);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_FRONT);
- glBegin(GL_POLYGON);
- glVertex2f(-0.5f, -0.5f);
- glVertex2f(0.0f,-0.5f);
- glVertex2f(0.0f,0.0f);
- glVertex2f(-0.5f,0.0f);
- glEnd();
- glDisable(GL_CULL_FACE);
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
22 画板,镂空的实现
- <span style="font-size:18px;"><strong><span style="white-space:pre"> </span>glEnable(GL_POLYGON_STIPPLE);激活多面体镂空模式
- glPolygonStipple(Mask); 镂空数组</strong></span>
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<stdio.h>
- #include<STDLIB.H>
- void myDisplay()
- {
- static GLubyte Mask[128];
- FILE *fp;
- fp = fopen("mmmm.bmp", "rb");
- if(!fp)
- exit(0);
- if(fseek(fp, -(int)sizeof(Mask),SEEK_END))
- exit(0);
- if(!fread(Mask,sizeof(Mask),1,fp))
- exit(0);
- fclose(fp);
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glEnable(GL_POLYGON_STIPPLE);
- glPolygonStipple(Mask);
- glRectf(-0.5f,-0.5f,0.0f,0.0f);
- glDisable(GL_POLYGON_STIPPLE);
- glRectf(0.0f,0.0f,0.5f,0.5f);
- glFlush();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
23 默认光滑模式
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<math.h>
- const GLdouble Pi = 3.1415926536;
- void myDisplay()
- {
- int i;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_TRIANGLE_FAN);
- glColor3f(0.0,0.0,1.0);
- glVertex2f(0.0f,0.0f);
- for(i=0;i<=8;++i)
- {
- glColor3f(i&0x04, i&0x02, i&0x01);
- glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));
- }
- glEnd();
- glFlush();
- }
- int main(int argv, char* argc[])
- {
- glutInit(&argv, argc);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- //Sleep(10*1000);
- glutMainLoop();
- return 0;
- }</strong></span>
23.1 设置了清除色
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<math.h>
- const GLdouble Pi = 3.1415926536;
- void myDisplay()
- {
- int i;
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_TRIANGLE_FAN);
- //glColor3f(0.0,0.0,1.0);
- glVertex2f(0.0f,0.0f);
- for(i=0;i<=8;++i)
- {
- glColor3f(i&0x04, i&0x02, i&0x01);
- glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));
- }
- glEnd();
- glFlush();
- }
- int main(int argv, char* argc[])
- {
- glutInit(&argv, argc);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- //Sleep(10*1000);
- glutMainLoop();
- return 0;
- }</strong></span>
23.2
- <span style="font-size:18px;"><strong>glShadeModel(GL_FLAT);采用平板展现模式---其对应光滑渐变模式</strong></span>
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<math.h>
- const GLdouble Pi = 3.1415926536;
- void myDisplay()
- {
- int i;
- glShadeModel(GL_FLAT);
- glClearColor(1.0,1.0,1.0,0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_TRIANGLE_FAN);
- //glColor3f(0.0,0.0,1.0);
- glVertex2f(0.0f,0.0f);
- for(i=0;i<=8;++i)
- {
- glColor3f(i&0x04, i&0x02, i&0x01);
- glVertex2f((float)cos(i*Pi/4), (float)sin(i*Pi/4));
- }
- glEnd();
- glFlush();
- }
- int main(int argv, char* argc[])
- {
- glutInit(&argv, argc);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(myDisplay);
- //Sleep(10*1000);
- glutMainLoop();
- return 0;
- }</strong></span>
24 今天是周一,明天周二,计算机图形学上机实验,不能太给老是丢人,就勉强自己写了个三维的,借用隔壁同学的方法使它旋转起来了,发现这方法竟然是下一天的课程,呵呵
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<windows.h>
- #include<math.h>
- static int day = 200;
- void myDisplay()
- {
- glEnable(GL_DEPTH_TEST); //启动深度测试
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清空深度缓冲和颜色缓冲
- glMatrixMode(GL_PROJECTION); //操作投影矩阵
- glLoadIdentity(); //进行变换前通常把当前矩阵设置为单位矩阵
- gluPerspective(75,1,1,400000000); //设置可视空间,得到透视效果(可视角,高宽比,最近可视距离,最远可视距离)
- glMatrixMode(GL_MODELVIEW); //设置当前操作的矩阵为“模型视图矩阵”
- glLoadIdentity(); //把当前矩阵设置为单位矩阵
- gluLookAt(0,-200000000,200000000,0,0,0,0,0,1); //设定观察点位置(观察点位置,目标位置,观察者上方向)
- glColor3f(1.0f,0.0f,0.0f);
- //glRotatef(day/360.0*360.0, 0.0f,0.0f,-1.0f);
- glutSolidSphere(69600000,50,50);
- glColor3f(0.0f,0.0f,1.0f);
- glRotatef(day, 0.0f,0.0f,-1.0f);
- glTranslatef(150000000,0.0f,0.0f);
- glutSolidSphere(15945000,50,50);
- glColor3f(1.0f,1.0f,0.0f);
- glRotatef(day/30.0*360.0-day,0.0f,0.0f,-1.0f);
- glTranslatef(38000000,0.0f,0.0f);
- glutSolidSphere(4345000,50,50);
- glutSwapBuffers();
- }
- void play()
- {
- day++;
- if(day >= 360)
- day = 0;
- myDisplay();
- Sleep(100);
- glutPostRedisplay();
- }
- int main(int argv, char* argc[])
- {
- glutInit(&argv, argc);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(400,400);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(play);
- glutMainLoop();
- return 0;
- }</strong></span>
10.16
今天只有晚上有时间了,白天都满课
25 光照,材质等,不是很懂,光照必须要会用!
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
- #define WIDTH 400
- #define HEIGHT 400
- static GLfloat angle = 0.0f;
- void myDisplay()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //创建透视效果视图
- glMatrixMode(GL_PROJECTION); //操作投影矩阵
- glLoadIdentity(); //当前矩阵设置为单位矩阵
- gluPerspective(90.0f, 1.0f,1.0f,20.0f); //得到透视效果
- glMatrixMode(GL_MODELVIEW); //操作“模型视图”矩阵
- glLoadIdentity();
- gluLookAt(0.0,0.0,-10.0,0.0,0.0,0.0,0.0,1.0,0.0);
- //定义太阳光源,它是一种白色光源
- {
- GLfloat sun_light_position[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat sun_light_ambient[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat sun_light_diffuse[] = {1.0f,1.0f,1.0f,1.0f};
- GLfloat sun_light_specular[] = {1.0f,1.0f,1.0f,1.0f};
- glLightfv(GL_LIGHT0, GL_POSITION, sun_light_position);
- glLightfv(GL_LIGHT0, GL_AMBIENT, sun_light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, sun_light_specular);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHTING);
- glEnable(GL_DEPTH_TEST);
- }
- //定义太阳的材质并绘制太阳
- {
- GLfloat sun_mat_ambient[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat sun_mat_diffuse[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat sun_mat_specular[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat sun_mat_emission[] = {0.5f,0.0f,0.0f,1.0f};
- GLfloat sun_mat_shininess = 0.0f;
- glMaterialfv(GL_FRONT, GL_AMBIENT, sun_mat_ambient); //环境变量
- glMaterialfv(GL_FRONT, GL_DIFFUSE, sun_mat_diffuse); //散射模式
- glMaterialfv(GL_FRONT, GL_SPECULAR, sun_mat_specular); //镜面反射
- glMaterialfv(GL_FRONT, GL_EMISSION, sun_mat_emission); //发射,散发喷射
- glMaterialf(GL_FRONT, GL_SHININESS, sun_mat_shininess);
- glutSolidSphere(2.0,40,32);
- }
- //定义地球材质并绘制地球
- {
- GLfloat earth_mat_ambient[] = {0.0f,0.0f,0.5f,1.0f};
- GLfloat earth_mat_diffuse[] = {0.0f,0.0f,0.5f,1.0f};
- GLfloat earth_mat_specular[] = {0.0f,0.0f,1.0f,1.0f};
- GLfloat earth_mat_emission[] = {0.0f,0.0f,0.0f,1.0f};
- GLfloat earth_mat_shininess = 30.0f;
- glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_ambient); //环境变量
- glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diffuse); //散射模式
- glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular); //镜面反射
- glMaterialfv(GL_FRONT, GL_EMISSION, earth_mat_emission); //发射,散发喷射
- glMaterialf(GL_FRONT, GL_SHININESS, earth_mat_shininess);
- glRotatef(angle,0.0f,-1.0f,0.0f);
- glTranslatef(5.0f,0.0f,0.0f);
- glutSolidSphere(1.5,40,32);
- }
- glutSwapBuffers();
- }
- void myIdle()
- {
- angle += 1.0f;
- if(angle >= 360.0f)
- angle = 0.0f;
- myDisplay();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(WIDTH, HEIGHT);
- glutCreateWindow("opengl光照演示");
- glutDisplayFunc(&myDisplay);
- glutIdleFunc(&myIdle); //回调
- glutMainLoop();
- return 0;
- }</strong></span>
10.17
今天周三,满课,且晚上还有数据库上机实验,自己电脑不能用,中午看过这课后借同学的手机敲了代码练习
26 列表的使用(一次编译,多次使用,节省效率)、glutIdleFunc(&myIdle)调用cpu空闲资源且控制旋转角度,注意矩阵的push和pop
#include<GL/glut.h>
#include<math.h>
#include<windows.h>
#define WIDTH 400
#define HEIGHT 400
#define ColoredVertex(c,v) do{glColor3fv(c);glVertex3fv(v);}while(0)
GLfloat angle=0.0f;
void myDisplay()
{
static int list = 0;
if(list == 0)
{
GLfloat
PointA[] = {0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},
PointB[] = {-0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6},
PointC[] = {0.0f,-sqrt(6.0f)/12,sqrt(3.0f)/3},
PointD[] = {0.0f,sqrt(6.0f)/4,0};
GLfloat
ColorR[] = {1,0,0},
ColorG[] = {0,1,0},
ColorB[] = {0,0,1},
ColorY[] = {1,1,0};
list = glGenLists(1);
glNewList(list,GL_COMPILE);
glBegin(GL_TRIANGLES);
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorY,PointD);
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorY,PointD);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorY,PointD);
glEnd();
glEndList();
glEnable(GL_DEPTH_TEST);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,1,0.5,0);
glCallList(list);
glPopMatrix();
glutSwapBuffers();
}
void myIdle()
{
++angle;
if(angle >= 360.0f)
angle = 0.0f;
Sleep(1000/10);
myDisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("study");
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}
10.18
今天学颜色的混合,会有半透明的效果
27. glBlendFunc(GL_ONE,GL_ZERO);完全使用源色
#include<GL/glut.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ZERO);
glColor4f(1,0,0,0.5);
glRectf(-1,-1,0.5,0.5);
glColor4f(0,1,0,0.5);
glRectf(-0.5,-0.5,1,1);
glutSwapBuffers();
}
void myIdle()
{
myDisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("study");
myDisplay();
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}
27.1两种颜色混合
glBlendFunc(GL_ONE, GL_ONE);,则表示完全使用源颜色和目标颜色,最终的颜色实际上就是两种颜色的简单相加。例如红色(1, 0, 0)和绿色(0, 1, 0)相加得到(1, 1, 0),结果为黄色
#include<GL/glut.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,GL_ONE); //改动
glColor4f(1,0,0,0.5);
glRectf(-1,-1,0.5,0.5);
glColor4f(0,1,0,0.5);
glRectf(-0.5,-0.5,1,1);
glutSwapBuffers();
}
void myIdle()
{
myDisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("study");
myDisplay();
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}
27.2
GL_ONE_MINUS_SRC_ALPHA:表示用1.0减去源颜色的alpha值来作为因子。
GL_ONE_MINUS_DST_ALPHA:表示用1.0减去目标颜色的alpha值来作为因子。
>#include<GL/glut.h>
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //改动
glColor4f(1,0,0,0.5);
glRectf(-1,-1,0.5,0.5);
glColor4f(0,1,0,0.5);
glRectf(-0.5,-0.5,1,1);
glutSwapBuffers();
}
void myIdle()
{
myDisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("study");
myDisplay();
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}
28 光源,绘制半透明物体,注意深度测试的控制
在进行三维混合时,不仅要考虑源因子和目标因子,还应该考虑深度缓冲区。必须先绘制所有不透明的物体,再绘制半透明的物体。在绘制半透明物体时前,还需要将深度缓冲区设置为只读形式,否则可能出现画面错误。
<span style="font-size:18px;"><strong>#include<GL/glut.h>
//在1,1,-1处设置白色的光源
void setLight()
{
static const GLfloat light_position[] = {1.0f,1.0f,-1.0f,1.0f};
static const GLfloat light_ambient[] = {0.2f,0.2f,0.2f,1.0f};
static const GLfloat light_diffuse[] = {1.0f,1.0f,1.0f,1.0f};
static const GLfloat light_specular[] = {1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0,GL_POSITION, light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
//设置材质
void setMatirial(const GLfloat mat_diffuse[4], GLfloat mat_shininess)
{
static const GLfloat mat_specular[] = {0.0f,0.0f,0.0f,1.0f};
static const GLfloat mat_emission[] = {0.0f,0.0f,0.0f,1.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);
glMaterialf(GL_FRONT,GL_SHININESS,mat_shininess);
}
void myDisplay()
{
//定义一些材质颜色
const static GLfloat red_color[] = {1.0f,0.0f,0.0f,1.0f};
const static GLfloat green_color[] = {0.0f,1.0f,0.0f,0.3333f};
const static GLfloat blue_color[] = {0.0f,0.0f,1.0f,0.5f};
//清除屏幕
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//设置光源
setLight();
//启动混合并设置混合因子
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//以(0,0,0.5)为中心,绘制一个半径为0.3的不透明红色球体(离观察者最远)
setMatirial(red_color, 30.0);
glPushMatrix();
glTranslatef(0.0f,0.0f,0.5f);
glutSolidSphere(0.3,30,30);
glPopMatrix();
//绘制半透明物体
glDepthMask(GL_FALSE);
//以(0.2,0,-0.5)为中心,绘制一个半径为0.2的半透明蓝色球体(离观察者最近)
setMatirial(blue_color, 30.0);
glPushMatrix();
glTranslatef(0.2f,0.0f,-0.5f);
glutSolidSphere(0.2,30,30);
glPopMatrix();
//以(0.1,0,0)为中心,绘制一个半径为0.15的半透明绿色球体(在两球体之间)
setMatirial(green_color, 30.0);
glPushMatrix();
glTranslatef(0.1,0,0);
glutSolidSphere(0.15,30,30);
glPopMatrix();
//深度缓冲区恢复为可读可写模式
glDepthMask(GL_TRUE);
glutSwapBuffers();
}
void myIdle()
{
myDisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("study");
myDisplay();
glutDisplayFunc(&myDisplay);
//glutIdleFunc(&myIdle);
glutMainLoop();
return 0;
}</strong></span>