OpenGL基本图形的绘制
1. 使用glColor,glPointSize 函数绘制三个不同颜色和大小的点
// T.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glBegin(GL_POINTS);
glColor3f(1.0,0.0,0.0);
glVertex2f(0.3f,0.5f);
glEnd();
glPointSize(8.0f);
glBegin(GL_POINTS);
glColor3f(0.0,1.0,0.0);
glVertex2f(0.0f,0.0f);
glEnd();
glPointSize(10.0f);
glBegin(GL_POINTS);
glColor3f(0.0,0.0,1.0);
glVertex2f(-0.3f,-0.5f);
glEnd();
glFlush();
}
int main(int argc,char* argv[]) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("Three Point");
glutDisplayFunc(&Display);
glutMainLoop();
return 0;
}
2. 使用glColor,glLineWidth,glLineStripple 函数绘制实心、虚线及渐变色线
(1)实心线
// T.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glEnable(GL_LINE_STIPPLE);
glShadeModel(GL_SMOOTH);
glLineStipple(2,0xFFFF);
glLineWidth(2);
glBegin(GL_LINES);
glVertex2i(-100,0);
glVertex2i(100,0);
glEnd();
glFlush();
}
int main(int argc,char* argv[]) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("Solid Line");
glutDisplayFunc(&Display);
glutMainLoop();
return 0;
}
(2)虚线
// T.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glEnable(GL_LINE_STIPPLE);
glShadeModel(GL_SMOOTH);
glLineStipple(4,0xAAAA);
glLineWidth(2);
glBegin(GL_LINES);
glVertex2i(-100,0);
glVertex2i(100,0);
glEnd();
glFlush();
}
int main(int argc,char* argv[]) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200,200);
glutInitWindowSize(400,400);
glutCreateWindow("Dotted Line");
glutDisplayFunc(&Display);
glutMainLoop();
return 0;
}
(3)渐变线
// T.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include
GLuint Line;
void Initial(void) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Line = glGenLists(1);
glNewList(Line, GL_COMPILE); glTranslatef(-50.0, 20.0, 0.0);
glColor3f(255.0, 0.0, 0.0);
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(255.0, 0.0, 0.0);
glVertex2i(0,-20);
glColor3f(0.0, 0.0, 255.0);
glVertex2i(100,-20);
glEnd();
glEndList();
void ChangeSize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-70.0f, 70.0f, -70.0f, 70.0f);
}
void Display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, 0.0);
glCallList(Line);
glFlush();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(600,450); glutInitWindowPosition(100,100);
} glutCreateWindow("渐变色线"); glutDisplayFunc(Display); glutReshapeFunc(ChangeSize); Initial(); glutMainLoop(); return 0;
3. 使用glColor,glPolygonMode 函数绘制纯色、渐变色、轮廓多边形
(1)纯色六边形
// a.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include
void init(){
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void polygon(void){
glClear(GL_COLOR_BUFFER_BIT);
int p1 [] = {20, 75};
int p2 [] = {60, 5};
int p3 [] = {140, 5};
int p4 [] = {180, 75};
int p5 [] = {140, 145};
int p6 [] = {60, 145};
glColor3f(0.0, 0.5, 1.0);
glBegin(GL_POLYGON);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
glFlush();
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400, 300);
glutCreateWindow("纯色六边形");
init();
glutDisplayFunc(polygon);
glutMainLoop();
}
(2)渐变色六边形
// a.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include
void init(){
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void polygon(void){
glClear(GL_COLOR_BUFFER_BIT);
int p1 [] = {20, 75};
int p2 [] = {60, 5};
int p3 [] = {140, 5};
int p4 [] = {180, 75};
int p5 [] = {140, 145};
int p6 [] = {60, 145};
glColor3f(0.0, 0.5, 1.0);
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex2iv(p1);
glColor3f(0.0,1.0,0.0);
glVertex2iv(p2);
glColor3f(1.0,0.0,0.0);
glVertex2iv(p3);
glColor3f(0.0,0.0,1.0);
glVertex2iv(p4);
glColor3f(0.0,1.0,0.0);
glVertex2iv(p5);
glColor3f(1.0,0.0,0.0);
glVertex2iv(p6);
glEnd();
glFlush();
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400, 300);
glutCreateWindow("渐变色六边形");
init();
glutDisplayFunc(polygon);
glutMainLoop();
}
(3)轮廓渐变色六边形
// a.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include
void init(){
glClearColor (1.0, 1.0, 1.0, 0.0);
glPolygonMode (GL_FRONT_AND_BACK,GL_LINE); gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void polygon(void){
glClear(GL_COLOR_BUFFER_BIT);
int p1 [] = {20, 75};
int p2 [] = {60, 5};
int p3 [] = {140, 5};
int p4 [] = {180, 75};
int p5 [] = {140, 145};
int p6 [] = {60, 145};
glColor3f(0.0, 0.5, 1.0);
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,1.0);
glVertex2iv(p1);
glColor3f(0.0,1.0,0.0);
glVertex2iv(p2);
glColor3f(1.0,0.0,0.0);
glVertex2iv(p3);
glColor3f(0.0,0.0,1.0);
glVertex2iv(p4);
glColor3f(0.0,1.0,0.0);
glVertex2iv(p5);
glColor3f(1.0,0.0,0.0);
glVertex2iv(p6);
glEnd();
glFlush();
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400, 300);
glutCreateWindow("轮廓渐变色六边形"); init();
glutDisplayFunc(polygon);
glutMainLoop(); }