刘总的笔记小站

生活常识,娱乐搞笑,编程技巧,智能家居,深度学习,网络神经,数据挖掘

glut绘制正余弦曲线

1,下载最新的glut动态库,1998年发布,并且不再更新: https://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip

2,编译openglDemo源码:参见命令行:cl.exe openTran.cpp /EHsc /I"./" glut32.lib /link /LIBPATH:"./GL"

3,程序效果:

image.png


4,源码示例:


#include <math.h>

#include "GL/glut.h"

#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )

void init(void){
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
}

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    //glTranslatef(0.8, 0.0, 0.0);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(-0.5, -0.5);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2f(0.5, -0.5);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(0.5, 0.5);
    glEnd();
    
    float PI=3.1415926f;
    glBegin(GL_LINE_STRIP);
	for(float x=-5*PI;x<5*PI;x+=0.01f)
	{
		//glVertex2f(x/(5*PI),sin(x));
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(x/(5*PI),sin(x));
        glColor3f(0.0, 1.0, 0.0);
        glVertex2f(x/(5*PI), 0);

	}
	glEnd();
    
    
    glFlush();
}

void myReshape(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    if (w <= h)
    gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w);
    else
    gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5);
    
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 200);
    glutInitWindowSize(800, 800);
    glutCreateWindow("triangle");
    init();

    glutReshapeFunc(myReshape);
    glutDisplayFunc(myDisplay);
    glutMainLoop();
    
    return 0;
}
//#include <GL/glut.h> 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <OpenGL/gl.h> 
#include <OpenGL/glu.h> 
#include <GLUT/glut.h> 

// gcc -framework OpenGL -framework GLUT -framework Foundation -o opengl opengl.c

#define __LOG__
#ifdef __LOG__
#define log(format,...)  printf("LOG:[%s][%03d][%s][%s][%s]"format"\n", __FILE__, __LINE__, __DATE__, __TIME__, __FUNCTION__, ##__VA_ARGS__)
#else
#define log(format,...)
#endif


void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);//设置背景颜色为黑色
    glShadeModel(GL_SMOOTH);//设置为光滑明暗模式
}
void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);// 将缓存清除为预先的设置值,即黑色
    //glTranslatef(0.8, 0.0, 0.0);//平移函数,暂时可以不用
    glBegin(GL_TRIANGLES);//开始画三角形
    glColor3f(1.0, 0.0, 0.0);//设置第一个顶点为红色
    glVertex2f(-1.0, -1.0);//设置第一个顶点的坐标
    glColor3f(0.0, 1.0, 0.0);//设置第二个顶点为绿色
    glVertex2f(0.0, -1.0);//设置第二个顶点的坐标
    glColor3f(0.0, 0.0, 1.0);//设置第三个顶点为蓝色
    glVertex2f(-0.5, 1.0);//设置第三个顶点的坐标
    glEnd();//三角形结束
    glFlush();//强制OpenGL函数在有限时间内运行
}
void myReshape(GLsizei w, GLsizei h)
{
    glViewport(0, 0, w, h);//设置视口
    glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION
    glLoadIdentity();//将当前矩阵置换为单位阵
    if (w <= h)
        gluOrtho2D(-1.0, 1.5, -1.5, 1.5*(GLfloat)h / (GLfloat)w);//定义二维正视投影矩阵
    else
        gluOrtho2D(-1.0, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5);
    glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW
}
void myKeyFunc (unsigned char key, int x, int y)
{
    printf("myKeyFunc key = char[%c] [0x%x]\n", key, key);
    //fflush(stdout);
   switch (key) {
      case 27:
         exit(0);
         break;
      default:
         break;
   }
}
int main(int argc, char ** argv)
{
    /*初始化*/
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//单缓冲、RGB模式
    glutInitWindowSize(960, 540);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("GL_TRIANGLES");//窗口标题
    init();

    printf("GL_VERSION %s\n", (char *)glGetString(GL_VERSION));
    printf("GL_VENDOR %s\n", (char *)glGetString(GL_VENDOR));
    printf("GL_RENDERER %s\n", (char *)glGetString(GL_RENDERER));
    printf("GL_EXTENSIONS %.64s\n", (char *)glGetString(GL_EXTENSIONS));

    /*绘制与显示*/
    glutReshapeFunc(myReshape);//窗口大小发生改变时采取的行为
    glutDisplayFunc(myDisplay);//显示绘制图形
    glutKeyboardFunc(myKeyFunc);
    glutMainLoop();//循环
    return(0);
}
#include <stdio.h>
#include <stdlib.h>

// #include <GL/glut.h>

#include <OpenGL/gl.h> 
#include <OpenGL/glu.h> 
#include <GLUT/glut.h> 

#define Width 64  
#define Height 64  
GLubyte image[Height][Width][3];//rgb   
GLint height;  
void image_create()//在内存中创建一幅棋盘图像的矩阵   
{  
    int c;  
    for(int i=0;i<Height;i++)  
    {  
        for(int j=0;j<Width;j++)  
        {  
            c=(((i&0x8)==0)^((j&0x8)==0))*255;  
            image[i][j][0]=(GLubyte)c;  
            image[i][j][1]=(GLubyte)c;  
            image[i][j][2]=(GLubyte)c;  
        }  
    }     
}  
void setup_image()  
{  
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);  
    glShadeModel(GL_FLAT);  
    image_create();  
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);//边界对齐   
}  
void image_motion(int x,int y)  
{  
    glRasterPos2i(x,y);  
    glPixelZoom(2.0,2.0);  
    glCopyPixels(0,0,Width,Height,GL_COLOR);  
    glFlush();  
}   
void image_display()  
{  
    glClear(GL_COLOR_BUFFER_BIT);  
    glRasterPos2i(0,0);//和位图一样先设好光栅位置   
    glDrawPixels(Width,Height,GL_RGB,GL_UNSIGNED_BYTE,image);//绘制   
    glFlush();  
    image_motion(2*Width,2*Height);  
}  
void reshape_image(int w, int h)  
{  
    if (h == 0)  
        h = 1;  
    height=h;  
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);  
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);  
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  
}  
  
int main_image()  
{  
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
    glutInitWindowSize(100, 100);  
    glutCreateWindow("text");  
    setup_image();  
    glutDisplayFunc(image_display);  
    glutReshapeFunc(reshape_image);  

    glutMainLoop();  
    return 0;  
}  




int main(int argc, char ** argv)
{
    /*初始化*/
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//单缓冲、RGB模式
    glutInitWindowSize(960, 540);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("三角形");//窗口标题
    //init();
    /*绘制与显示*/
    glutReshapeFunc(reshape_image);//窗口大小发生改变时采取的行为
    glutDisplayFunc(image_display);//显示绘制图形
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glutMainLoop();//循环
    return(0);
}


发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
搜索
«   2024年9月   »
1
2345678
9101112131415
16171819202122
23242526272829
30
网站分类
最新留言
文章归档
网站收藏
友情链接
图标汇集
Powered by Z-BlogPHP

  • Copyright ©2021 @liuzong All rights reserved.
  • 陕ICP备17016542号