OpenGL 绘图基础(OpenGL编译环境配置 + 正方形 +五角星)

visual c++ 6.0  OpenGL编译环境配置

    安装GLUT工具包GLUT不是OpenGL所必须的,但它会给的学习带来一定的方便,推荐安装。
  Windows环境下安装GLUT的步骤:
   (1)“d:\ProgramFiles\MicrosoftVisualStudio\VC98\lib文件夹”)。
   把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹,即lib文件夹。
   (2)把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。没用的话直接放在工程文件下
  (典型的位置为:C:\Windows\System32)这是非常重要的动态链接库设置。

     第三步,创建工程,其步骤如下:

       (3)创建一个Win32ConsoleApplication。

   (4) 链接OpenGLlibraries:在VisualC++中先单击Project,再单击Settings,再找到Link单击,最后在Object/librarymodules的最前面加上opengl32.libGlut32.libGlaux.libglu32.lib。
     (5)单击ProjectSettings中的C/C++标签,将Preprocessordefinitions中的_CONSOLE改为__WINDOWS。

  最后单击好。


#include <windows.h>

#include <gl/glut.h>

void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//用当前的清除颜色清除窗口
        glColor3f(1.0f,1.0f,0.0f); //绘制绘图颜色
	glLineWidth(1);           //线的宽度

	/*glRectf(-25.0f,25.0f,25.0f,-25.0f); //绘制填充矩形*/

	glBegin(GL_LINE_LOOP);
	      glVertex3f(0.0f,0.0f,0.0f);
              glVertex3f(0.0f,50.0f,0.0f);
		/* glColor3f(1.0f,1.0f,0.5f); 这样再画的话,颜色就变了*/
              glVertex3f(-50.0f,0.0f,0.0f);
	glEnd();
	glFlush();   //强制刷新绘图命令
    
}
void ChangeSize(GLsizei w,GLsizei h)
{
	GLfloat aspectRatio;
	if(h==0) h=1; //防止被0所除
	glViewport(0,0,w,h); //可以对窗口区域进行划分,计算机图形学中,在屏幕上打开窗口的任务是由窗口系统,而不是OpenGL负责的。
	glMatrixMode(GL_PROJECTION);//重置投影矩阵堆栈
	glLoadIdentity(); //在进行变换前把当前矩阵设置为单位矩阵
//使正方形不变形的工作
	aspectRatio=(GLfloat)w/(GLfloat)h;
    if(w<=h)
	  glOrtho(-100.0,100.0,-100/aspectRatio,100.0/aspectRatio,1.0,-1.0);  //截取该区域,就保证形状不变了
	else
	  glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100,100.0,1.0,-1.0);
	 glMatrixMode(GL_MODELVIEW); //重置模型视图堆栈
         glLoadIdentity();
}

void SetupRC(void)  
{
	glClearColor(0.0f,0.0f,1.0f,1.0f); //设置清除颜色
}

int main(int argc, char* argv[])
{
	glutInit(&argc,argv);  //对GLUT函数库进行初始化
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); //创建窗口时使用哪种类型的显示模式
	glutCreateWindow("simple");    //创建glut窗口
	glutDisplayFunc(RenderScene); //当窗口被绘制时,调用RenderScene函数
	SetupRC();//执行渲染之前应完成的所有OpenGL初始化工作
	glutReshapeFunc(ChangeSize);
	glutMainLoop(); // 让整个绘图循环进行,相当于死循环。
	return 0;
}
注 :RenderScene 这个名字等价于 myDisplay
#include <windows.h>
#include <glut.h>    
#include <math.h>
  
float PI=3.1415926f;  
float R=0.8f;  //半径  
  
void myDisplay(void)      
{   
    
    //画五角星  
    glClear(GL_COLOR_BUFFER_BIT);  
    glClearColor(0,0,0,0);  
    glColor4f(0,0,1,0);  
    glBegin(GL_LINE_LOOP);  
  
    //以下ABCDE分别是五角星的5个顶点  
    GLfloat xA=R*cos(90*2*PI/360);     
    GLfloat yA=R*sin(90*2*PI/360);  
  
    GLfloat xB=R*cos(306*2*PI/360);  
    GLfloat yB=R*sin(306*2*PI/360);  
  
    GLfloat xC=R*cos(162*2*PI/360);  
    GLfloat yC=R*sin(162*2*PI/360);  
  
    GLfloat xD=R*cos(18*2*PI/360);  
    GLfloat yD=R*sin(18*2*PI/360);  
  
    GLfloat xE=R*cos(234*2*PI/360);  
    GLfloat yE=R*sin(234*2*PI/360);  
  
    glVertex2f(xA,yA);  
    glVertex2f(xB,yB);  
    glVertex2f(xC,yC);  
    glVertex2f(xD,yD);  
    glVertex2f(xE,yE);  
    glEnd();  
    glFlush();  
 
}    
  
int main(int argc, char *argv[])      
{      
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);    
    glutInitWindowPosition(100, 100);      
    glutInitWindowSize(400, 400);      
    glutCreateWindow("simple");     
    glutDisplayFunc(myDisplay);   
    glutMainLoop();   // 让整个绘图循环进行,相当于死循环。
    return 0;      
}    


猜你喜欢

转载自blog.csdn.net/xigongdali/article/details/80564152
今日推荐