Exemple #1
0
int kore(int argc, char** argv) {
	Kore::System::setName("Shader");
	Kore::System::setup();
	Kore::WindowOptions options;
	options.title = "Shader";
	options.width = 1024;
	options.height = 768;
	options.x = 100;
	options.y = 100;
	options.targetDisplay = -1;
	options.mode = WindowModeWindow;
	options.rendererOptions.depthBufferBits = 16;
	options.rendererOptions.stencilBufferBits = 8;
	options.rendererOptions.textureFormat = 0;
	options.rendererOptions.antialiasing = 0;
	Kore::System::initWindow(options);
	Kore::System::setCallback(update);

	FileReader vs("shader.vert");
	FileReader fs("shader.frag");
	vertexShader = new Graphics4::Shader(vs.readAll(), vs.size(), Graphics4::VertexShader);
	fragmentShader = new Graphics4::Shader(fs.readAll(), fs.size(), Graphics4::FragmentShader);
	Graphics4::VertexStructure structure;
	structure.add("pos", Graphics4::Float3VertexData);
	pipeline = new Graphics4::PipelineState();
	pipeline->inputLayout[0] = &structure;
	pipeline->inputLayout[1] = nullptr;
	pipeline->vertexShader = vertexShader;
	pipeline->fragmentShader = fragmentShader;
	pipeline->compile();

	vertices = new Graphics4::VertexBuffer(3, structure);
	float* v = vertices->lock();
	v[0] = -1;
	v[1] = -1;
	v[2] = 0.5;
	v[3] = 1;
	v[4] = -1;
	v[5] = 0.5;
	v[6] = -1;
	v[7] = 1;
	v[8] = 0.5;
	vertices->unlock();

	indices = new Graphics4::IndexBuffer(3);
	int* i = indices->lock();
	i[0] = 0;
	i[1] = 1;
	i[2] = 2;
	indices->unlock();

	Kore::System::start();

	return 0;
}
Exemple #2
0
void OpenGL::initWindowsGLContext(int window, int depthBufferBits, int stencilBufferBits) {
	const Kore::WindowData &data = Kore::Window::get(window)->_data;
	HWND windowHandle = data.handle;

#ifndef VR_RIFT
	PIXELFORMATDESCRIPTOR pfd = {sizeof(PIXELFORMATDESCRIPTOR),
	                             1,
	                             PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
	                             PFD_TYPE_RGBA,
	                             32,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             0,
	                             (BYTE)depthBufferBits,
	                             (BYTE)stencilBufferBits,
	                             0,
	                             PFD_MAIN_PLANE,
	                             0,
	                             0,
	                             0,
	                             0};

	windows[window].deviceContext = GetDC(windowHandle);
	GLuint pixelFormat = ChoosePixelFormat(windows[window].deviceContext, &pfd);
	SetPixelFormat(windows[window].deviceContext, pixelFormat, &pfd);
	HGLRC tempGlContext = wglCreateContext(windows[window].deviceContext);
	wglMakeCurrent(windows[window].deviceContext, tempGlContext);

	if (!glewInitialized) {
		glewInit();
		glewInitialized = true;
	}

	if (wglewIsSupported("WGL_ARB_create_context") == 1) {
		int attributes[] = {WGL_CONTEXT_MAJOR_VERSION_ARB,
		                    4,
		                    WGL_CONTEXT_MINOR_VERSION_ARB,
		                    2,
		                    WGL_CONTEXT_FLAGS_ARB,
		                    WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
		                    WGL_CONTEXT_PROFILE_MASK_ARB,
		                    WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
		                    0};

		windows[window].glContext = wglCreateContextAttribsARB(windows[window].deviceContext, windows[0].glContext, attributes);
		glCheckErrors();
		wglMakeCurrent(nullptr, nullptr);
		wglDeleteContext(tempGlContext);
		wglMakeCurrent(windows[window].deviceContext, windows[window].glContext);
		glCheckErrors();
	}
	else {
		windows[window].glContext = tempGlContext;
	}
#else
	deviceContexts[window] = GetDC(windowHandle);
	glContexts[window] = wglGetCurrentContext();
	if (!glewInitialized) {
		glewInit();
		glewInitialized = true;
	}
#endif

	if (window != 0) {
		wglShareLists(windows[0].glContext, windows[window].glContext);
		wglMakeCurrent(windows[0].deviceContext, windows[0].glContext);
		windows[window].renderTarget = new Graphics4::RenderTarget(data.manualWidth, data.manualHeight, depthBufferBits);
		if (windowVertexBuffer == nullptr) {
			wglMakeCurrent(windows[window].deviceContext, windows[window].glContext);
			Graphics4::VertexStructure structure;
			structure.add("pos", Graphics4::Float2VertexData);
			windowVertexBuffer = new Graphics4::VertexBuffer(4, structure);
			float* vertices = windowVertexBuffer->lock();
			vertices[0] = -1.0f;
			vertices[1] = -1.0f;
			vertices[2] = -1.0f;
			vertices[3] = 1.0f;
			vertices[4] = 1.0f;
			vertices[5] = 1.0f;
			vertices[6] = 1.0f;
			vertices[7] = -1.0f;
			windowVertexBuffer->unlock();

			windowIndexBuffer = new Graphics4::IndexBuffer(6);
			int* indices = windowIndexBuffer->lock();
			indices[0] = 0;
			indices[1] = 1;
			indices[2] = 2;
			indices[3] = 0;
			indices[4] = 2;
			indices[5] = 3;
			windowIndexBuffer->unlock();

			Graphics4::Shader* windowVertexShader = new Graphics4::Shader("#version 450\n"
			                                                              "in vec2 pos;\n"
			                                                              "out vec2 texCoord;\n"
			                                                              "void main() {\n"
			                                                              "gl_Position = vec4(pos, 0.5, 1.0);\n"
			                                                              "texCoord = (pos + 1.0) / 2.0;\n"
			                                                              "}\n",
			                                                              Graphics4::VertexShader);

			Graphics4::Shader* windowFragmentShader = new Graphics4::Shader("#version 450\n"
			                                                                "uniform sampler2D tex;\n"
			                                                                "in vec2 texCoord;\n"
			                                                                "out vec4 frag;\n"
			                                                                "void main() {\n"
			                                                                "frag = texture(tex, texCoord);\n"
			                                                                "}\n",
			                                                                Graphics4::FragmentShader);

			windowPipeline = new Graphics4::PipelineState();
			windowPipeline->inputLayout[0] = &structure;
			windowPipeline->inputLayout[1] = nullptr;
			windowPipeline->vertexShader = windowVertexShader;
			windowPipeline->fragmentShader = windowFragmentShader;
			windowPipeline->compile();

			wglMakeCurrent(windows[0].deviceContext, windows[0].glContext);
		}
	}
	wglMakeCurrent(windows[window].deviceContext, windows[window].glContext);
	glGetIntegerv(GL_FRAMEBUFFER_BINDING, &windows[window].framebuffer);
#ifdef KORE_IOS
	glGenVertexArraysOES(1, &arrayId[windowId]);
	glCheckErrors();
#elif !defined(KORE_ANDROID) && !defined(KORE_HTML5) && !defined(KORE_TIZEN) && !defined(KORE_PI)
	glGenVertexArrays(1, &windows[window].vertexArray);
	glCheckErrors();
#endif
	wglMakeCurrent(windows[0].deviceContext, windows[0].glContext);
	glBindVertexArray(windows[0].vertexArray);
	glCheckErrors();
}
int kore(int argc, char** argv) {
	Kore::System::setName("Tessellation");
	Kore::System::setup();
	Kore::WindowOptions options;
	options.title = "Tessellation";
	options.width = 1024;
	options.height = 768;
	options.x = 100;
	options.y = 100;
	options.targetDisplay = -1;
	options.mode = WindowModeWindow;
	options.rendererOptions.depthBufferBits = 16;
	options.rendererOptions.stencilBufferBits = 8;
	options.rendererOptions.textureFormat = 0;
	options.rendererOptions.antialiasing = 0;
	Kore::System::initWindow(options);
	Kore::System::setCallback(update);

	FileReader vs("test.vert");
	FileReader fs("test.frag");
	FileReader gs("test.geom");
	FileReader tese("test.tese");
	FileReader tesc("test.tesc");
	vertexShader = new Graphics4::Shader(vs.readAll(), vs.size(), Graphics4::VertexShader);
	fragmentShader = new Graphics4::Shader(fs.readAll(), fs.size(), Graphics4::FragmentShader);
	geometryShader = new Graphics4::Shader(gs.readAll(), gs.size(), Graphics4::GeometryShader);
	tessEvalShader = new Graphics4::Shader(tese.readAll(), tese.size(), Graphics4::TessellationEvaluationShader);
	tessControlShader = new Graphics4::Shader(tesc.readAll(), tesc.size(), Graphics4::TessellationControlShader);
	Graphics4::VertexStructure structure;
	structure.add("Position", Graphics4::Float3VertexData);
	pipeline = new Graphics4::PipelineState();
	pipeline->vertexShader = vertexShader;
	pipeline->fragmentShader = fragmentShader;
	pipeline->geometryShader = geometryShader;
	pipeline->tessellationEvaluationShader = tessEvalShader;
	pipeline->tessellationControlShader = tessControlShader;
    pipeline->inputLayout[0] = &structure;
    pipeline->inputLayout[1] = nullptr;
    pipeline->depthWrite = true;
    pipeline->depthMode = Graphics4::ZCompareMode::ZCompareLess;
	pipeline->compile();

	tessLevelInnerLocation = pipeline->getConstantLocation("TessLevelInner");
	tessLevelOuterLocation = pipeline->getConstantLocation("TessLevelOuter");
	lightPositionLocation = pipeline->getConstantLocation("LightPosition");
	projectionLocation = pipeline->getConstantLocation("Projection");
	modelviewLocation = pipeline->getConstantLocation("Modelview");
	normalMatrixLocation = pipeline->getConstantLocation("NormalMatrix");
	ambientMaterialLocation = pipeline->getConstantLocation("AmbientMaterial");
	diffuseMaterialLocation = pipeline->getConstantLocation("DiffuseMaterial");

	{
		vertices = new Graphics4::VertexBuffer(12, structure);
		float* data = vertices->lock();
		int i = 0;

		data[i++] = 0.000f; data[i++] = 0.000f; data[i++] = 1.000f;
		data[i++] = 0.894f; data[i++] = 0.000f; data[i++] = 0.447f;
		data[i++] = 0.276f; data[i++] = 0.851f; data[i++] = 0.447f;
		data[i++] = -0.724f; data[i++] = 0.526f; data[i++] = 0.447f;
		data[i++] = -0.724f; data[i++] = -0.526f; data[i++] = 0.447f;
		data[i++] = 0.276f; data[i++] = -0.851f; data[i++] = 0.447f;
		data[i++] = 0.724f; data[i++] = 0.526f; data[i++] = -0.447f;
		data[i++] = -0.276f; data[i++] = 0.851f; data[i++] = -0.447f;
		data[i++] = -0.894f; data[i++] = 0.000f; data[i++] = -0.447f;
		data[i++] = -0.276f; data[i++] = -0.851f; data[i++] = -0.447f;
		data[i++] = 0.724f; data[i++] = -0.526f; data[i++] = -0.447f;
		data[i++] = 0.000f; data[i++] = 0.000f; data[i++] = -1.000f;

		vertices->unlock();
	}

	{
		indices = new Graphics4::IndexBuffer(20 * 3);
		int i = 0;
		int* data = indices->lock();

		data[i++] = 2; data[i++] = 1; data[i++] = 0;
		data[i++] = 3; data[i++] = 2; data[i++] = 0;
		data[i++] = 4; data[i++] = 3; data[i++] = 0;
		data[i++] = 5; data[i++] = 4; data[i++] = 0;
		data[i++] = 1; data[i++] = 5; data[i++] = 0;
		data[i++] = 11; data[i++] = 6; data[i++] = 7;
		data[i++] = 11; data[i++] = 7; data[i++] = 8;
		data[i++] = 11; data[i++] = 8; data[i++] = 9;
		data[i++] = 11; data[i++] = 9; data[i++] = 10;
		data[i++] = 11; data[i++] = 10; data[i++] = 6;
		data[i++] = 1; data[i++] = 2; data[i++] = 6;
		data[i++] = 2; data[i++] = 3; data[i++] = 7;
		data[i++] = 3; data[i++] = 4; data[i++] = 8;
		data[i++] = 4; data[i++] = 5; data[i++] = 9;
		data[i++] = 5; data[i++] = 1; data[i++] = 10;
		data[i++] = 2; data[i++] = 7; data[i++] = 6;
		data[i++] = 3; data[i++] = 8; data[i++] = 7;
		data[i++] = 4; data[i++] = 9; data[i++] = 8;
		data[i++] = 5; data[i++] = 10; data[i++] = 9;
		data[i++] = 1; data[i++] = 6; data[i++] = 10;

		indices->unlock();
	}

	Kore::System::start();

	return 0;
}