标签:
http://blog.csdn.net/slience_perseverance/article/details/8096233
10月13号下午3:00队长给我开了一个会,10.14号开始学习opengl
今天10月21号,期间,虽然有时候课程很满,但每天都至少写一个程序。
当然,这些只是我7天来业余时间的学习,我觉得这个网址不错,大家如果也想学习opengl,并且具有一定的C语言C++基础,入门课程推荐大家去学习这个网址http://www.cnblogs.com/crazyxiaom/articles/2073586.html
我的这些代码等都是从这个网址学习的,推荐你还是去这个网址学习,那更全更准确。
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
1.
- <span style="font-size:18px;"><strong>#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;
- }
- </strong></span>
2.
- <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);
- 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;
-
- }</strong></span>
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);
- glBegin(GL_POINTS);
- 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("study02");
- glutDisplayFunc(&myDisplay);
- glutMainLoop();
- return 0;
- }
- </strong></span>
4.
- <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_LINES);
-
-
-
-
- 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;
- }
- </strong></span>
5.
- <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_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;
- }</strong></span>
6.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(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);
-
-
- 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>
7
- <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.8f, 0.4f);
- glVertex2f(0.4f, 0.8f);
- glVertex2f(0.0f, 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("Study04");
- glutDisplayFunc(myDisplay);
- glutMainLoop();
- return 0;
- }</strong></span>
7.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(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);
-
-
- 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>
7.2
- <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);
-
-
- 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>
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>
8.
- <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>
15.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);
- 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>
16.设置了线宽
- <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);
-
- 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);
-
- 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);
- <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>
20.opengl中面是具有两面的,opengl画点的顺序不变,但从面的两个面来看这些点的相连顺序相反。设置不同的目标方向,出现的面就不同。
- <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>
- 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>
- 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);
-
- 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);
-
- 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);
-
- 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);
-
- 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);
-
- 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);
-
- 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);
-
- 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
- <span style="font-size:18px;"><strong>#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;
- }</strong></span>
10.18
今天学颜色的混合,会有半透明的效果
27. glBlendFunc(GL_ONE,GL_ZERO);完全使用源色
- <span style="font-size:18px;"><strong>#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;
-
- }</strong></span>
27.1两种颜色混合
glBlendFunc(GL_ONE, GL_ONE);,则表示完全使用源颜色和目标颜色,最终的颜色实际上就是两种颜色的简单相加。例如红色(1, 0, 0)和绿色(0, 1, 0)相加得到(1, 1, 0),结果为黄色
- <span style="font-size:18px;"><strong>#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;
-
- }</strong></span>
27.2
GL_ONE_MINUS_SRC_ALPHA:表示用1.0减去源颜色的alpha值来作为因子。
GL_ONE_MINUS_DST_ALPHA:表示用1.0减去目标颜色的alpha值来作为因子。
- <span style="font-size:18px;"><strong>#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;
-
- }
- </strong></span>
28 光源,绘制半透明物体,注意深度测试的控制
在进行三维混合时,不仅要考虑源因子和目标因子,还应该考虑深度缓冲区。必须先绘制所有不透明的物体,再绘制半透明的物体。在绘制半透明物体时前,还需要将深度缓冲区设置为只读形式,否则可能出现画面错误。
- <span style="font-size:18px;"><strong>#include<GL/glut.h>
-
- 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);
-
-
- setMatirial(red_color, 30.0);
- glPushMatrix();
- glTranslatef(0.0f,0.0f,0.5f);
- glutSolidSphere(0.3,30,30);
- glPopMatrix();
-
-
- glDepthMask(GL_FALSE);
-
-
- setMatirial(blue_color, 30.0);
- glPushMatrix();
- glTranslatef(0.2f,0.0f,-0.5f);
- glutSolidSphere(0.2,30,30);
- glPopMatrix();
-
-
-
-
-
- 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);
-
- glutMainLoop();
- return 0;
-
- }</strong></span>
10.19
29 读取bmp图片的宽度和高度值,代码跟下一个程序开头类似
主代码:
- <pre name="code" class="cpp"><span style="font-size:18px;"><strong>static GLint ImageWidth;
- static GLint ImageHeight;</strong></span></pre>
- <pre></pre>
- <p></p>
- <pre></pre>
- <pre name="code" class="cpp"><span style="font-size:18px;"><strong>
- FILE* pFile = fopen("1234.bmp", "rb");
- if(pFile == 0)
- exit(0);
-
-
- fseek(pFile, 0x0012, SEEK_SET);
- fread(&ImageWidth,sizeof(ImageWidth),1,pFile);
- fread(&ImageHeight,sizeof(ImageHeight),1,pFile);</strong></span></pre><br>
- <p></p>
- <p><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351343996_9120.png" alt=""><br>
- </strong></span></p>
- <p><span style="font-size:18px"><strong><br>
- </strong></span></p>
- <p><span style="font-size:18px"><strong>30 读取bmp图片文件--存像素数值,画出来</strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>
- #include<stdio.h>
- #include<stdlib.h>
-
- #define FileName "1234.bmp"
-
- static GLint ImageWidth;
- static GLint ImageHeight;
- static GLint PixelLength;
- static GLubyte* PixelData;
-
- void display()
- {
-
-
-
-
- glDrawPixels(ImageWidth,ImageHeight,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,PixelData);
-
- glutSwapBuffers();
- }
-
- int main(int argc, char* argv[])
- {
-
- FILE* pFile = fopen("1234.bmp", "rb");
- if(pFile == 0)
- exit(0);
-
-
- fseek(pFile, 0x0012, SEEK_SET);
- fread(&ImageWidth,sizeof(ImageWidth),1,pFile);
- fread(&ImageHeight,sizeof(ImageHeight),1,pFile);
-
-
- PixelLength = ImageWidth*3;
- while(PixelLength%4 != 0)
- ++PixelLength;
- PixelLength *= ImageHeight;
-
-
- PixelData = (GLubyte*)malloc(PixelLength);
- if(PixelData == 0)
- exit(0);
-
- fseek(pFile,54,SEEK_SET);
- fread(PixelData,PixelLength,1,pFile);
-
-
- fclose(pFile);
-
-
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(ImageWidth,ImageHeight);
- glutCreateWindow(FileName);
- glutDisplayFunc(&display);
- glutMainLoop();
- free(PixelData);
- return 0;
- }</span></strong></pre>
- <p></p>
- <p><img src="http://img.my.csdn.net/uploads/201210/27/1351343956_5014.png" alt=""><br>
- </p>
- <p><strong><span style="font-size:18px"><br>
- </span></strong></p>
- <p><strong><span style="font-size:18px">31 像素的拷贝<span style="color:rgb(0,0,255); font-family:georgia,Verdana,Helvetica,Arial; line-height:19px; text-indent:26px">glCopyPixels()--坐标点坐标,宽度值、高度值、GL_COLOR</span></span></strong></p>
- <p></p>
- <pre name="code" class="cpp"><span style="font-size:18px;"><strong>#include<GL/glut.h>
- #include<iostream>
- using namespace std;
-
- #define WindowWidth 400
- #define WindowHeight 400
-
-
- #define BMP_Header_Length 54
-
- void grap()
- {
- FILE* pDummyFile;
- FILE* pWritingFile;
- GLubyte* pPixelData;
- GLubyte BMP_Header[BMP_Header_Length];
- GLint i, j;
- GLint PixelDataLength;
-
-
- i = WindowWidth * 3;
- while(i%4 != 0)
- ++i;
- PixelDataLength = i*WindowHeight;
-
-
- pPixelData = (GLubyte*)malloc(PixelDataLength);
- if(pPixelData == 0)
- exit(0);
-
- pDummyFile = fopen("dummy.bmp", "rb");
- if(pDummyFile == 0)
- exit(0);
-
- pWritingFile = fopen("grab.bmp", "wb");
- if(pWritingFile == 0)
- exit(0);
-
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- glReadPixels(0,0,WindowWidth, WindowHeight,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);
-
-
- fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);
- fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);
- fseek(pWritingFile,0x0012,SEEK_SET);
- i = WindowWidth;
- j = WindowHeight;
- fwrite(&i,sizeof(i),1,pWritingFile);
- fwrite(&j,sizeof(j),1,pWritingFile);
-
-
- fseek(pWritingFile,0,SEEK_END);
- fwrite(pPixelData,PixelDataLength,1,pWritingFile);
-
-
- fclose(pDummyFile);
- fclose(pWritingFile);
- free(pPixelData);
- }
-
-
- void myDisplay()
- {
-
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0,0.0,0.0); glVertex2f(0.0f,0.0f);
- glColor3f(0.0,1.0,0.0); glVertex2f(1.0f,0.0f);
- glColor3f(0.0,0.0,1.0); glVertex2f(0.5f,1.0f);
- glEnd();
- glPixelZoom(-0.5f,-0.5f);
- glRasterPos2i(1,1);
- glCopyPixels(WindowWidth/2,WindowHeight/2,
- WindowWidth/2,WindowHeight/2,GL_COLOR);
- glutSwapBuffers();
- grap();
- }
-
- void myIdle()
- {
- cout<<"doing"<<endl;
- grap();
- return;
- }
-
- 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();
-
- cout<<"ok"<<endl;
- return 0;
- }</strong></span></pre>
- <p></p>
- <p><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351343928_8023.png" alt=""></strong></span></p>
- <p><span style="font-size:18px"><strong>10.20 纹理测试</strong></span></p>
- <p><span style="font-size:18px"><strong>32.1<span style="font-family:georgia,Verdana,Helvetica,Arial; line-height:19px; text-indent:26px; background-color:rgb(255,255,255)">纹理的使用方法,只要指定每一个顶点在纹理图象中所对应的像素位置,OpenGL就会自动计算顶点以外的其它点在纹理图象中所对应的像素位置</span></strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400
- #define WindowHeight 400
- #define WindowTitle "OpenGL纹理测试"
-
- #include<GL/glut.h>
- #include<stdio.h>
- #include<stdlib.h>
-
-
- #define BMP_Header_Length 54
- void grap()
- {
- FILE* pDummyFile;
- FILE* pWritingFile;
- GLubyte* pPixelData;
- GLubyte BMP_Header[BMP_Header_Length];
- GLint i,j;
- GLint PixelDataLength;
-
-
- i = WindowWidth * 3;
- while(i%4 == 0)
- ++i;
- PixelDataLength = i * WindowHeight;
-
-
- pPixelData = (GLubyte*)malloc(PixelDataLength);
- if(pPixelData == 0)
- exit(0);
-
- pDummyFile = fopen("dummy.bmp","rb");
- if(pDummyFile == 0)
- exit(0);
-
- pWritingFile = fopen("grap.bmp","wb");
- if(pWritingFile == 0)
- exit(0);
-
-
- glPixelStorei(GL_UNPACK_ALIGNMENT,4);
-
- glReadPixels(0,0,WindowWidth,WindowHeight,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);
-
-
- fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);
- fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);
- fseek(pWritingFile,0x0012,SEEK_SET);
- i = WindowWidth;
- j = WindowHeight;
- fwrite(&i,sizeof(i),1,pWritingFile);
- fwrite(&j,sizeof(j),1,pWritingFile);
-
-
- fseek(pWritingFile,54,SEEK_SET);
- fwrite(pPixelData,PixelDataLength,1,pWritingFile);
-
-
- fclose(pDummyFile);
- fclose(pWritingFile);
- free(pPixelData);
- }
-
- int power_of_two(int n)
- {
- if(n <= 0)
- return 0;
- return (n&(n-1)) == 0;
- }
-
- GLuint load_texture(const char* file_name)
- {
- GLint width,height,total_bytes;
- GLubyte* pixels=0;
- GLuint last_texture_ID,texture_ID=0;
-
-
- FILE* pFile=fopen(file_name,"rb");
- if(pFile == 0)
- return 0;
-
-
- fseek(pFile,0x0012,SEEK_SET);
- fread(&width,4,1,pFile);
- fread(&height,4,1,pFile);
- fseek(pFile,BMP_Header_Length,SEEK_SET);
-
-
- {
- GLint line_bytes = width*3;
- while(line_bytes % 4 != 0)
- ++line_bytes;
- total_bytes = line_bytes*height;
- }
-
-
- pixels = (GLubyte*)malloc(total_bytes);
- if(pixels == 0)
- {
- fclose(pFile);
- return 0;
- }
-
-
- if(fread(pixels,total_bytes,1,pFile) <= 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
-
-
-
- {
- GLint max;
- glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);
- if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)
- {
- const GLint new_width = 256;
- const GLint new_height = 256;
- GLint new_line_bytes,new_total_bytes;
- GLubyte* new_pixels=0;
-
-
- new_line_bytes = new_width * 3;
- while(new_line_bytes % 4 != 0)
- ++new_line_bytes;
- new_total_bytes = new_line_bytes * new_height;
-
-
- new_pixels = (GLubyte*)malloc(new_total_bytes);
- if(new_pixels == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
- gluScaleImage(GL_RGB,
- width,height,GL_UNSIGNED_BYTE,pixels,
- new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);
-
-
- free(pixels);
- pixels = new_pixels;
- width = new_width;
- height = new_height;
- }
- }
-
-
- glGenTextures(1,&texture_ID);
- if(texture_ID == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
- glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);
- glBindTexture(GL_TEXTURE_2D,texture_ID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);
- glBindTexture(GL_TEXTURE_2D,last_texture_ID);
-
-
-
-
- free(pixels);
- return texture_ID;
- }
-
-
- GLuint texGround;
- GLuint texWall;
-
- void display()
- {
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(75,1,1,21);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(1,5,5,0,0,0,0,0,1);
-
-
- glBindTexture(GL_TEXTURE_2D,texWall);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-8.0f,-8.0f,0.0f);
- glTexCoord2f(0.0f,5.0f); glVertex3f(-8.0f,8.0f,0.0f);
- glTexCoord2f(5.0f,5.0f); glVertex3f(8.0f,8.0f,0.0f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(8.0f,-8.0f,0.0f);
- glEnd();
-
-
- glBindTexture(GL_TEXTURE_2D,texWall);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);
- glEnd();
-
-
- glRotatef(-90,0,0,1);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);
- glEnd();
-
-
- glutSwapBuffers();
- grap();
- }
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(WindowWidth,WindowHeight);
- glutCreateWindow(WindowTitle);
- glutDisplayFunc(&display);
-
-
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- texGround = load_texture("ground.bmp");
- texWall = load_texture("wall.bmp");
-
-
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351344610_2938.png" alt=""><br>
- <p><br>
- </p>
- <p><strong><span style="font-size:18px">32 跟上个程序原理一样,上一个载入的都是墙的坐标,所以图像也不是很正确</span></strong></p>
- <p></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400
- #define WindowHeight 400
- #define WindowTitle "OpenGL纹理测试"
-
- #include<GL/glut.h>
- #include<stdio.h>
- #include<stdlib.h>
-
-
- #define BMP_Header_Length 54
- void grap()
- {
- FILE* pDummyFile;
- FILE* pWritingFile;
- GLubyte* pPixelData;
- GLubyte BMP_Header[BMP_Header_Length];
- GLint i,j;
- GLint PixelDataLength;
-
-
- i = WindowWidth * 3;
- while(i%4 == 0)
- ++i;
- PixelDataLength = i * WindowHeight;
-
-
- pPixelData = (GLubyte*)malloc(PixelDataLength);
- if(pPixelData == 0)
- exit(0);
-
- pDummyFile = fopen("dummy.bmp","rb");
- if(pDummyFile == 0)
- exit(0);
-
- pWritingFile = fopen("grap.bmp","wb");
- if(pWritingFile == 0)
- exit(0);
-
-
- glPixelStorei(GL_UNPACK_ALIGNMENT,4);
-
- glReadPixels(0,0,WindowWidth,WindowHeight,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);
-
-
- fread(BMP_Header,sizeof(BMP_Header),1,pDummyFile);
- fwrite(BMP_Header,sizeof(BMP_Header),1,pWritingFile);
- fseek(pWritingFile,0x0012,SEEK_SET);
- i = WindowWidth;
- j = WindowHeight;
- fwrite(&i,sizeof(i),1,pWritingFile);
- fwrite(&j,sizeof(j),1,pWritingFile);
-
-
- fseek(pWritingFile,54,SEEK_SET);
- fwrite(pPixelData,PixelDataLength,1,pWritingFile);
-
-
- fclose(pDummyFile);
- fclose(pWritingFile);
- free(pPixelData);
- }
-
- int power_of_two(int n)
- {
- if(n <= 0)
- return 0;
- return (n&(n-1)) == 0;
- }
-
- GLuint load_texture(const char* file_name)
- {
- GLint width,height,total_bytes;
- GLubyte* pixels=0;
- GLuint last_texture_ID,texture_ID=0;
-
-
- FILE* pFile=fopen(file_name,"rb");
- if(pFile == 0)
- return 0;
-
-
- fseek(pFile,0x0012,SEEK_SET);
- fread(&width,4,1,pFile);
- fread(&height,4,1,pFile);
- fseek(pFile,BMP_Header_Length,SEEK_SET);
-
-
- {
- GLint line_bytes = width*3;
- while(line_bytes % 4 != 0)
- ++line_bytes;
- total_bytes = line_bytes*height;
- }
-
-
- pixels = (GLubyte*)malloc(total_bytes);
- if(pixels == 0)
- {
- fclose(pFile);
- return 0;
- }
-
-
- if(fread(pixels,total_bytes,1,pFile) <= 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
-
-
-
- {
- GLint max;
- glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);
- if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)
- {
- const GLint new_width = 256;
- const GLint new_height = 256;
- GLint new_line_bytes,new_total_bytes;
- GLubyte* new_pixels=0;
-
-
- new_line_bytes = new_width * 3;
- while(new_line_bytes % 4 != 0)
- ++new_line_bytes;
- new_total_bytes = new_line_bytes * new_height;
-
-
- new_pixels = (GLubyte*)malloc(new_total_bytes);
- if(new_pixels == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
- gluScaleImage(GL_RGB,
- width,height,GL_UNSIGNED_BYTE,pixels,
- new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);
-
-
- free(pixels);
- pixels = new_pixels;
- width = new_width;
- height = new_height;
- }
- }
-
-
- glGenTextures(1,&texture_ID);
- if(texture_ID == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
- glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);
- glBindTexture(GL_TEXTURE_2D,texture_ID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);
- glBindTexture(GL_TEXTURE_2D,last_texture_ID);
-
-
-
-
- free(pixels);
- return texture_ID;
- }
-
-
- GLuint texGround;
- GLuint texWall;
-
- void display()
- {
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(75,1,1,21);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(1,5,5,0,0,0,0,0,1);
-
-
- glBindTexture(GL_TEXTURE_2D,texGround);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-8.0f,-8.0f,0.0f);
- glTexCoord2f(0.0f,5.0f); glVertex3f(-8.0f,8.0f,0.0f);
- glTexCoord2f(5.0f,5.0f); glVertex3f(8.0f,8.0f,0.0f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(8.0f,-8.0f,0.0f);
- glEnd();
-
-
- glBindTexture(GL_TEXTURE_2D,texWall);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);
- glEnd();
-
-
- glRotatef(-90,0,0,1);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f,0.0f); glVertex3f(-6.0f,-3.0f,0.0f);
- glTexCoord2f(0.0f,1.0f); glVertex3f(-6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,1.0f); glVertex3f(6.0f,-3.0f,1.5f);
- glTexCoord2f(5.0f,0.0f); glVertex3f(6.0f,-3.0f,0.0f);
- glEnd();
-
-
- glutSwapBuffers();
- grap();
- }
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(WindowWidth,WindowHeight);
- glutCreateWindow(WindowTitle);
- glutDisplayFunc(&display);
-
-
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- texGround = load_texture("ground.bmp");
- texWall = load_texture("wall.bmp");
-
-
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351344918_9096.png" alt=""><br>
- <p><br>
- </p>
- <p><strong><span style="font-size:18px">33.alpha测试</span></strong></p>
- <p></p>
- <p></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#define WindowWidth 400
- #define WindowHeight 400
- #define WindowTitle "OpenGL纹理测试"
-
- #include<GL/glut.h>
- #include<stdio.h>
- #include<stdlib.h>
-
-
- #define BMP_Header_Length 54
-
-
- int power_of_two(int n)
- {
- if(n <= 0)
- return 0;
- return (n&(n-1)) == 0;
- }
-
- GLuint load_texture(const char* file_name)
- {
- GLint width,height,total_bytes;
- GLubyte* pixels=0;
- GLuint last_texture_ID,texture_ID=0;
-
-
- FILE* pFile=fopen(file_name,"rb");
- if(pFile == 0)
- return 0;
-
-
- fseek(pFile,0x0012,SEEK_SET);
- fread(&width,4,1,pFile);
- fread(&height,4,1,pFile);
- fseek(pFile,BMP_Header_Length,SEEK_SET);
-
-
- {
- GLint line_bytes = width*3;
- while(line_bytes % 4 != 0)
- ++line_bytes;
- total_bytes = line_bytes*height;
- }
-
-
- pixels = (GLubyte*)malloc(total_bytes);
- if(pixels == 0)
- {
- fclose(pFile);
- return 0;
- }
-
-
- if(fread(pixels,total_bytes,1,pFile) <= 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
-
-
-
- {
- GLint max;
- glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);
- if(!power_of_two(width) || !power_of_two(height) || width>max || height>max)
- {
- const GLint new_width = 256;
- const GLint new_height = 256;
- GLint new_line_bytes,new_total_bytes;
- GLubyte* new_pixels=0;
-
-
- new_line_bytes = new_width * 3;
- while(new_line_bytes % 4 != 0)
- ++new_line_bytes;
- new_total_bytes = new_line_bytes * new_height;
-
-
- new_pixels = (GLubyte*)malloc(new_total_bytes);
- if(new_pixels == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
- gluScaleImage(GL_RGB,
- width,height,GL_UNSIGNED_BYTE,pixels,
- new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);
-
-
- free(pixels);
- pixels = new_pixels;
- width = new_width;
- height = new_height;
- }
- }
-
-
- glGenTextures(1,&texture_ID);
- if(texture_ID == 0)
- {
- free(pixels);
- fclose(pFile);
- return 0;
- }
-
-
-
- glGetIntegerv(GL_TEXTURE_BINDING_2D,(int*)&last_texture_ID);
- glBindTexture(GL_TEXTURE_2D,texture_ID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,
- GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);
- glBindTexture(GL_TEXTURE_2D,last_texture_ID);
-
-
-
-
- free(pixels);
- return texture_ID;
- }
-
- void texture_colorKey(GLubyte r,GLubyte g,GLubyte b,GLubyte absolute)
- {
- GLint width,height;
- GLubyte* pixels=0;
-
-
- glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_WIDTH,&width);
- glGetTexLevelParameteriv(GL_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);
-
-
- pixels = (GLubyte*)malloc(width*height*4);
- if(pixels == 0)
- return;
- glGetTexImage(GL_TEXTURE_2D,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,pixels);
-
-
-
-
- {
- GLint i;
- GLint count = width*height;
- for(i=0;i<count;++i)
- {
- if(abs(pixels[i*4]-b) <= absolute
- && abs(pixels[i*4+1]-g) <= absolute
- && abs(pixels[i*4+2]-r) <= absolute)
- pixels[i*4+3] = 0;
- else
- pixels[i*4+3] = 255;
- }
- }
-
-
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,
- GL_BGRA_EXT,GL_UNSIGNED_BYTE,pixels);
- free(pixels);
- }
-
- void display()
- {
- static int initialized=0;
- static GLuint texWindow=0;
- static GLuint texPicture=0;
-
-
-
- if(!initialized)
- {
- texPicture = load_texture("picture.bmp");
- texWindow = load_texture("window.bmp");
- glBindTexture(GL_TEXTURE_2D,texWindow);
- texture_colorKey(255,255,255,10);
-
- glEnable(GL_TEXTURE_2D);
-
- initialized = 1;
- }
-
- glClear(GL_COLOR_BUFFER_BIT);
-
-
- glBindTexture(GL_TEXTURE_2D,texPicture);
- glDisable(GL_ALPHA_TEST);
- glBegin(GL_QUADS);
- glTexCoord2f(0,0); glVertex2f(-1.0f,-1.0f);
- glTexCoord2f(0,1); glVertex2f(-1.0f,1.0f);
- glTexCoord2f(1,1); glVertex2f(1.0f,1.0f);
- glTexCoord2f(1,0); glVertex2f(1.0f,-1.0f);
- glEnd();
-
-
- glBindTexture(GL_TEXTURE_2D,texWindow);
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GREATER,0.5f);
-
- glBegin(GL_QUADS);
- glTexCoord2f(0,0); glVertex2f(-1.0f,-1.0f);
- glTexCoord2f(0,1); glVertex2f(-1.0f,1.0f);
- glTexCoord2f(1,1); glVertex2f(1.0f,1.0f);
- glTexCoord2f(1,0); glVertex2f(1.0f,-1.0f);
- glEnd();
-
-
-
- glutSwapBuffers();
- }
-
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(WindowWidth,WindowHeight);
- glutCreateWindow("alpha-test");
-
-
-
-
- glutDisplayFunc(&display);
-
-
-
- glutMainLoop();
- return 0;
- }</span></strong></pre><span style="font-size:18px"><strong><img src="http://img.my.csdn.net/uploads/201210/27/1351345099_4328.png" alt=""><br>
- </strong></span>
- <p></p>
- <p></p>
- <p><span style="font-size:18px"><strong>10.21</strong></span></p>
- <p><span style="font-size:18px"><strong>34. 模版测试。我的测试失败,具体的写法可以参考开头给的网址。</strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>
-
- void draw_sphere()
- {
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- {
- GLfloat
- pos[] = {5.0f,5.0f,0.0f,1.0f},
- ambient[] = {0.0f,0.0f,1.0f,1.0f};
- glLightfv(GL_LIGHT0,GL_POSITION,pos);
- glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
- }
-
-
- glColor3f(1,0,0);
- glPushMatrix();
- glTranslatef(0,0,2);
- glutSolidSphere(0.5,20,20);
- glPopMatrix();
- }
-
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60,1,5,25);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(5,0,6.5,0,0,0,0,1,0);
-
- glEnable(GL_DEPTH_TEST);
-
-
-
- draw_sphere();
-
-
-
-
-
- glClearStencil(0);
- glClear(GL_STENCIL_BUFFER_BIT);
- glStencilFunc(GL_ALWAYS,1,0xFF);
- glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
- glEnable(GL_STENCIL_TEST);
-
- glDisable(GL_LIGHTING);
- glColor3f(0.5f,0.5f,0.5f);
- glDepthMask(GL_FALSE);
- glRectf(-1.5f,-1.5f,1.5f,1.5f);
- glDepthMask(GL_TRUE);
-
-
-
-
- glEnable(GL_STENCIL_TEST);
- glStencilFunc(GL_EQUAL,1,0xFF);
- glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
- glScalef(1.0f,1.0f,-1.0f);
- draw_sphere();
-
-
- glutSwapBuffers();
- }
-
- int main(int argc, char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
- glutInitWindowPosition(200,200);
- glutInitWindowSize(400,400);
- glutCreateWindow("study");
- glutDisplayFunc(&display);
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345241_6397.png" alt=""><br>
- <p></p>
- <p><span style="font-size:18px"><strong>35 这题只敲了代码,没插件就没有运行,也没有图,具体大家可以看开头的网址</strong></span></p>
- <p></p>
- <pre name="code" class="cpp"><span style="font-size:18px;"><strong>
- #include<GL/glut.h>
- #include<stdio.h>
-
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
-
-
-
-
-
-
-
-
- GLint viewport[4];
- GLdouble modelview[16],projection[16];
- GLdouble x, y, z;
-
- printf("不支持GL_ARB_window_pos\n");
- printf("使用glRasterPos函数\n");
-
- glGetIntegerv(GL_VIEWPORT,viewport);
- glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
- glGetDoublev(GL_PROJECTION_MATRIX,projection);
- gluUnProject(100,100,0.5,modelview,projection,viewport,&x,&y,&z);
- glRasterPos3d(x,y,z);
-
- {
- GLubyte pixels[5][4][4];
-
- int i,j;
- for(i=0;i<5;++i)
- for(j=0;j<5;++j)
- {
- pixels[i][j][0] = 255;
- pixels[i][j][1] = 0;
- pixels[i][j][2] = 0;
- pixels[i][j][3] = 255;
- }
- glDrawPixels(5,5,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
- }
- glutSwapBuffers();
- }
-
- int main(int argc,char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(512,512);
- glutCreateWindow("OpenGL");
- glutDisplayFunc(&display);
- glutMainLoop();
- return 0;
- }</strong></span></pre>
- <p></p>
- <p><span style="font-size:18px"><strong>36 显示字体 用的显示列表</strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>
- #include<windows.h>
-
- #define MAX_CHAR 128
-
-
- void drawString(const char* str)
- {
- static int isFirstCall = 1;
- static GLuint lists;
-
- if(isFirstCall){
-
- isFirstCall = 0;
-
-
- lists = glGenLists(MAX_CHAR);
-
-
- wglUseFontBitmaps(wglGetCurrentDC(),0,MAX_CHAR,lists);
-
-
- for(;*str!=‘\0‘;++str)
- glCallList(lists + *str);
- }
- }
-
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0f,0.0f,0.0f);
- glRasterPos2f(0.0f,0.0f);
- drawString("hello,world");
-
- glutSwapBuffers();
- }
-
-
- int main(int argc,char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(512,512);
- glutCreateWindow("OpenGL");
- glutDisplayFunc(&display);
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345370_9989.png" alt=""><br>
- <p></p>
- <p><span style="font-size:18px"><strong>37 字体设置</strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>
- #include<windows.h>
-
- #define MAX_CHAR 128
-
- void drawString(const char* str)
- {
- static int isFirstCall = 1;
- static GLuint lists;
-
- if(isFirstCall){
-
- isFirstCall = 0;
-
-
- lists = glGenLists(MAX_CHAR);
-
-
- wglUseFontBitmaps(wglGetCurrentDC(),0,MAX_CHAR,lists);
-
-
- for(;*str!=‘\0‘;++str)
- glCallList(lists + *str);
- }
- }
-
- void selectFont(int size,int charset,const char* face){
- HFONT hFont=CreateFontA(size,0,0,0,FW_MEDIUM,0,0,0,
- charset,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,face);
- HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(),hFont);
- DeleteObject(hOldFont);
- }
-
- void display()
- {
- selectFont(48,ANSI_CHARSET,"Comic Sans MS");
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0f,0.0f,0.0f);
- glRasterPos2f(0.0f,0.0f);
- drawString("Hello,World!");
-
- glutSwapBuffers();
- }
-
-
- int main(int argc,char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(512,512);
- glutCreateWindow("OpenGL");
- glutDisplayFunc(&display);
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345437_6278.png" alt=""><br>
- <p></p>
- <p><span style="font-size:18px"><strong>38 显示汉字</strong></span></p>
- <p><span style="font-size:18px"><strong></strong></span></p>
- <pre name="code" class="cpp"><strong><span style="font-size:18px;">#include<GL/glut.h>
- #include<windows.h>
-
- #define MAX_CHAR 128
-
- void drawCNString(const char* str)
- {
- int len,i;
- wchar_t* wstring;
- HDC hDC=wglGetCurrentDC();
- GLuint list = glGenLists(1);
-
-
-
-
- len=0;
- for(i=0;str[i]!=‘\0‘;++i)
- {
- if(IsDBCSLeadByte(str[i]))
- ++i;
- ++len;
- }
-
-
- wstring = (wchar_t*)malloc((len+1)*sizeof(wchar_t));
- MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,str,-1,wstring,len);
- wstring[len] = ‘\0‘;
-
-
- for(i=0;i<len;++i)
- {
- wglUseFontBitmapsW(hDC,wstring[i],1,list);
- glCallList(list);
- }
-
-
- free(wstring);
- glDeleteLists(list,1);
- }
-
- void selectFont(int size,int charset,const char* face){
- HFONT hFont=CreateFontA(size,0,0,0,FW_MEDIUM,0,0,0,
- charset,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY,DEFAULT_PITCH | FF_SWISS,face);
- HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(),hFont);
- DeleteObject(hOldFont);
- }
-
- void display()
- {
-
- glClear(GL_COLOR_BUFFER_BIT);
-
- selectFont(48,ANSI_CHARSET,"Comic Sans MS");
- glColor3f(1.0f,0.0f,0.0f);
- glRasterPos2f(-0.7f,0.4f);
- drawCNString("Hello,World!");
-
- selectFont(48,GB2312_CHARSET,"楷体_GB2312");
- glColor3f(1.0f,1.0f,0.0f);
- glRasterPos2f(-0.7f,-0.1f);
- drawCNString("当代中国汉字");
-
- selectFont(48,GB2312_CHARSET,"华文仿宋");
- glColor3f(0.0f,1.0f,0.0f);
- glRasterPos2f(-0.7f,-0.6f);
- drawCNString("傳統中國漢字");
-
- glutSwapBuffers();
- }
-
-
- int main(int argc,char* argv[])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(512,512);
- glutCreateWindow("OpenGL");
- glutDisplayFunc(&display);
- glutMainLoop();
- return 0;
- }</span></strong></pre><img src="http://img.my.csdn.net/uploads/201210/27/1351345485_1205.png" alt=""><br>
- <p></p>
- <p><span style="font-size:18px; color:#ff0000"><strong>OVER!</strong></span></p>
- <p><br>
- </p>
7天学习opengl入门
标签:
原文地址:http://www.cnblogs.com/xuejinhui/p/4353032.html