#include #include #include // GLM include #include "glm/glm.hpp" #include "glm/gtc/matrix_transform.hpp" #include "glm/gtc/type_ptr.hpp" //include Soil #include "SOIL2/SOIL2.h" using namespace std; #define WINDOW_TITLE "Brick Pyramid" // Shader program macro #ifndef GLSL #define GLSL(Version, Source) "#version " #Version "\n" #Source #endif // Var declaration for shader, window size, initialization, buffer and array objects GLint shaderProgram, WindowWidth = 800, WindowHeight = 600; GLuint VBO, VAO, EBO, texture; // Function prototypes void UResizeWindow(int, int); void URenderGraphics(void); void UCreateShader(void); void UCreateBuffers(void); void UGenerateTexture(void); // Vertex shader source const GLchar * vertexShaderSource = GLSL(330, layout(location = 0) in vec3 position; layout(location = 2) in vec2 textureCoordinate; out vec2 mobileTextureCoordinate; //declare a vec 4 variable //Global variables for the transform matrices uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main(){ gl_Position = projection * view * model * vec4(position, 1.0f);//transform vertices mobileTextureCoordinate = vec2(textureCoordinate.x, 1.0f - textureCoordinate.y); } ); /*Fragment shader program source code*/ const GLchar * fragmentShaderSource = GLSL(330, in vec2 mobileTextureCoordinate; out vec4 gpuTexture;//out vertex_Color; uniform sampler2D uTexture; void main(){ gpuTexture = texture(uTexture, mobileTextureCoordinate); } ); int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(WindowWidth, WindowHeight); glutCreateWindow(WINDOW_TITLE); glutReshapeFunc(UResizeWindow); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { std::cout << "Failed to initialize GLEW" << std::endl; return -1; } UCreateShader(); UCreateBuffers(); UGenerateTexture(); // Use the Shader program glUseProgram(shaderProgram); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color glutDisplayFunc(URenderGraphics); glutMainLoop(); // Destroys Buffer objects once used glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); return 0; } // Resize windiw to fit primitives void UResizeWindow(int w, int h) { WindowWidth = w; WindowHeight = h; glViewport(0, 0, WindowWidth, WindowHeight); } // Graphics rendering void URenderGraphics(void) { // Enable z-depth glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Activate the vertex array object beforte renderingand transforming them glBindVertexArray(VAO); // Declare a 4x4 identity matrix uniform variable to handle transformations glm::mat4 model(1.0f); // Place the object at the center of the viewport model = glm::translate(model, glm::vec3(0.5, 0.0f, 0.0f)); // Rotate the object 15 degrees on the x-axis // I also realized that the transform for my model was way off. Using the updated // code with my old transform made it impossible to see clearly model = glm::rotate(model, glutGet(GLUT_ELAPSED_TIME) * -0.0005f, glm::vec3(0.0, 1.0f, 0.0f)); // Increase the object size by a scale of 2 model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); // transforms the camera and set the // I also did not set the view using the (1.0f) param glm::mat4 view; view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f)); // Perspective projection glm::mat4 projection; projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f); // Retrieves and passes transform matirices to the shader program GLint modelLoc = glGetUniformLocation(shaderProgram, "model"); GLint viewLoc = glGetUniformLocation(shaderProgram, "view"); GLint projLoc = glGetUniformLocation(shaderProgram, "projection"); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection)); glutPostRedisplay(); // Draws the triangles glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glutSwapBuffers(); } // Creates the shader program void UCreateShader() { // Vertex shader GLint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); // Fragment shader GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); //Shader program shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader);; glLinkProgram(shaderProgram); // Delete the vertex and fragment shaders once linked glDeleteShader(vertexShader); glDeleteShader(fragmentShader); } // creates the buffer and array objects void UCreateBuffers() { // Position and color data GLfloat vertices[] = { // Vertex Positions // Color Data // Vertex position -0.5f, -0.5f, 0.0f, 1.0, 1.0f, 0.0f, // 0 0.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // 1 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, // 2 -0.5f, -0.5f, -1.0f, 1.0f, 0.0f, 1.0f, // 3 0.5f, -0.5f, -1.0f, 0.5f, 0.5f, 1.0f, // 4 }; // Index data to share postion data GLuint indices[] = { 0, 1, 2, // Front Triangle 0, 3, 1, // Right Side triangle 3, 1, 4, // Back triangle 4, 1, 2, // Left back side triangle 0, 3, 4, // Bottom Triangle 0, 4, 2 }; // Generate buffer ids glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); // Activate the vertex array object before binding and settomg any VBOs or vertex attribute pointers glBindVertexArray(VAO); // Activate the VBO glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Activate the lement buffer object / indicies glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); // set attribute pointer 0 to hold position data glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); // Set attribute pointer 1 to hold Color data glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); // Sets polygon mode allows me to see wireframe view //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBindVertexArray(0); } void UGenerateTexture(){ glEnable(GL_TEXTURE_2D); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); int width, height; unsigned char* image = SOIL_load_image("snhu.jpg", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(image); glBindTexture(GL_TEXTURE_2D, 0); }