Exemple #1
0
int PCRender::Init(SWindow *win) {
    GLint vpos_loc, tbuf_loc;
    m_sWindow = win;

    if (m_iWidth * m_iHeight > 0) {
        glfwSetWindowSize(m_sWindow, m_iWidth, m_iHeight);
        GLFWvidmode *mode = (GLFWvidmode*)glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(m_sWindow, (mode->width-m_iWidth)/2, (mode->height-m_iHeight)/2);
    }

    glfwMakeContextCurrent(m_sWindow);
    gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

    glGenBuffers(1, &m_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    m_shaderProgram = MakeShaderProgram(vs, fs);
    if (m_shaderProgram == 0u) {
        return -1;
    }

    vpos_loc = glGetAttribLocation(m_shaderProgram , "vertexIn");
    glEnableVertexAttribArray(vpos_loc);
    glVertexAttribPointer(vpos_loc, 2, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 4, (void*) 0);

    tbuf_loc = glGetAttribLocation(m_shaderProgram , "textureIn");
    glEnableVertexAttribArray(tbuf_loc);
    glVertexAttribPointer(tbuf_loc, 2, GL_FLOAT, GL_FALSE,
                          sizeof(float) * 4, (void*) (sizeof(float) * 2));

    //Init Texture
    glGenTextures(3, m_textures);
    glBindTexture(GL_TEXTURE_2D, m_textures[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, m_textures[1]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, m_textures[2]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glfwMakeContextCurrent(NULL);

    return 0;
}
void LoadTextureDrawHelper(
	TEXTURE_DRAW_HELPER* helper,
	const float base_texture_coord[8],
	char* utf8_vertex_shader_file_path,
	char* utf8_fragment_shader_file_path,
	APPLICATION* application
)
{
	char *system_path;

	MakeShaderProgram(&helper->program.base_data);

	system_path = g_locale_from_utf8(utf8_vertex_shader_file_path, -1, NULL, NULL, NULL);
	LoadTextureDrawHelperProgram(&helper->program, system_path, GL_VERTEX_SHADER);
	g_free(system_path);

	system_path = g_locale_from_utf8(utf8_fragment_shader_file_path, -1, NULL, NULL, NULL);
	LoadTextureDrawHelperProgram(&helper->program, system_path, GL_FRAGMENT_SHADER);
	g_free(system_path);

	if(TextureDrawHelperProgramLink(&helper->program) != FALSE)
	{
		float positions[8] = {0, 0, 1, 0, 1, -1, 0, -1};
		MakeVertexBundle(helper->bundle, VERTEX_BUNDLE_VERTEX_BUFFER, TEXTURE_DRAW_HELPER_PROGRAM_VERTEX_TYPE_POSITION,
			GL_DYNAMIC_DRAW, positions, sizeof(positions));
		(void)memcpy(positions, base_texture_coord, sizeof(positions));
		MakeVertexBundle(helper->bundle, VERTEX_BUNDLE_VERTEX_BUFFER, TEXTURE_DRAW_HELPER_PROGRAM_VERTEX_TYPE_TEXTURE_COORD,
			GL_STATIC_DRAW, positions, sizeof(positions));
		(void)MakeVertexBundleLayout(helper->layout);
		(void)VertexBundleLayoutBind(helper->layout);
		TextureDrawHelperBindVertexBundle(helper, FALSE);
		glEnableVertexAttribArray(TEXTURE_DRAW_HELPER_PROGRAM_VERTEX_TYPE_POSITION);
		glEnableVertexAttribArray(TEXTURE_DRAW_HELPER_PROGRAM_VERTEX_TYPE_TEXTURE_COORD);
		TextureDrawHelperUnbindVertexBundle(helper, FALSE);
		(void)VertexBundleLayoutUnbind(helper->layout);
		VertexBundleUnbind(VERTEX_BUNDLE_VERTEX_BUFFER);
	}
}