OSG绘制基本图形实验一
实验一:基本图形生成算法实验说明

实验一基本图形生成一.实验学时4学时二.实验类型设计型实验三.实验目的和要求1、了解光栅化图形学的主要理论和知识。
2、了解VS的Windows窗口编程中几何图形的绘制调用接口。
3、掌握线段的生成算法原理和算法,掌握微分画线法、中点画线算法的原理,学会用C++程序进行算法的设计。
4、选作:掌握多边形填充算法的基本原理和方法,掌握有序边表法和扫描线种子填充算法的原理,采用C++进行算法的设计。
四、实验内容(一)完成一个图形生成算法的仿真环境,在一个窗口区域中画出坐标系和坐标网格,其中每个坐标网格模拟显示器上的一个像素点。
实验步骤如下:(1)在VS中打开实验的的工程。
(2)在工程中增加一个菜单项:打开linemenu.rc资源在下面增加一个多边形填充的选项。
操作界面图见图1所示。
图1 菜单资源图对新增的菜单选项设置ID值见图2所示。
图2 ID定义界面图(3)在resource.h文件中增加ID_FILL的定义图3 ID定义界面图(4)在circle.cpp文件中增加一个多边形填充的处理switch(message){case WM_COMMAND:hdc=GetDC(hwnd);if(LOWORD(wParam) == ID_BACK){hnewpen=CreatePen(PS_SOLID,1,RGB(0,255,255));holdpen=(HPEN)SelectObject(hdc,hnewpen);DrawBackground(hdc);hnewpen=CreatePen(PS_SOLID,2,RGB(0,0,0));holdpen=(HPEN)SelectObject(hdc,hnewpen);DrawAB(hdc);}else if(LOWORD(wParam)==ID_MID){hnewpen=CreatePen(PS_SOLID,2,RGB(255,0,0));holdpen=(HPEN)SelectObject(hdc,hnewpen);DDACircle(hdc,1,5,20,RGB(0,255,255));}else if(LOWORD(wParam) == ID_BRE){hnewpen=CreatePen(PS_SOLID,2,RGB(255,0,0));holdpen=(HPEN)SelectObject(hdc,hnewpen);p[0].SetX(0);p[0].SetY(0);p[1].SetX(5);p[1].SetY(8);p[2].SetX(1);p[2].SetY(10);p[3].SetX(0);p[3].SetY(0);ScanFill(hdc,p,RGB(0,255,255));}SelectObject(hdc,holdpen);DeleteObject(hnewpen);ReleaseDC(hwnd,hdc);break;(二)设计微分画线算法和中点画线算法程序,并进行调试。
osg基本几何图元

osg基本⼏何图元转⾃://osg 基本⼏何图元// ogs中所有加⼊场景中的数据都会加⼊到⼀个Group类对象中,⼏何图元作为⼀个对象由osg::Geode类来组织管理。
// 绘制⼏何图元对象时,先创建⼀个Geometry对象,这个对象中要设置绘制所需的基本信息,图元的顶点、顶点颜⾊、顶点关联⽅式以及法线。
#include <osgViewer/Viewer>#include <osgDB/ReadFile>#include <osg/Node>#include <osg/Geode>#include <osg/Geometry>int main(int argc, char** argv){osgViewer::Viewer view;osg::ref_ptr<osg::Group> root = new osg::Group;#pragma region ⼏何图元模块osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;//定义顶点osg::ref_ptr<osg::Vec3Array> vertexArray = new osg::Vec3Array;geometry->setVertexArray(vertexArray);vertexArray->push_back(osg::Vec3(-1.f, 0.f, 1.f));vertexArray->push_back(osg::Vec3(1.f, 0.f, -1.f));vertexArray->push_back(osg::Vec3(1.f, 0.f, 1.f));//定义颜⾊数组osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array();geometry->setColorArray(colorArray);geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);colorArray->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));colorArray->push_back(osg::Vec4(0.f, 1.f, 0.f, 1.f));colorArray->push_back(osg::Vec4(0.f, 0.f, 1.f, 1.f));//定义法线osg::ref_ptr<osg::Vec3Array> normalArray = new osg::Vec3Array();geometry->setNormalArray(normalArray);geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);normalArray->push_back(osg::Vec3(0.f, -1.f, 0.f));//设置顶点关联⽅式//PrimitiveSet类,这个类松散地封装了OpenGL的绘图基元,//包括点(POINTS),线(LINES),多段线(LINE_STRIP),封闭线(LINE_LOOP),四边形(QUADS),多边形(POLYGON)等。
osg polytopeintersector 例子

osg polytopeintersector 例子`osg::PolytopeIntersector`是 OpenSceneGraph(OSG)中的一个类,用于执行多边形与场景图中其他几何图形的相交测试。
以下是一个简单的示例代码,演示了如何使用`osg::PolytopeIntersector`来进行相交测试:```cpp#include <osg/Group>#include <osg/Geometry>#include <osg/MatrixTransform>#include <osg/PolytopeIntersector>int main() {// 创建一个简单的场景图osg::ref_ptr<osg::Group> root = new osg::Group();// 创建一个多边形几何图形osg::ref_ptr<osg::Geometry> polygon = new osg::Geometry();polygon->setVertexArray(osg::Vec3Array::create());polygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON, 0, 4));polygon->setColorArray(osg::ColorArray::create());polygon->setColorBinding(osg::Geometry::BIND_OVERALL);// 设置多边形的顶点坐标polygon->getVertexArray()->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));polygon->getVertexArray()->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));polygon->getVertexArray()->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));polygon->getVertexArray()->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));// 将多边形添加到场景图中root->addChild(polygon.get());// 创建一个矩阵变换节点,用于旋转多边形osg::ref_ptr<osg::MatrixTransform> matrix = new osg::MatrixTransform();matrix->setMatrix(osg::Matrix::rotate(osg::inDegrees(45.0f),osg::Vec3(0.0f, 0.0f, 1.0f)));root->addChild(matrix.get());// 创建 PolytopeIntersector 对象osg::PolytopeIntersector intersector;// 设置视角和投影矩阵osg::Camera *camera = new osg::Camera();camera->setViewport(0, 0, 640, 480);camera->setProjectionMatrix(osg::Matrix::ortho2D(0, 640, 0, 480));root->addChild(camera);// 遍历场景图,执行相交测试osg::NodeVisitor nv;nv.setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);nv.addIntersector(&intersector);root->accept(nv);// 检查是否有相交的结果if (intersector.hits()) {std::cout << "Polygon intersects with scene" << std::endl;// 获取相交的信息const osg::PolytopeIntersector::HitList& hits = intersector.getHits();for (const osg::PolytopeIntersector::Hit& hit : hits) {std::cout << "Intersect at (" << hit.getWorld坐标()[0] << ", " << hit.getWorld坐标()[1] << ")" << std::endl;}} else {std::cout << "Polygon does not intersect with scene" << std::endl;}return 0;}```在上述代码中,我们首先创建了一个包含多边形的场景图。
osg几何体顶点坐标单位

osg几何体顶点坐标单位摘要:1.OSG 几何体简介2.几何体顶点坐标的单位3.几何体顶点坐标的转换4.应用实例正文:1.OSG 几何体简介OSG(Open Scene Graph)是一款开源的图形渲染引擎,广泛应用于三维计算机图形学、虚拟现实、游戏开发等领域。
在OSG 中,几何体是构建3D 场景的基本元素,它可以是简单的几何体如立方体、球体,也可以是复杂的模型如人体、建筑等。
几何体由顶点、线段和面片组成,其中顶点是几何体的基本构建块,通过顶点可以描述几何体的形状和位置。
2.几何体顶点坐标的单位在OSG 中,几何体顶点坐标的单位通常是浮点数,表示一个三维空间中的点。
这个单位可以是任意的,但为了保证渲染的准确性,一般会选择一个合适的单位,例如米、厘米等。
在实际应用中,为了方便计算和渲染,可以将顶点坐标的单位设置为相同的值,例如全部设置为米。
3.几何体顶点坐标的转换在3D 图形渲染中,由于不同的几何体可能具有不同的坐标系,因此需要对顶点坐标进行转换。
OSG 提供了一些函数来实现坐标转换,例如osg::convert() 函数。
通过这个函数,可以将一个几何体的顶点坐标转换为另一个坐标系下的顶点坐标。
例如,将一个局部坐标系下的顶点坐标转换为世界坐标系下的顶点坐标。
4.应用实例假设我们有一个立方体几何体,它的顶点坐标如下:```顶点1:(1, 1, 1)顶点2:(1, -1, 1)顶点3:(-1, -1, 1)顶点4:(-1, 1, 1)顶点5:(1, 1, -1)顶点6:(1, -1, -1)顶点7:(-1, -1, -1)顶点8:(-1, 1, -1)```现在,我们需要将这个立方体从局部坐标系转换到世界坐标系。
首先,创建一个osg::Node 对象,然后将立方体的顶点坐标添加到该节点的vertexData 中。
接下来,创建一个osg::Geode 对象,将节点设置为该几何体的节点,并将几何体类型设置为osg::Geode::GEOMETRY_CUBE。
osg在场景中绘制坐标轴(xyz)

osg在场景中绘制坐标轴(xyz)//x y z font_sizeosg::Geode* makeCoordinate(float a_x,float a_y,float a_z,float font_size){osg::ref_ptr<osg::Sphere> pSphereShape = new osg::Sphere(osg::Vec3(0, 0, 0), 1.0f);osg::ref_ptr<osg::ShapeDrawable> pShapeDrawable = new osg::ShapeDrawable(pSphereShape.get());pShapeDrawable->setColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));//创建保存⼏何信息的对象osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();//创建四个顶点osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));v->push_back(osg::Vec3(a_x, 0.0f, 0.0f));v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));v->push_back(osg::Vec3(0.0f, a_y, 0.0f));v->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));v->push_back(osg::Vec3(0.0f, 0.0f, a_z));geom->setVertexArray(v.get());osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array();c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));c->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));c->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));c->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));geom->setColorArray(c.get());geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);//xyzgeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 2, 2));geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 4, 2));osg::ref_ptr<osgText::Text> pTextXAuxis1 = new osgText::Text;pTextXAuxis1->setText(L"X");pTextXAuxis1->setFont("Fonts/simhei.ttf");pTextXAuxis1->setAxisAlignment(osgText::Text::SCREEN);pTextXAuxis1->setCharacterSize(font_size);pTextXAuxis1->setPosition(osg::Vec3(a_x, 0.0f, 0.0f));osg::ref_ptr<osgText::Text> pTextYAuxis1 = new osgText::Text;pTextYAuxis1->setText(L"Y");pTextYAuxis1->setFont("Fonts/simhei.ttf");pTextYAuxis1->setAxisAlignment(osgText::Text::SCREEN);pTextYAuxis1->setCharacterSize(font_size);pTextYAuxis1->setPosition(osg::Vec3(0.0f, a_y, 0.0f));osg::ref_ptr<osgText::Text> pTextZAuxis1 = new osgText::Text;pTextZAuxis1->setText(L"Z");pTextZAuxis1->setFont("Fonts/simhei.ttf");pTextZAuxis1->setAxisAlignment(osgText::Text::SCREEN);pTextZAuxis1->setCharacterSize(font_size);pTextZAuxis1->setPosition(osg::Vec3(0.0f, 0.0f, a_z));osg::ref_ptr<osg::Geode> geode = new osg::Geode();geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);geode->getOrCreateStateSet()->setAttribute(new osg::LineWidth(3.0), osg::StateAttribute::ON);geode->addDrawable(pShapeDrawable.get());geode->addDrawable(geom.get());geode->addDrawable(pTextXAuxis1.get());geode->addDrawable(pTextYAuxis1.get());geode->addDrawable(pTextZAuxis1.get());return geode.release();}效果图⽚:。
Photoshop实验报告——制作圣诞树,光晕效果,绘制风景画

Photoshop平面设计实验报告书课程: Photoshop平面设计专业:房地产经营与管理班级: 0534111学号: 053411168姓名:付艳2013年3月30日河南城建学院绘图工具使用图像编辑工具的使用实验目的:让学生了解并掌握选区工具、描边、填充、渐变等工具的使用方法实验要求:要求能够按照试验指导书的步骤完成给定的练习实验器材:装有Photoshop的计算机实验一:制作圣诞树实验步骤1,按Ctrl+O组合键选择素材库中素材,选择“矩形选框”工具在图像窗口中绘制矩形选区。
2,选择“移动”工具将选区中的图像拖拽到文件窗口的中心位置,在“图层”控制面板中生成新的图层并将其命名为“卡片”。
3,按Ctrl+O组合键打开素材中“03”,选择“椭圆选框”工具,按住shift键的同时拖拽鼠标绘制圆形选区。
选择“移动”工具,将选区中的图像拖拽到01文件窗口的左下方,在“图层”控制面板中生成新的图层并将其命名为04“绿色小球”。
并依此制成05“星星”,“06红袜子”,07“小鼓”,08“圣诞小马”。
4,按ctrl+O组合键,打开素材09,选择“快速选择”工具,工具属性栏中设置在图像窗口的白色背景区域中单击鼠标,图像周围生成选区。
5,按ctrl+shift+I组合键,将选区反选。
选择“移动”工具,将选区中的图像拖拽到01文件窗口中的右下方,在“图层”控制面板中生成新的图层并命名为“礼品盒”。
圣诞树装饰效果制作完成。
实验二:制作光晕效果实验步骤1,按ctrl+O组合键。
打开素材“制作光晕效果|01”文件,选择“椭圆选框”工具,在图像窗口中拖拽鼠标绘制给椭圆形选区。
2,选择“选择|修改|羽化”命令,弹出“羽化选取”对话框进行设置,单击“确定”按钮。
选择“选择|反向”命令,将选区反选。
3,将前景色设为白色,按Alt+Delete组合键,用前景色填充选区,按ctrl+D组合键,取消选区。
光晕效果制作完成。
实验三:绘制风景画实验步骤1,按ctrl+O组合键,新建一个文件:宽度为15cm,高度为15cm,分辨率为300像素/英寸,颜色模式为RGB,背景内容为白色,单击“确定”按钮。
osg几何体顶点坐标单位

osg几何体顶点坐标单位摘要:1.OSG简介2.几何体顶点坐标单位的作用3.如何在OSG中设置几何体顶点坐标单位4.实例演示5.总结正文:【1.OSG简介】OSG(Open Scene Graph)是一款开源的三维图形渲染框架,它允许开发者使用硬件加速的图形渲染管道来实现复杂的三维场景。
OSG具有良好的跨平台性,支持多种编程语言,广泛应用于游戏、虚拟现实、科学可视化等领域。
【2.几何体顶点坐标单位的作用】在OSG中,几何体(Geometry)是由顶点(Vertex)、边(Edge)和面(Face)组成的三维图形基本单元。
顶点坐标单位(Vertex Coordinate Unit)用于描述几何体顶点的位置信息。
合理设置顶点坐标单位,可以提高渲染性能,避免不必要的计算错误。
【3.如何在OSG中设置几何体顶点坐标单位】设置几何体顶点坐标单位的方法如下:1)首先,创建一个osg::Geometry对象。
2)使用osg::Geometry::setVertexArrayEnabled()方法启用顶点数组。
3)使用osg::Geometry::setVertexBuffer()方法设置顶点缓冲区。
4)使用osg::Geometry::setUseDisplayList()方法设置是否使用显示列表。
5)使用osg::Geometry::addVertex()方法添加顶点坐标。
【4.实例演示】以下是一个简单的OSG几何体顶点坐标单位设置示例:```cpp#include <osg/Geometry>#include <osg/Geode>#include <osgViewer/Viewer>int main(){osgViewer::Viewer viewer;// 创建一个Geode对象osg::Geode* geode = new osg::Geode;// 创建一个Geometry对象osg::Geometry* geometry = new osg::Geometry;// 设置顶点数组启用geometry->setVertexArrayEnabled(true);// 设置顶点缓冲区osg::ref_ptr<osg::Array> vertices = new osg::Array;vertices->push_back(osg::Vec3(0, 0, 0));vertices->push_back(osg::Vec3(1, 0, 0));vertices->push_back(osg::Vec3(0, 1, 0));geometry->setVertexBuffer(vertices.get());// 设置顶点坐标geometry->addVertex(osg::Vec3(0, 0, 0));geometry->addVertex(osg::Vec3(1, 0, 0));geometry->addVertex(osg::Vec3(0, 1, 0));// 将几何体添加到Geode中geode->addDrawable(geometry);// 设置场景根节点viewer.setSceneData(geode);// 初始化视图viewer.realize();while (!viewer.done()){viewer.frame();}return 0;}```【5.总结】在OSG中,合理设置几何体顶点坐标单位可以提高渲染性能,避免不必要的计算错误。
计算机图形学上机报告

计算机图形学上机报告计算机图形学上机实验报告计算机科学与技术学院班级:学号:姓名:指导教师:完成⽇期:实验⼀:基本图元绘制⼀. 实验⽬的1. 了解如何利⽤OpenGL库绘制图形2. 理解glut程序框架3. 理解窗⼝到视区的变换4. 理解OpenGL实现动画的原理⼆. 实验内容1. 实现中点Bresenham算法画直线2. 实现改进Bresenham算法画直线3. 实现圆的绘制三. 实验结果1.DDA算法画直线2. 中点Bresenham算法画直线3. 改进Bresenham算法画直线4. Bresenham算法画圆四. 源程序#include#include#include "stdio.h"int m_PointNumber = 0; //动画时绘制点的数⽬int m_DrawMode = 4; //绘制模式 1 DDA算法画直线// 2 中点Bresenham算法画直线// 5 四分法绘制椭圆//绘制坐标线void DrawCordinateLine(void){int i = 0 ;//坐标线为⿊⾊glColor3f(0.0f, 0.0f ,0.0f);glBegin(GL_LINES);for (i=10;i<=250;i=i+10){glVertex2f((float)(i), 0.0f);glVertex2f((float)(i), 250.0f);glVertex2f(0.0f, (float)(i));glVertex2f(250.0f, (float)(i));}glEnd();}//绘制⼀个点,这⾥⽤⼀个正⽅形表⽰⼀个点。
void putpixel(GLsizei x, GLsizei y){glRectf(10*x,10*y,10*x+10,10*y+10);}void DDACreateLine(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num) {//设置颜⾊glColor3f(1.0f,0.0f,0.0f);//对画线动画进⾏控制if(num == 1)printf("DDA画线算法:各点坐标\n");else if(num==0)return;//画线算法的实现GLsizei dx,dy,epsl,k;GLfloat x,y,xIncre,yIncre;x = x0;y = y0;if(abs(dx) > abs(dy)) epsl = abs(dx);else epsl = abs(dy);xIncre = (float)dx / epsl ;yIncre = (float)dy / epsl ;for(k = 0; k<=epsl; k++){putpixel((int)(x+0.5), (int)(y+0.5));if (k>=num-1) {printf("x=%f,y=%f,取整后x=%d,y=%d\n", x, y, (int)(x+0.5),(int)(y+0.5));break;}x += xIncre;y += yIncre;if(x >= 25 || y >= 25) break;}}void BresenhamLine(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num) {glColor3f(1.0f,0.0f,0.0f);if(num == 1)printf("中点Bresenham算法画直线:各点坐标及判别式的值\n");else if(num==0)return;//画线算法的实现GLsizei dx,dy,d,UpIncre,DownIncre,x,y,xend=0;if(x0>x1){x=x1;x1=x0;x0=x;y=y1;y1=y0;y0=y;}x=x0;y=y0;dx=x1-x0;dy=y1-y0;d=dx-2*dy;putpixel(x,y);if (x>=num-1) {break;}x++;if(d<0){y++;d+=UpIncre;}else d+=DownIncre;if(x >= 50 || y >= 50) break;}}void Bresenham2Line(GLsizei x0, GLsizei y0, GLsizei x1, GLsizei y1, GLsizei num) { glColor3f(1.0f,0.0f,0.0f);if(num == 1)printf("改进的Bresenham算法画直线:各点坐标及判别式的值\n");else if(num==0)return;//画线算法的实现GLsizei x,y,dx,dy,e;dx=x1-x0;dy=y1-y0;e=-dx;x=x0;y=y0;while (x<=x1){putpixel(x,y);if (x>=num-1) {break;}x++;e=e+2*dy;if(e>0){y++;if(x >= 50 || y >= 50) break;}}void BresenhamCircle(GLsizei x, GLsizei y, GLsizei r, GLsizei num) {glColor3f(1.0f,0.0f,0.0f);if(num == 1)printf("Bresenham算法画圆:各点坐标及判别式的值\n");x=0,y=r;GLsizei d=1-r;while (xputpixel(x,y);if (x>=num) {break;}putpixel(y,x);if (x>=num) {break;}putpixel(-y,x);if (x>=num) {break;}putpixel(-x,y);if (x>=num) {break;}putpixel(-x,-y);if (x>=num) {break;}putpixel(-y,-x);if (x>=num) {putpixel(y,-x);if (x>=num) {break;}putpixel(x,-y);if(d<0)d+=2*x+3;else{d+=2*(x-y)+5;y--;}x++;}}//初始化窗⼝void Initial(void){// 设置窗⼝颜⾊为蓝⾊glClearColor(1.0f, 1.0f, 1.0f, 1.0f);}// 窗⼝⼤⼩改变时调⽤的登记函数void ChangeSize(GLsizei w, GLsizei h){if(h == 0) h = 1;// 设置视区尺⼨glViewport(0, 0, w, h);// 重置坐标系统glMatrixMode(GL_PROJECTION); glLoadIdentity();// 建⽴修剪空间的范围if (w <= h)glOrtho (0.0f, 250.0f, 0.0f, 250.0f*h/w, 1.0, -1.0); elseglOrtho (0.0f, 250.0f*w/h, 0.0f, 250.0f, 1.0, -1.0); }void ReDraw(void){//⽤当前背景⾊填充窗⼝glClear(GL_COLOR_BUFFER_BIT);//画出坐标线DrawCordinateLine();switch(m_DrawMode){case 1:DDACreateLine(0,0,20,15,m_PointNumber); break;case 2:BresenhamLine(0,0,20,15,m_PointNumber); break;case 3:Bresenham2Line(1,1,8,6,m_PointNumber); break;case 4:BresenhamCircle(5,5,18,m_PointNumber); break;default:break;}glFlush();}//设置时间回调函数void TimerFunc(int value){if(m_PointNumber == 0)value = 1;m_PointNumber = value; glutPostRedisplay();glutTimerFunc(500, TimerFunc, value+1); }void Keyboard(unsigned char key, int x, int y){if (key == '1') m_DrawMode = 1;if (key == '2') m_DrawMode = 2;if (key == '3') m_DrawMode = 3;if (key == '4') m_DrawMode = 4;m_PointNumber = 0;glutPostRedisplay();}//void main(void)int main(int argc, char* argv[]){glutInit(&argc, argv);//初始化GLUT库OpenGL窗⼝的显⽰模式glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(800,600); glutInitWindowPosition(100,100); glutCreateWindow("基本图元绘制程序"); glutDisplayFunc(ReDraw);glutReshapeFunc(ChangeSize); glutKeyboardFunc(Keyboard);//键盘响应回调函数glutTimerFunc(500, TimerFunc, 1);// 窗⼝初始化Initial();glutMainLoop(); //启动主GLUT事件处理循环return 0;}实验⼆:⽇地⽉模型⼀. 实验⽬的1. 理解OpenGL中的变换过程2. 理解透视投影与平⾏投影的不同3. 了解深度测试⼆. 实验内容1. 通过变换调整观察的位置与⽅向三. 实验结果太阳、地球和⽉亮的运动模型图1.图2.图3.四. 源程序#include#include#include#includevoid Initial(){glEnable(GL_DEPTH_TEST); // 启⽤深度测试glFrontFace(GL_CCW); // 指定逆时针绕法表⽰多边形正⾯glClearColor(1.0f, 1.0f, 1.0f, 1.0f ); //背景为⽩⾊}void ChangeSize(int w, int h){if(h == 0) h = 1;// 设置视区尺⼨glViewport(0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();// 设置修剪空间GLfloat fAspect;fAspect = (float)w/(float)h;gluPerspective(45.0, fAspect, 1.0, 500.0);/*if (w <= h)glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);elseglOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f); */glLoadIdentity();}void RenderScene(void){// 绕原⼦核旋转的⾓度static float fElect1 = 0.0f;static float f2=0.0f;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 重置模型视图矩阵glMatrixMode(GL_MODELVIEW);glLoadIdentity();//将图形沿z轴负向移动glTranslatef(0.0f, 0.0f, -250.0f);// 绘制红⾊的原⼦核glColor3f(1.0f, 0.0f, 0.0f);glutSolidSphere(50.0f, 15, 15);// 当前绘制颜⾊变为黄⾊glColor3f(0.0f, 0.0f, 0.0f);//绘制第⼀个电⼦//保存当前的模型视图矩阵glRotatef(fElect1, 0.0f, 1.0f, 0.0f);//绕y轴旋转⼀定的⾓度glTranslatef(80.0f, 0.0f, 0.0f);//平移⼀段距离glutSolidSphere(12.0f, 15, 15);//画出电⼦// 恢复矩阵// 第⼆个电⼦glPushMatrix();glRotatef(45.0f, 0.0f, 0.0f, 1.0f);glRotatef(f2, 0.0f, 1.0f, 0.0f);glTranslatef(-20.0f, 0.0f, 0.0f);glutSolidSphere(6.0f, 15, 15);glPopMatrix();/* // 第三个电⼦glPushMatrix();glRotatef(-45.0f,0.0f, 0.0f, 1.0f);glTranslatef(0.0f, 0.0f, 60.0f);glutSolidSphere(6.0f, 15, 15);glPopMatrix();*/// 增加旋转步长fElect1 += 10.0f;f2=fElect1*6;if(fElect1 > 360.0f){fElect1 = 10.0f;}glutSwapBuffers();}void TimerFunc(int value){glutPostRedisplay();glutTimerFunc(100, TimerFunc, 1);}int main(int argc, char* argv[]){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("⽇地⽉模型");glutReshapeFunc(ChangeSize);glutDisplayFunc(RenderScene);glutTimerFunc(500, TimerFunc, 1);Initial();glutMainLoop();return 0;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
天津理工大学计算机科学与技术学院实验报告2015 至2016 学年第二学期课程名称计算机图形学学号学生姓名年级专业课程号实验地点实验时间主讲教师辅导教师实验(一)实验名称绘制基本图形软件环境Visual Studio 2010 OSG-3.1.0Windows 7硬件环境2G内存显卡GT610 CPU奔腾双核硬盘320G实验目的1. 理解图形元素显示的基本原理,掌握扫描转换直线段的常用算法原理,扫描转换圆弧的常用算法原理。
2. 熟悉OpenSceneGraph常用几个类(Group,Geode,Geometry,Node,Viewer,osg::Vec3Array)3. 熟悉OpenSceneGraph构建场景的基本框架4. 掌握绘制不同大小和颜色的点的方法5. 掌握绘制不同线型和颜色的直线的方法(直线,折线,环线)6. 掌握绘制彩色多边形边框的方法(各种设备)7. 熟悉OpenSceneGraph的编程环境实验内容(应包括实验题目、实验要求、实验任务等)实验题目:1.绘制基本形状(osg::PrimitiveSet::POINTS,osg::PrimitiveSet::LINES,osg::PrimitiveSet::LINE_STRIP,osg::PrimitiveSet::LINE_LOOP,osg::PrimitiveSet::POLYGON,osg::PrimitiveSet::QUADS,osg::PrimitiveSet::QUAD_STRIP,osg::PrimitiveSet::TRIANGLES,osg::PrimitiveSet::TRIANGLE_STRIP,osg::PrimitiveSet::TRIANGLE_FAN)。
2.实现各自名字的绘制。
3.颜色的设置要合适。
实验要求:1.设计结构合理、扩展灵活。
2.每个设备类独立封装。
3.提交电子版实验报告及工程代码,试验报告模板可参考计算机学院统一模板4.提交的工程代码必须删除其中的Debug或Release文件夹实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)节点模型:NodeGroupGeodeGeometry点线三角形四边形文字实验步骤:1.在纸上绘制草图,建立三维坐标系,并在坐标系中画出个图形,并设定各点坐标,使其坐标符合逻辑,以供绘制图形坐标的选择;2.创建Group节点,Geode节点,Geomotry节点,3.创建顶点坐标数组,颜色数组,法线数组;4.实现基本绘制。
流程图:开始设置顶点坐标,并设置其对应顶点坐标设置顶点坐标与颜色对应关系,为一一对应模式使用addDrawable函数用viewer设置SetSceneData()viewer.run()结束结果展示:源代码:#include <Windows.h>#include<osg/Geode>#include<osgDB/ReadFile>#include<osgUtil/SmoothingVisitor>#include<osgViewer/Viewer>#include <osg/Geometry>#include<osg/Matrixd>#include<osg/MatrixTransform>#include<osg/ShapeDrawable>#include<osgText/Text>#include <osgText/Font>#include <locale.h>void createContent1(osgText::Text& textObject,const char* string); osg::Geode* draw(){osg::ref_ptr<osg::Node> node = new osg::Node;osg::ref_ptr<osg::Group> group = new osg::Group;osg::ref_ptr<osg::Geode> geode = new osg::Geode;osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;osg::ref_ptr<osg::Vec3Array> point = new osg::Vec3Array;osg::ref_ptr<osg::Vec3Array> color = new osg::Vec3Array;osg::ref_ptr<osg::Vec3Array> normal =new osg::Vec3Array;point->push_back(osg::Vec3(0,0,0));point->push_back(osg::Vec3(0,3,0));color->push_back(osg::Vec3(0,0,0));color->push_back(osg::Vec3(0,0,0));point->push_back(osg::Vec3(-20,0,20));point->push_back(osg::Vec3(-15,0,20));point->push_back(osg::Vec3(-15,0,18));point->push_back(osg::Vec3(-20,0,18));color->push_back(osg::Vec3(1,0,0));color->push_back(osg::Vec3(0,1,0));color->push_back(osg::Vec3(0,1,1));color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-14,0,20));point->push_back(osg::Vec3(-10,0,20));point->push_back(osg::Vec3(-10,0,18));point->push_back(osg::Vec3(-14,0,18));color->push_back(osg::Vec3(1,0,0));color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-9,0,20)); point->push_back(osg::Vec3(-6,0,20)); point->push_back(osg::Vec3(-6,0,18)); point->push_back(osg::Vec3(-9,0,18));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-5,0,20)); point->push_back(osg::Vec3(-3,0,21)); point->push_back(osg::Vec3(-1,0,20)); point->push_back(osg::Vec3(-1,0,19)); point->push_back(osg::Vec3(-3,0,18)); point->push_back(osg::Vec3(-5,0,19));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0)); color->push_back(osg::Vec3(0,0.7,0.5)); color->push_back(osg::Vec3(1,1,1));point->push_back(osg::Vec3(0,0,20)); point->push_back(osg::Vec3(1,0,20)); point->push_back(osg::Vec3(1,0,18)); point->push_back(osg::Vec3(0,0,18));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-20,0,15)); point->push_back(osg::Vec3(-20,0,11)); point->push_back(osg::Vec3(-15,0,11)); point->push_back(osg::Vec3(-15,0,15)); point->push_back(osg::Vec3(-12,0,9)); point->push_back(osg::Vec3(-12,0,13));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0)); color->push_back(osg::Vec3(0,0.7,0.5)); color->push_back(osg::Vec3(1,1,1));point->push_back(osg::Vec3(-11,0,15)); point->push_back(osg::Vec3(-11,0,11)); point->push_back(osg::Vec3(-9,0,15)); point->push_back(osg::Vec3(-9,0,11)); point->push_back(osg::Vec3(-7,0,13)); point->push_back(osg::Vec3(-7,0,9));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0)); color->push_back(osg::Vec3(0,0.7,0.5)); color->push_back(osg::Vec3(1,1,1));point->push_back(osg::Vec3(-5,0,10)); point->push_back(osg::Vec3(-1,0,15)); point->push_back(osg::Vec3(1,0,10)); point->push_back(osg::Vec3(-1,0,5));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-20,0,-1)); point->push_back(osg::Vec3(-16,0,5)); point->push_back(osg::Vec3(-14,0,-1)); point->push_back(osg::Vec3(-16,0,-6));color->push_back(osg::Vec3(1,0,0)); color->push_back(osg::Vec3(0,1,0)); color->push_back(osg::Vec3(0,1,1)); color->push_back(osg::Vec3(1,1,0));point->push_back(osg::Vec3(-12,0,-1)); point->push_back(osg::Vec3(-8,0,5));point->push_back(osg::Vec3(-6,0,-1));point->push_back(osg::Vec3(-8,0,-6));color->push_back(osg::Vec3(1,0,0));color->push_back(osg::Vec3(0,1,0));color->push_back(osg::Vec3(0,1,1));color->push_back(osg::Vec3(1,1,0));normal->push_back(osg::Vec3(0,-1,0));geometry->setVertexArray(point.get());geometry->setColorArray(color.get());geometry->setNormalArray(normal.get());geometry->setNormalBinding(osg::Geometry::BIND_OVERALL);geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::POINTS,0,2));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::LINES,2,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::LINE_STRIP,6,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::LINE_STRIP,10,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::POLYGON,14,6));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::LINE_LOOP,20,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::QUADS,24,6));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::QUAD_STRIP,30,6));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::TRIANGLES,36,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::TRIANGLE_STRIP,40,4));geometry->addPrimitiveSet(newosg::DrawArrays(osg::DrawArrays::TRIANGLE_FAN,44,4));setlocale(LC_ALL,".936");// 配置地域化信息const char* titleString="zhengzhanwei";osg::ref_ptr<osgText::Text> text0 = new osgText::Text;osg::ref_ptr<osgText::Font> font = new osgText::Font();font = osgText::readFontFile("C:\\WINDOWS\\Fonts\\simhei.ttf");text0->setFont(font.get());//text0->setFont("C:\\WINDOWS\\Fonts\\simhei.ttf");text0->setFontResolution( 128, 128 );text0->setColor(osg::Vec4(255, 0, 0, 1));text0->setCharacterSize( .6f );text0->setPosition( osg::Vec3( 0.f, 0.f, 0.f ) );text0->setAxisAlignment( osgText::Text::XZ_PLANE );text0->setAlignment( osgText::Text::CENTER_TOP );createContent1(*text0,titleString);geode->addDrawable( text0.get() );geode->addDrawable(geometry);return geode.release();}void createContent1(osgText::Text& textObject,const char* string) {int requiredSize=mbstowcs(NULL,string,0);//如果mbstowcs第一参数为NULL那么返回字符串的数目wchar_t* wText=new wchar_t[requiredSize+1];mbstowcs(wText,string,requiredSize+1);//由char转换成wchar类型textObject.setText(wText);delete wText;}int main(int argc,char ** argv){osgViewer::Viewer viewer;viewer.setSceneData(draw());return viewer.run();}。