void GimRasterizer::init_extensions() {
     if(!glewIsSupported(REQUIRED_EXTENSIONS)) {
         Logger::err("Fatal") << "Could not initialize OpenGL extensions"
                              << std::endl ;
         ogf_assert_not_reached ;
     }
     
     glGenProgramsNV(1, &fragment_program);
 }
Beispiel #2
0
void GPUProgram::genPrograms(int num, unsigned int* id) const {

    switch (extension) {
    case NVIDIA:
        alwaysAssertM(glGenProgramsNV != NULL, "Requires an NVIDIA card with the GL_NV_vertex_program extension");
        glGenProgramsNV(num, id);
        break;

    case ARB:
        glGenProgramsARB(num, id);
        break;
    }
}
Beispiel #3
0
//Set up openGL
bool GLInit()
{
	//set viewport
	int height;
	if (window.height==0)
		height=1;
	else
		height=window.height;
	
	glViewport(0, 0, window.width, height);					//reset viewport

	//set up projection matrix
	glMatrixMode(GL_PROJECTION);							//select projection matrix
	glLoadIdentity();										//reset
	gluPerspective(45.0f, (GLfloat)window.width/(GLfloat)height, 0.1f, 100.0f);
	
	//load identity modelview
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//other states
	//shading
	glShadeModel(GL_SMOOTH);
	glClearColor(	backgroundColor.r,
					backgroundColor.g,
					backgroundColor.b,
					backgroundColor.a);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	//depth
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	//hints
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


	//load vertex programs
	glGenProgramsNV(1, &chromaticSingleVP);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, chromaticSingleVP);
	LoadNV_vertex_program("chromatic single vp.txt", chromaticSingleVP);

	glGenProgramsNV(1, &simpleSingleVP);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, simpleSingleVP);
	LoadNV_vertex_program("simple single vp.txt", simpleSingleVP);

	glGenProgramsNV(1, &chromaticTwin1VP);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, chromaticTwin1VP);
	LoadNV_vertex_program("chromatic twin 1 vp.txt", chromaticTwin1VP);

	glGenProgramsNV(1, &chromaticTwin2VP);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, chromaticTwin2VP);
	LoadNV_vertex_program("chromatic twin 2 vp.txt", chromaticTwin2VP);




	//Set track matrices
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_IDENTITY_NV);
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 8, GL_MODELVIEW, GL_INVERSE_TRANSPOSE_NV);
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 12, GL_TEXTURE, GL_IDENTITY_NV);

	//Send fresnel multipication factor to c[28]
	glProgramParameter4fNV(	GL_VERTEX_PROGRAM_NV, 28, fresnel, fresnel, fresnel, 1.0f);
	
	//Send refractive indices to c[32], c[33], c[34]
	glProgramParameter4fNV(	GL_VERTEX_PROGRAM_NV, 32, index, index*index, 0.0f, 1.0f);
	glProgramParameter4fNV(	GL_VERTEX_PROGRAM_NV, 33, index+indexDelta,
							(index+indexDelta)*(index+indexDelta), 0.0f, 1.0f);
	glProgramParameter4fNV(	GL_VERTEX_PROGRAM_NV, 34, index+2*indexDelta,
							(index+2*indexDelta)*(index+2*indexDelta), 0.0f, 1.0f);
	//Send useful constants to c[64]
	glProgramParameter4fNV(	GL_VERTEX_PROGRAM_NV, 64, 0.0f, 1.0f, 2.0f, 0.5f);



	//Init states to begin
	if(renderPath==CHROMATIC_SINGLE)
		InitChromaticSingleStates();

	if(renderPath==SIMPLE_SINGLE)
		InitSimpleSingleStates();
	
	return true;
}
Beispiel #4
0
static void Init( void )
{
   static const char *prog1 =
      "!!VP1.0\n"
      "MUL   o[COL0].xyz, R0, c[35]; \n"
      "END\n";
   static const char *prog2 =
      "!!VP1.0\n"
      "#\n"
      "# c[0-3]  = modelview projection (composite) matrix\n"
      "# c[32]   = normalized light direction in object-space\n"
      "# c[35]   = yellow diffuse material, (1.0, 1.0, 0.0, 1.0)\n"
      "# c[64].x = 0.0\n"
      "# c[64].z = 0.125, a scaling factor\n"
      "#\n"
      "# outputs diffuse illumination for color and perturbed position\n"
      "#\n"
      "DP3   R0, c[32], v[NRML];     # light direction DOT normal\n"
      "MUL   o[COL0].xyz, R0, c[35]; \n"
      "MAX   R0, c[64].x, R0; \n"
      "MUL   R0, R0, v[NRML]; \n"
      "MUL   R0, R0, c[64].z;  \n"
      "ADD   R1, v[OPOS], -R0;       # perturb object space position\n"
      "DP4   o[HPOS].x, c[0], R1; \n"
      "DP4   o[HPOS].y, c[1], R1; \n"
      "DP4   o[HPOS].z, c[2], R1; \n"
      "DP4   o[HPOS].w, c[3], R1; \n"
      "END\n";
   static const char *prog3 = 
      "!!VP1.0\n"
      "DP4   o[HPOS].x, c[0], v[OPOS];\n"
      "DP4   o[HPOS].y, c[1], v[OPOS];\n"
      "DP4   o[HPOS].z, c[2], v[OPOS];\n"
      "DP4   o[HPOS].w, c[3], v[OPOS];\n"
      "DP3   R0.x, c[4], v[NRML];\n"
      "DP3   R0.y, c[5], v[NRML]; \n"
      "DP3   R0.z, c[6], v[NRML];           # R0 = n' = transformed normal\n"
      "DP3   R1.x, c[32], R0;               # R1.x = Lpos DOT n'\n"
      "DP3   R1.y, c[33], R0;               # R1.y = hHat DOT n'\n"
      "MOV   R1.w, c[38].x;                 # R1.w = specular power\n"
      "LIT   R2, R1;                        # Compute lighting values\n"
      "MAD   R3, c[35].x, R2.y, c[35].y;    # diffuse + emissive\n"
      "MAD   o[COL0].xyz, c[36], R2.z, R3;  # + specular\n"
      "END\n";
   static const char *prog4 = 
      "!!VP1.0\n"
      "DP4   R2, R3, c[A0.x];\n"
      "DP4   R2, R3, c[A0.x + 5];\n"
      "DP4   o[HPOS], R3, c[A0.x - 4];\n"
      "END\n";
   static const char *prog5 = 
      "!!VSP1.0\n"
      "DP4   R2, R3, c[A0.x];\n"
      "DP4   R2, R3, v[0];\n"
      "DP4   c[3], R3, R2;\n"
      "END\n";


   GLuint progs[5];

   glGenProgramsNV(2, progs);
   assert(progs[0]);
   assert(progs[1]);
   assert(progs[0] != progs[1]);

   glGenProgramsNV(3, progs + 2);
   assert(progs[2]);
   assert(progs[3]);
   assert(progs[2] != progs[3]);
   assert(progs[0] != progs[2]);


   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 1,
                   strlen(prog1),
                   (const GLubyte *) prog1);
   assert(!glIsProgramNV(1));

   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 2,
                   strlen(prog2),
                   (const GLubyte *) prog2);
   assert(glIsProgramNV(2));

   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 3,
                   strlen(prog3),
                   (const GLubyte *) prog3);
   assert(glIsProgramNV(3));

   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, 4,
                   strlen(prog4),
                   (const GLubyte *) prog4);
   assert(glIsProgramNV(4));

   glLoadProgramNV(GL_VERTEX_STATE_PROGRAM_NV, 5,
                   strlen(prog5),
                   (const GLubyte *) prog5);
   assert(glIsProgramNV(5));

   printf("glGetError = %d\n", (int) glGetError());
}
Beispiel #5
0
static void Init( void )
{
   GLint errno;
   GLuint prognum;
   char buf[16 * 1024];
   GLuint sz;
   FILE *f;

   if ((f = fopen(filename, "r")) == NULL) {
      fprintf(stderr, "couldn't open %s\n", filename);
      exit(1);
   }

   sz = (GLuint) fread(buf, 1, sizeof(buf) - 1, f);
   buf[sizeof(buf) - 1] = '\0';
   if (!feof(f)) {
      fprintf(stderr, "file too long\n");
      fclose(f);
      exit(1);
   }

   fclose(f);
   fprintf(stderr, "%.*s\n", sz, buf);

   if (strncmp( buf, "!!VP", 4 ) == 0) {
      glEnable( GL_VERTEX_PROGRAM_NV );
      glGenProgramsNV( 1, &prognum );
      glBindProgramNV( GL_VERTEX_PROGRAM_NV, prognum );
      glLoadProgramNV( GL_VERTEX_PROGRAM_NV, prognum, sz, (const GLubyte *) buf );
      assert( glIsProgramNV( prognum ) );
   }
   else {
      glEnable(GL_VERTEX_PROGRAM_ARB);

      glGenProgramsARB(1, &prognum);

      glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
      glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
		        sz, (const GLubyte *) buf);
      if (glGetError()) {
         printf("Program failed to compile:\n%s\n", buf);
         printf("Error: %s\n",
                (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
         exit(1);
      }
      assert(glIsProgramARB(prognum));
   }

   errno = glGetError();
   printf("glGetError = %d\n", errno);
   if (errno != GL_NO_ERROR)
   {
      GLint errorpos;

      glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
      printf("errorpos: %d\n", errorpos);
      printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
   }

   {
      const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
      const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
      const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
      const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
      glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
      glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
      glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
   }
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_NVProgram_nglGenProgramsNV(JNIEnv *env, jclass clazz, jint n, jlong programs, jlong function_pointer) {
	GLuint *programs_address = (GLuint *)(intptr_t)programs;
	glGenProgramsNVPROC glGenProgramsNV = (glGenProgramsNVPROC)((intptr_t)function_pointer);
	glGenProgramsNV(n, programs_address);
}
Beispiel #7
0
//Set up openGL
bool GLInit()
{
    //set viewport
    int height;
    if (window.height==0)
        height=1;
    else
        height=window.height;

    glViewport(0, 0, window.width, height);					//reset viewport

    //set up projection matrix
    glMatrixMode(GL_PROJECTION);							//select projection matrix
    glLoadIdentity();										//reset
    gluPerspective(45.0f, (GLfloat)window.width/(GLfloat)height, 1.0f, 100.0f);

    //load identity modelview
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //other states
    //shading
    glShadeModel(GL_SMOOTH);
    glClearColor(	backgroundColor.r,
                    backgroundColor.g,
                    backgroundColor.b,
                    backgroundColor.a);
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

    //depth
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    //hints
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glEnable(GL_CULL_FACE);

    //set up sphere
    sphere=gluNewQuadric();

    //Set up vertex arrays for quad
    glVertexPointer(3, GL_FLOAT, 0, vertexPositions);
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glEnable(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_COORD_ARRAY);

    //load vertex program
    glGenProgramsNV(1, &vertexProgram);
    glBindProgramNV(GL_VERTEX_PROGRAM_NV, vertexProgram);
    LoadNV_vertex_program("vertex program.txt", vertexProgram);

    //Set track matrices
    glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
    glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_TEXTURE0_ARB, GL_IDENTITY_NV);

    //Set texture atten states
    SetTextureAttenStates();

    return true;
}
Beispiel #8
0
void
compile(const char *filename, GLenum target, int use_ARB)
{
	GLenum err;
	GLuint prognum[2];
	char *buf;
	char *ptr;
	unsigned sz;
	int expected_fail;
	char *converted_buffers[2];
	size_t buffer_sizes[2];
	unsigned i;


	if (!piglit_automatic) {
		printf("%s:\n", filename);
	}

	buf = piglit_load_text_file(filename, &sz);
	if (buf == NULL) {
		piglit_report_result(PIGLIT_FAIL);
	}


	/* Scan the program source looking for two different things.  First,
	 * look for comments of the form '# FAIL'.  This signals that the
	 * program is expected to fail compilation.  Second, look for comments
	 * of the form '# REQUIRE GL_XXX_xxxx_xxxx'.  This signals that the
	 * program will only compile if some OpenGL extension is available.
	 */
	expected_fail = (strstr(buf, "# FAIL") != NULL);

	ptr = buf;
	while (ptr != NULL) {
		ptr = strstr(ptr, "# REQUIRE ");
		if (ptr != NULL) {
			char extension[128];
			unsigned i;

			ptr += strlen("# REQUIRE ");

			for (i = 0; !isspace((int) ptr[i]) && (ptr[i] != '\0'); i++) {
				extension[i] = ptr[i];
			}

			extension[i] = '\0';
			piglit_require_extension(extension);
		}
	}


	converted_buffers[0] = unix_line_endings(buf, sz);
	buffer_sizes[0] = strlen(converted_buffers[0]);
	converted_buffers[1] = dos_line_endings(buf, sz);
	buffer_sizes[1] = strlen(converted_buffers[1]);

	if (use_ARB) {
		glEnable(target);
		glGenProgramsARB(2, prognum);
	} else {
		glGenProgramsNV(2, prognum);
	}


	for (i = 0; i < 2; i++) {
		/* The use_ARB flag is used instead of the target because
		 * GL_VERTEX_PROGRAM_ARB and GL_VERTEX_PROGRAM_NV have the same
		 * value.
		 */
		if (use_ARB) {
			glBindProgramARB(target, prognum[i]);
			glProgramStringARB(target, GL_PROGRAM_FORMAT_ASCII_ARB,
					   buffer_sizes[i],
					   (const GLubyte *) converted_buffers[i]);
		} else {
			glBindProgramNV(target, prognum[i]);
			glLoadProgramNV(target, prognum[i],
					buffer_sizes[i],
					(const GLubyte *) converted_buffers[i]);
		}


		err = glGetError();
		if (err != GL_NO_ERROR) {
			GLint errorpos;

			glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
			if (!piglit_automatic) {
				printf("glGetError = 0x%04x\n", err);
				printf("errorpos: %d\n", errorpos);
				printf("%s\n",
				       (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
			}
		}

		if ((err == GL_NO_ERROR) != (expected_fail == FALSE)) {
			piglit_report_result(PIGLIT_FAIL);
		}
	}

	free(buf);
	free(converted_buffers[0]);
	free(converted_buffers[1]);

}
Beispiel #9
0
//Set up variables
bool DemoInit()
{
	//Seed random number generator	
	srand( (unsigned)time( NULL ) );

	//Initialise the array of vertices
	numVertices=gridDensity*gridDensity;
	vertices=new SIMPLE_VERTEX[numVertices];
	if(!vertices)
	{
		LOG::Instance()->OutputError("Unable to allocate space for %d vertices", numVertices);
		return false;
	}

	for(int i=0; i<gridDensity; ++i)
	{
		for(int j=0; j<gridDensity; ++j)
		{
			vertices[i*gridDensity+j].position.Set(	(float(i)/(gridDensity-1))*2-1,
													0.0f,
													(float(j)/(gridDensity-1))*2-1);
			
			vertices[i*gridDensity+j].normal.Set(	0.0f, 1.0f, 0.0f);
			
			vertices[i*gridDensity+j].texCoords.Set( (float(i)/(gridDensity-1)),
													-(float(j)/(gridDensity-1)));
		}
	}

	//Initialise the indices
	numIndices=2*(gridDensity)*(gridDensity-1);
	indices=new GLuint[numIndices];
	if(!indices)
	{
		LOG::Instance()->OutputError("Unable to allocate space for %d indices", numIndices);
		return false;
	}

	for(int i=0; i<gridDensity-1; ++i)
	{
		for(int j=0; j<gridDensity; ++j)
		{
			indices[(i*gridDensity+j)*2  ]=(i+1)*gridDensity+j;
			indices[(i*gridDensity+j)*2+1]=i*gridDensity+j;
		}
	}

	//Load texture
	IMAGE floorImage;
	floorImage.Load("OpenGL.tga");
	if(floorImage.paletted)
		floorImage.ExpandPalette();
	
	glGenTextures(1, &floorTexture);
	glBindTexture(GL_TEXTURE_2D, floorTexture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	gluBuild2DMipmaps(	GL_TEXTURE_2D, GL_RGBA8, floorImage.width, floorImage.height,
						floorImage.format, GL_UNSIGNED_BYTE, floorImage.data);

	//Initialise the lights
	for(int i=0; i<numLights; ++i)
	{
		lights[i].position.Set(	(float(rand())/RAND_MAX)*2-1,
								0.02f,
								(float(rand())/RAND_MAX)*2-1);
		lights[i].velocity.Set(	(float(rand())/RAND_MAX)*2-1,
								0.0f,
								(float(rand())/RAND_MAX)*2-1);
		lights[i].color.Set(	(float(rand())/RAND_MAX)*0.75f,
								(float(rand())/RAND_MAX)*0.75f,
								(float(rand())/RAND_MAX)*0.75f,
								1.0f);
	}

	//Load vertex programs
	if(GLEE_NV_vertex_program)
	{
		glGenProgramsNV(1, &vp1);
		glBindProgramNV(GL_VERTEX_PROGRAM_NV, vp1);
		LoadARB_program(GL_VERTEX_PROGRAM_NV, "vp1.txt");
	}

	if(GLEE_NV_vertex_program2)
	{
		glGenProgramsNV(1, &vp2);
		glBindProgramNV(GL_VERTEX_PROGRAM_NV, vp2);
		LoadARB_program(GL_VERTEX_PROGRAM_NV, "vp2.txt");
	}

	if(GLEE_NV_vertex_program || GLEE_NV_vertex_program2)
	{
		//Track modelview-projection matrix
		glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
	}

	//reset timer
	timer.Reset();

	return true;
}
Beispiel #10
0
/////////////////////////////////////////////////////////
// render
//
/////////////////////////////////////////////////////////
void vertex_program :: LoadProgram(void)
{
    if(NULL==m_programString)return;
    GLint err=-1;

    if((GEM_PROGRAM_NV == m_programType) && (!GLEW_NV_vertex_program)) {
        error("NV vertex programs not supported by this system");
        return;
    }

    if((GEM_PROGRAM_ARB == m_programType) && (!GLEW_ARB_vertex_program)) {
        error("ARB vertex programs not supported by this system");
        return;
    }

    switch(m_programType) {
    case  GEM_PROGRAM_NV:
        if (m_programID==0)
        {
            glEnable(m_programTarget);
            glGenProgramsNV(1, &m_programID);
            glBindProgramNV(m_programTarget, m_programID);
            glLoadProgramNV(m_programTarget, m_programID, m_size, (GLubyte*)m_programString);
            glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV, &err);
        } else {
            glEnable(m_programTarget);
            glBindProgramNV(m_programTarget, m_programID);
            return;
        }
        break;
    case  GEM_PROGRAM_ARB:
        if (m_programID==0)
        {
            glEnable(m_programTarget);
            glGenProgramsARB(1, &m_programID);
            glBindProgramARB( m_programTarget, m_programID);
            glProgramStringARB( m_programTarget, GL_PROGRAM_FORMAT_ASCII_ARB, m_size, m_programString);
            glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &err);
        } else {
            glEnable(m_programTarget);
            glBindProgramARB(m_programTarget, m_programID);
            return;
        }
        break;
    default:
        return;
    }

    if(err != -1) {
        int line = 0;
        char *s = m_programString;
        while(err-- && *s) if(*s++ == '\n') line++;
        while(s >= m_programString && *s != '\n') s--;
        char *e = ++s;
        while(*e != '\n' && *e != '\0') e++;
        *e = '\0';
        error("program error at line %d:\n\"%s\"\n",line,s);
        post("%s\n", glGetString(GL_PROGRAM_ERROR_STRING_ARB));
    }

    if(GLEW_ARB_vertex_program) {
        GLint isUnderNativeLimits;
        glGetProgramivARB( m_programTarget, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB, &isUnderNativeLimits);

        // If the program is over the hardware's limits, print out some information
        if (isUnderNativeLimits!=1)
        {
            // Go through the most common limits that are exceeded
            error("is beyond hardware limits");

            GLint aluInstructions, maxAluInstructions;
            glGetProgramivARB(m_programTarget, GL_PROGRAM_ALU_INSTRUCTIONS_ARB, &aluInstructions);
            glGetProgramivARB(m_programTarget, GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB, &maxAluInstructions);
            if (aluInstructions>maxAluInstructions)
                post("[%s]: Compiles to too many ALU instructions (%d, limit is %d)\n", m_buf.c_str(), aluInstructions, maxAluInstructions);

            GLint textureInstructions, maxTextureInstructions;
            glGetProgramivARB(m_programTarget, GL_PROGRAM_TEX_INSTRUCTIONS_ARB, &textureInstructions);
            glGetProgramivARB(m_programTarget, GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, &maxTextureInstructions);
            if (textureInstructions>maxTextureInstructions)
                post("[%s]: Compiles to too many texture instructions (%d, limit is %d)\n", m_buf.c_str(), textureInstructions, maxTextureInstructions);

            GLint textureIndirections, maxTextureIndirections;
            glGetProgramivARB(m_programTarget, GL_PROGRAM_TEX_INDIRECTIONS_ARB, &textureIndirections);
            glGetProgramivARB(m_programTarget, GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB, &maxTextureIndirections);
            if (textureIndirections>maxTextureIndirections)
                post("[%s]: Compiles to too many texture indirections (%d, limit is %d)\n", m_buf.c_str(), textureIndirections, maxTextureIndirections);

            GLint nativeTextureIndirections, maxNativeTextureIndirections;
            glGetProgramivARB(m_programTarget, GL_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, &nativeTextureIndirections);
            glGetProgramivARB(m_programTarget, GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, &maxNativeTextureIndirections);
            if (nativeTextureIndirections>maxNativeTextureIndirections)
                post("[%s]: Compiles to too many native texture indirections (%d, limit is %d)\n", m_buf.c_str(), nativeTextureIndirections, maxNativeTextureIndirections);

            GLint nativeAluInstructions, maxNativeAluInstructions;
            glGetProgramivARB(m_programTarget, GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, &nativeAluInstructions);
            glGetProgramivARB(m_programTarget, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, &maxNativeAluInstructions);
            if (nativeAluInstructions>maxNativeAluInstructions)
                post("[%s]: Compiles to too many native ALU instructions (%d, limit is %d)\n", m_buf.c_str(), nativeAluInstructions, maxNativeAluInstructions);
        }
    }
}
Beispiel #11
0
static void Init( void )
{
   static const char *fragProgramText =
      "!!FP1.0\n"
      "DECLARE Diffuse; \n"
      "DECLARE Specular; \n"
      "DECLARE LightPos; \n"

      "# Compute normalized LightPos, put it in R0\n"
      "DP3 R0.x, LightPos, LightPos;\n"
      "RSQ R0.y, R0.x;\n"
      "MUL R0, LightPos, R0.y;\n"

      "# Compute normalized normal, put it in R1\n"
      "DP3 R1, f[TEX0], f[TEX0]; \n"
      "RSQ R1.y, R1.x;\n"
      "MUL R1, f[TEX0], R1.y;\n"

      "# Compute dot product of light direction and normal vector\n"
      "DP3_SAT R2, R0, R1;"

      "MUL R3, Diffuse, R2;    # diffuse attenuation\n"

      "POW R4, R2.x, {20.0}.x; # specular exponent\n"

      "MUL R5, Specular, R4;   # specular attenuation\n"

      "ADD o[COLR], R3, R5;    # add diffuse and specular colors\n"
      "END \n"
      ;

   static const char *vertProgramText =
      "!!VP1.0\n"
      "# typical modelview/projection transform\n"
      "DP4   o[HPOS].x, c[0], v[OPOS] ;\n"
      "DP4   o[HPOS].y, c[1], v[OPOS] ;\n"
      "DP4   o[HPOS].z, c[2], v[OPOS] ;\n"
      "DP4   o[HPOS].w, c[3], v[OPOS] ;\n"
      "# transform normal by inv transpose of modelview, put in tex0\n"
      "DP3   o[TEX0].x, c[4], v[NRML] ;\n"
      "DP3   o[TEX0].y, c[5], v[NRML] ;\n"
      "DP3   o[TEX0].z, c[6], v[NRML] ;\n"
      "DP3   o[TEX0].w, c[7], v[NRML] ;\n"
      "END\n";
   ;

   if (!glutExtensionSupported("GL_NV_vertex_program")) {
      printf("Sorry, this demo requires GL_NV_vertex_program\n");
      exit(1);
   }
   if (!glutExtensionSupported("GL_NV_fragment_program")) {
      printf("Sorry, this demo requires GL_NV_fragment_program\n");
      exit(1);
   }
         
   glGenProgramsNV(1, &FragProg);
   assert(FragProg > 0);
   glGenProgramsNV(1, &VertProg);
   assert(VertProg > 0);

   /*
    * Fragment program
    */
   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, FragProg,
                   strlen(fragProgramText),
                   (const GLubyte *) fragProgramText);
   assert(glIsProgramNV(FragProg));
   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, FragProg);

   NAMED_PARAMETER4FV(FragProg, "Diffuse", Diffuse);
   NAMED_PARAMETER4FV(FragProg, "Specular", Specular);

   /*
    * Vertex program
    */
   glLoadProgramNV(GL_VERTEX_PROGRAM_NV, VertProg,
                   strlen(vertProgramText),
                   (const GLubyte *) vertProgramText);
   assert(glIsProgramNV(VertProg));
   glBindProgramNV(GL_VERTEX_PROGRAM_NV, VertProg);
   glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);
   glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 4, GL_MODELVIEW, GL_INVERSE_TRANSPOSE_NV);

   /*
    * Misc init
    */
   glClearColor(0.3, 0.3, 0.3, 0.0);
   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.0);

   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("Press p to toggle between per-pixel and per-vertex lighting\n");
}
Beispiel #12
0
void Shader::load(const char *name) {
	Parser *parser = new Parser(name);
	// matrixes
	num_matrixes = 0;
	for(int i = 0; i < NUM_MATRIXES; i++) {
		char buf[1024];
		sprintf(buf,"matrix%d",i);
		if(parser->get(buf)) {
			matrixes[num_matrixes].num = i;
			getMatrix(parser->get(buf),&matrixes[num_matrixes]);
			num_matrixes++;
		}
	}
	// vertex program local parameters
	num_vertex_parameters = 0;
	for(int i = 0; i < NUM_LOCAL_PARAMETERS; i++) {
		char buf[1024];
		sprintf(buf,"vertex_local%d",i);
		if(parser->get(buf)) {
			vertex_parameters[num_vertex_parameters].num = i;
			getLocalParameter(parser->get(buf),&vertex_parameters[num_vertex_parameters]);
			num_vertex_parameters++;
		}
	}
	// fragement program local parameters
	num_fragment_parameters = 0;
	for(int i = 0; i < NUM_LOCAL_PARAMETERS; i++) {
		char buf[1024];
		sprintf(buf,"fragment_local%d",i);
		if(parser->get(buf)) {
			fragment_parameters[num_fragment_parameters].num = i;
			getLocalParameter(parser->get(buf),&fragment_parameters[num_fragment_parameters]);
			num_fragment_parameters++;
		}
	}
	char *data;
	// vertex program
	vertex_target = 0;
	vertex_id = 0;
	if((data = parser->get("vertex"))) {
		int error = -1;
		if(!strncmp(data,"!!ARBvp1.0",10)) {
			vertex_target = GL_VERTEX_PROGRAM_ARB;
			glGenProgramsARB(1,&vertex_id);
			glBindProgramARB(vertex_target,vertex_id);
			glProgramStringARB(vertex_target,GL_PROGRAM_FORMAT_ASCII_ARB,strlen(data),data);
			glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB,&error);
		} else {
			char *s = data;
			while(*s != '\0' && *s != '\n') s++;
			*s = '\0';
			fprintf(stderr,"Shader::Shader(): unknown vertex program header \"%s\" in \"%s\" file\n",data,name);
		}
		if(error != -1) {
			int line = 0;
			char *s = data;
			while(error-- && *s) if(*s++ == '\n') line++;
			while(s >= data && *s != '\n') s--;
			char *e = ++s;
			while(*e != '\0' && *e != '\n') e++;
			*e = '\0';
			fprintf(stderr,"Shader::Shader(): vertex program error in \"%s\" file at line %d:\n\"%s\"\n",name,line,s);
		}
	}
	// fragment program
	fragment_target = 0;
	fragment_id = 0;
	if((data = parser->get("fragment"))) {
		int error = -1;
		if(!strncmp(data,"!!ARBfp1.0",10)) {
			fragment_target = GL_FRAGMENT_PROGRAM_ARB;
			glGenProgramsARB(1,&fragment_id);
			glBindProgramARB(fragment_target,fragment_id);
			glProgramStringARB(fragment_target,GL_PROGRAM_FORMAT_ASCII_ARB,strlen(data),data);
			glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB,&error);
		} else if(!strncmp(data,"!!FP1.0",7)) {
			fragment_target = GL_FRAGMENT_PROGRAM_NV;
			glGenProgramsNV(1,&fragment_id);
			glBindProgramNV(fragment_target,fragment_id);
			glLoadProgramNV(fragment_target,fragment_id,strlen(data),(GLubyte*)data);
			glGetIntegerv(GL_PROGRAM_ERROR_POSITION_NV,&error);
		} else if(!strncmp(data,"!!ARBtec1.0",11)) {	// arb texture env combine
			fragment_target = GL_COMBINE;
			fragment_id = compileARBtec(data);
		} else {
			char *s = data;
			while(*s != '\0' && *s != '\n') s++;
			*s = '\0';
			fprintf(stderr,"Shader::Shader(): unknown fragment program header \"%s\" in \"%s\" file\n",data,name);
		}
		if(error != -1) {
			int line = 0;
			char *s = data;
			while(error-- && *s) if(*s++ == '\n') line++;
			while(s >= data && *s != '\n') s--;
			char *e = ++s;
			while(*e != '\0' && *e != '\n') e++;
			*e = '\0';
			fprintf(stderr,"Shader::Shader(): fragment program error in \"%s\" file at line %d:\n\"%s\"\n",name,line,s);
		}
	}
	delete parser;
}
Beispiel #13
0
//Set up openGL
bool GLInit()
{
	//set viewport
	int height = 480;
	int width = 640;

	
	glViewport(0, 0, width, height);					//reset viewport

	//set up projection matrix
	glMatrixMode(GL_PROJECTION);							//select projection matrix
	glLoadIdentity();										//reset
	gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f);
	
	//load identity modelview
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//other states
	//shading
	glShadeModel(GL_SMOOTH);
	glClearColor(	backgroundColor.r,
					backgroundColor.g,
					backgroundColor.b,
					backgroundColor.a);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	//depth
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	//hints
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	//Set up vertex arrays for torus
	//Fixed 17th November 2002
	//Instead of passing tangents as texture coords 1 and 2, use aliased attributes 9 and 10.
	//This way, these texture coordinates will reach the vertex program on a geforce 2
	//which only has 2 texture units.
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, sizeof(TORUS_VERTEX), &torus.vertices[0].position);

	glEnableClientState(GL_NORMAL_ARRAY);
	glNormalPointer(GL_FLOAT, sizeof(TORUS_VERTEX), &torus.vertices[0].normal);

	//Pass texture coords to unit 0
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glTexCoordPointer(2, GL_FLOAT, sizeof(TORUS_VERTEX), &torus.vertices[0].s);

	//Pass tangent,binormal to attributes 9, 10
	glEnableClientState(GL_VERTEX_ATTRIB_ARRAY9_NV);
	glVertexAttribPointerNV(9, 3, GL_FLOAT, sizeof(TORUS_VERTEX), &torus.vertices[0].sTangent);
	
	glEnableClientState(GL_VERTEX_ATTRIB_ARRAY10_NV);
	glVertexAttribPointerNV(10, 3, GL_FLOAT, sizeof(TORUS_VERTEX), &torus.vertices[0].tTangent);
	
	//Use compiled vertex arrays
	glLockArraysEXT(0, torus.numVertices);


	//Load vertex programs
	glGenProgramsNV(1, &singlePassVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, singlePassVertexProgram);
	LoadNV_vertex_program("single pass vertex program.txt", singlePassVertexProgram);

	glGenProgramsNV(1, &diffuseDecalVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, diffuseDecalVertexProgram);
	LoadNV_vertex_program("diffuse decal vertex program.txt", diffuseDecalVertexProgram);

	glGenProgramsNV(1, &lookUpSpecularVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, lookUpSpecularVertexProgram);
	LoadNV_vertex_program("look up specular vertex program.txt", lookUpSpecularVertexProgram);

	glGenProgramsNV(1, &diffuseVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, diffuseVertexProgram);
	LoadNV_vertex_program("diffuse vertex program.txt", diffuseVertexProgram);
	
	glGenProgramsNV(1, &decalVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, decalVertexProgram);
	LoadNV_vertex_program("decal vertex program.txt", decalVertexProgram);

	glGenProgramsNV(1, &simpleSpecularVertexProgram);
	glBindProgramNV(GL_VERTEX_PROGRAM_NV, simpleSpecularVertexProgram);
	LoadNV_vertex_program("simple specular vertex program.txt", simpleSpecularVertexProgram);

	//Set Tracking Matrix
	//Modelview Projection in registers c[0]-c[3]
	glTrackMatrixNV(GL_VERTEX_PROGRAM_NV, 0, GL_MODELVIEW_PROJECTION_NV, GL_IDENTITY_NV);

	return true;
}
Beispiel #14
0
static void Init( void )
{
#if 0
   static const char *prog0 =
      "!!FP1.0\n"
      "MUL   o[COLR], R0, f[WPOS]; \n"
      "ADD   o[COLH], H3, f[TEX0]; \n"
      "ADD_SAT o[COLH], H3, f[TEX0]; \n"
      "ADDX o[COLH], H3, f[TEX0]; \n"
      "ADDHC o[COLH], H3, f[TEX0]; \n"
      "ADDXC o[COLH], H3, f[TEX0]; \n"
      "ADDXC_SAT o[COLH], H30, f[TEX0]; \n"
      "MUL   o[COLR].xy, R0.wzyx, f[WPOS]; \n"
      "MUL   o[COLR], H0, f[WPOS]; \n"
      "MUL   o[COLR], -H0, f[WPOS]; \n"
      "MOV   RC, H1; \n"
      "MOV   HC, H2; \n"
      "END \n"
      ;
#endif

   /* masked updates, defines, declarations */
   static const char *prog1 =
      "!!FP1.0\n"
      "DEFINE foo = {1, 2, 3, 4}; \n"
      "DEFINE foo2 = 5; \n"
      "DECLARE foo3 = {5, 6, 7, 8}; \n"
      "DECLARE bar = 3; \n"
      "DECLARE bar2; \n"
      "DECLARE bar3 = bar; \n"
      "#DECLARE bar4 = { a, b, c, d }; \n"
      "MOV o[COLR].xy,   R0; \n"
      "MOV o[COLR] (NE), R0; \n"
      "MOV o[COLR] (NE.wzyx), R0; \n"
      "MOV o[COLR].xy (NE.wzyx), R0; \n"
      "MOV RC.x (EQ), R1.x; \n"
      "KIL NE; \n"
      "KIL EQ.xyxy; \n"
      "END \n"
      ;

   /* texture instructions */
   static const char *prog2 =
      "!!FP1.0\n"
      "TEX R0, f[TEX0], TEX0, 2D; \n"
      "TEX R1, f[TEX1], TEX1, CUBE; \n"
      "TEX R2, f[TEX2], TEX2, 3D; \n"
      "TXP R3, f[TEX3], TEX3, RECT; \n"
      "TXD R3, R2, R1, f[TEX3], TEX3, RECT; \n"
      "MUL o[COLR], R0, f[COL0]; \n"
      "END \n"
      ;

   /* test negation, absolute value */
   static const char *prog3 =
      "!!FP1.0\n"
      "MOV R0, -R1; \n"
      "MOV R0, +R1; \n"
      "MOV R0, |-R1|; \n"
      "MOV R0, |+R1|; \n"
      "MOV R0, -|R1|; \n"
      "MOV R0, +|R1|; \n"
      "MOV R0, -|-R1|; \n"
      "MOV R0, -|+R1|; \n"
      "MOV o[COLR], R0; \n"
      "END \n"
      ;

   /* literal constant sources */
   static const char *prog4 =
      "!!FP1.0\n"
      "DEFINE Pi = 3.14159; \n"
      "MOV R0, {1, -2, +3, 4}; \n"
      "MOV R0, 5; \n"
      "MOV R0, -5; \n"
      "MOV R0, +5; \n"
      "MOV R0, Pi; \n"
      "MOV o[COLR], R0; \n"
      "END \n"
      ;

   /* change the fragment color in a simple way */
   static const char *prog10 =
      "!!FP1.0\n"
      "DEFINE blue = {0, 0, 1, 0};\n"
      "DECLARE color; \n"
      "MOV R0, f[COL0]; \n"
      "#ADD o[COLR], R0, f[COL0]; \n"
      "#ADD o[COLR], blue, f[COL0]; \n"
      "#ADD o[COLR], {1, 0, 0, 0}, f[COL0]; \n"
      "ADD o[COLR], color, f[COL0]; \n"
      "END \n"
      ;

   GLuint progs[20];

   if (!glutExtensionSupported ("GL_NV_fragment_program")) {
	   printf("Sorry, this program requires GL_NV_fragment_program\n");
	   exit(1);
   }

   glGenProgramsNV(20, progs);
   assert(progs[0]);
   assert(progs[1]);
   assert(progs[0] != progs[1]);

#if 0
   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[0],
                   strlen(prog0),
                   (const GLubyte *) prog0);
   assert(glIsProgramNV(progs[0]));
#endif

   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[1],
                   strlen(prog1),
                   (const GLubyte *) prog1);
   assert(glIsProgramNV(progs[1]));

   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2],
                   strlen(prog2),
                   (const GLubyte *) prog2);
   assert(glIsProgramNV(progs[2]));
   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[2]);

   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3],
                   strlen(prog3),
                   (const GLubyte *) prog3);
   assert(glIsProgramNV(progs[3]));
   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[3]);

   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4],
                   strlen(prog4),
                   (const GLubyte *) prog4);
   assert(glIsProgramNV(progs[4]));
   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[4]);


   /* a real program */
   glLoadProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10],
                   strlen(prog10),
                   (const GLubyte *) prog10);
   assert(glIsProgramNV(progs[10]));
   glBindProgramNV(GL_FRAGMENT_PROGRAM_NV, progs[10]);

   glProgramNamedParameter4fNV(progs[10],
                               strlen("color"), (const GLubyte *) "color",
                               1, 0, 0, 1);

   glEnable(GL_FRAGMENT_PROGRAM_NV);
   glEnable(GL_ALPHA_TEST);
   glAlphaFunc(GL_ALWAYS, 0.0);

   printf("glGetError = %d\n", (int) glGetError());
}