?? texture.c
字號:
/* texture.c Nate Robins, 1997 Tool for teaching about OpenGL texture. */#include <math.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <GL/glut.h>#include "glm.h"
#pragma comment( linker, "/entry:\"mainCRTStartup\"" ) // set the entry point to be main()
typedef struct _cell { int id; int x, y; float min, max; float value; float step; char* info; char* format;} cell;cell texcoords[2*4] = { { 1, 140, 370, -2.0, 2.0, 0.0, 0.01, "Specifies S value of 1st texture coordinate", "%.1f" }, { 2, 190, 370, -2.0, 2.0, 0.0, 0.01, "Specifies T value of 1st texture coordinate", "%.1f" }, { 3, 140, 400, -2.0, 2.0, 1.0, 0.01, "Specifies S value of 2nd texture coordinate", "%.1f" }, { 4, 190, 400, -2.0, 2.0, 0.0, 0.01, "Specifies T value of 2nd texture coordinate", "%.1f" }, { 5, 140, 430, -2.0, 2.0, 1.0, 0.01, "Specifies S value of 3rd texture coordinate", "%.1f" }, { 6, 190, 430, -2.0, 2.0, 1.0, 0.01, "Specifies T value of 3rd texture coordinate", "%.1f" }, { 7, 140, 460, -2.0, 2.0, 0.0, 0.01, "Specifies S value of 4th texture coordinate", "%.1f" }, { 8, 190, 460, -2.0, 2.0, 1.0, 0.01, "Specifies T value of 4th texture coordinate", "%.1f" },};cell vertices[3*4] = { { 9, 350, 370, -2.0, 2.0, -1.0, 0.01, "Specifies X coordinate of 1st vertex", "%.1f" }, { 10, 400, 370, -2.0, 2.0, -1.0, 0.01, "Specifies Y coordinate of 1st vertex", "%.1f" }, { 11, 450, 370, -2.0, 2.0, 0.0, 0.01, "Specifies Z coordinate of 1st vertex", "%.1f" }, { 12, 350, 400, -2.0, 2.0, 1.0, 0.01, "Specifies X coordinate of 2nd vertex", "%.1f" }, { 13, 400, 400, -2.0, 2.0, -1.0, 0.01, "Specifies Y coordinate of 2nd vertex", "%.1f" }, { 14, 450, 400, -2.0, 2.0, 0.0, 0.01, "Specifies Z coordinate of 2nd vertex", "%.1f" }, { 15, 350, 430, -2.0, 2.0, 1.0, 0.01, "Specifies X coordinate of 3rd vertex", "%.1f" }, { 16, 400, 430, -2.0, 2.0, 1.0, 0.01, "Specifies Y coordinate of 3rd vertex", "%.1f" }, { 17, 450, 430, -2.0, 2.0, 0.0, 0.01, "Specifies Z coordinate of 3rd vertex", "%.1f" }, { 18, 350, 460, -2.0, 2.0, -1.0, 0.01, "Specifies X coordinate of 4th vertex", "%.1f" }, { 19, 400, 460, -2.0, 2.0, 1.0, 0.01, "Specifies Y coordinate of 4th vertex", "%.1f" }, { 20, 450, 460, -2.0, 2.0, 0.0, 0.01, "Specifies Z coordinate of 4th vertex", "%.1f" },};GLenum minfilter = GL_NEAREST;GLenum magfilter = GL_NEAREST;GLenum env = GL_MODULATE;GLenum wraps = GL_REPEAT;GLenum wrapt = GL_REPEAT;cell translation[3] = { { 21, 120, 90, -5.0, 5.0, 0.0, 0.01, "Specifies X coordinate of translation vector.", "%.2f" }, { 22, 180, 90, -5.0, 5.0, 0.0, 0.01, "Specifies Y coordinate of translation vector.", "%.2f" }, { 23, 240, 90, -5.0, 5.0, 0.0, 0.01, "Specifies Z coordinate of translation vector.", "%.2f" },};cell rotation[4] = { { 24, 120, 120, -360.0, 360.0, 0.0, 1.0, "Specifies angle of rotation, in degrees.", "%.1f" }, { 25, 180, 120, -1.0, 1.0, 0.0, 0.01, "Specifies X coordinate of vector to rotate about.", "%.2f" }, { 26, 240, 120, -1.0, 1.0, 0.0, 0.01, "Specifies Y coordinate of vector to rotate about.", "%.2f" }, { 27, 300, 120, -1.0, 1.0, 1.0, 0.01, "Specifies Z coordinate of vector to rotate about.", "%.2f" },};cell scale[3] = { { 28, 120, 150, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along X axis.", "%.2f" }, { 29, 180, 150, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along Y axis.", "%.2f" }, { 30, 240, 150, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along Z axis.", "%.2f" },};cell pcolor[4] = { { 31, 120, 310, 0.0, 1.0, 0.6, 0.01, "Specifies red component of polygon color.", "%.2f" }, { 32, 180, 310, 0.0, 1.0, 0.6, 0.01, "Specifies green component of polygon color.", "%.2f" }, { 33, 240, 310, 0.0, 1.0, 0.6, 0.01, "Specifies blue component of polygon color.", "%.2f" }, { 34, 300, 310, 0.0, 1.0, 1.0, 0.01, "Specifies alpha component of polygon color.", "%.2f" },};cell bcolor[4] = { { 39, 240, 30, 0.0, 1.0, 1.0, 0.01, "Specifies red component of texture border color.", "%.2f" }, { 40, 300, 30, 0.0, 1.0, 0.0, 0.01, "Specifies green component of texture border color.", "%.2f" }, { 41, 360, 30, 0.0, 1.0, 0.0, 0.01, "Specifies blue component of texture border color.", "%.2f" }, { 42, 420, 30, 0.0, 1.0, 1.0, 0.01, "Specifies alpha component of texture border color.", "%.2f" },};cell ecolor[4] = { { 35, 240, 60, 0.0, 1.0, 0.0, 0.01, "Specifies red component of texture environment color.", "%.2f" }, { 36, 300, 60, 0.0, 1.0, 1.0, 0.01, "Specifies green component of texture environment color.", "%.2f" }, { 37, 360, 60, 0.0, 1.0, 0.0, 0.01, "Specifies blue component of texture environment color.", "%.2f" }, { 38, 420, 60, 0.0, 1.0, 1.0, 0.01, "Specifies alpha component of texture environment color.", "%.2f" },};GLfloat eye[3] = { 0.0, 0.0, 3.0 };GLfloat at[3] = { 0.0, 0.0, 0.0 };GLfloat up[3] = { 0.0, 1.0, 0.0 };void redisplay_all(void);GLuint window, world, screen, command;GLuint sub_width = 256, sub_height = 256;GLint selection = 0;GLfloat spin_x = 0.0;GLfloat spin_y = 0.0;int iheight, iwidth;unsigned char* image = NULL;int twidth, theight;int stipple;#if defined(GL_VERSION_1_1)#define GL_REPLACE_EXT GL_REPLACE#endifGLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_10;voidsetfont(char* name, int size){ font_style = GLUT_BITMAP_HELVETICA_10; if (strcmp(name, "helvetica") == 0) { if (size == 12) font_style = GLUT_BITMAP_HELVETICA_12; else if (size == 18) font_style = GLUT_BITMAP_HELVETICA_18; } else if (strcmp(name, "times roman") == 0) { font_style = GLUT_BITMAP_TIMES_ROMAN_10; if (size == 24) font_style = GLUT_BITMAP_TIMES_ROMAN_24; } else if (strcmp(name, "8x13") == 0) { font_style = GLUT_BITMAP_8_BY_13; } else if (strcmp(name, "9x15") == 0) { font_style = GLUT_BITMAP_9_BY_15; }}void drawstr(GLuint x, GLuint y, char* format, ...){ va_list args; char buffer[255], *s; va_start(args, format); vsprintf(buffer, format, args); va_end(args); glRasterPos2i(x, y); for (s = buffer; *s; s++) glutBitmapCharacter(font_style, *s);}voidcell_draw(cell* cell){ glColor3ub(0, 255, 128); if (selection == cell->id) { glColor3ub(255, 255, 0); drawstr(10, 525, cell->info); glColor3ub(255, 0, 0); } drawstr(cell->x, cell->y, cell->format, cell->value);}intcell_hit(cell* cell, int x, int y){ if (x > cell->x && x < cell->x+50 && y > cell->y-20 && y < cell->y+10) return cell->id; return 0;}voidcell_update(cell* cell, int update){ if (selection != cell->id) return; cell->value += update * cell->step; if (cell->value < cell->min) cell->value = cell->min; else if (cell->value > cell->max) cell->value = cell->max; }voidcell_vector(float* dst, cell* cell, int num){ while (--num >= 0) dst[num] = cell[num].value;}voiddrawaxes(void){ glColor3ub(255, 0, 0); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, -0.25, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.75, 0.0, 0.25); glVertex3f(0.75, 0.0, -0.25); glVertex3f(1.0, 0.0, 0.0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.75, 0.25); glVertex3f(0.0, 0.75, -0.25); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.25, 0.75, 0.0); glVertex3f(-0.25, 0.75, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glVertex3f(0.25, 0.0, 0.75); glVertex3f(-0.25, 0.0, 0.75); glVertex3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.25, 0.75); glVertex3f(0.0, -0.25, 0.75); glVertex3f(0.0, 0.0, 1.0); glEnd(); glColor3ub(255, 255, 0); glRasterPos3f(1.1, 0.0, 0.0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'x'); glRasterPos3f(0.0, 1.1, 0.0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'y'); glRasterPos3f(0.0, 0.0, 1.1); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'z');}voidtexenv(void){ GLfloat env_color[4], border_color[4]; cell_vector(env_color, ecolor, 4); cell_vector(border_color, bcolor, 4); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, env_color); glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);}voidtexture(void){ texenv(); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, iwidth, iheight, GL_RGB, GL_UNSIGNED_BYTE, image);}voidmain_reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, width, height, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); #define GAP 25 /* gap between subwindows */ sub_width = (width-GAP*3)/3; sub_height = (height-GAP*3)/2; glutSetWindow(screen); glutPositionWindow(GAP, GAP); glutReshapeWindow(sub_width, sub_height); glutSetWindow(world); glutPositionWindow(GAP, GAP+sub_height+GAP); glutReshapeWindow(sub_width, sub_height); glutSetWindow(command); glutPositionWindow(GAP+sub_width+GAP, GAP); glutReshapeWindow(sub_width*2, sub_height*2+GAP);}voidmain_display(void){ glClearColor(0.8, 0.8, 0.8, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3ub(0, 0, 0); setfont("helvetica", 12); drawstr(GAP, GAP-5, "Screen-space view"); drawstr(GAP+sub_width+GAP, GAP-5, "Command manipulation window"); drawstr(GAP, GAP+sub_height+GAP-5, "Texture-space view"); glutSwapBuffers();}voidworld_reshape(int width, int height){ twidth = width; theight = height; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width, 0, height, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0.0, 0.0, 0.0, 1.0); glDisable(GL_DEPTH_TEST); glEnable(GL_LIGHT0);}voidworld_display(void){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, twidth, 0, theight, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); glColor3ub(255, 255, 255); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glBegin(GL_POLYGON); glTexCoord2f(-0.5, -0.5); glVertex2i(0, 0); glTexCoord2f(1.5, -0.5); glVertex2i(twidth, 0); glTexCoord2f(1.5, 1.5);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -