1. 程式人生 > >OpenGL實驗一 基本的畫線和重繪

OpenGL實驗一 基本的畫線和重繪

 

一、 實驗目的:
掌握開發環境配置方法和基本圖元繪製函式
 

二、 實驗內容:
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 <iostream>
#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;

} `