neHe OpenGL lession 3,neheopengllession
#include <GLUT/GLUT.h>#include <OpenGL/OpenGL.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>/* ASCII code for the escape key. */#define ESCAPE 27/* The number of our GLUT window */int window;/* A general OpenGL initializtion function. Sets all of teh initial parameters. *//* We call this right after our OpenGL window is created. */void initGL(int width, int height) {glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // this will Clear the background color // to blackglClearDepth(1.0);// Enables Clearting Of the depth bufferglDepthFunc(GL_LESS); // The type of depth test to doglEnable(GL_DEPTH_TEST); // Enable Depth Testing.glShadeModel(GL_SMOOTH); // Enable smooth color shadingglMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset the projection matrix// @see: http://hi.baidu.com/korndorben/item/76c5020453f18212cc34ea90gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); // Calcuate the aspect ratio of teh windowglMatrixMode(GL_MODELVIEW);}/* The function called when our window is resized (which shouldn't happend, because we're fullscreen) */void resizeGLScene(int width, int height) {if (height == 0) // Prevent a divide by zero if the window is too smallheight = 1;glViewport(0, 0, width, height); // Reset the current Viewport and perspective // transformationglMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);}/* The main drawing function */void drawGLScene() {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and// the dpeth bufferglLoadIdentity(); // Reset the viewglTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 units And into the screen 6.0// draw a triangle (in smooth colring mode)glBegin(GL_POLYGON); // start drawing a polygonglColor3f(1.0f, 0.0f, 0.0f);// set the color to redglVertex3f(0.0f, 1.0f, 0.0f); // topglColor3f(0.0f, 1.0f, 0.0f); // Set the Color to green.glVertex3f(1.0f, -1.0f, 0.0f); // bottom rightglColor3f(0.0f, 0.0f, 1.0f); // set the color to blue.glVertex3f(-1.0f, -1.0f, 0.0f); // bottom leftglEnd();// We're down with the polygon (smooth color interpolation);glTranslatef(3.0f, 0.0f, 0.0f);// draw a square (quadrilateral)glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade.glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f);// Top leftglVertex3f( 1.0f, 1.0f, 0.0f);// top rightglVertex3f( 1.0f, -1.0f, 0.0f);// bottom rightglVertex3f(-1.0f, -1.0f, 0.0f); // bottom leftglEnd();// Done with the polygon// we need ot swap the buffer to display our drawing.glutSwapBuffers();}/* The function called whenever a key is pressed. */void keyPressed(unsigned char key, int x, int y) {usleep(100); // sleep to avoid thrashing this procedure *//* If escape is pressd, kill everything. */if (key == ESCAPE) {/* shut down our window. */glutDestroyWindow(window);/* exit the program...normal termination. */exit(0);}}int main(int argc, char **argv) {glutInit(&argc, argv);/* Select type of Display mode:Double bufferRGBA colorAlpha components supportedDepth buffer */glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);/* GET A 640 X 360 window */glutInitWindowSize(640, 480);/* the window starts at the upper left corner of the screen. */glutInitWindowPosition(0, 0);/* Open a window */window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe 'oo");/* Register the function to do all our OpenGL drawing. */glutDisplayFunc(&drawGLScene);/* Go fullscree. Tis is as soon as possible. *///glutFullScreen();/* Even if there are no events, redraw our gl scene. */glutIdleFunc(&drawGLScene);/* Register the function called when our window is resized. */glutReshapeFunc(&resizeGLScene);/* Register the function called when the keyboard is pressed. */glutKeyboardFunc(&keyPressed);/* Initialize our window. */initGL(640, 480);/* Start event procesing engine */glutMainLoop();return 1;}
Mac OS X 運行:
$: clang -o lession lession3.c -Wno-deprecated -framework OpenGL -framework GLUT
$: ./lession
結果:一個三角形。一個正方形。
opengl 讀取、顯示 dem 的原始碼(高分獎勵、好的話再加分)
讀dem與三維顯示是兩碼事。
dem只含網格點上的數值。二進位檔案。資料按地理上的行排列,第1行完了是第2行,第2行完了是第3行,...直到最後一行。
每個數是 short int, 也就是 16 bits (2個bytes).
dem 的 short int 是 big endian. 如果 在 PC 上使用,你要交換2個bytes的順序,例如依次讀入的 byte
byte1 byte2 byte3 byte4 byte5 byte6 。。。
你要兩兩交換成 byte2 byte1 byte4 byte3 byte6 byte5
再構成 short int,short int ,short int 。。。。
到底這個瓦片的地理資料有幾行幾列等等,你要下載它的 Header File (.HDR)
裡面有
BYTEORDER,LAYOUT,
NROWS, NCOLS,
NBANDS,NBITS, BANDROWBYTES,
TOTALROWBYTES,
BANDGAPBYTES, NODATA,
ULXMAP, ULYMAP,
XDIM, YDIM,
換算成 經緯度或投影成 x,y, 你需要 (.HDR)
OPENGL 畫 3 維圖很簡單,你有了 每行每列的xyz,很好畫。
vs2008中怎使用Opengl
下載GLUT工具包,然後如下:
1、將下載的壓縮包解開,將得到5個檔案
2、在“我的電腦”中搜尋“gl.h”,並找到其所在檔案夾。把解壓得到的glut.h放到這個檔案夾。
3、把解壓得到的glut.lib和glut32.lib放到靜態函數庫所在檔案夾(其安裝目錄下面的“VC\lib”檔案夾)。
4、把解壓得到的glut.dll和glut32.dll放到作業系統目錄下面的system32檔案夾內。
我的目錄是D:\VC98\Lib\GLAUX.LIB ,你有嗎,如果你沒有GLAUX.LIB這個檔案那我發給你。是Lib檔案夾。那就可以運行啦