Cg Shader不显示

时间:2012-11-16 20:29:21

标签: opengl shader cg

我正在尝试在我之前编写的OpenGL应用程序中运行一个简单的cg(顶点)着色器,但是当我启动程序时没有任何显示但是如果我禁用着色器它会完美运行。

这是着色器:

struct output{
  float4 color : COLOR;
};

output main(float4 color : COLOR,uniform float delta){
  output OUT;
  OUT.color = float4(abs(sin(color[0] + delta/10.0)),abs(sin(color[1] + delta/10.0)),abs(sin(color[2]+delta/10.0)),1);
  return OUT;
}

这是我写的简短程序:

#include "stdafx.h"
#include <GL\glut.h>
#include <Cg\cg.h>
#include <Cg\cgGL.h> 
#include <windows.h>

CGprogram program;
float delta;
void display()
{
  cgGLBindProgram(program);
  cgGLSetParameter1f(cgGetNamedParameter(program,"delta"),delta);
  glClearColor(0.0,0.0,0.0,1.0);
  glClear(GL_COLOR_BUFFER_BIT);
  glTranslatef(0.0,0.0,-5.0);
  glFlush();
  glutSwapBuffers();
}
void reshape(int x, int y)
{
    if (y == 0 || x == 0) return;
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();
    gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
    glMatrixMode(GL_MODELVIEW);
    glViewport(0,0,x,y); 
}
void OnIdle(void) {
    delta++;
    Sleep(100);
    glutPostRedisplay();
}
int main(int argc, char **argv){
  glutInit(&argc, argv);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutCreateWindow("Cg test");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutIdleFunc(OnIdle);
 CGcontext context = cgCreateContext();
 if(context == NULL){
    printf("Failed to create CGcontext\n");
  return 1;
 }
 program = cgCreateProgramFromFile(context,CG_SOURCE,"Test1.cg",CG_PROFILE_ARBVP1,"main",NULL);
  cgGLLoadProgram(program);
  cgGLEnableProfile(CG_PROFILE_ARBVP1);
  printf("%s\n", cgGetErrorString(cgGetError()));
  glutMainLoop();
    return 0;
}

0 个答案:

没有答案