欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenGL实验一 基本的画线

程序员文章站 2022-03-05 09:27:47
...

一、 实验目的:
掌握开发环境配置方法和基本图元绘制函数

二、 实验内容:
1、熟悉开发环境
2、掌握点、线等基本图元绘制函数

三、 开发工具简介、实现效果及步骤

1、 开发工具简介
codeblocks
OpenGL
2、 实现效果、步骤(或流程)
(1) 配置开发环境
(2) 初始化
(3) 设置线的粗细和 颜色的渐变
(4) 设置点的大小
四:创新实现:
重绘函数的学习

参考:
https://blog.csdn.net/wuxinliulei/article/details/9102997
https://blog.csdn.net/chenxiqilin/article/details/50833112
参考了很多 百度都能搜到的
源代码:
#include <windows.h>
#include <GL/glut.h>
#include
#include <math.h>
#include <stdio.h>
using namespace std;

double WINHEIGHT = 0,WINWIDTH = 0;

void init();
void MyIdle();
void MyReshape(int w, int h);

void display(void);
void MyMouse(int button, int state, int x, int y);
void MyKeyboard(unsigned char key, int x, int y);
void MySpecialKey(int key, int x, int y);

int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow(“simple”);

init();
glutReshapeFunc (MyReshape);
glutIdleFunc(MyIdle);


glutDisplayFunc(display);
glutMouseFunc (MyMouse);
glutKeyboardFunc (MyKeyboard);
glutSpecialFunc(MySpecialKey);
glutMainLoop();

}

void display(void)
{
cout<<"Displaying… ";
glClear(GL_COLOR_BUFFER_BIT);

glColor3f (0, 0, 0);
glPointSize(10);
glLineWidth(3);
//glRectf(100.0f, 100.0f, 200.0f, 200.0f);
glBegin (GL_LINE_LOOP);
glVertex2f (100, 100);
glVertex2f (200, 300);
glVertex2f (400, 200);
glEnd ( );

glFlush();
cout<<"Displayed! "<<endl;

}

void MyMouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
{
cout<<"DOWN ";
}
if(state == GLUT_UP)
{
cout<<“UP”<<endl;
}
}

void MyKeyboard(unsigned char key, int x, int y)
{
if(key==27)
{
exit(0);
}
}

void MySpecialKey(int key, int x, int y){
glutGetModifiers();
}

//返回调整大小后的窗口大小 w,h
void MyReshape(GLsizei w, GLsizei h)
{
//view port 左下角为0,0
//窗口在高度方向变长,截图也要在该方向上拉长相同比例
//宽度方向拉长,截图也要在该方向上拉长相同比例
cout<<"Reshaping… “<<w<<” "<<h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if(w<=h)
    gluOrtho2D(0,500,    0,500 * h/w);
else
    gluOrtho2D(0,500 * w/h,  0,500);
glViewport(0,0,w,h);


WINWIDTH = w;
WINHEIGHT = h;
cout<<"Reshaped"<<endl;

}

void MyIdle(){
glutPostRedisplay();
}

void init()
{
//反射任何光,为白色
//gluOrtho2D(left, right, bottom, top);
//glortho(x_min x_max y_min y_max near top)

cout<<"Initing...    ";
glClearColor (1.0, 1.0, 1.0, 1.0);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0f, WINWIDTH, 0.0f, WINHEIGHT, 1.0, -1.0);

cout<<"Inited!"<<endl;

}

相关标签: OpenGL