I need to transform this code to come up with a pyramid instead of a cube and i am having trouble with this.
This is all using C++ and modern openGL
/* Header Inclusions */
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include “SOIL2/SOIL2.h”
using namespace std; // Standard namespace
#define WINDOW_TITLE “Modern OpenGL” // Window title Macro
/*Shader program Macro*/
#ifndef GLSL
#define GLSL(Version, Source) “#version ” #Version “\n” #Source
#endif
//Variable declarations for shader, window size initialization, buffer and array objects
GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO, EBO, texture;
GLfloat degrees = glm::radians(-45.0f);
/*Function prototypes*/
void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UGenerateTexture(void);
/*Vertex Shader Source Code*/
const GLchar * vertexShaderSource = GLSL(330,
layout (location = 0) in vec3 position;
layout (location = 2) in vec2 textureCoordinates;
out vec2 mobileTextureCoordinate;
//global var for transform matrices
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_position = projection * view * model * vec4(position, 1.0f);
mobileTextureCoordinate = vec2(textureCoordinates.x, 1.0f – textureCoordinates.y);
}
);
//Fragment Shader source
const GLchar * fragmentShaderSource = GLSL(330,
in vec2 mobileTextureCoordinate;
out vec4 gpuTexture;
uniform sampler2D uTexture;
void main(){
gpuTexture = texture(uTexture, mobileTextureCoordinate);
}
);
/*Main Program*/
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(0.0f, 0.0f, 0.0f, 1.0f); // Set background color
glutDisplayFunc(URenderGraphics);
glutMainLoop();
// Destroys Buffer objects once used
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
return 0;
}
/*Resizes the window*/
void UResizeWindow(int w, int h)
{
WindowWidth = w;
WindowHeight = h;
glViewport(0, 0, WindowWidth, WindowHeight);
}
/*Renders graphics */
void URenderGraphics(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
//TRANSFORM OBJECT
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0, 0.0f, 0.0f));
model = glm::rotate(model, degrees, glm::vec3(0.0, 0.0f, 0.0f));
model = glm::scale(model, glm::vec3(2.0, 2.0f, 2.0f));
//Transforms the camera
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f));
//create perspectif projection
glm::mat4 projection;
projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
//retrieves and passes the transform matrice to the shader
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();
glBindTexture(GL_TEXTURE_2D, texture);
//draw triangles
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glutSwapBuffers();
}
/*Create the Shader program*/
void UCreateShader()
{
// Vertex shader
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER); // Creates the vertex shader
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); // Attaches the Vertex shader to the source code
glCompileShader(vertexShader); // Compiles the Fragment shader
//Fragment Shader
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); // Create the Fragment shader
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); // Attaches the Fragment shader to source code
glCompileShader(fragmentShader); // Compiles the Fragment shader
// Shader program
shaderProgram = glCreateProgram(); // Creates the Shader program and returns an id
glAttachShader(shaderProgram, vertexShader); // Attach vertex shader to shader program
glAttachShader(shaderProgram, fragmentShader);; // Attach Fragment shader to the Shader program
glLinkProgram(shaderProgram); //Link Vertex and Fragment shaders to Shader program
// Delete the vertex and Fragment shaders once linked
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
/*Create the Buffer and Array Objects*/
void UCreateBuffers()
{
// Position and Texture data
GLfloat vertices[] = { //PPOSITION //TEXTURE COORDINATES
-0.05f, -0.05f, -0.5f, 0.0f, 0.0f,
0.05f, -0.05f, -0.5f, 1.0f, 0.0f,
0.05f, 0.05f, -0.5f, 1.0f, 1.0f,
0.05f, 0.05f, -0.5f, 1.0f, 1.0f,
-0.05f, 0.05f, -0.5f, 0.0f, 1.0f,
-0.05f, -0.05f, -0.5f, 0.0f, 0.0f,
-0.05f, -0.05f, 0.5f, 0.0f, 0.0f,
0.05f, -0.05f, 0.5f, 1.0f, 0.0f,
0.05f, 0.05f, 0.5f, 1.0f, 1.0f,
0.05f, 0.05f, 0.5f, 1.0f, 1.0f,
-0.05f, 0.05f, 0.5f, 0.0f, 1.0f,
-0.05f, -0.05f, 0.5f, 0.0f, 0.0f,
-0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
-0.05f, 0.05f, -0.5f, 1.0f, 1.0f,
-0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
-0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
-0.05f, -0.05f, 0.5f, 0.0f, 0.0f,
-0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
0.05f, 0.05f, -0.5f, 1.0f, 1.0f,
0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
0.05f, -0.05f, 0.5f, 0.0f, 0.0f,
0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
-0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
0.05f, -0.05f, -0.5f, 1.0f, 1.0f,
0.05f, -0.05f, 0.5f, 1.0f, 0.0f,
0.05f, -0.05f, 0.5f, 1.0f, 0.0f,
-0.05f, -0.05f, 0.5f, 0.0f, 0.0f,
-0.05f, -0.05f, -0.5f, 0.0f, 1.0f,
-0.05f, 0.05f, -0.5f, 0.0f, 1.0f,
0.05f, 0.05f, -0.5f, 1.0f, 1.0f,
0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
0.05f, 0.05f, 0.5f, 1.0f, 0.0f,
-0.05f, 0.05f, 0.5f, 0.0f, 0.0f,
-0.05f, 0.05f, -0.5f, 0.0f, 1.0f,
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//activate vao
glBindVertexArray(VAO);
//Activate the VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//ACTIVATE ebo
//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, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//set attrib pointer 1 to hold color data
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//set attrib pointer 2 to hold texture coordinate
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
}
void UGenerateTexture()
{
//generating and loading texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height;
//load image from project folder
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);
}Download Attachments:
- Ec 1.png
Skip QuestionShow CommentReport Issue