Пример #1
0
int main(int /*argc*/, char ** /*argv*/) {
	BaseApp app;
	GLuint program;
	GLuint vao;
	GLuint vbo;
	GLuint ebo;

	mat4 p;
	int z = 4;
	auto mainWindow = app.getMainWindow();

	std::string prefix = app.getResourceDir() + "Shaders/Examples/e04_Textures/";
	std::string texPrefix = app.getResourceDir() + "Textures/PGP2015/";

	GLuint texture[9];

	app.addInitCallback([&]() {
		auto vs = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "empty.vert"));
		auto gs = compileShader(GL_GEOMETRY_SHADER, Loader::text(prefix + "quad.geo"));
		auto fs = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "texture.frag"));
		program = createProgram(vs, gs, fs);

		glCreateVertexArrays(1, &vao);
		glBindVertexArray(vao);
		texture[0] = Loader::texture(texPrefix + "up.bmp");
		texture[1] = Loader::texture(texPrefix + "down.bmp");
		texture[2] = Loader::texture(texPrefix + "front.bmp");
		texture[3] = Loader::texture(texPrefix + "back.bmp");
		texture[4] = Loader::texture(texPrefix + "left.bmp");
		texture[5] = Loader::texture(texPrefix + "right.bmp");
		texture[6] = Loader::texture(texPrefix + "du2.bmp");
		texture[7] = Loader::texture(texPrefix + "du05a.bmp");
		texture[8] = Loader::texture(texPrefix + "du05b.bmp");

	});

	app.addDrawCallback([&]() {
		int w = mainWindow->getWidth();
		int h = mainWindow->getHeight();
		glViewport(0, 0, w, h); 
		glClearColor(0.2, 0.2, 0.2, 1);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glEnable(GL_DEPTH_TEST);
		glUseProgram(program);

		for (int y = 0; y < 3; y++){
			for (int x = 0; x < 3; x++) {
				glUniform2f(glGetUniformLocation(program, "posMin"), -1 + x*(2.0 / 3), -1 + y*(2.0 / 3));
				glUniform2f(glGetUniformLocation(program, "posMax"), -1 + (x + 1)*(2.0 / 3), -1 + (y + 1)*(2.0 / 3));
				glBindTextureUnit(0, texture[y * 3 + x]);
				glDrawArrays(GL_POINTS, 0, 1);
			}
		}

	});
	return app.run();
}
Пример #2
0
int main(int /*argc*/, char ** /*argv*/) {
  BaseApp app;
  ProgramObject program;

  auto mainWindow = app.getMainWindow();

  std::string prefix = app.getResourceDir() + "Shaders/Examples/e06_ModelLoader/";

  PerspectiveCamera cam;
  OrbitManipulator manipulator(&cam);
  manipulator.setupCallbacks(app);
  NodeShared root;

  GLuint query[2];

  
  app.addInitCallback([&]() {
    auto vs = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "phong.vert"));
    auto fs = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "phong.frag"));
    program = createProgram(vs, fs);

    root = Loader::scene(app.getResourceDir() + "Models/sponza/sponza.fbx");
    glCreateQueries(GL_TIMESTAMP, 2, query);
    SDL_GL_SetSwapInterval(0);
  });

  app.addResizeCallback([&](int w, int h) {
    glViewport(0, 0, w, h);
    cam.setAspect(float(w) / float(h));
  });

  app.addDrawCallback([&]() {


    glQueryCounter(query[0], GL_TIMESTAMP);
    glClearColor(0.2, 0.2, 0.2, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    //bunny

    program.use();
    program.setMatrix4fv("p", value_ptr(cam.getProjection()));
    program.setMatrix4fv("v", value_ptr(cam.getView()));

    drawNode(program, root);
    glQueryCounter(query[1], GL_TIMESTAMP);

    GLuint64 time1, time2;
    glGetQueryObjectui64v(query[0], GL_QUERY_RESULT, &time1);
    glGetQueryObjectui64v(query[1], GL_QUERY_RESULT, &time2);

    std::string s = "fps: " + std::to_string(1e9 / (time2 - time1)) + " (" + std::to_string(ImGui::GetIO().Framerate) + ")";
    label(s, 0, 0, 300, 100);
  });
  return app.run();
}
Пример #3
0
int main(int /*argc*/, char ** /*argv*/) {
	BaseApp app;
	ProgramObject programEnv, program;

	auto mainWindow = app.getMainWindow();

	PerspectiveCamera cam;
	OrbitManipulator manipulator(&cam);
	manipulator.setupCallbacks(app);
	manipulator.setZoom(3);
	
	GLuint diffuseTexture;
	GLuint specularTexture;

	GLuint vao;
	GLuint vaoEmpty;
	GLuint vbo;

	int32_t sphereSizeX = 20;
	int32_t sphereSizeY = 20;

	app.addInitCallback([&]() {
		std::string prefix = app.getResourceDir() + "Shaders/Tutorial/";
		auto vs = compileShader(GL_VERTEX_SHADER,
			"#version 450\n",
			Loader::text(prefix + "uv.vp"));
		auto fs = compileShader(GL_FRAGMENT_SHADER,
			"#version 450\n",
			Loader::text(prefix + "lighting.vp"),
			Loader::text(prefix + "uv.fp"));
		program = createProgram(vs, fs);
		
		std::string texPrefix = app.getResourceDir() + "Textures/Tutorial/";
		diffuseTexture = Loader::texture(texPrefix + "earth.png");
		specularTexture = Loader::texture(texPrefix + "earth_s.png");

		glCreateBuffers(1, &vbo);
		const uint32_t floatsPerVertex = 6;
		const uint32_t vertiesPerFace = 6;
		float*vertices = new float[sphereSizeX*sphereSizeY*vertiesPerFace*floatsPerVertex];
		for (int32_t y = 0; y<sphereSizeY; ++y) {
			for (int32_t x = 0; x<sphereSizeX; ++x) {
				for (uint32_t k = 0; k<vertiesPerFace; ++k) {
					const int32_t xOffset[] = { 0,1,0,0,1,1 };
					const int32_t yOffset[] = { 0,0,1,1,0,1 };
					float u = (float)(x + xOffset[k]) / sphereSizeX;
					float v = (float)(y + yOffset[k]) / sphereSizeY;
					float xAngle = -u*glm::two_pi<float>();
					float yAngle = v*glm::pi<float>();
					uint32_t faceId = y*sphereSizeX + x;
					uint32_t faceVertex = faceId*vertiesPerFace + k;
					vertices[faceVertex*floatsPerVertex + 0] = glm::cos(xAngle)*glm::sin(yAngle);
					vertices[faceVertex*floatsPerVertex + 1] = -glm::cos(yAngle);
					vertices[faceVertex*floatsPerVertex + 2] = glm::sin(xAngle)*glm::sin(yAngle);
					vertices[faceVertex*floatsPerVertex + 3] = 1;
					vertices[faceVertex*floatsPerVertex + 4] = u;
					vertices[faceVertex*floatsPerVertex + 5] = v;
				}
			}
		}
		glNamedBufferData(vbo, sizeof(float)*sphereSizeX*sphereSizeY*vertiesPerFace*floatsPerVertex, vertices, GL_STATIC_DRAW);
		delete[]vertices;

		glCreateVertexArrays(1, &vao);
		glEnableVertexArrayAttrib(vao, 0); 
		glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, 0, 0); 
		glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(float)*floatsPerVertex);
		glEnableVertexArrayAttrib(vao, 1);
		glVertexArrayAttribFormat(vao, 1, 2, GL_FLOAT, 0, 0);
		glVertexArrayVertexBuffer(vao, 1, vbo, sizeof(float) * 4, sizeof(float)*floatsPerVertex);
		
		glClearColor(0, 0, 0, 1);
		glDisable(GL_CULL_FACE);
		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LEQUAL);
		glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
	});

	app.addResizeCallback([&](int w, int h) {
		glViewport(0, 0, w, h);
		cam.setAspect(float(w) / float(h));
	});

	app.addDrawCallback([&]() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		program.use();
		glBindTextureUnit(0, diffuseTexture);
		glBindTextureUnit(1, specularTexture);

		program.setMatrix4fv("p", value_ptr(cam.getProjection()));
		program.setMatrix4fv("v", value_ptr(cam.getView()));

		glBindVertexArray(vao);
		glDrawArrays(GL_TRIANGLES, 0, sphereSizeX*sphereSizeY * 6);
		glBindVertexArray(0);
	});
	return app.run();
}
Пример #4
0
int main(int /*argc*/, char ** /*argv*/) {
	BaseApp app;	
	GLuint program;
	GLuint vao;
	GLuint vbo;
	GLuint ebo;

	mat4 p;
	int z = 4;
	auto window0 = app.getMainWindow();
	window0->setSize(200, 200);
	window0->move(100, 100);
	auto window1 = app.addWindow(200, 200);
	window1->move(100, 300);
	auto window2 = app.addWindow(200, 200);
	window2->move(300, 100);
	auto window3 = app.addWindow(200, 200);
	window3->move(300, 300);

	std::string prefix = app.getResourceDir() + "Shaders/Examples/e03_MultipleWindows/";

	app.addInitCallback([&]() {
		auto vs = compileShader(GL_VERTEX_SHADER, Loader::text(prefix+"lambert.vert"));
		auto fs = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "lambert.frag"));
		program = createProgram(vs, fs);	

		bunnyInit(vao, vbo, ebo);
	});

	app.addDrawCallback([&]() {
		glClearColor(0.2, 0.2, 0.2, 1);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glEnable(GL_DEPTH_TEST);
		glUseProgram(program);	

		int w = window0->getWidth();
		int h = window0->getHeight();
		glViewport(0, 0, w, h);
		p = perspective(radians(45.0f),float(w)/float(h), 0.1f, 1000.0f);
		glUniformMatrix4fv(glGetUniformLocation(program, "p"), 1, 0, value_ptr(p));
		int z = 4;
		mat4 v = lookAt(vec3(z,z,z), vec3(0, 0, 0), vec3(0, 1, 0));
		glUniformMatrix4fv(glGetUniformLocation(program, "v"), 1, 0, value_ptr(v));

		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);		
		
		bunnyDraw();
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	});

	app.addDrawCallback([&]() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		int w = window1->getWidth();
		int h = window1->getHeight();
		glViewport(0, 0, w, h);
		mat4 v = lookAt(vec3(z, 0, 0), vec3(0, 0, 0), vec3(0, 1, 0));
		glUniformMatrix4fv(glGetUniformLocation(program, "v"), 1, 0, value_ptr(v));
		bunnyDraw();
	}, window1);
	
	app.addDrawCallback([&]() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		int w = window2->getWidth();
		int h = window2->getHeight();
		glViewport(0, 0, w, h);
		mat4 v = lookAt(vec3(0, z, 0), vec3(0, 0, 0), vec3(1, 0, 0));
		glUniformMatrix4fv(glGetUniformLocation(program, "v"), 1, 0, value_ptr(v));
		bunnyDraw();
	}, window2);

	app.addDrawCallback([&]() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		int w = window3->getWidth();
		int h = window3->getHeight();
		glViewport(0, 0, w, h);
		mat4 v = lookAt(vec3(0, 0, z), vec3(0, 0, 0), vec3(0, 1, 0));
		glUniformMatrix4fv(glGetUniformLocation(program, "v"), 1, 0, value_ptr(v));
		bunnyDraw();
	}, window3);
	return app.run();
}
Пример #5
0
int main(int /*argc*/, char ** /*argv*/) {
  BaseApp app;
  ProgramObject programPlanet;
  ProgramObject programAtmosphere;
  ProgramObject programStars;


  auto mainWindow = app.getMainWindow();

  PerspectiveCamera cam;
  OrbitManipulator manipulator(&cam);
  manipulator.setupCallbacks(app);

  GLuint vao;
  GLuint vbo;

  bool wireframe = false;
  int starCount = 20000;
  int seed = 0;

  app.addInitCallback([&]() {
    string prefix = app.getResourceDir() + "shaders/Nei/n01_Planet/";
    auto vert = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "empty.vert"));
    auto tesc = compileShader(GL_TESS_CONTROL_SHADER, Loader::text(prefix + "planet.tesc"));
    auto tese = compileShader(GL_TESS_EVALUATION_SHADER, Loader::text(prefix + "planet.tese"));
    auto frag = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "planet.frag"));
    programPlanet = createProgram(vert, tesc, tese, frag);
    
    vert = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "empty.vert"));
    tesc = compileShader(GL_TESS_CONTROL_SHADER, Loader::text(prefix + "atmosphere.tesc"));
    tese = compileShader(GL_TESS_EVALUATION_SHADER, Loader::text(prefix + "atmosphere.tese"));
    frag = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "atmosphere.frag"));
    programAtmosphere = createProgram(vert, tesc, tese, frag);
    
    vert = compileShader(GL_VERTEX_SHADER, Loader::text(prefix + "stars.vert"));
    frag = compileShader(GL_FRAGMENT_SHADER, Loader::text(prefix + "stars.frag"));
    programStars = createProgram(vert, frag);

    glCreateVertexArrays(1, &vao);
    float distance = 400;

    vector<vec4> vec;
    vec.reserve(starCount);
    for (int i = 0; i<starCount; i++) {
      float a = ((float)rand() / RAND_MAX - 0.5);
      float b = ((float)rand() / RAND_MAX - 0.5);
      float c = ((float)rand() / RAND_MAX - 0.5);
      float d = (float)rand() / RAND_MAX * 5 + 1;

      vec4 v(a, b, c, 0);
      v = normalize(v);
      v *= distance;
      v.w = d;

      vec.push_back(v);
    }

    glCreateBuffers(1, &vbo);
    glNamedBufferData(vbo, sizeof(vec4)*vec.size(), vec.data(), GL_STATIC_DRAW);

    glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(vec4));
    glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, 0, 0);
    glEnableVertexArrayAttrib(vao, 0);
    glBindVertexArray(vao);
  });
  app.addUpdateCallback([&](float dt) {
    manipulator.update(dt);
  });
  app.addDrawCallback([&]() {
    int w = mainWindow->getWidth();
    int h = mainWindow->getHeight();
    glViewport(0, 0, w, h);

    glClearColor(0, 0, 0, 1);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL);

    programStars.use();
    programStars.setMatrix4fv("v", value_ptr(cam.getView()));
    programStars.setMatrix4fv("p", value_ptr(cam.getProjection()));

    glEnable(GL_PROGRAM_POINT_SIZE);    
    glDrawArrays(GL_POINTS, 0, starCount);

    
    programPlanet.use();
    programPlanet.setMatrix4fv("v", value_ptr(cam.getView()));
    programPlanet.setMatrix4fv("p", value_ptr(cam.getProjection()));
    programPlanet.set3fv("camPos", value_ptr(cam.getEye()));
    programPlanet.set1i("seed", seed);
    programPlanet.set1f("time", app.getTimeFromStart());

    glPatchParameteri(GL_PATCH_VERTICES, 1);
    glDrawArraysInstanced(GL_PATCHES, 0, 1, 16);
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    programAtmosphere.use();
    programAtmosphere.setMatrix4fv("v", value_ptr(cam.getView()));
    programAtmosphere.setMatrix4fv("p", value_ptr(cam.getProjection()));
    programAtmosphere.set3fv("camPos", value_ptr(cam.getEye()));
    programAtmosphere.set1ui("seed", seed);
    programAtmosphere.set1f("time", app.getTimeFromStart());

    glPatchParameteri(GL_PATCH_VERTICES, 1);
    glDrawArrays(GL_PATCHES, 0, 1);
  });
  return app.run();
}