龙芯俱乐部开源技术社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1735|回复: 0

【OpenGL】【3D】一个OpenGL示例程序

[复制链接]

57

主题

83

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
10412
发表于 2017-1-19 20:38:47 | 显示全部楼层 |阅读模式
本帖最后由 lophyxp 于 2017-1-19 20:38 编辑

  • 先上源代码

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <stdbool.h>
    4. #include "GL/glew.h"
    5. #include "GL/glut.h"

    6. #ifdef WIN32
    7.        #define filename "d:\\tex03.bmp"
    8. #elif LINUX
    9.        #define filename "/home/lophyxp/tex03.bmp"
    10. #endif

    11. static GLint imagewidth;
    12. static GLint imageheight;
    13. static GLint pixellength;
    14. static GLubyte* pixeldata;


    15. void readBMP() {
    16.         //打开文件
    17.         FILE* pfile = fopen(filename, "rb");
    18.         if (pfile == 0) {
    19.                 printf("%s is not a BMP file.\n", filename);
    20.                 exit(0);
    21.         }

    22.         //读取图像大小
    23.         fseek(pfile, 0x0012, SEEK_SET);
    24.         fread(&imagewidth, sizeof(imagewidth), 1, pfile);
    25.         fread(&imageheight, sizeof(imageheight), 1, pfile);

    26.         //计算像素数据长度,处理对齐
    27.         pixellength = imagewidth * 3;
    28.         while (pixellength % 4 != 0)pixellength++;
    29.         pixellength *= imageheight;

    30.         //读取像素数据
    31.         pixeldata = (GLubyte*)malloc(pixellength);
    32.         if (pixeldata == 0) exit(0);

    33.         fseek(pfile, 54, SEEK_SET);
    34.         fread(pixeldata, pixellength, 1, pfile);

    35.         //关闭文件
    36.         fclose(pfile);
    37. }

    38. typedef struct materialStruct {
    39.         GLfloat ambient[4];
    40.         GLfloat diffuse[4];
    41.         GLfloat specular[4];
    42.         GLfloat shininess;
    43. }materialStruct;

    44. materialStruct brassMaterials = {
    45.         { 0.33, 0.22, 0.03, 1.0 },
    46.         { 0.78, 0.57, 0.11, 1.0 },
    47.         { 0.99, 0.91, 0.81, 1.0 },
    48.         27.8
    49. };

    50. materialStruct redPlasticMaterials = {
    51.         { 0.3, 0.0, 0.0, 1.0 },
    52.         { 0.6, 0.0, 0.0, 1.0 },
    53.         { 0.8, 0.6, 0.6, 1.0 },
    54.         32.0
    55. };

    56. materialStruct whiteShinyMaterials = {
    57.         { 1.0, 1.0, 1.0, 1.0 },
    58.         { 1.0, 1.0, 1.0, 1.0 },
    59.         { 1.0, 1.0, 1.0, 1.0 },
    60.         100.0
    61. };

    62. void materials(materialStruct *materials) {
    63.         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, materials->ambient);
    64.         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materials->diffuse);
    65.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materials->specular);
    66.         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials->shininess);
    67. }

    68. typedef struct lightingStruct {
    69.         GLfloat ambient[4];
    70.         GLfloat diffuse[4];
    71.         GLfloat specular[4];
    72. }lightingStruct;

    73. lightingStruct whiteLighting = {
    74.         { 0.0, 0.0, 0.0, 1.0 },
    75.         { 1.0, 1.0, 1.0, 1.0 },
    76.         { 1.0, 1.0, 1.0, 1.0 }
    77. };

    78. lightingStruct coloredLighting = {
    79.         { 0.2, 0.0, 0.0, 1.0 },
    80.         { 0.0, 1.0, 0.0, 1.0 },
    81.         { 0.0, 0.0, 1.0, 1.0 }
    82. };

    83. void lighting(lightingStruct *lighting) {
    84.         glLightfv(GL_LIGHT0, GL_AMBIENT, lighting->ambient);
    85.         glLightfv(GL_LIGHT0, GL_DIFFUSE, lighting->diffuse);
    86.         glLightfv(GL_LIGHT0, GL_SPECULAR, lighting->specular);
    87. }

    88. void init() {
    89.         glViewport(0, 0, 500, 500);
    90.         glMatrixMode(GL_PROJECTION);
    91.         glLoadIdentity();
    92.         glOrtho(-2.0, 2.0, -2.0,
    93.                 2.0, -10.0, 10.0);

    94.         // Texture_2d ================================
    95.         glEnable(GL_DEPTH_TEST);
    96.         glEnable(GL_TEXTURE_2D);
    97.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imagewidth, imageheight, 0, GL_BGR, GL_UNSIGNED_BYTE, pixeldata);
    98.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    99.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    100.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    101.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    102.         //glDisable(GL_TEXTURE_2D);

    103.         // Blend ======================================
    104.         glEnable(GL_BLEND);
    105.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    106.         //glBlendFunc(GL_SRC_ALPHA, GL_ONE);

    107.         // Lighting ================================
    108.         //GLfloat light_pos[] = { 1.0,2.0,3.0,1.0 };
    109.         glEnable(GL_LIGHTING);
    110.         glEnable(GL_LIGHT0);
    111.         /*glMatrixMode(GL_MODELVIEW);
    112.         glLoadIdentity();
    113.         glLightfv(GL_LIGHT0, GL_POSITION, light_pos);*/
    114.         lighting(&whiteLighting);
    115.         materials(&brassMaterials);

    116.         glShadeModel(GL_SMOOTH);

    117.         // Fog ===================================
    118.         GLuint filter = 2;
    119.         GLuint fogMode[] = { GL_EXP, GL_EXP2, GL_LINE };
    120.         GLuint fogfilter = 0;
    121.         GLfloat fogColor[4] = { 0.5f, 0.5f, 0.5f, 1.0f };

    122.         glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    123.         glFogi(GL_FOG_MODE, fogMode[fogfilter]);
    124.         glFogfv(GL_FOG_COLOR, fogColor);
    125.         glFogf(GL_FOG_DENSITY, 0.35f);
    126.         glHint(GL_FOG_HINT, GL_DONT_CARE);
    127.         glFogf(GL_FOG_START, 1.0f);
    128.         glFogf(GL_FOG_END, 5.0f);
    129.         glEnable(GL_FOG);
    130. }

    131. GLfloat vertics[][3]={
    132.         {-1.0, -1.0, 1.0},        {-1.0,1.0,1.0},                //0, 1
    133.         {1.0,1.0,1.0},                {1.0,-1.0,1.0},                //2, 3
    134.         {-1.0,-1.0,-1.0},        {-1.0,1.0,-1.0},        //4, 5
    135.         {1.0,1.0,-1.0},                {1.0,-1.0,-1.0}                //6, 7
    136. };
    137. GLfloat colors[][4] = {
    138.         {1.0,0.0,0.0,0.5},{0.0,1.0,1.0,0.5},
    139.         {1.0,1.0,0.0,0.5},{0.0,1.0,0.0,0.5},
    140.         {0.0,0.0,1.0,0.5},{1.0,0.0,1.0,0.5},
    141.         {0.0,0.0,0.0,0.5},{1.0,1.0,1.0,0.5}
    142. };
    143. GLfloat normals[][3] = {
    144.         {-1.0, 0.0, 0.0}, {1.0, 0.0, 0.0},
    145.         {0.0, -1.0, 0.0}, {0.0, 1.0, 0.0},
    146.         {0.0, 0.0, -1.0}, {0.0, 0.0, 1.0}
    147. };

    148. void polygon(int a, int b, int c, int d) {
    149.         glBegin(GL_POLYGON);
    150.                 glColor3fv(colors[a]);
    151.                 glTexCoord2f(0.0, 0.0);
    152.                 glVertex3fv(vertics[a]);
    153.                 glColor3fv(colors[b]);
    154.                 glTexCoord2f(0.0, 1.0);
    155.                 glVertex3fv(vertics[b]);
    156.                 glColor3fv(colors[c]);
    157.                 glTexCoord2f(1.0, 1.0);
    158.                 glVertex3fv(vertics[c]);
    159.                 glColor3fv(colors[d]);
    160.                 glTexCoord2f(1.0, 0.0);
    161.                 glVertex3fv(vertics[d]);
    162.         glEnd();
    163. }

    164. void cube() {
    165.         glNormal3fv(normals[5]);        //z
    166.         polygon(0, 3, 2, 1);
    167.         glNormal3fv(normals[1]);        //x
    168.         polygon(2, 3, 7, 6);
    169.         glNormal3fv(normals[2]);        //-y
    170.         polygon(3, 0, 4, 7);
    171.         glNormal3fv(normals[3]);        //y
    172.         polygon(1, 2, 6, 5);
    173.         glNormal3fv(normals[4]);        //-z
    174.         polygon(4, 5, 6, 7);
    175.         glNormal3fv(normals[0]);        //-x
    176.         polygon(5, 4, 0, 1);
    177. }

    178. GLUquadricObj* Quadric;

    179. void quadric() {
    180.         Quadric = gluNewQuadric();
    181.         gluQuadricDrawStyle(Quadric, GLU_FILL);
    182.         gluQuadricNormals(Quadric, GLU_SMOOTH);
    183.         gluQuadricTexture(Quadric, GL_TRUE);
    184.         gluSphere(Quadric, 1.5, 120, 120);
    185.         //gluCylinder(Quadric, 0.5, 0.5, 2.0, 120, 120);
    186.         //gluDisk(Quadric, 0.5, 1.0, 120, 120);
    187.         //gluPartialDisk(Quadric, 0.5, 1.0, 120, 120, 90.0, 180.0);

    188.         gluDeleteQuadric(Quadric);
    189. }

    190. static GLfloat theta[] = { 0.0, 0.0, 0.0 };
    191. static GLint axis = 1;        //0:x, 1:y, 2:z
    192. static GLfloat position0[] = { 10.0,20.0,30.0,1.0 };

    193. void display(void)
    194. {
    195.         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    196.         glMatrixMode(GL_MODELVIEW);
    197.         glLoadIdentity();
    198.         //gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    199.         glPushMatrix();
    200.         glRotatef(theta[0], 1.0, 0.0, 0.0);
    201.         glRotatef(theta[1], 0.0, 1.0, 0.0);
    202.         glRotatef(theta[2], 0.0, 0.0, 1.0);

    203.         glLightfv(GL_LIGHT0, GL_POSITION, position0);

    204.         
    205.         //cube();
    206.         //glutSolidTeapot(1.0);
    207.         glRotatef(-90.0, 1.0, 0.0, 0.0);  //quardic round axis-z to round axis-y
    208.         quadric();
    209.         glPopMatrix();

    210.         //glDrawPixels(imagewidth, imageheight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixeldata);

    211.         glFlush();
    212.         glutSwapBuffers();
    213. }

    214. void myMouse(int btn, int state, int x, int y) {
    215.         if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    216.                 axis = 0;
    217.         if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
    218.                 axis = 1;
    219.         if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
    220.                 axis = 2;
    221. }

    222. void myIdle() {
    223.         theta[axis] += 0.1;
    224.         if (theta[axis] > 360.0) theta[axis] -= 360.0;
    225.         
    226.         glutPostRedisplay();
    227. }

    228. void myKeyboard(unsigned char k, int x, int y) {
    229.         static bool gp = true;
    230.         switch (k) {
    231.         case '1':
    232.                 glutIdleFunc(NULL);
    233.                 break;
    234.         case '2':
    235.                 glutIdleFunc(&myIdle);
    236.                 break;
    237.         case '3':
    238.                 glEnable(GL_TEXTURE_2D);
    239.                 break;
    240.         case '4':
    241.                 glDisable(GL_TEXTURE_2D);
    242.                 break;
    243.         case 'w':
    244.                 materials(&redPlasticMaterials);
    245.                 break;
    246.         case 'e':
    247.                 materials(&brassMaterials);
    248.                 break;
    249.         case 'r':
    250.                 materials(&whiteShinyMaterials);
    251.                 break;
    252.         case 's':
    253.                 lighting(&whiteLighting);
    254.                 break;
    255.         case 'd':
    256.                 lighting(&coloredLighting);
    257.                 break;
    258.         case 'f':
    259.                 if (gp == true) {
    260.                         glDisable(GL_FOG);
    261.                         gp = false;
    262.                 }
    263.                 else {
    264.                         glEnable(GL_FOG);
    265.                         gp = true;
    266.                 }
    267.                 break;
    268.         case 'q':
    269.                 exit(0);
    270.                 break;
    271.         }
    272. }

    273. void myReshape(int w, int h) {
    274.         glViewport(0, 0, w, h);
    275.         glMatrixMode(GL_PROJECTION);
    276.         glLoadIdentity();
    277.         if(w<=h)
    278.                 glOrtho(-2.0, 2.0, -2.0*(GLfloat)h / (GLfloat)w,
    279.                         2.0*(GLfloat)h / (GLfloat)w, -10.0, 10.0);
    280.         else
    281.                 glOrtho(-2.0*(GLfloat)w / (GLfloat)h,
    282.                         2.0*(GLfloat)w / (GLfloat)h, -2.0, 2.0,  -10.0, 10.0);
    283.         glMatrixMode(GL_MODELVIEW);
    284. }

    285. int main(int argc, char* argv[]) {
    286.         readBMP();
    287.         glutInit(&argc, argv);
    288.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    289.         glutInitWindowPosition(10, 10);
    290.         glutInitWindowSize(500, 500);
    291.         glutCreateWindow(filename);
    292.         init();
    293.         glutDisplayFunc(&display);
    294.         glutReshapeFunc(&myReshape);
    295.         glutIdleFunc(&myIdle);
    296.         glutMouseFunc(&myMouse);
    297.         glutKeyboardFunc(&myKeyboard);
    298.         glutMainLoop();
    299.         //-------------------------------------
    300.         free(pixeldata);
    301.         return 0;
    302. }
    复制代码

  • 下载纹理文件

    Windows下,解压缩后得到tex.bmp文件,放到D盘根目录下;Linux下,解压缩以后放到/home/lophyxp/目录下。
  • Windows下编译
    下载oglEarth.rar五个文件,解压缩到
    我的文档\Visual Studio 2015\Projects目录下,打开Visual Studio 2015,打开项目,选择oglEarth\oglEarth.sln,点“本地Windows调试器”前面的绿色箭头。
  • Linux下编译
    下载,执行
    1. tar xzvf oglEarth.tar.gz
    2. make main
    3. ./main
    复制代码

  • 运行效果

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
=====================================
这是一条神奇的小尾巴~~~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|龙芯俱乐部开源技术社区

GMT+8, 2024-4-29 11:41 , Processed in 0.096331 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表