|
|
CgFX:
- texture tex0;
- sampler2D sampler0 = sampler_state
- {
- Texture = <tex0>;
- AddressU = Clamp;
- AddressV = Clamp;
- MinFilter = Linear;
- MagFilter = Linear;
- MipFilter = Linear;
- };
- float4 PS(float2 texCoord : TEXCOORD0,
- uniform sampler2D sampler0) : COLOR
- {
- return float4(tex2D(sampler0, texCoord).rgb, 1);
- }
- technique Tec0
- {
- pass p0
- {
- CullMode = None;
- PixelShader = compile arbfp1 PS(sampler0);
- }
- }
复制代码
OpenGL:
- #include <iostream>
- #include <ctime>
- #include <sstream>
- #include <gl/glut.h>
- #include <CgFX/ICgFXEffect.h>
- using namespace std;
- ICgFXEffect* effect;
- const int WIDTH = 512;
- const int HEIGHT = 512;
- GLubyte data[HEIGHT][WIDTH][4];
- GLuint texture[1];
- void Check(bool error)
- {
- if (error)
- {
- const char* errors = NULL;
- CgFXGetErrors(&errors);
- if (errors != NULL)
- {
- cout << errors << endl;
- }
- else
- {
- cout << "Unknown Error" << endl;
- }
- }
- }
- void init()
- {
- glClearColor(1, 1, 1, 1);
-
- srand(clock());
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- glGenTextures(1, &texture[0]);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- for (int y = 0; y < HEIGHT; ++ y)
- {
- for (int x = 0; x < WIDTH; ++ x)
- {
- data[y][x][0] = rand();
- data[y][x][1] = rand();
- data[y][x][2] = rand();
- data[y][x][3] = 255;
- }
- }
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0][0]);
- Check(FAILED(CgFXCreateEffectFromFileA("TestTexture.fx", NULL, &effect, NULL)));
- Check(FAILED(CgFXSetDevice("OpenGL", NULL)));
- Check(FAILED(effect->SetTechnique(effect->GetTechnique(0))));
- Check(FAILED(effect->ValidateTechnique(effect->GetCurrentTechnique())));
- }
- void display()
- {
- static int frame = 0;
- static clock_t lastTime = clock();
-
- clock_t curTime = clock();
- ++ frame;
- if (curTime - lastTime > 1000)
- {
- float FPS = 1000.0f * frame / (curTime - lastTime);
-
- lastTime = curTime;
-
- std::stringstream ss;
- ss << "Test Texture - " << FPS << std::ends;
- glutSetWindowTitle(ss.str().c_str());
-
- frame = 0;
- }
- Check(FAILED(effect->SetTexture(effect->GetParameterByName(NULL, "tex0"),
- static_cast<DWORD>(texture[0]))));
- glClear(GL_COLOR_BUFFER_BIT);
- UINT numPasses;
- Check(FAILED(effect->Begin(&numPasses, 0)));
- Check(FAILED(effect->Pass(0)));
- glColor4f(1, 1, 1, 1);
-
- glBegin(GL_QUADS);
- {
- glTexCoord2f(0, 0);
- glVertex2f(-WIDTH / 2, -HEIGHT / 2);
- glTexCoord2f(0, 1);
- glVertex2f(WIDTH / 2, -HEIGHT / 2);
- glTexCoord2f(1, 1);
- glVertex2f(WIDTH / 2, HEIGHT / 2);
- glTexCoord2f(1, 0);
- glVertex2f(-WIDTH / 2, HEIGHT / 2);
- }
- glEnd();
- Check(FAILED(effect->End()));
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void reshape(GLsizei w, GLsizei h)
- {
- glViewport(0, 0, w, h);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 10);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(WIDTH, HEIGHT);
- glutInitWindowPosition(0, 0);
- glutCreateWindow("Test Texture");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
-
- return 0;
- }
复制代码
但这样结果不对,窗口内全黑。似乎是纹理传不过去。请问什么问题? |
|