예제 #1
0
파일: scene.c 프로젝트: K0F/glsl_lighting
void
sceneInit(void)
{
	GLint result;

	/* create program object and attach shaders */
	g_program = glCreateProgram();
	shaderAttachFromFile(g_program, GL_VERTEX_SHADER, "data/shader.vp");
	shaderAttachFromFile(g_program, GL_FRAGMENT_SHADER, "data/shader.fp");

	/* link the program and make sure that there were no errors */
	glLinkProgram(g_program);
	glGetProgramiv(g_program, GL_LINK_STATUS, &result);
	if(result == GL_FALSE) {
		GLint length;
		char *log;

		/* get the program info log */
		glGetProgramiv(g_program, GL_INFO_LOG_LENGTH, &length);
		log = malloc(length);
		glGetProgramInfoLog(g_program, length, &result, log);

		/* print an error message and the info log */
		fprintf(stderr, "sceneInit(): Program linking failed: %s\n", log);
		free(log);

		/* delete the program */
		glDeleteProgram(g_program);
		g_program = 0;
	}

	/* get uniform locations */
	g_programCameraPositionLocation = glGetUniformLocation(g_program, "cameraPosition");
	g_programLightPositionLocation = glGetUniformLocation(g_program, "lightPosition");
	g_programLightColorLocation = glGetUniformLocation(g_program, "lightColor");

	/* set up red/green/blue lights */
	g_lightColor[0] = 1.0f; g_lightColor[1] = 0.0f; g_lightColor[2] = 0.0f;
	g_lightColor[3] = 0.0f; g_lightColor[4] = 1.0f; g_lightColor[5] = 0.0f;
	g_lightColor[6] = 0.0f; g_lightColor[7] = 0.0f; g_lightColor[8] = 1.0f;

	/* create cylinder */
	createCylinder(36);

	/* do the first cycle to initialize positions */
	g_lightRotation = 0.0f;
	sceneCycle();

	/* setup camera */
	g_cameraPosition[0] = 0.0f;
	g_cameraPosition[1] = 0.0f;
	g_cameraPosition[2] = 4.0f;
	glLoadIdentity();
	glTranslatef(-g_cameraPosition[0], -g_cameraPosition[1], -g_cameraPosition[2]);
}
예제 #2
0
파일: main.cpp 프로젝트: astrellon/GPP
// Initializes OpenGL. Loads the vertex and fragment shaders, and creates the
// sphere mesh.
void glInit(void) 
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
	// Enable texture for the text fields.
	glEnable(GL_TEXTURE_2D);

	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glFrontFace(GL_CW);

	g_program = glCreateProgram();
	shaderAttachFromFile(g_program, GL_VERTEX_SHADER, "data/vertex.vp");
	shaderAttachFromFile(g_program, GL_FRAGMENT_SHADER, "data/fragment.fp");

	GLint result;
	glLinkProgram(g_program);
	glGetProgramiv(g_program, GL_LINK_STATUS, &result);
	if(result == GL_FALSE) {
		GLint length;
		char *log;

		// get the program info log
		glGetProgramiv(g_program, GL_INFO_LOG_LENGTH, &length);
		log = (char *)malloc(length);
		glGetProgramInfoLog(g_program, length, &result, log);

		// print an error message and the info log
		fprintf(stderr, "init(): Program linking failed: %s\n", log);
		free(log);

		// delete the program
		glDeleteProgram(g_program);
		g_program = 0;
	}

	// get uniform locations
	g_programCameraPositionLocation = glGetUniformLocation(g_program, "cameraPosition");
	g_programObjectPositionLocation = glGetUniformLocation(g_program, "objectPositions");
	g_programLightDirectionLocation = glGetUniformLocation(g_program, "lightDirection");
	g_programUserSphereLocation = glGetUniformLocation(g_program, "userSphere");
	
	g_lightDirection[0] = 1.0f;
	g_lightDirection[1] = 0.0f;
	g_lightDirection[2] = 0.0f;

	g_cameraPosition[0] = 0.0f;
	g_cameraPosition[1] = 0.0f;
	g_cameraPosition[2] = -zoom;

	sphereMesh = createSphere(12, 12);
}