Created
December 6, 2019 08:47
-
-
Save StillManic/57fea7e6492fa666c3989d987b1f8f20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <GL/glew.h> | |
#include <GLFW/glfw3.h> | |
GLFWwindow* window; | |
#include <glm/glm.hpp> | |
#include <glm/gtx/transform.hpp> | |
#include <glm/gtc/matrix_transform.hpp> | |
using namespace glm; | |
#include <common/shader.hpp> | |
#include <common/texture.hpp> | |
#include <common/controls.hpp> | |
int main(void) { | |
// ----- INITIALIZE GLFW ----- | |
if(!glfwInit()) { | |
fprintf(stderr, "Failed to initialize GLFW\n"); | |
getchar(); | |
return -1; | |
} | |
// ----- SET UP WINDOW ----- | |
// 4x antialiasing | |
glfwWindowHint(GLFW_SAMPLES, 4); | |
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE); | |
// These set OpenGL to version 3.3 | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
// To make MacOS happy; should not be needed | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | |
// We don't want the old OpenGL | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
// ----- OPEN WINDOW AND CREATE ITS OPENGL CONTEXT ----- | |
window = glfwCreateWindow(1024, 768, "Playground", NULL, NULL); | |
if (window == NULL) { | |
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); | |
getchar(); | |
glfwTerminate(); | |
return -1; | |
} | |
glfwMakeContextCurrent(window); | |
// ----- INITIALIZE GLEW ----- | |
glewExperimental = true; // Needed for core profile | |
if (glewInit() != GLEW_OK) { | |
fprintf(stderr, "Failed to initialize GLEW\n"); | |
getchar(); | |
glfwTerminate(); | |
return -1; | |
} | |
// ----- ENSURE CAPTURE OF ESCAPE KEY PRESS ----- | |
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); | |
// ----- BACKGROUND COLOR (DARK BLUE) ----- | |
glClearColor(0.0f, 0.0f, 0.4f, 0.0f); | |
// ----- DEPTH ----- | |
glEnable(GL_DEPTH_TEST); | |
// Accept fragment if it is closer to the camera than the former one | |
glDepthFunc(GL_LESS); | |
// ----- BACKFACE CULLING ----- | |
glEnable(GL_CULL_FACE); | |
// ----- VERTEX ARRAY ----- | |
GLuint VertexArrayID; | |
glGenVertexArrays(1, &VertexArrayID); | |
glBindVertexArray(VertexArrayID); | |
// ----- LOAD SHADERS (LoadShaders() in "common/shader.cpp") ----- | |
GLuint programID = LoadShaders("SimpleVertexShader.glsl", "SimpleFragmentShader.glsl"); | |
// ----- GET HANDLE FOR "MVP" UNIFORM ----- | |
GLuint MatrixID = glGetUniformLocation(programID, "MVP"); | |
// ----- TEXTURES ----- | |
// Load textures using any two methods | |
//GLuint Texture = loadBMP_custom("uvtemplate.bmp"); | |
GLuint Texture = loadDDS("uvtemplate.DDS"); | |
// Get a handle for our "myTextureSampler" uniform | |
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler"); | |
// ----- VERTEX BUFFER ----- | |
static const GLfloat g_vertex_buffer_data[] = { | |
-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, | |
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, | |
-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, | |
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, | |
-1.0f, 1.0f, 1.0f, | |
1.0f, 1.0f, 1.0f, | |
-1.0f, 1.0f, 1.0f, | |
1.0f,-1.0f, 1.0f | |
}; | |
GLuint vertexBuffer; | |
glGenBuffers(1, &vertexBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); | |
// ----- UV COORDINATES ----- | |
// Two UV coordinatesfor each vertex. They were created with Blender. | |
static const GLfloat g_uv_buffer_data[] = { | |
0.000059f, 1.0f-0.000004f, | |
0.000103f, 1.0f-0.336048f, | |
0.335973f, 1.0f-0.335903f, | |
1.000023f, 1.0f-0.000013f, | |
0.667979f, 1.0f-0.335851f, | |
0.999958f, 1.0f-0.336064f, | |
0.667979f, 1.0f-0.335851f, | |
0.336024f, 1.0f-0.671877f, | |
0.667969f, 1.0f-0.671889f, | |
1.000023f, 1.0f-0.000013f, | |
0.668104f, 1.0f-0.000013f, | |
0.667979f, 1.0f-0.335851f, | |
0.000059f, 1.0f-0.000004f, | |
0.335973f, 1.0f-0.335903f, | |
0.336098f, 1.0f-0.000071f, | |
0.667979f, 1.0f-0.335851f, | |
0.335973f, 1.0f-0.335903f, | |
0.336024f, 1.0f-0.671877f, | |
1.000004f, 1.0f-0.671847f, | |
0.999958f, 1.0f-0.336064f, | |
0.667979f, 1.0f-0.335851f, | |
0.668104f, 1.0f-0.000013f, | |
0.335973f, 1.0f-0.335903f, | |
0.667979f, 1.0f-0.335851f, | |
0.335973f, 1.0f-0.335903f, | |
0.668104f, 1.0f-0.000013f, | |
0.336098f, 1.0f-0.000071f, | |
0.000103f, 1.0f-0.336048f, | |
0.000004f, 1.0f-0.671870f, | |
0.336024f, 1.0f-0.671877f, | |
0.000103f, 1.0f-0.336048f, | |
0.336024f, 1.0f-0.671877f, | |
0.335973f, 1.0f-0.335903f, | |
0.667969f, 1.0f-0.671889f, | |
1.000004f, 1.0f-0.671847f, | |
0.667979f, 1.0f-0.335851f | |
}; | |
GLuint uvBuffer; | |
glGenBuffers(1, &uvBuffer); | |
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW); | |
// ----- MAIN LOOP ----- | |
do { | |
// Clear the screen | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// Use our shader | |
glUseProgram(programID); | |
/*glPushMatrix(); | |
glBegin(GL_LINES); | |
glClearColor(0, 0, 0, 0); | |
glVertex2f(-10, 0); | |
glVertex2f(10, 0); | |
glVertex2f(0, -10); | |
glVertex2f(0, 10); | |
glEnd(); | |
glPopMatrix();*/ | |
// Compute the MVP matrix from keyboard and mouse input | |
computeMatricesFromInputs(); | |
mat4 ProjectionMatrix = getProjectionMatrix(); | |
mat4 ViewMatrix = getViewMatrix(); | |
mat4 ModelMatrix = mat4(1.0); | |
mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; | |
// Send transform to currently bound shader, in "MVP" uniform | |
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); | |
// Bind our texture in Texture Unit 0 | |
glActiveTexture(GL_TEXTURE0); | |
glBindTexture(GL_TEXTURE_2D, Texture); | |
// Set "myTextureSampler" in vertex shader to use TU0 | |
glUniform1i(TextureID, 0); | |
// 1st attribute buffer : vertices | |
glEnableVertexAttribArray(0); | |
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
glVertexAttribPointer( | |
0, // attr 0, no reason for 0, must match shader | |
3, // size | |
GL_FLOAT, // type | |
GL_FALSE, // normalized? | |
0, // stride | |
(void*) 0 // array buffer offset | |
); | |
// 2nd attribute buffer : uvs | |
glEnableVertexAttribArray(1); | |
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer); | |
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0); | |
// Draw the triangle! | |
glDrawArrays(GL_TRIANGLES, 0,12*3); // Start at v0, 3 total -> 1 triangle | |
glDisableVertexAttribArray(0); | |
glDisableVertexAttribArray(1); | |
// Swap buffers | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} // Check if the ESC key was pressed or the window was closed | |
while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); | |
// Cleanup VBO and shader | |
glDeleteBuffers(1, &vertexBuffer); | |
glDeleteBuffers(1, &uvBuffer); | |
glDeleteProgram(programID); | |
glDeleteTextures(1, &Texture); | |
glDeleteVertexArrays(1, &VertexArrayID); | |
// Close OpenGL window and terminate GLFW | |
glfwTerminate(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
// Interpolated values from vertex shaders | |
in vec2 UV; | |
// Output data | |
out vec3 color; | |
// Values that stay constant for the whole mesh | |
uniform sampler2D myTextureSampler; | |
void main() { | |
color = texture(myTextureSampler, UV).rgb; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
// Input vertex data (different for all executions of this shader) | |
layout(location = 0) in vec3 vertexPosition_modelspace; | |
layout(location = 1) in vec2 vertexUV; | |
// Output data (interpolated for each fragment | |
out vec2 UV; | |
// Constants for whole mesh | |
uniform mat4 MVP; | |
void main() { | |
gl_Position = MVP * vec4(vertexPosition_modelspace, 1); | |
// Because DXT compression comes from DX11, the V axis is flipped | |
// This only applies to DDS texture files | |
UV.x = vertexUV.x; | |
UV.y = (1.0 - vertexUV.y); | |
//mat4 myMatrix; | |
//vec4 myVector; | |
// fill myMatrix and myVector somehow | |
//vec4 transformedVector = myMatrix * myVector; | |
//mat4 transform = mat2 * mat1; | |
//vec4 out_vec = transform * in_vec; | |
//transformed_vertex = MVP * in_vertex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment