游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1523|回复: 3

纹理传不过去?

[复制链接]

2

主题

8

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2004-6-27 14:42:00 | 显示全部楼层 |阅读模式
CgFX:

  1. texture tex0;
  2. sampler2D sampler0 = sampler_state
  3. {
  4.         Texture = <tex0>;
  5.         AddressU = Clamp;
  6.         AddressV = Clamp;
  7.         MinFilter = Linear;
  8.         MagFilter = Linear;
  9.         MipFilter = Linear;
  10. };

  11. float4 PS(float2 texCoord : TEXCOORD0,
  12.              uniform sampler2D sampler0) : COLOR
  13. {
  14.     return float4(tex2D(sampler0, texCoord).rgb, 1);
  15. }

  16. technique Tec0
  17. {
  18.         pass p0
  19.         {
  20.                 CullMode = None;
  21.                 PixelShader = compile arbfp1 PS(sampler0);
  22.         }
  23. }
复制代码


OpenGL:

  1. #include <iostream>
  2. #include <ctime>
  3. #include <sstream>

  4. #include <gl/glut.h>
  5. #include <CgFX/ICgFXEffect.h>

  6. using namespace std;

  7. ICgFXEffect* effect;

  8. const int WIDTH = 512;
  9. const int HEIGHT = 512;

  10. GLubyte data[HEIGHT][WIDTH][4];
  11. GLuint texture[1];

  12. void Check(bool error)
  13. {
  14.         if (error)
  15.         {
  16.                 const char* errors = NULL;
  17.                 CgFXGetErrors(&errors);
  18.                 if (errors != NULL)
  19.                 {
  20.                         cout << errors << endl;
  21.                 }
  22.                 else
  23.                 {
  24.                         cout << "Unknown Error" << endl;
  25.                 }
  26.         }
  27. }

  28. void init()
  29. {
  30.         glClearColor(1, 1, 1, 1);
  31.        
  32.         srand(clock());

  33.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  34.        
  35.         glGenTextures(1, &texture[0]);
  36.         glBindTexture(GL_TEXTURE_2D, texture[0]);

  37.         for (int y = 0; y < HEIGHT; ++ y)
  38.         {
  39.                 for (int x = 0; x < WIDTH; ++ x)
  40.                 {
  41.                         data[y][x][0] = rand();
  42.                         data[y][x][1] = rand();
  43.                         data[y][x][2] = rand();
  44.                         data[y][x][3] = 255;
  45.                 }
  46.         }

  47.         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0][0]);
  48.         Check(FAILED(CgFXCreateEffectFromFileA("TestTexture.fx", NULL, &effect, NULL)));
  49.     Check(FAILED(CgFXSetDevice("OpenGL", NULL)));

  50.         Check(FAILED(effect->SetTechnique(effect->GetTechnique(0))));

  51.         Check(FAILED(effect->ValidateTechnique(effect->GetCurrentTechnique())));
  52. }

  53. void display()
  54. {
  55.         static int frame = 0;
  56.         static clock_t lastTime = clock();
  57.        
  58.         clock_t curTime = clock();
  59.         ++ frame;
  60.         if (curTime - lastTime > 1000)
  61.         {
  62.                 float FPS = 1000.0f * frame / (curTime - lastTime);
  63.                
  64.                 lastTime = curTime;
  65.                
  66.                 std::stringstream ss;
  67.                 ss << "Test Texture - " << FPS << std::ends;
  68.                 glutSetWindowTitle(ss.str().c_str());
  69.                
  70.                 frame = 0;
  71.         }

  72.         Check(FAILED(effect->SetTexture(effect->GetParameterByName(NULL, "tex0"),
  73.                 static_cast<DWORD>(texture[0]))));

  74.         glClear(GL_COLOR_BUFFER_BIT);

  75.         UINT numPasses;
  76.         Check(FAILED(effect->Begin(&numPasses, 0)));
  77.         Check(FAILED(effect->Pass(0)));

  78.         glColor4f(1, 1, 1, 1);
  79.        
  80.         glBegin(GL_QUADS);
  81.         {
  82.                 glTexCoord2f(0, 0);
  83.                 glVertex2f(-WIDTH / 2, -HEIGHT / 2);

  84.                 glTexCoord2f(0, 1);
  85.                 glVertex2f(WIDTH / 2, -HEIGHT / 2);

  86.                 glTexCoord2f(1, 1);
  87.                 glVertex2f(WIDTH / 2, HEIGHT / 2);

  88.                 glTexCoord2f(1, 0);
  89.                 glVertex2f(-WIDTH / 2, HEIGHT / 2);
  90.         }
  91.         glEnd();

  92.         Check(FAILED(effect->End()));

  93.         glutSwapBuffers();
  94.         glutPostRedisplay();
  95. }

  96. void reshape(GLsizei w, GLsizei h)
  97. {
  98.         glViewport(0, 0, w, h);
  99.        
  100.         glMatrixMode(GL_PROJECTION);
  101.         glLoadIdentity();
  102.         glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 10);
  103.        
  104.         glMatrixMode(GL_MODELVIEW);
  105.         glLoadIdentity();
  106. }

  107. int main(int argc, char* argv[])
  108. {
  109.         glutInit(&argc, argv);
  110.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  111.         glutInitWindowSize(WIDTH, HEIGHT);
  112.         glutInitWindowPosition(0, 0);
  113.         glutCreateWindow("Test Texture");
  114.         init();
  115.         glutDisplayFunc(display);
  116.         glutReshapeFunc(reshape);
  117.         glutMainLoop();
  118.        
  119.         return 0;
  120. }
复制代码


但这样结果不对,窗口内全黑。似乎是纹理传不过去。请问什么问题?

89

主题

4036

帖子

4132

积分

论坛元老

Rank: 8Rank: 8

积分
4132
发表于 2004-6-27 14:48:00 | 显示全部楼层

Re:纹理传不过去?

CG Effect 没用过。顶一下

3

主题

7

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2006-4-25 01:05:00 | 显示全部楼层

Re: 纹理传不过去?

CgFX/ICgFXEffect.h 是哪儿提供的文件?

132

主题

1341

帖子

1341

积分

金牌会员

Rank: 6Rank: 6

积分
1341
发表于 2006-4-26 13:06:00 | 显示全部楼层

Re:纹理传不过去?

Check(FAILED(effect->SetTexture(effect->GetParameterByName(NULL, "tex0"),

-------------------------------------------------------------------------------------------------------------
这句是不是有错?
而且我没看到你有纹理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 08:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表