zoukankan      html  css  js  c++  java
  • window下Opengl与vs2012环境配置

    一、opengl与C++环境配置

    1. 下载opengl包。

    2. 将压缩包解压后,

      (1)将.dll文件(GLU.DLL, GLUT.DLL, GLUT32.DLL)放到C:\Windows\System32目录下;

      (2)将.h文件(GL.H, GLAUX.H, GLU.H, GLUT.H)放到\\Microsoft Visual Studio 11.0\VC\include\gl目录下,如果gl文件夹没有就新建;

      (3)将.lib文件(glut.lib, glut32.lib, glaux.lib)放到\\Microsoft Visual Studio 11.0\VC\lib目录下;

    3. 一般情况下,至此vs与opengl的环境搭建完毕,如果出现其他的问题,可能是链接库没有设置好!

    4. 示例代码

     1 #include<GL/glut.h>
     2 // 绘制立方体
     3 // 将立方体的八个顶点保存到一个数组里面 
     4 static const float vertex_list[][3] = 
     5 { 
     6     -0.5f, -0.5f, -0.5f, 
     7     0.5f, -0.5f, -0.5f, 
     8     -0.5f, 0.5f, -0.5f, 
     9     0.5f, 0.5f, -0.5f, 
    10     -0.5f, -0.5f, 0.5f, 
    11     0.5f, -0.5f, 0.5f, 
    12     -0.5f, 0.5f, 0.5f, 
    13     0.5f, 0.5f, 0.5f, 
    14 }; 
    15 // 将要使用的顶点的序号保存到一个数组里面 
    16 static const GLint index_list[][2] = 
    17 { 
    18     {0, 1}, {2, 3}, {4, 5}, {6, 7},    
    19     {0, 2}, {1, 3}, {4, 6}, {5, 7},
    20     {0, 4}, {1, 5}, {7, 3}, {2, 6}
    21 }; 
    22 // 绘制立方体
    23 void DrawCube(void)
    24 {
    25     int i,j;
    26     glBegin(GL_LINES); 
    27     for(i=0; i<12; ++i) // 12 条线段
    28     {
    29         for(j=0; j<2; ++j) // 每条线段 2个顶点
    30         {
    31             glVertex3fv(vertex_list[index_list[i][j]]);     
    32         }
    33     }
    34     glEnd();
    35 }
    36 
    37 static float rotate = 0;
    38 static int times = 0;
    39 void renderScene(void) 
    40 {
    41     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    42     glMatrixMode(GL_MODELVIEW); 
    43     glLoadIdentity();
    44     glPushMatrix();
    45     //glTranslatef(-0.2, 0, 0); // 平移
    46     //glScalef(2, 1, 1);    // 缩放
    47     times++;
    48     if(times > 100)
    49         times = 0;
    50     if(times % 100 == 0)
    51         rotate += 0.3;
    52     glRotatef(rotate, 0, 1, 0);
    53     glRotatef(rotate, 1, 0, 0);
    54     glColor3f(1, 0, 0);
    55     DrawCube();
    56     glPopMatrix();
    57     glutSwapBuffers();
    58 }
    59 
    60 void main(int argc, char **argv) 
    61 {
    62     glutInit(&argc, argv);
    63     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    64     glutInitWindowPosition(100,100);
    65     glutInitWindowSize(500, 500);
    66     glutCreateWindow("GLDemo");
    67     glutDisplayFunc(renderScene);
    68     glutIdleFunc(renderScene);
    69     glutMainLoop();
    70 }
    View Code

    二、opengl与C#环境配置

    opengl与C#的环境配置,最流行的是tao库!以tao库配置为例!

    1. 首先下载tao库。 http://www.taoframework.com,并安装在自己熟悉的目录下!方便寻找dll文件和相应的附录学习资源

    2. 新建C#项目,并在引用栏导入tao库.dll。一般情况下最常使用的库有Tao.OpenGl、Tao.FreeGlut和Tao.Platform.Windows;

    并在C#文件开头加入代码:

    using Tao.OpenGl;
    using Tao.FreeGlut;
    using Tao.Platform.Windows;

    3. 示例代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using Tao.OpenGl;
     7 using Tao.FreeGlut;
     8 namespace Okokok
     9 {
    10     class Program
    11     {
    12         static public void Display()
    13         {
    14             
    15             Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
    16             Gl.glColor3d(1.0, 1, 1);
    17             
    18             Gl.glTranslated(-10,0,0);
    19             Gl.glLoadIdentity();
    20             Glu.gluLookAt(0.0,0.0,5.0,  0.0,0.0,0.0,  0.0,1.0,0.0);
    21             //glut库中提供的画图函数
    22             Glut.glutWireTeapot(1.5);
    23 //            Glut.glutWireIcosahedron();
    24 //            Glut.glutWireCube(1.7);
    25 //            Glut.glutWireTetrahedron();
    26 //            Glut.glutWireTorus(0.2, 1, 100, 100);
    27             // Glut.glutWireCone(2, 1, 100, 100);
    28 //            Glut.glutWireSphere(2, 100, 100);
    29             Glut.glutSwapBuffers();
    30 
    31         }
    32         static public void Reshape(int w, int h)
    33         {
    34             Gl.glViewport(0, 0, w, h);
    35             Gl.glMatrixMode(Gl.GL_PROJECTION);
    36             Gl.glLoadIdentity();
    37             Glu.gluPerspective(60, (double)w/(double)h, 1, 20);
    38             Gl.glMatrixMode(Gl.GL_MODELVIEW);
    39             Gl.glLoadIdentity();
    40             Glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    41 
    42             Gl.glRotated(10,1, 1, 1);
    43         }
    44         static public void Shape()
    45         {
    46             Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
    47             Glut.glutWireTeapot(1.8);
    48             Glut.glutWireCube(1.7);
    49             Glut.glutWireIcosahedron();
    50             Gl.glFlush();
    51 
    52         }
    53         static void Main(string[] args)
    54         {
    55             Glut.glutInit();
    56             Glut.glutInitDisplayMode(Glut.GLUT_RGBA | Glut.GLUT_SINGLE);
    57             Glut.glutInitWindowPosition(100, 100);
    58             Glut.glutInitWindowSize(600, 600);
    59             Glut.glutCreateWindow("TTTTT");
    60             Glut.glutDisplayFunc(Display);
    61             Glut.glutReshapeFunc(Reshape);
    62             Glut.glutMainLoop();
    63         }
    64     }
    65 }
    View Code

    4. 问题调试:如果运行后发现问题,确少FreeGlut.dll文件,就需要重新将这个文件添加到系统目录C:\Windows\System32下。

    总结,如果还有其他问题,百度之!

  • 相关阅读:
    SCOI2020游记
    关于我
    WC2020游记
    CSP-S 2019 游记
    回文自动机学习笔记
    全自动数字论证机(迫真)
    树状数组上二分
    《伊豆的舞女》 读书小记
    雅礼集训2019 Day5
    雅礼集训2019 Day4
  • 原文地址:https://www.cnblogs.com/z1987/p/4516003.html
Copyright © 2011-2022 走看看