Computer Graphics - code_3

#include <iostream>
#include <GL/glut.h>
#include <cmath>

#define PI 3.141592653

#pragma comment( lib , "opengl32.lib")
#pragma comment( lib , "glu32.lib" )
#pragma comment( lib , "glut32.lib" )

using namespace std;

int Wnd_W = 800 / 2;
int Wnd_H = 600 / 2;
double aspect = Wnd_W / Wnd_H;

double Ortho_left = -Wnd_W / 1.5;
double Ortho_right = Wnd_W / 1.5;
double Ortho_bottom = -Wnd_H / 1.5;
double Ortho_top = Wnd_H / 1.5;
double near = -500.0;
double far = 2000.0;

double LookAt_eyex = 125;
double LookAt_eyey = 105;
double LookAt_eyez = -175;
double LookAt_centerx = 10;
double LookAt_centery = 0;
double LookAt_centerz = 0;
double LookAt_upx = 0.25;
double LookAt_upy = -0.5;
double LookAt_upz = -2.5;

class Point {		//point struct
public:
	int x;
	int y;
	int z;
	Point() :x(0), y(0), z(0) {}
	Point(int  a, int  b, int c) :x(a), y(b), z(c) {}
	Point(const Point& src) : x(src.x), y(src.y), z(src.z) {}
	Point& operator=(const Point &src) {
		if (this != &src) {
			this->x = src.x;
			this->y = src.y;
			this->z = src.z;
		}
		return *this;
	}
};

//color table
GLfloat black_color[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat red_color[] = { 1.0,0.0,0.0,1.0 };
GLfloat green_color[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat blue_color[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat white_color[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat grey_color[] = { 0.5, 0.5, 0.5, 1.0 };

inline void GL_Line(int x0, int y0, int z0, int x1, int y1, int z1, GLfloat* color, int width = 1) {//作直线
	glColor4fv(color);
	glLineWidth(width);
	glBegin(GL_LINES);
	glVertex3f(x0, y0, z0);
	glVertex3f(x1, y1, z1);
	glEnd();
}

inline void GL_Line(Point p1, Point p2, GLfloat* color, int width = 1) {//作直线
	glColor4fv(color);
	glLineWidth(width);
	glBegin(GL_LINES);
	glVertex3d(p1.x, p1.y, p1.z);
	glVertex3d(p2.x, p2.y, p2.z);
	glEnd();
}

void draw_rect(Point p1, Point p2, Point p3, Point p4, GLfloat* color, int width = 1) {
	glColor4fv(color);
	glLineWidth(width);
	glBegin(GL_LINE_LOOP);
	glVertex3d(p1.x, p1.y, p1.z);
	glVertex3d(p2.x, p2.y, p2.z);
	glVertex3d(p3.x, p3.y, p3.z);
	glVertex3d(p4.x, p4.y, p4.z);
	glEnd();
}

void draw_cube(GLfloat* color, int width = 1) {
	Point p1(-100, 100, 100);
	Point p2(100, 100, 100);
	Point p3(100, -100, 100);
	Point p4(-100, -100, 100);
	Point p5(-100, 100, -100);
	Point p6(100, 100, -100);
	Point p7(100, -100, -100);
	Point p8(-100, -100, -100);

	draw_rect(p1, p2, p3, p4, color, width);
	draw_rect(p5, p6, p2, p1, color, width);
	draw_rect(p8, p7, p6, p5, color, width);
	draw_rect(p4, p3, p7, p8, color, width);
}

inline double transradian(int angle) {
	return angle*PI / 180;//将角度转换为弧度
}

//球体
void draw_sphere(double R, int n, GLfloat* color, int width = 1) {
	int longitude = 0;//纬度
	int latitude = 0;//经度
	int incremental = 360/n;

	glColor4fv(color);
	glLineWidth(width);

	for (longitude = 0; longitude <= 180; longitude += incremental) {
		glBegin(GL_LINE_LOOP);
		for (latitude = -180; latitude <= 180; latitude += incremental) {
			glVertex3f(
				R*sin(transradian(longitude))*cos(transradian(latitude)),
				R*sin(transradian(longitude))*sin(transradian(latitude)),
				R*cos(transradian(longitude))
				);
		}
		glEnd();

	}

	for (latitude = -180; latitude <= 180; latitude += incremental) {
		glBegin(GL_LINE_LOOP);
		for (longitude = 0; longitude <= 180; longitude += incremental) {
			glVertex3f(
				R*sin(transradian(longitude))*cos(transradian(latitude)),
				R*sin(transradian(longitude))*sin(transradian(latitude)),
				R*cos(transradian(longitude))
				);
		}
		glEnd();
	}

}

//椭球
void draw_oblong(double Rx, double Ry, double Rz, int n, GLfloat* color, int width = 1) {
	int longitude = 0;//纬度
	int latitude = 0;//经度
	int incremental = 360 / n;

	glColor4fv(color);
	glLineWidth(width);

	for (longitude = 0; longitude <= 180; longitude += incremental) {
		glBegin(GL_LINE_LOOP);
		for (latitude = -180; latitude <= 180; latitude += incremental) {
			glVertex3f(
				Rx*sin(transradian(longitude))*cos(transradian(latitude)),
				Ry*sin(transradian(longitude))*sin(transradian(latitude)),
				Rz*cos(transradian(longitude))
				);
		}
		glEnd();

	}

	for (latitude = -180; latitude <= 180; latitude += incremental) {
		glBegin(GL_LINE_LOOP);
		for (longitude = 0; longitude <= 180; longitude += incremental) {
			glVertex3f(
				Rx*sin(transradian(longitude))*cos(transradian(latitude)),
				Ry*sin(transradian(longitude))*sin(transradian(latitude)),
				Rz*cos(transradian(longitude))
				);
		}
		glEnd();
	}
}

//环
void draw_torus(double R, double R0, int nlong, int nlat, GLfloat* color, int width = 1) {
	int longitude = 0;//纬度
	int latitude = 0;//经度
	int incremental_long = 360 / nlong;
	int incremental_lat = 360 / nlat;

	glColor4fv(color);
	glLineWidth(width);

	for (longitude = -180; longitude <= 180; longitude += incremental_long) {
		glBegin(GL_LINE_LOOP);
		for (latitude = -180; latitude <= 180; latitude += incremental_lat) {
			glVertex3f(
				(R0 + R*sin(transradian(longitude)))*cos(transradian(latitude)),
				(R0 + R*sin(transradian(longitude)))*sin(transradian(latitude)),
					R*cos(transradian(longitude))
				);
		}
		glEnd();

	}

	for (latitude = -180; latitude <= 180; latitude += incremental_lat) {
		glBegin(GL_LINE_LOOP);
		for (longitude = -180; longitude <= 180; longitude += incremental_long) {
			glVertex3f(
				(R0 + R*sin(transradian(longitude)))*cos(transradian(latitude)),
				(R0 + R*sin(transradian(longitude)))*sin(transradian(latitude)),
				R*cos(transradian(longitude))
				);
		}
		glEnd();
	}

}

void display() {
	glClear(GL_COLOR_BUFFER_BIT);
	GL_Line(0, -Wnd_H, 0, 0, Wnd_H, 0, grey_color, 2);//坐标轴
	GL_Line(-Wnd_W, 0, 0, Wnd_W, 0, 0, blue_color, 2);
	GL_Line(0, 0, -5000, 0, 0, 5000, green_color, 2);

	//球体
	draw_sphere(150, 30, white_color);

	//椭球
	draw_oblong(75, 75, 150, 40, white_color);

	//环
	draw_torus(40, 200, 30, 60, grey_color);

	glFlush();
}

void init(void) {
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(Ortho_left, Ortho_right, Ortho_bottom, Ortho_top, near, far);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(LookAt_eyex, LookAt_eyey, LookAt_eyez, LookAt_centerx, LookAt_centery, LookAt_centerz, LookAt_upx, LookAt_upy, LookAt_upz);
}

void main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(150, 150);
	glutInitWindowSize(Wnd_W * 2, Wnd_H * 2);
	glViewport(0, 0, Wnd_W, Wnd_H);
	glutCreateWindow("Demo");
	init();
	glutDisplayFunc(&display);
	glutMainLoop();
}

  

时间: 2024-12-23 19:36:07

Computer Graphics - code_3的相关文章

以图学习linux graphics -Mesa (computer graphics) 软件架构图

查看: 212|回复: 1    以图学习linux graphics -Mesa (computer graphics) 软件架构图 [复制链接]     titer1 轻车都尉(从四品) 注册时间 2014-8-22 积分 1095 串个门 加好友 打招呼 发消息 电梯直达 1#  发表于 2014-10-3 23:29:43 |只看该作者 |倒序浏览 本帖最后由 titer1 于 2014-10-3 23:29 编辑 Linux_kernel_and_OpenGL_video_game 更

HDU 4716 A Computer Graphics Problem(模拟啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 Problem Description In this problem we talk about the study of Computer Graphics. Of course, this is very, very hard. We have designed a new mobile phone, your task is to write a interface to displa

水题 HDOJ 4716 A Computer Graphics Problem

题目传送门 1 /* 2 水题:看见x是十的倍数就简单了 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e4 + 10; 13

Computer Graphics - code_1

/*------------------------------------ author:XD_G location:SWUN time:09/2015~01/2016 course:Computer Graphics teacher:Tianyun Huang ------------------------------------*/ #include <iostream> #include <vector> #include <GL/glut.h> #pragm

A Computer Graphics Problem 4176 2013上海邀请赛

A Computer Graphics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 688 Problem Description In this problem we talk about the study of Computer Graphics.

Computer Graphics Research Software

Helping you avoid re-inventing the wheel since 2009! Last updated December 5, 2012.Try searching this page for keywords like 'segmentation' or 'PLY'.If you would like to contribute links, please e-mail them to [email protected]. Papers & Archives Gra

《Computer Graphics》 Peter.Shirley读书笔记

1.3图形学API使用图形函数库的关键在于处理应用程序接口(API),多数API都具有使用回调(callback)的用户界面工具包.当前主要有两种API模式.第一种是Java的集成方式,图形与用户界面工具包被集成在一起,都是可移植的包,并且完全标准化,作为语言的一部分得到支持:第二种的代表是D3D和OpenGL,画图命令是软件库的一部分,软件库与某种语言绑定(如C++),而用户界面软件是独立的实体,一般随系统不同而不同. 1.4最常用的模型由三维三角形组成 1.5三维图形流水线(graphic

Computer Graphics - code_2

#include <windows.h> #include <stdlib.h> #include <string.h> #include <tchar.h> #include <vector> #include <string> #include <time.h> using namespace std; typedef struct Point { //点结构 public: int x; int y; struct

A trip through the Graphics Pipeline 2011_05

After the last post about texture samplers, we’re now back in the 3D frontend. We’re done with vertex shading, so now we can start actually rendering stuff, right? Well, not quite. You see, there’s a bunch still left to do before we actually start ra