示例#1
0
GLuint
ShaderAPITest::make_program(const char *vs_src, const char *fs_src)
{
	GLuint id, vs, fs;

	assert_no_error();
	id = glCreateProgram_func();
	if (vs_src) {
		vs = make_shader(GL_VERTEX_SHADER, vs_src);
		glAttachShader_func(id, vs);
		glDeleteShader_func(vs);
	}
	if (fs_src) {
		fs = make_shader(GL_FRAGMENT_SHADER, fs_src);
		glAttachShader_func(id, fs);
		glDeleteShader_func(fs);
	}
	glLinkProgram_func(id);
	check_link_status(id);
	glUseProgram_func(id);
	glDeleteProgram_func(id);
	assert_no_error();
	return id;
}
示例#2
0
static void
Init(void)
{
   const char *version;
   GLint i;

   version = (const char *) glGetString(GL_VERSION);
   if (version[0] != '2' || version[1] != '.') {
      printf("Warning: this program expects OpenGL 2.0\n");
      /*exit(1);*/
   }

   GetExtensionFuncs();

   vertShader = glCreateShader_func(GL_VERTEX_SHADER);
   ReadShader(vertShader, VertProgFile);

   fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
   ReadShader(fragShader, FragProgFile);

   program = glCreateProgram_func();
   glAttachShader_func(program, fragShader);
   glAttachShader_func(program, vertShader);
   glLinkProgram_func(program);
   CheckLink(program);
   glUseProgram_func(program);

   for (i = 0; Uniforms[i].name; i++) {
      Uniforms[i].location
         = glGetUniformLocation_func(program, Uniforms[i].name);
      printf("Uniform %s location: %d\n", Uniforms[i].name,
             Uniforms[i].location);
      switch (Uniforms[i].size) {
      case 1:
         glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
         break;
      case 2:
         glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
         break;
      case 3:
         glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
         break;
      case 4:
         glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
         break;
      default:
         abort();
      }
   }

   assert(glGetError() == 0);

   glClearColor(0.4f, 0.4f, 0.8f, 0.0f);

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));

   assert(glIsProgram_func(program));
   assert(glIsShader_func(fragShader));
   assert(glIsShader_func(vertShader));

   glColor3f(1, 0, 0);
}
static void
Init(void)
{
   static const char *fragShaderText =
      "uniform vec4 diffuse;\n"
      "uniform vec4 specular;\n"
      "varying vec3 normal;\n"
      "void main() {\n"
      "   // Compute dot product of light direction and normal vector\n"
      "   float dotProd = max(dot(gl_LightSource[0].position.xyz, \n"
      "                           normalize(normal)), 0.0);\n"
      "   // Compute diffuse and specular contributions\n"
      "   gl_FragColor = diffuse * dotProd + specular * pow(dotProd, 20.0);\n"
      "}\n";
   static const char *vertShaderText =
      "varying vec3 normal;\n"
      "void main() {\n"
      "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
      "   normal = gl_NormalMatrix * gl_Normal;\n"
      "}\n";
   const char *version;

   version = (const char *) glGetString(GL_VERSION);
   if (version[0] != '2' || version[1] != '.') {
      printf("This program requires OpenGL 2.x, found %s\n", version);
      exit(1);
   }

   GetExtensionFuncs();

   fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
   if (FragProgFile)
      ReadShader(fragShader, FragProgFile);
   else
      LoadAndCompileShader(fragShader, fragShaderText);


   vertShader = glCreateShader_func(GL_VERTEX_SHADER);
   if (VertProgFile)
      ReadShader(vertShader, VertProgFile);
   else
      LoadAndCompileShader(vertShader, vertShaderText);

   program = glCreateProgram_func();
   glAttachShader_func(program, fragShader);
   glAttachShader_func(program, vertShader);
   glLinkProgram_func(program);
   CheckLink(program);
   glUseProgram_func(program);

   uDiffuse = glGetUniformLocation_func(program, "diffuse");
   uSpecular = glGetUniformLocation_func(program, "specular");
   uTexture = glGetUniformLocation_func(program, "texture");
   printf("DiffusePos %d  SpecularPos %d  TexturePos %d\n",
          uDiffuse, uSpecular, uTexture);

   glUniform4fv_func(uDiffuse, 1, diffuse);
   glUniform4fv_func(uSpecular, 1, specular);
   /*   assert(glGetError() == 0);*/
   glUniform1i_func(uTexture, 2);  /* use texture unit 2 */
   /*assert(glGetError() == 0);*/

   if (CoordAttrib) {
      int i;
      glBindAttribLocation_func(program, CoordAttrib, "coord");
      i = glGetAttribLocation_func(program, "coord");
      assert(i >= 0);
      if (i != CoordAttrib) {
         printf("Hmmm, NVIDIA bug?\n");
         CoordAttrib = i;
      }
      else {
         printf("Mesa bind attrib: coord = %d\n", i);
      }
   }

   /*assert(glGetError() == 0);*/

   glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHT0);
   glEnable(GL_LIGHTING);
   glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
   glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 20.0f);

   MakeSphere();
   MakeRect();

   CurList = SphereList;

#if TEXTURE
   MakeTexture();
#endif

   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
   printf("Press p to toggle between per-pixel and per-vertex lighting\n");

   /* test glGetShaderSource() */
   if (0) {
      GLsizei len = strlen(fragShaderText) + 1;
      GLsizei lenOut;
      GLchar *src =(GLchar *) malloc(len * sizeof(GLchar));
      glGetShaderSource_func(fragShader, 0, NULL, src);
      glGetShaderSource_func(fragShader, len, &lenOut, src);
      assert(len == lenOut + 1);
      assert(strcmp(src, fragShaderText) == 0);
      free(src);
   }

   assert(glIsProgram_func(program));
   assert(glIsShader_func(fragShader));
   assert(glIsShader_func(vertShader));

   glColor3f(1, 0, 0);

   /* for testing state vars */
   {
      static GLfloat fc[4] = { 1, 1, 0, 0 };
      static GLfloat amb[4] = { 1, 0, 1, 0 };
      glFogfv(GL_FOG_COLOR, fc);
      glLightfv(GL_LIGHT1, GL_AMBIENT, amb);
   }

#if 0
   TestFunctions();
#else
   (void) TestFunctions;
#endif
}