| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3462 人关注过本帖
标题:求opengl画函数图像
只看楼主 加入收藏
caoweizhong
Rank: 2
等 级:论坛游民
帖 子:19
专家分:65
注 册:2011-1-22
收藏
得分:17 
// 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)
{GLdouble x;
    glClear(GL_COLOR_BUFFER_BIT);       //clear the screen
    glBegin(GL_POINTS);
for(x=0;x<4.0;x+=0.005)
  {
    //GLdouble func = exp(-x) * cos(2*3.14159265*x);
GLdouble func = sin(x*2);
    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
}

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
}
2011-02-04 22:11
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:0 
再往高点,输入一段C语言函数,输出一个3D函数图像

很给里

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-04 22:13
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
收藏
得分:0 
晕死,弄了半天还是用不了glut,你们什么ide啊

The tools I recommended:
GUI: CSharp(VS), QT;    Core Code: Plain C (Tiny C Compiler);    Web: Python, JavaScript;    Android: Java;    Embedded System: ASM&C (Linux)
2011-02-04 23:22
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:0 
你们什么ide????VC6啊

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-05 20:26
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:0 
俺也学习一下.
2011-02-05 20:42
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:0 
VS2008也不错吧

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-05 20:51
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
收藏
得分:0 
我己经标明出VS2010,楼主没有看清楚吗,刚才笔误

[ 本帖最后由 点线面 于 2011-2-5 23:22 编辑 ]

小代码,大智慧
2011-02-05 22:56
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:0 
写VS2010 ????这么彪悍?

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-02-05 23:17
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
收藏
得分:0 
#include<math.h>
#include<GL/glut.h>
# define fun (sin(x)*(tan(x)+cos(x)))
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/16.0;
    B = screenWidth/2.0;
    C = D = screenHeight/2.0;

}
// --------Draw the "dot plots" of a function------

GLdouble h(GLdouble x)
{
         return fun;
}

void dotPlots(void)
{
     GLdouble x;
     glClear(GL_COLOR_BUFFER_BIT);       //clear the screen
     glBegin(GL_POINTS);
     for(x=-16.0;x<16.0;x+=0.0001)
     {
    //GLdouble func = exp(-x) * cos(2*3.14159265*x);
               GLdouble func = h(x);
               glVertex2d(A*x+B,(C*func/20.0+D));
      }
      glEnd();
    // Draw a horizontal line
       glBegin(GL_LINES);
       glVertex2i(0,screenHeight/2);
       glVertex2i(screenWidth,screenHeight/2);
       glEnd();
       glBegin(GL_LINES);
       glVertex2i(screenWidth/2,0);
       glVertex2i(screenWidth/2,screenHeight);
       glEnd();
       glFlush(); //send all output to display
}

 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
}

The tools I recommended:
GUI: CSharp(VS), QT;    Core Code: Plain C (Tiny C Compiler);    Web: Python, JavaScript;    Android: Java;    Embedded System: ASM&C (Linux)
2011-02-06 14:22
zjsxwc
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:252
专家分:601
注 册:2011-1-20
收藏
得分:0 
原来这么简单啊

The tools I recommended:
GUI: CSharp(VS), QT;    Core Code: Plain C (Tiny C Compiler);    Web: Python, JavaScript;    Android: Java;    Embedded System: ASM&C (Linux)
2011-02-06 14:23
快速回复:求opengl画函数图像
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026135 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved