19,468
社区成员
发帖
与我相关
我的任务
分享
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
glutWireCube(0.5);
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
}
void init()
{
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
}
#include <gl/glut.h>
#include <gl/gl.h>
#include <math.h>
static GLfloat GL_PI = 3.14159f;
//绘制场景
static void RenderSences()
{
GLfloat z = -50.0f;
//清空画布
glClear(GL_COLOR_BUFFER_BIT);
//设置画图使用的颜色
glColor3f(1.0f, 0.0f, 0.0f);
//保存矩阵
glPushMatrix();
//旋转, 如果不旋转,那些不同层次的点都重叠在一起,看不出3D效果了。
glRotatef(200.0f, 1.0f, 0.0f, 0.0f);
glRotatef(120.5f, 0.0f, 1.0f, 0.0f);
//获取点的大小的范围,以及增长的步长
GLfloat size[2];
GLfloat sizeStep;
glGetFloatv(GL_POINT_SIZE_RANGE, size);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &sizeStep);
GLfloat curStep = size[0];
for (GLfloat a = 0.0; a < GL_PI * 3.0f; a += 0.1f)
{
GLfloat x = 50.0f * cos(a);
GLfloat y = 50.0f * sin(a);
//设置点的大小
glPointSize(curStep);
curStep += sizeStep;
//设置顶点
glBegin(GL_POINTS);
glVertex3f(x, y, z);
glEnd();
//调整z值
z += 0.5f;
}
glPopMatrix();
glFlush();
}
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 100.0f;
if (h == 0)
{
h = 1;
}
//设置视口
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//设置可视区域
GLfloat aspect = (GLfloat)w / (GLfloat)h;
if (w <= h)
{
glOrtho(-nRange, nRange, -nRange/aspect, nRange/aspect, -nRange, nRange);
}
else
{
glOrtho(-nRange*aspect, nRange*aspect, -nRange, nRange, -nRange, nRange);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//初始化工作
static void SetupRC()
{
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
//创建窗口
static void CreateWindow()
{
glutInitWindowSize(200, 200);
glutCreateWindow("3d Points");
}
int main(int args, char **argv)
{
glutInit(&args, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
glutCreateWindow("draw points");
SetupRC();
glutDisplayFunc(RenderSences);
glutReshapeFunc(ChangeSize);
glutMainLoop();
return 0;
}