예제 #1
0
파일: exj_5.cpp 프로젝트: juantresde/opengl
void GenerateShaders( ) {
    //  Shaders
    try {
        vertex_shader.SourceFile("../shaders/diffuse_shading.vert");
        fragment_shader.SourceFile("../shaders/diffuse_shading.frag");
        vertex_shader.Compile();
        fragment_shader.Compile();
        diffuse_shading.addShader(vertex_shader.GetHandle());
        diffuse_shading.addShader(fragment_shader.GetHandle());
        diffuse_shading.link();
        diffuse_shading.unuse();
    }
    catch (ShaderProgramException excp) {
        cerr << excp.what () << endl;
        exit (EXIT_FAILURE);
    }

    try {
        ads_vertex_shader.SourceFile("../shaders/ads_shading.vert");
        ads_fragment_shader.SourceFile("../shaders/ads_shading.frag");
        ads_vertex_shader.Compile();
        ads_fragment_shader.Compile();
        ads_shading.addShader(ads_vertex_shader.GetHandle());
        ads_shading.addShader(ads_fragment_shader.GetHandle());
        ads_shading.link();
        ads_shading.unuse();
        ads_shading.printActiveUniforms();
    }
    catch (ShaderProgramException excp) {
        cerr << excp.what() << endl;
        exit (EXIT_FAILURE);
    }

//    global_shader = &diffuse_shading;
    global_shader = &ads_shading;
}
예제 #2
0
int main( void )
{
	for (int i=0; i<NUM_CIRCLE_VERTICES; i++) {
		float f = i * 2*M_PI / NUM_CIRCLE_VERTICES;
		g_vertex_buffer_data[3*i] = cos(f)+2.0f;
		g_vertex_buffer_data[3*i+1] = sin(f);
		g_vertex_buffer_data[3*i+2] = 0.0f;
	}

	// Initialise GLFW
	if( !glfwInit() )
	{
		fprintf( stderr, "Failed to initialize GLFW\n" );
		return -1;
	}

	glfwWindowHint(GLFW_SAMPLES, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	// Open a window and create its OpenGL context
	window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
	if( window == NULL ){
		GLenum error = glGetError();
		if (error != GL_NO_ERROR)
		{
			fprintf(stderr, "-1 OpenGL Error: %d\n", error);
		}
		fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	GLenum error = glGetError();
	if (error != GL_NO_ERROR)
	{
	    fprintf(stderr, "-1 OpenGL Error: %d\n", error);
	}

	glewExperimental = GL_TRUE;
	// Initialize GLEW
	if (glewInit() != GLEW_OK) {
		fprintf(stderr, "Failed to initialize GLEW\n");
		return -1;
	}


	error = glGetError();
	if (error != GL_NO_ERROR)
	{
	    fprintf(stderr, "0 OpenGL Error: %d\n", error);
	}

	// Ensure we can capture the escape key being pressed below
	glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

	// Dark blue background
	glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
	
	
	GLuint VertexArrayID;
	glGenVertexArrays(1, &VertexArrayID);
	glBindVertexArray(VertexArrayID);
	
	//glEnable (GL_BLEND);
	//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// This will identify our vertex buffer
	GLuint vertexbuffer;
	 
	// Generate 1 buffer, put the resulting identifier in vertexbuffer
	glGenBuffers(1, &vertexbuffer);
	 
	// The following commands will talk about our 'vertexbuffer' buffer
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	 
	// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

	// load shaders
	ShaderProgram shaderProgram;
	shaderProgram.addShader(GL_VERTEX_SHADER, "vertex");
	shaderProgram.addShader(GL_FRAGMENT_SHADER, "fragment");
	shaderProgram.addShader(GL_GEOMETRY_SHADER, "geometry");
	GLuint shader = shaderProgram.finalize();
	
	// matrices
	glm::mat4 projection = glm::perspective(45.0f, 4.0f/3.0f, 0.1f, 100.0f);
	glm::mat4 view       = glm::lookAt(
		glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
		glm::vec3(0,0,0), // and looks at the origin
		glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
	);
	glm::mat4 MVP0 = projection * view;
	
	glm::vec4 x_vec = MVP0 * glm::vec4(1.0f,0.0f,0.0f,1.0f);
	glm::vec4 z_vec = MVP0 * glm::vec4(0.0f,0.0f,1.0f,1.0f);

	glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

	float offset = 0.0f;
	do{
		offset += 1.f;
		// clear screen
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // actually use our shaders
		glUseProgram(shader);
		glm::mat4 rotation = glm::rotate(glm::mat4(1.0f),offset,glm::vec3(0,1,0));
        glm::mat4 MVP = MVP0 * rotation;
		// set offset in shader
		GLint offsetloc = glGetUniformLocation(shader,"offset");
		glUniform1f(offsetloc,offset);
		GLuint MVPloc = glGetUniformLocation(shader,"MVP");
		GLint segcntloc = glGetUniformLocation(shader,"segcount");
		int MAXSEGMENTS = 80;
		int segments = ((int) offset) % (2*MAXSEGMENTS);
		if (segments > MAXSEGMENTS)
			segments = 2*MAXSEGMENTS-segments;
		glUniform1i(segcntloc,segments);
		glUniformMatrix4fv(MVPloc, 1, GL_FALSE, &MVP[0][0]);
		// some gl magic; look it up.
		glEnableVertexAttribArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
		glVertexAttribPointer(
			0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
			3,                  // size
			GL_FLOAT,           // type
			GL_FALSE,           // normalized?
			0,                  // stride
			(void*)0            // array buffer offset
		);
		glDrawArrays(GL_LINE_LOOP, 0, NUM_CIRCLE_VERTICES);
		glDisableVertexAttribArray(0);
		// Swap buffers
		glfwSwapBuffers(window);
		glfwPollEvents();
		error = glGetError();
		if (error != GL_NO_ERROR)
		{
			fprintf(stderr, "0 OpenGL Error: %d\n", error);
		}
	} // Check if the ESC key was pressed or the window was closed
	while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
		   glfwWindowShouldClose(window) == 0 );

	// Close OpenGL window and terminate GLFW
	glfwTerminate();

	return 0;
}
예제 #3
0
파일: main.cpp 프로젝트: remeh/mehteor
int main(int argc, char* argv[]) {
    System::init();
    
    /*
     * Byte buffer
     *
     */
    ByteBuffer buffer(5);
    char abc[4] = "abc";
    buffer.write(abc,3);
    buffer.reset();
    buffer.resize(3,true);
    printf("%c\n",buffer.readByte());
    printf("%c\n",buffer.readByte());
    printf("%c\n",buffer.readByte());
    buffer.reset();

    /*
     * Canvas
     */
    Canvas canvas(640,480);
    if (!canvas.getSurface()) {
        return -1;
    }

    Thread thread("test thread", &testFunc, (void*)NULL);

    /*
     * Json Reader.
     */
    JsonReader reader;
    reader.read("res/config.json");
    Json::Value value = reader.getRoot();
    std::cout << reader.getRoot().get("verson", "1.2") << std::endl;

    JsonWriter writer;
    writer.write("res/1.json", reader.getRoot(), true);
    writer.write("res/2.json", reader.getRoot(), false);

    Shader vertex("res/shaders/vertex.glsl",Shader::ShaderType::VERTEX_SHADER);
    Shader fragment("res/shaders/fragment.glsl",Shader::ShaderType::FRAGMENT_SHADER);
    ShaderProgram shaderProgram;
    shaderProgram.addShader(&vertex);
    shaderProgram.addShader(&fragment);
    shaderProgram.link();

    Shader vertexRadialBlur("res/shaders/radialblur_vertex.glsl",Shader::ShaderType::VERTEX_SHADER);
    Shader fragmentRadialBlur("res/shaders/radialblur_fragment.glsl",Shader::ShaderType::FRAGMENT_SHADER);
    ShaderProgram radialBlur;
    radialBlur.addShader(&vertexRadialBlur);
    radialBlur.addShader(&fragmentRadialBlur);
    radialBlur.link();

    ResourcesManager resourcesManager;
    resourcesManager.loadTexture("lama1","res/lama1.png");
    resourcesManager.loadTexture("spritesheet","res/ch.png");
    resourcesManager.loadTexture("courrier","res/courrier_0.png");
    
    Sprite sprite2(resourcesManager.getTexture("lama1"));

    Rect<float> texCoords[] = { Rect<float>(32,0,32,32), Rect<float>(0,0,32,32), Rect<float>(32,0,32,32), Rect<float>(64,0,32,32)};
    unsigned int durations[] = { 100, 100, 100, 100 };
    Sprite sprite(resourcesManager.getTexture("spritesheet"));
    sprite.addAnimation("down", 4, durations, texCoords);
    sprite.setSize(128,128);
    sprite.setAnimation("down");

    SimpleScene2D scene2D(640,480);

    shared_ptr<SceneNode> node = shared_ptr<SceneNode>(new SceneNode(&vertex,&fragment));
    scene2D.addNode(node);

    for (int i = 0; i < 1; i++)
    {
        shared_ptr<SpriteActor> s = shared_ptr<SpriteActor>(new SpriteActor(sprite2));
        if (s)
        {
            node->addActor(s);
        }
    }
    node->addActor(shared_ptr<SpriteActor>(new SpriteActor(sprite)));

    BMFontReader fontReader("res/courrier.fnt");
    FontActorCreator fontActorCreator(resourcesManager,fontReader.read());
    shared_ptr<FontActor> fontActor = fontActorCreator.createFontActor("courrier", "This IS A TEXT TEST ! Hehehe", 1, 2, 2.0f, Vector4d<float>(0.2f,0.5f,1.0f,0.2f));
    node->addActor(fontActor);

    /*
    for (int i = 0; i < 5000; i++)
    {
        shared_ptr<SpriteActor> s = shared_ptr<SpriteActor>(new SpriteActor(sprite));
        if (s)
        {
            node->addActor(s);
        }
    }
    */

    InputDevicesManager& idm = canvas.getInputDevicesManager();

    idm.update();
    printf("%i %i\n",idm.getMouseX(),idm.getMouseY());

    if (idm.keyPressed(SDLK_SPACE)) {
        printf("SPACE!\n");
    }

    if (idm.leftButton()) {
        printf("left button\n");
    }
    if (idm.middleButton()) {
        printf("middle button\n");
    }
    if (idm.rightButton()) {
        printf("right button\n");
    }

    long t = System::currentTime()+1000;
    float nbFrame = 0.0f;

    scene2D.update();
    printf("scene 2D updated\n");
    char title[10];

    Sound snd("res/music.ogg");
    Sound snd2("res/boump.ogg");
    AudioSystem system;
    system.play(&snd,true);

    while (1) {
        canvas.clear(0.0f,0.0f,0.0f,1.0f);
        scene2D.update();
        scene2D.render();
        canvas.flip();
        idm.update();
        if (idm.leftButton()) {
            printf("left button\n");
        }
        if (idm.keyPressed(SDLK_ESCAPE)) {
            break;
        } else if (idm.keyPressed(SDLK_LEFT)) {
            sprite.setPosition(sprite.getX()-5.0f, sprite.getY());
        } else if (idm.keyPressed(SDLK_RIGHT)) {
            sprite.setPosition(sprite.getX()+5.0f, sprite.getY());
        } else if (idm.keyPressed(SDLK_DOWN)) {
            sprite.setPosition(sprite.getX(), sprite.getY()-5.0f);
        } else if (idm.keyPressed(SDLK_UP)) {
            sprite.setPosition(sprite.getX(), sprite.getY()+5.0f);
        } else if (idm.keyPressed(SDLK_SPACE)) {
            system.play(&snd2,false);
        } else if (idm.keyPressed(SDLK_o)) {
            sprite.setTint(sprite.getTint().x(), sprite.getTint().y(), sprite.getTint().z(), sprite.getTint().i() <= 0.9f ? sprite.getTint().i()+0.1f : 1.0f);
        } else if (idm.keyPressed(SDLK_l)) {
            sprite.setTint(sprite.getTint().x(), sprite.getTint().y(), sprite.getTint().z(), sprite.getTint().i() > 0.0f ? sprite.getTint().i()-0.1f : 0.0f);
        }
        nbFrame = nbFrame + 1.0f;
        if (t < System::currentTime()) {
            sprintf(title,"%.2f fps",nbFrame);
            canvas.setTitle(title);

            printf("%s\n",gluErrorString(glGetError()));
            nbFrame = 0.0f;
            t = System::currentTime()+1000;
        }
        System::sleep(16);
    }

    System::deinit();
    return 0;
}