0001:
0002:
0003:
0004:
0005:
0006:
0007:
0008:
0009:
0010: #include <GL/glut.h>
0011: #include <Cg/cg.h>
0012: #include <Cg/cgGL.h>
0013: #include <assert.h>
0014: #include "CBitmap.h"
0015:
0016:
0017: unsigned int tex;
0018:
0019:
0020: static cgContext *CgContext = NULL;
0021: static cgProgramIter *FragmentProgramIter = NULL;
0022: static cgBindIter *MapBind = NULL;
0023:
0024:
0025:
0026:
0027: static void draw_polygone()
0028: {
0029: glBegin(GL_TRIANGLE_STRIP);
0030: glTexCoord2f(1.0, 0.0);
0031: glVertex3f(-0.4, +0.8, 0.0f);
0032:
0033: glTexCoord2f(0.0, 0.0);
0034: glVertex3f( 0.4, +0.8, 0.0f);
0035:
0036: glTexCoord2f(1.0, 1.0);
0037: glVertex3f(-0.4, -0.8, 0.0f);
0038:
0039: glTexCoord2f(0.0, 1.0);
0040: glVertex3f( 0.4, -0.8, 0.0f);
0041: glEnd();
0042: }
0043:
0044:
0045:
0046: void display(void)
0047: {
0048: cgError Ret;
0049:
0050: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
0051:
0052:
0053: glMatrixMode(GL_MODELVIEW);
0054: glLoadIdentity();
0055: gluLookAt( 0.0, 0.0, 1.0,
0056: 0.0, 0.0, 0.0,
0057: 0.0, 1.0, 0.0);
0058:
0059:
0060: cgGLActiveTexture(MapBind);
0061: glEnable(GL_TEXTURE_2D);
0062: glBindTexture(GL_TEXTURE_2D, tex);
0063:
0064: glPushMatrix();
0065: glTranslatef(-0.5, 0.0, 0.0);
0066: draw_polygone();
0067: glPopMatrix();
0068:
0069: Ret = cgGLBindProgram(FragmentProgramIter);
0070: assert(Ret == cgNoError);
0071: cgGLEnableProgramType(cgFragmentProfile);
0072:
0073: glPushMatrix();
0074: glTranslatef(+0.5, 0.0, 0.0);
0075: draw_polygone();
0076: glPopMatrix();
0077:
0078:
0079: cgGLActiveTexture(MapBind);
0080: glDisable(GL_TEXTURE_2D);
0081:
0082: cgGLDisableProgramType(cgFragmentProfile);
0083:
0084: glutSwapBuffers();
0085: }
0086:
0087:
0088:
0089: void resize(int w, int h)
0090: {
0091:
0092: glViewport(0, 0, w, h);
0093:
0094:
0095: glMatrixMode(GL_PROJECTION);
0096: glLoadIdentity();
0097: gluPerspective( 90.0,
0098: 1.0,
0099: 0.1, 10.0);
0100: }
0101:
0102:
0103:
0104: void idle()
0105: {
0106: glutPostRedisplay();
0107: }
0108:
0109:
0110:
0111: static void InitializeCg()
0112: {
0113: cgError Ret;
0114:
0115:
0116: CgContext = cgCreateContext();
0117: assert(CgContext != NULL);
0118:
0119:
0120: Ret = cgAddProgramFromFile(CgContext, "fp.cg", cgFragmentProfile, NULL);
0121: assert(Ret == cgNoError);
0122:
0123:
0124: FragmentProgramIter = cgProgramByName(CgContext, "main");
0125: assert(FragmentProgramIter != NULL);
0126:
0127: #if 1
0128:
0129: fprintf(stderr, "---- プログラム はじめ ----\n"
0130: "%s"
0131: "---- プログラム 終わり ----\n",
0132: cgGetProgramObjectCode(FragmentProgramIter));
0133: #endif
0134: if(FragmentProgramIter != NULL) {
0135: GLuint ProgId = 1;
0136:
0137: Ret = cgGLLoadProgram(FragmentProgramIter, ProgId);
0138: assert(Ret == cgNoError);
0139:
0140: MapBind = cgGetBindByName(FragmentProgramIter, "SrcMap");
0141: assert(MapBind != NULL);
0142: }
0143:
0144: }
0145:
0146:
0147:
0148: int main(int argc, char *argv[])
0149: {
0150:
0151: glutInitWindowPosition(100, 100);
0152: glutInitWindowSize(640, 320);
0153: glutInit(&argc, argv);
0154: glutInitDisplayMode(GLUT_DOUBLE
0155: | GLUT_RGBA
0156: | GLUT_DEPTH);
0157: glutCreateWindow("Median filter");
0158:
0159:
0160: glutDisplayFunc(display);
0161: glutReshapeFunc(resize);
0162: glutIdleFunc(idle);
0163:
0164:
0165: InitializeCg();
0166:
0167:
0168: tex = CBitmap::Load("test.bmp");
0169: glBindTexture(GL_TEXTURE_2D, tex);
0170: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
0171: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
0172: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
0173: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
0174: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
0175:
0176:
0177: glClearColor(0.0, 0.0, 0.3, 0.0);
0178: glEnable(GL_DEPTH_TEST);
0179:
0180: glutMainLoop();
0181:
0182: if( MapBind )cgFreeBindIter( MapBind );
0183: cgFreeProgramIter( FragmentProgramIter );
0184: cgFreeContext( CgContext );
0185: cgCleanup( );
0186:
0187: return 0;
0188: }