用OpenGL在 visual studio 2005 下画简单的二维函数图像
2008-03-09 20:18
267 查看
书上又一例子,画公式 f(x) = e-x cos(2*pi*x) 的曲线,效果如下:
这是我在visual studio 2005 下编译通过的源代码:
// Draw the "dot plots" of a function
//P52 in <Computer Graphics Using OpenGL(Second Edition)>
#include<math.h>
#include<GL/glut.h>
const int screenWidth = 640; // width of screen window in pixels
const int screenHeight = 480; // height of screen window in pixels
GLdouble A,B,C,D; // values used for scaling and shifting
//----------Initialization--------------
void init(void)
...{
glClearColor(1.0,1.0,1.0,0.0); // Set white background color
glColor3f(0.0f,0.0f,0.0f); // Drawing color is black
glPointSize(2.0); // a 'dot' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION); // Set "camera shape"
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
// Set values used for scaling and shifting
A = screenWidth/4.0;
B = 0.0;
C = D = screenHeight/2.0;
}
// --------Draw the "dot plots" of a function------
void dotPlots(void)
...{
glClear(GL_COLOR_BUFFER_BIT); //clear the screen
glBegin(GL_POINTS);
for(GLdouble x = 0;x<4.0;x += 0.005)
...{
GLdouble func = exp(-x) * cos(2*3.14159265*x);
glVertex2d(A*x+B,C*func+D);
}
glEnd();
// Draw a horizontal line
glBegin(GL_LINES);
glVertex2i(0,screenHeight/2);
glVertex2i(screenWidth,screenHeight/2);
glEnd();
glFlush(); //send all output to display
}
//--------main-----------
void main(int argc,char** argv)
...{
glutInit(&argc, argv); // Initialize the toolkit
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // Set display mode
glutInitWindowPosition(100, 150); // Set window pozition on screen
glutInitWindowSize(screenWidth, screenHeight); // Set window size
glutCreateWindow("Dot plot of a Function"); // Open the screen window
glutDisplayFunc(dotPlots); // Register redraw function
init();
glutMainLoop(); // Go into a perpetual loop
}
在上面的 dotPlot()中的for循环里做一些更改就可以画另一个函数图了
这是我在visual studio 2005 下编译通过的源代码:
// Draw the "dot plots" of a function
//P52 in <Computer Graphics Using OpenGL(Second Edition)>
#include<math.h>
#include<GL/glut.h>
const int screenWidth = 640; // width of screen window in pixels
const int screenHeight = 480; // height of screen window in pixels
GLdouble A,B,C,D; // values used for scaling and shifting
//----------Initialization--------------
void init(void)
...{
glClearColor(1.0,1.0,1.0,0.0); // Set white background color
glColor3f(0.0f,0.0f,0.0f); // Drawing color is black
glPointSize(2.0); // a 'dot' is 2 by 2 pixels
glMatrixMode(GL_PROJECTION); // Set "camera shape"
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble)screenHeight);
// Set values used for scaling and shifting
A = screenWidth/4.0;
B = 0.0;
C = D = screenHeight/2.0;
}
// --------Draw the "dot plots" of a function------
void dotPlots(void)
...{
glClear(GL_COLOR_BUFFER_BIT); //clear the screen
glBegin(GL_POINTS);
for(GLdouble x = 0;x<4.0;x += 0.005)
...{
GLdouble func = exp(-x) * cos(2*3.14159265*x);
glVertex2d(A*x+B,C*func+D);
}
glEnd();
// Draw a horizontal line
glBegin(GL_LINES);
glVertex2i(0,screenHeight/2);
glVertex2i(screenWidth,screenHeight/2);
glEnd();
glFlush(); //send all output to display
}
//--------main-----------
void main(int argc,char** argv)
...{
glutInit(&argc, argv); // Initialize the toolkit
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); // Set display mode
glutInitWindowPosition(100, 150); // Set window pozition on screen
glutInitWindowSize(screenWidth, screenHeight); // Set window size
glutCreateWindow("Dot plot of a Function"); // Open the screen window
glutDisplayFunc(dotPlots); // Register redraw function
init();
glutMainLoop(); // Go into a perpetual loop
}
在上面的 dotPlot()中的for循环里做一些更改就可以画另一个函数图了
相关文章推荐
- opencv图像处理的一些常用函数简单使用
- 用 OpenGL 函数实现二维观察的窗口与视区的映射变换,修改视口定义函数,熟悉窗口与视区的概念
- ch4_3_5利用radon函数和iradon函数构造一个简单图像的投影并重建图像.m
- Visual Studio 2005 以上成员函数向导的修改!
- 使用Visual Studio 2005搭建opengl开发环境
- VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例
- 在visual studio 2005 下建立openGL开发环境 - [学习日志]
- 使用GeoGebra绘制简单函数图像
- 在visual studio 2005 下建立openGL开发环境
- 统计函数耗费时间,简单的图像运算,定义感兴趣区域 第二章
- Opencv简单编程--Mat图像作为函数参数
- 在visual studio 2005 下建立openGL开发环境( 转载)
- 计算机视觉与图像处理学习笔记(三)opencv的基本数据类型与简单图像处理函数
- sqlserver存储过程简单的身份验证程序(开发工具Visual Studio 2005)
- C#2005 一个简单的查询界面代码:DataGridView使用、图像显示、复合查询样例
- openGL—绘制简单二维图形
- 安装visual studio 2005 sp1 补丁后,简单的#include <memory>都编译出错
- Visual Studio 2005 与 Visual Studio 2008 项目文件共存问题简单解决办法
- 【matplotlib】绘制简单二维、三维图像
- Visual Studio 2005-如何添加 需要重载 的虚函数