Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtx/multiple.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <vector>
- #include <cstdio>
- #define GLSL(src) "#version 430 core\n" #src
- void APIENTRY debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* msg, const void* data)
- {
- printf("OpenGL debug: %s\n\n", msg);
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- void init_debug()
- {
- glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
- glDebugMessageCallback(debug_callback, nullptr);
- glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
- glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER, GL_DEBUG_SEVERITY_LOW, 0, nullptr, GL_FALSE);
- const char debug_message[] = "Debug output enabled.";
- glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, sizeof(debug_message), debug_message);
- }
- GLuint create_program(const char *vertex_source, const char *fragment_source)
- {
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vertex_source, nullptr);
- glCompileShader(vs);
- GLint status;
- glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
- if (status == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<char> errorLog(maxLength);
- glGetShaderInfoLog(vs, maxLength, &maxLength, &errorLog[0]);
- printf(&errorLog[0]);
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fragment_source, nullptr);
- glCompileShader(fs);
- glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
- if (status == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<char> errorLog(maxLength);
- glGetShaderInfoLog(fs, maxLength, &maxLength, &errorLog[0]);
- printf(&errorLog[0]);
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- GLuint shader_program = glCreateProgram();
- glAttachShader(shader_program, fs);
- glAttachShader(shader_program, vs);
- glLinkProgram(shader_program);
- glGetProgramiv(shader_program, GL_LINK_STATUS, &status);
- if (status == GL_FALSE)
- {
- GLint maxLength = 0;
- glGetProgramiv(shader_program, GL_INFO_LOG_LENGTH, &maxLength);
- std::vector<char> errorLog(maxLength);
- glGetProgramInfoLog(shader_program, maxLength, &maxLength, &errorLog[0]);
- printf(&errorLog[0]);
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- return shader_program;
- }
- enum buffer_type
- {
- BUFFER_INDEX,
- BUFFER_POSITION,
- BUFFER_TEXCOORD,
- BUFFER_MAX
- };
- enum texture_type
- {
- TEXTURE_INDEX,
- TEXTURE_POSITION,
- TEXTURE_TEXCOORD,
- TEXTURE_DIFUSE,
- TEXTURE_MAX
- };
- const int indexBufferData[] = {
- 4, 0, 5, 1, 1, 2,
- 5, 0, 6, 1, 2, 2,
- 6, 0, 7, 1, 3, 2,
- 7, 0, 4, 1, 0, 2,
- 0, 0, 1, 1, 3, 3,
- 7, 0, 6, 1, 5, 2,
- 0, 3, 4, 0, 1, 2,
- 1, 3, 5, 0, 2, 2,
- 2, 3, 6, 0, 3, 2,
- 3, 3, 7, 0, 0, 2,
- 3, 3, 0, 0, 2, 2,
- 4, 3, 7, 0, 5, 2
- };
- const float positionBufferData[] = {
- -1.0f, -1.0f, 1.0f,
- -1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, -1.0f,
- 1.0f, -1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, -1.0f,
- 1.0f, 1.0f, 1.0f
- };
- const float texcoordBufferData[] = {
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f
- };
- const char* vertex_shader = GLSL(
- layout(location = 0) uniform isamplerBuffer indexBuffer;
- layout(location = 1) uniform samplerBuffer positionBuffer;
- layout(location = 2) uniform samplerBuffer texcoordBuffer;
- layout(location = 4) uniform mat4 MVP;
- out vec2 texcoord;
- void main()
- {
- ivec2 index = texelFetch(indexBuffer, gl_VertexID).xy;
- vec3 position = texelFetch(positionBuffer, index.x).xyz;
- texcoord = texelFetch(texcoordBuffer, index.y).xy;
- gl_Position = MVP * vec4(position, 1.0);
- }
- );
- const char* fragment_shader = GLSL(
- in vec2 texcoord;
- out vec4 frag_color;
- layout(location = 3) uniform sampler2D texture0;
- void main()
- {
- vec3 diffuse = texture(texture0, texcoord).xyz;
- frag_color = vec4(diffuse, 1.0);
- }
- );
- int main()
- {
- if(!glfwInit())
- exit(EXIT_FAILURE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- glfwWindowHint(GLFW_SRGB_CAPABLE, GL_TRUE);
- glfwWindowHint(GLFW_SAMPLES, 16);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
- GLFWwindow* window = glfwCreateWindow(768, 768, "OpenGL", NULL, NULL);
- if(!window)
- {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwSetKeyCallback(window, key_callback);
- glfwMakeContextCurrent(window);
- glfwSwapInterval(-1);
- glewExperimental = GL_TRUE;
- GLenum glewError = glewInit();
- if(glewError != GLEW_OK)
- {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- printf("OpenGL Version: %s\n\n", glGetString(GL_VERSION));
- init_debug();
- GLuint vao = 0;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- GLint TextureBufferOffsetAlignment = 0;
- glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &TextureBufferOffsetAlignment);
- int IndexMultiple = glm::higherMultiple(int(sizeof(indexBufferData)), int(TextureBufferOffsetAlignment));
- int PositionMultiple = glm::higherMultiple(int(sizeof(positionBufferData)), int(TextureBufferOffsetAlignment));
- int TexcoordMultiple = glm::higherMultiple(int(sizeof(texcoordBufferData)), int(TextureBufferOffsetAlignment));
- GLuint BufferName[BUFFER_MAX] = {0};
- glGenBuffers(BUFFER_MAX, BufferName);
- glNamedBufferDataEXT(BufferName[BUFFER_INDEX], TextureBufferOffsetAlignment + IndexMultiple, nullptr, GL_STATIC_DRAW);
- glNamedBufferSubDataEXT(BufferName[BUFFER_INDEX], TextureBufferOffsetAlignment, sizeof(indexBufferData), indexBufferData);
- glNamedBufferDataEXT(BufferName[BUFFER_POSITION], TextureBufferOffsetAlignment + PositionMultiple, nullptr, GL_STATIC_DRAW);
- glNamedBufferSubDataEXT(BufferName[BUFFER_POSITION], TextureBufferOffsetAlignment, sizeof(positionBufferData), positionBufferData);
- glNamedBufferDataEXT(BufferName[BUFFER_TEXCOORD], TextureBufferOffsetAlignment + TexcoordMultiple, nullptr, GL_STATIC_DRAW);
- glNamedBufferSubDataEXT(BufferName[BUFFER_TEXCOORD], TextureBufferOffsetAlignment, sizeof(texcoordBufferData), texcoordBufferData);
- GLuint TextureName[TEXTURE_MAX] = {0};
- glGenTextures(TEXTURE_MAX, TextureName);
- glTextureBufferRangeEXT(TextureName[TEXTURE_INDEX], GL_TEXTURE_BUFFER, GL_RG32I, BufferName[BUFFER_INDEX], TextureBufferOffsetAlignment, sizeof(indexBufferData));
- glBindMultiTextureEXT(GL_TEXTURE0, GL_TEXTURE_BUFFER, TextureName[TEXTURE_INDEX]);
- glTextureBufferRangeEXT(TextureName[TEXTURE_POSITION], GL_TEXTURE_BUFFER, GL_RGB32F, BufferName[BUFFER_POSITION], TextureBufferOffsetAlignment, sizeof(positionBufferData));
- glBindMultiTextureEXT(GL_TEXTURE1, GL_TEXTURE_BUFFER, TextureName[TEXTURE_POSITION]);
- glTextureBufferRangeEXT(TextureName[TEXTURE_TEXCOORD], GL_TEXTURE_BUFFER, GL_RG32F, BufferName[BUFFER_TEXCOORD], TextureBufferOffsetAlignment, sizeof(texcoordBufferData));
- glBindMultiTextureEXT(GL_TEXTURE2, GL_TEXTURE_BUFFER, TextureName[TEXTURE_TEXCOORD]);
- unsigned char* image_buffer = new unsigned char[512*512*3];
- FILE* image_file = fopen("box_texture.raw", "rb");
- fread(image_buffer, 3, 512*512, image_file);
- fclose(image_file);
- glTextureStorage2DEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, 10, GL_SRGB8, 512, 512);
- glTextureSubImage2DEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, 0, 0, 0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, image_buffer);
- delete[] image_buffer;
- glGenerateTextureMipmapEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D);
- glBindMultiTextureEXT(GL_TEXTURE3, GL_TEXTURE_2D, TextureName[TEXTURE_DIFUSE]);
- glTextureParameteriEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTextureParameteriEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTextureParameteriEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTextureParameteriEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- GLfloat maxAniso = 1.0f;
- glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso);
- glTextureParameterfEXT(TextureName[TEXTURE_DIFUSE], GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso);
- GLuint shader_program = create_program(vertex_shader, fragment_shader);
- glUseProgram(shader_program);
- glProgramUniform1i(shader_program, 0, 0);
- glProgramUniform1i(shader_program, 1, 1);
- glProgramUniform1i(shader_program, 2, 2);
- glProgramUniform1i(shader_program, 3, 3);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_FRAMEBUFFER_SRGB);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glm::mat4 view = glm::lookAt(
- glm::vec3(0.0f, 2.0f, 6.0f),
- glm::vec3(0.0f, 0.0f, 0.0f),
- glm::vec3(0.0f, 1.0f, 0.0f)
- );
- glm::mat4 proj = glm::perspective(45.0f, 1.0f, 0.2f, 10.0f);
- double lastTime = glfwGetTime();
- int nbFrames = 0;
- while(!glfwWindowShouldClose(window))
- {
- double currentTime = glfwGetTime();
- nbFrames++;
- if (currentTime - lastTime >= 1.0)
- {
- char buff[128];
- sprintf(buff, "OpenGL [%.1f fps, %.3f ms]", double(nbFrames), 1000.0 / double(nbFrames));
- glfwSetWindowTitle(window, buff);
- nbFrames = 0;
- lastTime += 1.0;
- }
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glm::mat4 model = glm::rotate(glm::mat4(1.0f), (float)currentTime * 45.0f, glm::vec3(0, 1, 0));
- glm::mat4 MVP = proj * view * model;
- glProgramUniformMatrix4fv(shader_program, 4, 1, GL_FALSE, glm::value_ptr(MVP));
- glDrawArrays(GL_TRIANGLES, 0, 36);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement