Hello C++
// Sample code
#include
using namespace std;
const double pi = 3.14159;
struct User {
char username[10];
char password[20];
};
struct Student {
char id[20];
char name[10];
} stu1 = {"114514", "ZhangSan"}; // 全局变量
int main ()
{
// double r; // radius
// double circle; // circumference of circle
// cout << "Please enter radius: ";
// cin >> r;
// circle = 2 * pi * r;
// cout << "Circumference of circle is: " << circle <
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
Part 1
// File ID: Lab03a.cpp
// Title: Working with Graphics Primitives
// Author: Lorain
#define FREEGLUT_STATIC
#include
#include "math.h"
void define_to_OpenGL();
///
int main(int argc, char** argv)
{
glutInit(&argc, argv);
// Task 2
glutCreateWindow("Graphics Primitives");
glutDisplayFunc(define_to_OpenGL);
glutMainLoop();
}
///
void define_to_OpenGL()
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
// The stuff to appear on screen goes here
// Task 2 - Set the Dimensions
glutInitWindowSize(600, 400);
glutInitWindowPosition(50, 50);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, +500, -200, 200);
// Task 3 - Draw the Axes
glLineWidth(1.0);
glColor3f(0, 0, 0);
// 画一条线(x轴)
glBegin(GL_LINES); // 括号中定义绘制图形的类型(e.g. 点,线,三角形...)
glVertex2f(0.0, 0.0); // start location
glVertex2f(450.0, 0.0); // end location
glEnd();
// 再画一条线(y轴)
glBegin(GL_LINES);
glVertex2f(0.0, -150.0); // start location
glVertex2f(0.0, +150.0); // end location
glEnd();
// Task 4 - Draw a Dot at the Origin
glPointSize(10);
glColor3f(255, 255, 0); // 如果需要对颜色进行更改每次需要重新定义
glBegin(GL_POINTS);
glVertex2f(0, 0);
glEnd();
// Task 5 - Plot a Sine Wave
// draw a sine wave 绘制正弦波
int i;
float x, y;
glColor3f(0.0, 0.0, 1.0);
glPointSize(1);
glBegin(GL_POINTS);
for (i = 0; i < 361; i = i + 5)
{
x = (float)i;
y = 100.0 * sin(i * (6.284 / 360.0));
glVertex2f(x, y);
}
glEnd();
// Tasks 6, 7 and 8
// Task 6 - Draw a Triangle
// Task 7 - Draw A Multi-coloured Triangle
// Task 8 - Draw a Single-coloured Triangle
//glShadeModel(GL_FLAT); // 如果想要绘制单色的图形,告诉OpenGL关闭平滑功能即可
glBegin(GL_TRIANGLES);
glColor3f(255, 255, 0); // 这里对三角形每一个角的颜色进行定义
glVertex2f(-50, 50);
glColor3f(255, 0, 255);
glVertex2f(-50, 0);
glColor3f(0, 0, 255);
glVertex2f(0, 0);
glEnd();
glFlush();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 最终效果展示
Part 2