• opengl 正方体+模拟视角旋转


      1 // first_3D.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include "stdafx.h"
      5 #include <GL/glut.h>
      6 #include <math.h>
      7 
      8 // 将立方体的八个顶点保存到一个数组里面
      9 static const GLfloat vertex_list[8][3] = {
     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     -0.5f, -0.5f,  0.5f,
     15      0.5f, -0.5f,  0.5f,
     16     -0.5f,  0.5f,  0.5f,
     17      0.5f,  0.5f,  0.5f,
     18 };
     19 //变换后数组
     20 GLfloat vertex_list_new[8][3] = {
     21     -0.5f, -0.5f, -0.5f,
     22      0.5f, -0.5f, -0.5f,
     23     -0.5f,  0.5f, -0.5f,
     24      0.5f,  0.5f, -0.5f,
     25     -0.5f, -0.5f,  0.5f,
     26      0.5f, -0.5f,  0.5f,
     27     -0.5f,  0.5f,  0.5f,
     28      0.5f,  0.5f,  0.5f,
     29 };
     30 
     31 void work(float T[4][4])
     32 {
     33     GLfloat list_temp[8][3];
     34     for(int i=0;i<8;i++)
     35         for(int j=0;j<3;j++)
     36             list_temp[i][j] = vertex_list_new[i][j];
     37     for(int i=0;i<8;i++)
     38     {
     39         for(int j=0;j<4;j++)
     40         {
     41             float sum = 0;
     42             for(int k=0;k<4;k++)
     43             {
     44                 sum+=list_temp[i][k]*T[k][j];
     45             }
     46             vertex_list_new[i][j] = sum;
     47         }
     48     }
     49     return;
     50 }
     51 // 将要使用的顶点的序号保存到一个数组里面
     52 static const GLint index_list[][4] = {
     53     0, 2, 3, 1,
     54     0, 4, 6, 2,
     55     0, 1, 5, 4,
     56     4, 5, 7, 6,
     57     1, 3, 7, 5,
     58     2, 6, 7, 3,
     59 };
     60 
     61 void display()
     62 {
     63     glClear(GL_COLOR_BUFFER_BIT);
     64     glMatrixMode(GL_MODELVIEW); 
     65     glLoadIdentity(); 
     66     //gluLookAt(1.5,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//视点
     67 
     68     /* /坐标轴
     69     glLineWidth(1);
     70     glColor3f( 0.0, 0.0, 0.0);     // 黑色
     71     glBegin(GL_LINES);
     72     glVertex3f(-0xFFFFFFF,0,0);
     73     glVertex3f(0xFFFFFFF,0,0);
     74     glVertex3f(0,-0xFFFFFFF,0);
     75     glVertex3f(0,0xFFFFFFF,0);
     76     glVertex3f(0,0,-0xFFFFFFF);
     77     glVertex3f(0,0,0xFFFFFFF);
     78     glEnd();
     79     //坐标轴绘制结束*/
     80 
     81     /* /绘制原立方体
     82     glColor3f( 1.0, 1.0, 0.0);
     83     //glBegin(GL_QUADS);
     84     for(int i=0; i<6; ++i)         // 有六个面,循环六次
     85     {
     86         glBegin( GL_LINE_LOOP);
     87         for(int j=0; j<4; ++j)     // 每个面有四个顶点,循环四次
     88         {
     89             glVertex3fv(vertex_list[index_list[i][j]]);
     90         }
     91         glEnd();
     92     }
     93     //立方体绘制结束*/
     94     
     95     //绘制新立方体
     96     glColor3f( 1.0, 0.0, 0.0);
     97     //glBegin(GL_QUADS);
     98     for(int i=0; i<6; ++i)         // 有六个面,循环六次
     99     {
    100         glBegin( GL_LINE_LOOP);
    101         for(int j=0; j<4; ++j)     // 每个面有四个顶点,循环四次
    102         {
    103             glVertex3fv(vertex_list_new[index_list[i][j]]);
    104         }
    105         glEnd();
    106     }
    107     //立方体绘制结束
    108     glFlush();
    109     glutSwapBuffers();
    110 }
    111 
    112 void reshape(int w,int h) 
    113 { 
    114     glViewport(0,0,w,h); 
    115     glMatrixMode(GL_PROJECTION); 
    116     glLoadIdentity(); 
    117     glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0); 
    118 } 
    119 
    120 void init()
    121 {
    122     glClearColor( 255, 255, 255, 0.0);
    123     glMatrixMode(GL_PROJECTION);
    124     glLoadIdentity();
    125     //glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
    126     glMatrixMode(GL_MODELVIEW);
    127 }
    128 
    129 int main(int argc,char** argv)
    130 {
    131     //计算部分
    132     float a,b,c;//Ov
    133     a = 0;
    134     b = 0;
    135     c = 10;
    136     float R = sqrt(a*a+b*b+c*c);
    137     float d = 1.0;
    138     float costh = c/sqrt(a*a+c*c);
    139     float sinth = a/sqrt(a*a+c*c);
    140     float cosfy = b/R;
    141     float sinfy = sqrt(a*a+c*c)/R;
    142 
    143     
    144     float T1[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,0},{-a,-b,-c,1}};                    //原点到视点平移变换矩阵
    145     float T2[4][4] = {{-costh,0,-sinth,0},{0,1,0,0},{sinth,0,-costh,0},{0,0,0,1}};    //绕y1轴旋转变换
    146     float T3[4][4] = {{1,0,0,0},{0,sinfy,-cosfy,0},{0,cosfy,sinfy,0},{0,0,0,1}};      //绕x2轴旋转变换
    147     float T4[4][4] = {{-1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};                      //关于y3Ovz3面的反射变换
    148     //work(T1);work(T2);work(T3);work(T4);
    149     
    150     //世界--观察坐标系 合成矩阵
    151     float Tv[4][4] = {{cosfy,-cosfy*sinth,-sinfy*sinth,0},{0,sinfy,-cosfy,0},{-sinth,-cosfy*costh,-sinfy*costh,0},{0,0,R,1}};
    152     //work(Tv);
    153 
    154     
    155     float Tpersp[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,1,1/d},{0,0,0,0}}; //透视矩阵
    156     float Tproj[4][4] = {{-1,0,0,0},{0,1,0,0},{0,0,0,0},{0,0,0,1}};   //投影矩阵
    157     //work(Tpersp);work(Tproj);
    158     float Ts[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,0,1/d},{0,0,0,0}};     //合成
    159     //work(Ts);
    160 
    161     //总合成
    162     float T[4][4] = {{costh,-costh*sinth,0,-sinfy*sinth/d},{0,sinfy,0,-cosfy/d},{-sinth,-cosfy*costh,0,-sinfy*costh/d},{0,0,0,R/d}};
    163     work(T);
    164 
    165     float T_1[4][4] = {{1,0,0,0},{0,1,0,0},{0,0,0,-1/d},{0,0,0,R/d}};
    166     //work(T_1);
    167 
    168 
    169     glutInit(&argc,argv);
    170     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    171     glutInitWindowSize(500,500);
    172     glutInitWindowPosition(100,100);
    173     glutCreateWindow("立方体");
    174     glutReshapeFunc(reshape); 
    175     glutDisplayFunc(display);
    176     init();
    177     glutMainLoop();
    178     return 0;
    179 }
  • 相关阅读:
    053(五十六)
    【leetcode❤python】 Maximum Depth of Binary Tree
    【leetcode❤python】Find the Difference
    【leetcode❤python】Binary Watch
    【leetcode❤python】Convert a Number to Hexadecimal
    【leetcode❤python】83. Remove Duplicates from Sorted List
    【leetcode❤python】66. Plus One
    【leetcode❤python】70. Climbing Stairs
    【leetcode❤python】409. Longest Palindrome
    【leetcode❤python】387. First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/suthui/p/3604954.html
Copyright © 2020-2023  润新知