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: glOrtho(-1,1,-1,1,0.1,10);
0098:
0099:
0100:
0101: }
0102:
0103:
0104:
0105: void idle()
0106: {
0107: glutPostRedisplay();
0108: }
0109:
0110:
0111:
0112: static void InitializeCg()
0113: {
0114: cgError Ret;
0115:
0116:
0117: CgContext = cgCreateContext();
0118: assert(CgContext != NULL);
0119:
0120:
0121: Ret = cgAddProgramFromFile(CgContext, "fp.cg", cgFragmentProfile, NULL);
0122: assert(Ret == cgNoError);
0123:
0124:
0125: FragmentProgramIter = cgProgramByName(CgContext, "main");
0126: assert(FragmentProgramIter != NULL);
0127:
0128: #if 1
0129:
0130: fprintf(stderr, "---- プログラム はじめ ----\n"
0131: "%s"
0132: "---- プログラム 終わり ----\n",
0133: cgGetProgramObjectCode(FragmentProgramIter));
0134: #endif
0135: if(FragmentProgramIter != NULL) {
0136: GLuint ProgId = 1;
0137:
0138: Ret = cgGLLoadProgram(FragmentProgramIter, ProgId);
0139: assert(Ret == cgNoError);
0140:
0141: MapBind = cgGetBindByName(FragmentProgramIter, "SrcMap");
0142: assert(MapBind != NULL);
0143: }
0144:
0145: }
0146:
0147:
0148:
0149: int main(int argc, char *argv[])
0150: {
0151:
0152: glutInitWindowPosition(100, 100);
0153: glutInitWindowSize(640, 320);
0154: glutInit(&argc, argv);
0155: glutInitDisplayMode(GLUT_DOUBLE
0156: | GLUT_RGBA
0157: | GLUT_DEPTH);
0158: glutCreateWindow("Iris filter");
0159:
0160:
0161: glutDisplayFunc(display);
0162: glutReshapeFunc(resize);
0163: glutIdleFunc(idle);
0164:
0165:
0166: InitializeCg();
0167:
0168:
0169: tex = CBitmap::Load("hinode.bmp");
0170: glBindTexture(GL_TEXTURE_2D, tex);
0171: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
0172: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
0173: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
0174: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
0175: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
0176:
0177:
0178: glClearColor(0.0, 0.0, 0.3, 0.0);
0179: glEnable(GL_DEPTH_TEST);
0180:
0181: glutMainLoop();
0182:
0183: if( MapBind )cgFreeBindIter( MapBind );
0184: cgFreeProgramIter( FragmentProgramIter );
0185: cgFreeContext( CgContext );
0186: cgCleanup( );
0187:
0188: return 0;
0189: }