void onDraw(Graphics& g){
		g.pointSize(renderMode+0.5);
		g.color(RGB(1));
		mesh.colors().reset();
		switch (renderMode) {
			case 0:
				// do it on the CPU:
				for (int k=0; k<N; k++) {
				for (int j=0; j<N; j++) {
				for (int i=0; i<N; i++) {
					Color color;
					data.read_interp(color.components, i,j,k);
					mesh.color(color);
				}}}
				g.draw(mesh);
				break;
			case 1:
				// use 3D texcoords:
				tex.bind();
				g.draw(mesh);
				tex.unbind();
				break;
			case 2:
				// use shader:
				shader.begin();
				tex.bind();
				g.draw(mesh);
				tex.unbind();
				shader.end();
				break;
			default:;
		}
	}
inline void OmniStereoGraphicsRenderer::onDrawOmni(OmniStereo& omni) {
  graphics().error("start onDraw");
  mShader.begin();
  mOmni.uniforms(mShader);
  graphics().pushMatrix(graphics().MODELVIEW);
  onDraw(graphics());
  graphics().popMatrix(graphics().MODELVIEW);
  mShader.end();
}
inline bool OmniStereoGraphicsRenderer::onCreate() {
  mOmni.onCreate();

  Shader vert, frag;
  vert.source(OmniStereo::glsl() + vertexCode(), Shader::VERTEX).compile();
  vert.printLog();
  frag.source(fragmentCode(), Shader::FRAGMENT).compile();
  frag.printLog();
  mShader.attach(vert).attach(frag).link();
  mShader.printLog();
  mShader.begin();
  mShader.uniform("lighting", 0.0);
  mShader.uniform("texture", 0.0);
  mShader.end();

  return true;
}
Exemple #4
0
void run()
{
	if(!initialize())
		shutdown("Failed to initialize");

	if(!loadContent())
		shutdown("Failed to load resources");	

	Mesh cubeMesh = Mesh::genUnitColoredCube();
	MeshBuffer cubeBuffer(cubeMesh);
	Model cube(cubeBuffer);

	Mesh waterMesh = Mesh::genUnitColoredPlane(Color(0.57f, 0.63f, 0.98f));
	MeshBuffer waterBuffer(waterMesh);
	Model water(waterBuffer);

	BufferObject quadVbo;
	float quadVertices[] = {
		-1.0f, -1.0f, 0.0f, 0.0f,
		+1.0f, -1.0f, 1.0f, 0.0f,
		+1.0f, +1.0f, 1.0f, 1.0f,
		+1.0f, +1.0f, 1.0f, 1.0f,
		-1.0f, +1.0f, 0.0f, 1.0f,
		-1.0f, -1.0f, 0.0f, 0.0f
	};
	quadVbo.create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, sizeof(quadVertices), quadVertices);

	Mesh gridMesh;
	for(int i = 0; i <= 8; ++i)
	{
		float f = (i / 8.0) * 2.0f - 1.0f;
		int j = gridMesh.getPositionCount();
		gridMesh.addPosition(f * 3.0f, 0.0f, -3.0f);
		gridMesh.addPosition(f * 3.0f, 0.0f, +3.0f);
		gridMesh.addPosition(-3.0f, 0.0f, f * 3.0f);
		gridMesh.addPosition(+3.0f, 0.0f, f * 3.0f);
		gridMesh.addColor(Colors::White);
		gridMesh.addColor(Colors::White);
		gridMesh.addColor(Colors::White);
		gridMesh.addColor(Colors::White);
		gridMesh.addIndex(j + 0); gridMesh.addIndex(j + 1);
		gridMesh.addIndex(j + 2); gridMesh.addIndex(j + 3);
	}
	MeshBuffer gridBuffer(gridMesh);
	Model grid(gridBuffer);

	VertexArray vao;
	vao.create();
	vao.bind();

	mat4 perspectiveMatrix = glm::perspective(45.0f, windowWidth / float(windowHeight), 0.05f, 50.0f);
	
	// The geometry to be refracted and reflected are stored in these
	// In addition to RGB values, the world-space height is stored in the alpha-channel
	// of the refraction texture.
	// Fresnel equations is used to blend between the two textures
	RenderTexture refractionRT(windowWidth, windowHeight);
	RenderTexture reflectionRT(windowWidth, windowHeight);

	renderer.setClearColor(0.55f, 0.45f, 0.45f, 1.0f);
	renderer.setClearDepth(1.0);

	Timer timer;
	timer.start();
	double renderTime = 0.0;

	while(context.isOpen())
	{
		timer.step();
		double time = timer.getElapsedTime();
		update(time, timer.getDelta());
		double renderStart = timer.getElapsedTime();

		MatrixStack viewMatrix;
		viewMatrix.push();
		viewMatrix.translate(0.0f, 0.0f, -3.0f);
		viewMatrix.rotateX(xAxisRotation);
		viewMatrix.rotateY(yAxisRotation);

		renderer.setCullState(CullStates::CullNone);
		renderer.setDepthTestState(DepthTestStates::LessThanOrEqual);
		
		colorShader.begin();
		colorShader.setUniform("projection", perspectiveMatrix);
		cube.pushTransform();
		cube.translate(0.0f, 0.0f, 0.0f);
		cube.scale(0.5f);

		// Render the geometry to be refracted, store result in rt
		refractionRT.begin();
		renderer.clearColorAndDepth();
		colorShader.setUniform("view", viewMatrix.top());
		cube.draw(GL_TRIANGLES);
		grid.draw(GL_LINES);
		refractionRT.end();

		// Render the geometry to be reflected, store result in rt
		reflectionRT.begin();
		renderer.clearColorAndDepth();
		viewMatrix.push();
		viewMatrix.scale(1.0f, -1.0f, 1.0f); // Reflect about xz-plane
		colorShader.setUniform("view", viewMatrix.top());
		cube.draw(GL_TRIANGLES);
		viewMatrix.pop();
		reflectionRT.end();

		colorShader.end();
		cube.popTransform();

		// Render the water with the previous reflection/refraction texture
		waterShader.begin();
		waterShader.setUniform("time", time);
		glActiveTexture(GL_TEXTURE0 + 0);
		refractionRT.bindTexture();
		glActiveTexture(GL_TEXTURE0 + 1);
		reflectionRT.bindTexture();
		glActiveTexture(GL_TEXTURE0 + 2);
		waterNormals.bind();
		//waterShader.setUniform("view", viewMatrix.top());
		waterShader.setUniform("refraction_tex", 0);
		waterShader.setUniform("reflection_tex", 1);
		waterShader.setUniform("water_normals_tex", 2);
		//waterShader.setUniform("light0_pos", vec3(0.0f, 1.0f, 0.0f));
		//waterShader.setUniform("light0_col", vec3(1.0f, 0.8f, 0.5f));
		//waterShader.setUniform("ambient", vec3(67.0f/255.0f, 66.0f/255.0f, 63.0f/255.0f));
		quadVbo.bind();
		waterShader.setAttributefv("position", 2, 4, 0);
		waterShader.setAttributefv("texel", 2, 4, 2);
		glDrawArrays(GL_TRIANGLES, 0, 6);
		quadVbo.unbind();
		reflectionRT.unbindTexture();
		refractionRT.unbindTexture();
		waterNormals.unbind();
		waterShader.end();
		glActiveTexture(GL_TEXTURE0 + 0);

		// Render unmirrored scene
		//colorShader.begin();
		//renderer.clearColorAndDepth();
		//renderer.setCullState(CullStates::CullNone);
		//renderer.setBlendState(BlendStates::AlphaBlend);
		//renderer.setDepthTestState(DepthTestStates::LessThanOrEqual);
		//colorShader.setUniform("projection", perspectiveMatrix);
		//colorShader.setUniform("view", viewMatrix.top());
		//cube.pushTransform();
		//cube.translate(0.0f, 0.4f, 0.0f);
		//cube.scale(0.5f);
		//cube.draw(GL_TRIANGLES);

		/*grid.pushTransform();
		grid.translate(0.0f, -0.5f, 0.0f);
		grid.draw(GL_LINES);
		grid.popTransform();*/

		// Draw mirrored scene to a rendertarget
		/*rt.begin();
		renderer.clearColorAndDepth();
		
		viewMatrix.push();
		viewMatrix.scale(1.0f, -1.0f, 1.0f);
		colorShader.setUniform("view", viewMatrix.top());
		cube.draw(GL_TRIANGLES);
		cube.popTransform();
		viewMatrix.pop();
		rt.end();*/

		// Enable stencil testing and mask out a section containing the water mesh
		//glEnable(GL_STENCIL_TEST);
		//glStencilFunc(GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
		//glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
		//glStencilMask(0xFF); // Write to stencil buffer
		//glDepthMask(GL_FALSE); // Don't write to depth buffer
		//glClear(GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (0 by default)

		//// Draw water mesh
		//water.pushTransform();
		//water.scale(3.0f);
		//water.draw(GL_TRIANGLES);
		//water.popTransform();
		//colorShader.end();

		//// Draw previous rendertarget as a quad masked into the water plane
		//glStencilFunc(GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
		//glStencilMask(0x00); // Don't write anything to stencil buffer
		//glDepthMask(GL_TRUE);

		//glDisable(GL_STENCIL_TEST);

		viewMatrix.pop();

		context.display();
		renderTime = timer.getElapsedTime() - renderStart;
		if(renderTime < 0.013)
			context.sleep(0.013 - renderTime);

		if(checkGLErrors(std::cerr))
		{
			std::cin.get();
			context.close();
		}
	}

	waterNormals.dispose();
	colorShader.dispose();
	waterShader.dispose();
	vao.dispose();
	context.dispose();
}
Exemple #5
0
int main(int argc, char** argv)
{
    SDL_Window* window;
    SDL_GLContext ctx;
    GLuint vao;

    if(!initSDL(&window, &ctx) || !initGLEW())
    {
        return false;
    }

    // Create camera
    Camera camera(glm::radians(60.0f),
                  static_cast<float>(width)/static_cast<float>(height),
                  0.000001f,
                  1000.0f);
    camera.getTransform()->setPosition(0.0f,0.0f,5.0f);

    VertexShader vs;
    FragmentShader fs;
    ShaderProgram shaderProg;

    WavefrontMeshFactory factory;
    Mesh* mesh = factory.load("../../wreck/assets/uvcube.obj");
    if(!vs.load("../../wreck/assets/lambert.vs")) std::cout << "Vertex Shader error." << std::endl;
    if(!fs.load("../../wreck/assets/lambert.fs")) std::cout << "Fragment shader error." << std::endl;

    Texture texture;
    texture.load("../../wreck/assets/uvpattern.dds");

    Light light(glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(1.0f, 1.0f, 1.0f));
    light.setIntensity(1.0f);

    std::cout << "Linking..." << std::endl;
    shaderProg.setVertexShader(&vs);
    shaderProg.setFragmentShader(&fs);
    shaderProg.link();

    //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black

    Uint32 ticks = SDL_GetTicks();
    bool running = true;
    while(running)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            Uint32 elapsed = SDL_GetTicks()-ticks;
            ticks = SDL_GetTicks();

            switch (event.type)
            {
            case SDL_KEYDOWN:
                keyboardCameraControl(event, camera, elapsed);
                break;
            case SDL_KEYUP:
                // if escape is pressed, quit
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    running = false; // set status to 1 to exit main loop
                break;
            case SDL_MOUSEMOTION:
                mouseCameraControl(event, window, camera, elapsed);
                break;
            case SDL_QUIT:
                running = false;
                break;
            }
        }

        glm::vec3 lightPos = light.getTransform()->getPosition();
        glm::vec3 lightColor = light.getColor() * light.getIntensity();
        glm::vec4 diffuse = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
        glm::mat4 m = glm::mat4(1.0f);
        glm::mat4 v = camera.getViewMatrix();
        glm::mat4 p = camera.getProjectionMatrix();
        glm::mat4 mvp = p*v*m;


        // Begin drawing
        shaderProg.begin();
            mesh->use();
            texture.use();

            shaderProg.setUniformValue(0, mvp);
            shaderProg.setUniformValue(1, m);
            shaderProg.setUniformValue(2, v);
            shaderProg.setUniformValue(3, lightPos);
            shaderProg.setUniformValue(4, lightColor);
            //shaderProg.setUniformValue(1, diffuse);
            glDrawElements(GL_TRIANGLES, mesh->indices.size(), GL_UNSIGNED_INT, 0);
        shaderProg.end();

        SDL_GL_SwapWindow(window);
    }

    SDL_GL_DeleteContext(ctx);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
	bool onFrame(){

		gl.clearColor(0.1, 0.1, 0.1, 1);
		gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
		gl.viewport(0,0, width(), height());

		gl.matrixMode(gl.PROJECTION);
		gl.loadMatrix(Matrix4d::perspective(45, aspect(), 0.1, 100));

		gl.matrixMode(gl.MODELVIEW);
		gl.loadMatrix(Matrix4d::lookAt(Vec3d(0,0,4), Vec3d(0,0,0), Vec3d(0,1,0)));


		gl.disable(gl.LIGHTING);

//		shaderprogram.begin();
//		shaderprogram.uniform("tex0", 0);
//		GraphicsGL::gl_error("shader");
//		tex.bind(0);
//		gl.begin(gl.QUADS);
//		gl.color(1, 1, 1);
//		gl.texCoord(0, 0);
//		gl.vertex(0, 0, 0);
//		gl.texCoord(0, 1);
//		gl.vertex(0, 1, 0);
//		gl.texCoord(1, 1);
//		gl.vertex(1, 1, 0);
//		gl.texCoord(1, 0);
//		gl.vertex(1, 0, 0);
//		gl.end();
//		tex.unbind(0);
//		shaderprogram.end();


		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);    // Uses default lighting parameters
		glEnable(GL_DEPTH_TEST);
		glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
		glEnable(GL_NORMALIZE);
		glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

		const GLfloat pos[]={ 1.f, 1.f, 2.f, 0.f };
		glLightfv(GL_LIGHT0, GL_POSITION, pos);

//		gl.enable(gl.DEPTH_TEST);
////		gl.enable(gl.NORMALIZE);
//		light.twoSided(true);
//		light.dir(1.f, 1.f, 2.f);
//
//		light();
//		material();
//		glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

		gl.pushMatrix();

		// rotate it around the y axis
		gl.rotate(a, 0.f,1.f,0.f);
		a += 0.5;

		// scale the whole asset to fit into our view frustum
		float tmp = scene_max[0]-scene_min[0];
		tmp = al::max(scene_max[1] - scene_min[1],tmp);
		tmp = al::max(scene_max[2] - scene_min[2],tmp);
		tmp = 2.f / tmp;
		gl.scale(tmp);

		// center the model
		gl.translate( -scene_center );



		shaderprogram.begin();
		shaderprogram.uniform("tex0", 1);
		Graphics::error("tex0");
			tex.bind(1);
				scene_list.draw();
			tex.unbind(1);
		shaderprogram.end();

		gl.popMatrix();

		return true;
	}