Exemple #1
0
int main(int argc, char** argv)
{
    GLFWwindow* window;

    if (!glfwInit())
        return EXIT_FAILURE;

    glfwHints();
    window = glfwCreateWindow(
        g_windowWidth, g_windowHeight, "Dunjun", nullptr, nullptr);
    if (!window)
    {
        glfwTerminate();
        return EXIT_FAILURE;
    }

    glfwMakeContextCurrent(window);
    glewInit();

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    float vertices[] = {
    //      x,     y,     r,    g,   b
        +0.5f, +0.5f, 0.0f, 1.0f, 0.0f, //vertex 0
        -0.5f, +0.5f, 1.0f, 0.0f, 0.0f, //vertex 1
        +0.5f, -0.5f, 0.0f, 0.0f, 1.0f, //vertex 2
        -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, //vertex 3

    };

    GLuint vbo;  //vertex buffer object
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    Dunjun::ShaderProgram shaderProgram;
    shaderProgram.attachShaderFromFile(Dunjun::ShaderType::Vertex, "data/shaders/default.vert.glsl");
    shaderProgram.attachShaderFromFile(Dunjun::ShaderType::Fragment, "data/shaders/default.frag.glsl");

    shaderProgram.bindAttribLocation(0, "vertPosition");
    shaderProgram.bindAttribLocation(1, "vertColor");

    shaderProgram.link();
    shaderProgram.use();
    
    bool running    = true;
    bool fullscreen = false;
    while (running)
    {
        glClearColor(0.5f, 0.69f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        //Draw things
        {
            glEnableVertexAttribArray(0);
            glEnableVertexAttribArray(1);

            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const GLvoid*)(2*sizeof(float)));

            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
        }

        glfwSwapBuffers(window);
        glfwPollEvents();

        if (glfwWindowShouldClose(window) ||
            glfwGetKey(window, GLFW_KEY_ESCAPE))
            running = false;

        if (glfwGetKey(window, GLFW_KEY_F11))
        {
            fullscreen = !fullscreen;

            GLFWwindow* newWindow;
            glfwHints();
                if (fullscreen)
            {
                //int count;
                //const GLFWvidmode* modes =
                //    glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
                //for (int modeIndex = 0; modeIndex < count; ++modeIndex)
                //{
                //    std::cout << modeIndex << " - " << modes[modeIndex].width
                //              << "x" << modes[modeIndex].height << std::endl;
                //}
                newWindow = glfwCreateWindow(
                    1920, 1080, "Dunjun", glfwGetPrimaryMonitor(), window);
            }
            else
            {
                newWindow = glfwCreateWindow(
                    g_windowWidth, g_windowHeight, "Dunjun", nullptr, window);
            }
            glfwDestroyWindow(window);
            window = newWindow;
            glfwMakeContextCurrent(window);
        }
    }

    glfwDestroyWindow(window);

    glfwTerminate();
    return 0;
}
Exemple #2
0
int main(int argc, char** argv)
{
	if (!glewInit())
		return EXIT_FAILURE;


	GLFWwindow* window;

	/* Initialize the library */
	if (!glfwInit())
		return EXIT_FAILURE;

	/* Create a windowed mode window and its OpenGL context */
	glfwHints();
	window = glfwCreateWindow(
	    g_windowWidth, g_windowHeight, "Dunjun", nullptr, nullptr);
	if (!window)
	{
		glfwTerminate();
		return EXIT_FAILURE;
	}

	/* Make the window's context current */
	glfwMakeContextCurrent(window);

	glewInit();

	float vertices[] = {
		+0.0f, +0.5f, //Vertex 1
		-0.5f, -0.5f, //Vertex 2
		+0.5f, -0.5f, //Vertex 3
	};

	GLuint vbo; // Vertex Buffer Object
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	const char* vertexShaderText = {
		"#version 120\n"
		"\n"
		"attribute vec2 position;"
		"void main()"
		"{"
		"	gl_Position = vec4(position, 0.0, 1.0);"
		"}"
	};

	const char* fragmentShaderText = {
		"#version 120\n"
		"\n"
		"void main()"
		"{"
		"	gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
		"}"
	};

	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexShaderText, nullptr);
	glCompileShader(vertexShader);

	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentShaderText, nullptr);
	glCompileShader(fragmentShader);

	GLuint shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);

	glBindAttribLocation(shaderProgram, 0, "position");

	glLinkProgram(shaderProgram);

	glUseProgram(shaderProgram);

	bool running = true;
	bool fullscreen = false;
	/* Loop until the user closes the window */
	while (running)
	{
		/* Render here */
		glClearColor(0.5f, 0.69f, 1.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		/* Draw Things*/
		{
			glEnableVertexAttribArray(0);

			glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);

			glDrawArrays(GL_TRIANGLES, 0, 3);

			glDisableVertexAttribArray(0);
		}

		/* Swap front and back buffers */
		glfwSwapBuffers(window);

		/* Poll for and process events */
		glfwPollEvents();

		if (glfwWindowShouldClose(window) ||
		    glfwGetKey(window, GLFW_KEY_ESCAPE))
			running = false;

		/*if (glfwGetKey(window, GLFW_KEY_F11))
		{
			fullscreen = !fullscreen;

			GLFWwindow* newWindow;
			glfwHints();
			if (fullscreen) 
			{
				int count;
				const GLFWvidmode* modes =
				    glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);

				newWindow = glfwCreateWindow(modes[count - 1].width,
				                             modes[count - 1].height,
				                             "Dunjun",
				                             glfwGetPrimaryMonitor(),
				                             window);
			}
			else
			{
				newWindow = glfwCreateWindow(
				    g_windowWidth, g_windowHeight, "Dunjun", nullptr, window);
			}

			glfwDestroyWindow(window);
			window = newWindow;
			glfwMakeContextCurrent(window);
		}*/
	}

	glfwDestroyWindow(window);
	glfwTerminate();
	return EXIT_SUCCESS;
}