Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define GL_GLEXT_PROTOTYPES
- #include <GLFW/glfw3.h>
- #define GL_CHECK_ERROR() \
- do { \
- GLint error = glGetError(); \
- if (error != GL_NO_ERROR) \
- dprintf(2, "GL error %#x at line %d.\n", error, __LINE__ - 1); \
- } while (0)
- static const char* vertexShaderSource =
- "#version 410 core\n"
- "in vec2 vertex;"
- "in vec2 texcoords;"
- "invariant gl_Position;"
- "out vec2 Texcoords;"
- "void main()"
- "{"
- " gl_Position = vec4(vertex, 0.0, 1.0);"
- " Texcoords = texcoords;"
- "}";
- static const char* fragmentShaderSource =
- "#version 410 core\n"
- "in vec2 Texcoords;"
- "uniform sampler2D tex;"
- "out vec4 color;"
- "void main()"
- "{"
- //" color = vec4(vec2(textureSize(tex, 0)) / 2.0f, 0.0f, 1.0f);"
- " color = texture(tex, Texcoords);"
- //" color = vec4(texture(tex, Texcoords).xyz, 1.0);"
- //" color = vec4(Texcoords, 1.0, 1.0);"
- "}";
- void print_glfw_error(int errcode, const char* str)
- {
- dprintf(2, "[GLFW] Error %#x: %s\n", errcode, str);
- }
- int main()
- {
- const GLfloat vertices[] = {
- /* Vertices */ /* Texture coords */
- -1.0f, -1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, 0.0f, 1.0f
- };
- const GLubyte pixels[] = {
- 255, 255, 255, 255, 0, 0, 0, 255,
- 0, 0, 0, 255, 255, 255, 255, 255,
- };
- char buffer[65536] = {0};
- glfwSetErrorCallback(print_glfw_error);
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(800, 800, "GLFW", NULL, NULL);
- glfwMakeContextCurrent(window);
- GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- GL_CHECK_ERROR();
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- GL_CHECK_ERROR();
- glCompileShader(vertexShader);
- GL_CHECK_ERROR();
- glGetShaderInfoLog(vertexShader, sizeof(buffer), NULL, buffer);
- GL_CHECK_ERROR();
- dprintf(2, "Vertex shader log:\n%s", buffer);
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- GL_CHECK_ERROR();
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- GL_CHECK_ERROR();
- glCompileShader(fragmentShader);
- GL_CHECK_ERROR();
- glGetShaderInfoLog(fragmentShader, sizeof(buffer), NULL, buffer);
- GL_CHECK_ERROR();
- dprintf(2, "Fragment shader log:\n%s", buffer);
- GLuint shaderProgram = glCreateProgram();
- GL_CHECK_ERROR();
- glAttachShader(shaderProgram, vertexShader);
- GL_CHECK_ERROR();
- glAttachShader(shaderProgram, fragmentShader);
- GL_CHECK_ERROR();
- glLinkProgram(shaderProgram);
- GL_CHECK_ERROR();
- glGetProgramInfoLog(shaderProgram, sizeof(buffer), NULL, buffer);
- GL_CHECK_ERROR();
- dprintf(2, "Shader program log:\n%s", buffer);
- GLuint vertices_location = glGetAttribLocation(shaderProgram, "vertex");
- GL_CHECK_ERROR();
- GLuint texcoords_location = glGetAttribLocation(shaderProgram, "texcoords");
- GL_CHECK_ERROR();
- GLuint texture_location = glGetUniformLocation(shaderProgram, "tex");
- GL_CHECK_ERROR();
- GLuint vba;
- GLuint vertices_buffer;
- GLuint texture_buffer;
- GLuint texture;
- glGenVertexArrays(1, &vba);
- GL_CHECK_ERROR();
- glBindVertexArray(vba);
- GL_CHECK_ERROR();
- glGenBuffers(1, &vertices_buffer);
- GL_CHECK_ERROR();
- glGenTextures(1, &texture);
- GL_CHECK_ERROR();
- glActiveTexture(GL_TEXTURE0);
- GL_CHECK_ERROR();
- glBindTexture(GL_TEXTURE_2D, texture);
- GL_CHECK_ERROR();
- glGenBuffers(1, &texture_buffer);
- GL_CHECK_ERROR();
- glBindBuffer(GL_PIXEL_UNPACK_BUFFER, texture_buffer);
- GL_CHECK_ERROR();
- glBufferData(GL_PIXEL_UNPACK_BUFFER, sizeof(pixels), pixels, GL_STATIC_DRAW);
- GL_CHECK_ERROR();
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)0);
- GL_CHECK_ERROR();
- while (!glfwWindowShouldClose(window))
- {
- glClear(GL_COLOR_BUFFER_BIT);
- GL_CHECK_ERROR();
- glUseProgram(shaderProgram);
- GL_CHECK_ERROR();
- glBindVertexArray(vba);
- GL_CHECK_ERROR();
- glBindBuffer(GL_ARRAY_BUFFER, vertices_buffer);
- GL_CHECK_ERROR();
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
- GL_CHECK_ERROR();
- glVertexAttribPointer(vertices_location, 2, GL_FLOAT, GL_FALSE, 16, (GLvoid*)0);
- GL_CHECK_ERROR();
- glVertexAttribPointer(texcoords_location, 2, GL_FLOAT, GL_FALSE, 16, (GLvoid*)8);
- GL_CHECK_ERROR();
- glUniform1i(texture_location, 0);
- GL_CHECK_ERROR();
- glEnableVertexAttribArray(vertices_location);
- GL_CHECK_ERROR();
- glEnableVertexAttribArray(texcoords_location);
- GL_CHECK_ERROR();
- glActiveTexture(GL_TEXTURE0);
- GL_CHECK_ERROR();
- glBindTexture(GL_TEXTURE_2D, texture);
- GL_CHECK_ERROR();
- glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
- GL_CHECK_ERROR();
- glDisableVertexAttribArray(texcoords_location);
- GL_CHECK_ERROR();
- glDisableVertexAttribArray(vertices_location);
- GL_CHECK_ERROR();
- glfwSwapBuffers(window);
- glfwWaitEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement