OpenGL object not rendering correctly? - c++

The code works with out any errors, but the object displays at white. The IDE I am using is Eclipse.
I tried some debugging and found error #1282 in UCreateBuffers().
// header inclusions
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
//GLM math header inclusions
#include <GL/glm.hpp>
#include <GL/gtc/matrix_transform.hpp>
#include <GL/gtc/type_ptr.hpp>
using namespace std;
#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;
GLfloat cameraSpeed = 0.0005f;
GLchar currentKey; // will store key pressed
//global vector declarations
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 CameraUpY = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 CameraForwardZ = glm::vec3(0.0f, 0.0f, -1.0f);
// function prototypes
void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UKeyboard(unsigned char key, int x, int y);
void UKeyReleased(unsigned char key, int x, int y);
//vertex shader source code
const GLchar * vertexShaderSource = GLSL(330,
layout (location = 0) in vec3 position; // vertex data from vertex attrib pointer 0
layout (location = 1) in vec3 color; // color data from vertex attrib pointer 1
out vec3 mobileColor; // variable to transfer color data to the fragment shader
// 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); //transforms vertices to clip coordinates
mobileColor = color; // references incoming color data
}
);
// fragment shader source code
const GLchar * fragmentShaderSource = GLSL(330,
in vec3 mobileColor; // variable to hold incoming color data from vertex shader
out vec4 gpuColor; // variable to pass color data to the gpu
void main()
{
gpuColor = vec4(mobileColor, 1.0); // sends color data to the gpu for rendering
}
);
//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();
//use the shader program
glUseProgram(shaderProgram);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //set background color
glutDisplayFunc(URenderGraphics);
glutKeyboardFunc(UKeyboard); // detects key press
glutKeyboardUpFunc(UKeyReleased); // detects key release
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); // enable z-depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO); // Activate the vertex array object before rendering and transforming them
// Camera Movement Logic
if(currentKey =='w')
cameraPosition += cameraSpeed * CameraForwardZ;
if(currentKey =='s')
cameraPosition -= cameraSpeed * CameraForwardZ;
if(currentKey =='a')
cameraPosition -= glm::normalize(glm::cross(CameraForwardZ, CameraUpY)) * cameraSpeed;
if(currentKey =='d')
cameraPosition += glm::normalize(glm::cross(CameraForwardZ, CameraUpY)) * cameraSpeed;
// transforms the object
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // place the object at the center of the viewport
model = glm::rotate(model, 45.0f, glm::vec3(0.0, 1.0f, 0.0f)); // rotate the object 45 degrees on the x
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); // increase the object size by a scale of 2
//transform the camera
glm::mat4 view;
view = glm::lookAt(cameraPosition, cameraPosition + CameraForwardZ, CameraUpY);
//creates a perspective projection
glm::mat4 projection;
projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
//retrieves and passes transform matrices 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
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glutSwapBuffers(); // flips the back buffer with the front buffer every frame
}
//creates 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 vertex shader
//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);
}
void UCreateBuffers()
{
GLfloat vertices[] = {
//positions //Color
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
};
//generate buffer Ids
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//activate the vertex array object before binding and setting any VBOs and Vertex Attribute Pointers
glBindVertexArray(VAO);
//Activate the VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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); // Enables vertex Attribute
//set attribute pointer 1 to hold color data
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1); // enables vertex attribute
glBindVertexArray(0);// enable the Vao which is good pratice
}
// implements the UKeyboar function
void UKeyboard(unsigned char key, GLint x, GLint y)
{
switch(key){
case 'w':
cout << "You pressed w!" <<endl;
break;
case 's':
cout << "You pressed s!" <<endl;
break;
case 'a':
cout << "You pressed a!" <<endl;
break;
case 'd':
cout << "You pressed d!" <<endl;
break;
default:
cout << "Press a key!" << endl;
}
}
//implements the UkeyRelease function
void UKeyReleased(unsigned char key, GLint x, GLint y)
{
cout << "Key released!" <<endl;
}

The shaders doesn't compile. GLSL is case sensitive. The version qualifier has to be #version rather than #Version:
#define GLSL(Version, Source) "#Version " #Version "\n" #Source
#define GLSL(Version, Source) "#version " #Version "\n" #Source
I recommend to check for compile and link errors and to add an error logging.
Compile errors can be get by glGetShaderiv / glGetShaderInfoLog:
e.g.
#include <vector>
void UCompileShader( GLuint shader )
{
glCompileShader( shader );
GLint status;
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetShaderInfoLog( shader, logLen, &written, log.data() );
std::cout << "compile error:" << std::endl << log.data() << std::endl;
}
}
Link errors can be get by glGetProgramiv / glGetProgramInfoLog:
e.g.
void ULinkShader( GLuint program )
{
glLinkProgram( program );
GLint status;
glGetProgramiv( program, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetProgramInfoLog( program, logLen, &written, log.data() );
std::cout << "link error:" << std::endl << log.data() << std::endl;
}
}
//creates the shader program
void UCreateShader()
{
//vertex shader
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
UCompileShader(vertexShader); // compiles the vertex shader
//fragment shader
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
UCompileShader(fragmentShader);
//shader program
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
ULinkShader(shaderProgram);
// delete the vertex and fragment shaders once linked
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}

Related

c++ opengl model matrix stretch

I was programming easy 3d space in Opengl. I wanted to create two cubes(one orange, second green). I have used same model, view, projection matrix for each. Every cube should bein same position and they should look same, but they look like this :
i have no idea why this is happening.Is it because the two model matrixes are same?
Here is the code(main.cpp)
`#include "src/Program.hpp"
#include <GLFW/glfw3.h>
// functions
void framebufferResizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void errorCallback(int error, const char* description) {
std::cout << "error :" << description << std::endl;
}
//variables
int window_width = 800;
int window_height = 600;
int main() {
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwSetErrorCallback(errorCallback);
GLFWwindow* window = glfwCreateWindow(window_width, window_height, "Opengl_triangle", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
Program light("src/vertex.glsl", "src/fragment.glsl");
Program object("src/vertex.glsl", "src/fragment_green.glsl");
/// going 3D
// calculations
glm::mat4 model(1.f);
model = glm::rotate(model,glm::radians(20.f), glm::vec3(1.f, 0.f, 0.6f));
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.f, 0.f, -5.f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.f), float(window_width / window_height), 0.1f, 100.f);
// sending it to the shaders LIGHT
light.use();
light.uniformMatrix4(model, "model");
light.uniformMatrix4(view, "view");
light.uniformMatrix4(projection, "projection");
// sending it to the shaders OBJECT
object.use();
object.uniformMatrix4(model, "model");
object.uniformMatrix4(view, "view");
object.uniformMatrix4(projection, "projection");
//fun stuff
float vertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
unsigned int VBO, VAO_light, VAO_object ;
glGenVertexArrays(1, &VAO_light);
glGenVertexArrays(1, &VAO_object);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO_light);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0);
glBindVertexArray(VAO_object);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexAttribArray(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while (!glfwWindowShouldClose(window)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
light.use();
glBindVertexArray(VAO_light);
glDrawArrays(GL_TRIANGLES, 0, 36);
object.use();
glBindVertexArray(VAO_object);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
here are shaders fragment:
`#version 330 core
out vec4 fragColor;
void main() {
fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
};`
vertex :
`#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos , 1.0f);
};`
here are my shader and program classess:
`class Program {
Shader vertex;
Shader fragment;
GLuint ID;
public:
// essential methods
Program() : ID(0) {
std::cout << "program default constructor" << std::endl;
}
Program(const char* vertexShader_path, const char * fragmentShader_path) {
// recreate shaders
vertex = Shader(vertexShader_path, GL_VERTEX_SHADER);
fragment = Shader(fragmentShader_path, GL_FRAGMENT_SHADER);
// create shader program
ID = glCreateProgram();
glAttachShader(ID, vertex.getID());
glAttachShader(ID, fragment.getID());
glLinkProgram(ID);
checkProgramLinking(ID);
}
/// usage methods
void use() { glUseProgram(ID); }
GLuint getID() { return ID; }
// accesing uniforms methods
void uniformMatrix4(glm::mat4 matrix, const char* matrixName) {
GLuint uniformLocation = glGetUniformLocation(ID, matrixName);
glUniformMatrix4fv(uniformLocation, 1, GL_FALSE, glm::value_ptr(matrix));
}
private:
void checkProgramLinking(unsigned int& program) {
int success;
char log[512];
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 512, NULL, log);
std::cout << "error linking program : " << log << std::endl;
}
}
}
class Shader {
GLuint ID;
public:
// essential methods
Shader() : ID(0) {
std::cout << "default shader constructor" << std::endl;
}
Shader(const char* path, GLenum type) {
// converting shader code from file
std::string shaderCodeS;
std::ifstream shaderFile;
shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
shaderFile.open(path);
std::stringstream shaderStream;
shaderStream << shaderFile.rdbuf();
shaderFile.close();
shaderCodeS = shaderStream.str();
}
catch (std::ifstream::failure& e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* shaderCode = shaderCodeS.c_str();
// shader creation
ID = glCreateShader(type);
glShaderSource(ID, 1, &shaderCode, NULL);
glCompileShader(ID);
checkShaderCompilation(ID);
std::cout << "shader argumented constructor" << std::endl;
}
// methods for usage
GLuint getID() { return ID; }
private:
void checkShaderCompilation(unsigned int& shader) {
int success;
char log[512];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, NULL, log);
std::cout << "error compiling shader:" << log << std::endl;
}
}
};
I have found out! I forgot to make projection and view matrix an identity matrix before doing mathematical calculations. Here is how it should be:
glm::mat4 model(1.f);
glm::mat4 view(1.f);
view = glm::translate(view, glm::vec3(0.f, 0.f, -3.f));
glm::mat4 projection(1.f);
projection = glm::perspective(glm::radians(45.f), float(window_width / window_height), 0.1f, 100.f);`

OPENGL Showing white box instead of moveable cube

Need help with this problem on opengl. It will compile but wont do anything other than create a white box instead of the cube it should be. I finally got my compiler working properly but now this is happening and am unsure what I am doing wrong. Should rotate around and be a multicolor cube.
#include <Windows.h>
#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>
using namespace std;
#define GLSL(Version, Source) "#version " #Version "\n" #Source
#define WINDOW_TITLE "5-1 Practice Activity 6"
GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO;
GLfloat cameraSpeed = 0.0005f;
GLchar currentKey;
int keymod;
GLfloat scale_by_y = 2.0f;
GLfloat scale_by_z = 2.0f;
GLfloat scale_by_x = 2.0f;
GLfloat lastMouseX = 400, lastMouseY = 300;
GLfloat mouseXoffset, mouseYoffset, yaw = 0.0f, pitch = 0.0f;
GLfloat sensitivity = 0.1f;
bool mouseDetected = true;
bool rotate = false;
bool checkMotion = false;
bool checkZoom = false;
glm::vec3 CameraPosition = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 CameraUpY = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 CameraForwardZ = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 front;
void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UMouseMove(int x, int y);
void OnMouseClick(int button, int state, int x, int y);
void onMotion(int x, int y);
const GLchar* vertexShaderSource = "#version 400 core\n"
"layout (location = 0) in vec3 position;"
"layout (location = 1) in vec3 color;"
"out vec3 mobileColor;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"{\n"
"gl_Position = projection * view * model * vec4(position, 1.0f);"
"mobileColor = color;"
"}\n";
const GLchar* fragmentShaderSource = "#version 400 core\n"
"in vec3 mobileColor;"
"out vec4 gpuColor;"
"void main()\n"
"{\n"
"gpuColor = vec4(mobileColor, 1.0);"
"}\n";
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();
glUseProgram(shaderProgram);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(URenderGraphics);
glutPassiveMotionFunc(UMouseMove);
glutMotionFunc(onMotion);
glutMouseFunc(OnMouseClick);
glutMainLoop();
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
return 0;
}
void UResizeWindow(int w, int h) {
WindowWidth = w;
WindowHeight = h;
glViewport(0, 0, WindowWidth, WindowHeight);
}
void URenderGraphics(void) {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);
CameraForwardZ = front;
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
model = glm::rotate(model, 45.0f, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::scale(model, glm::vec3(scale_by_x, scale_by_y, scale_by_z));
glm::mat4 view;
view = glm::lookAt(CameraForwardZ, CameraPosition, CameraUpY);
glm::mat4 projection;
projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
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();
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glutSwapBuffers();
}
void UCreateShader() {
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void UCreateBuffers() {
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f//pot here?
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Active the Element Buffer Object / Indices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)nullptr);
glEnableVertexAttribArray(0); // Enables vertex attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1); // Enables vertex attribute
glBindVertexArray(0); //Deactivates the VAO which is good
}
void UMouseMove(int x, int y) {
front.x = 10.0f * cos(yaw);
front.y = 10.0f * sin(pitch);
front.z = sin(yaw) * cos(pitch) * 10.0f;
}
void onMotion(int curr_x, int curr_y) {
if (checkMotion) {
mouseXoffset = curr_x - lastMouseX;
mouseYoffset = lastMouseY - curr_y;
lastMouseX = curr_x;
lastMouseY = curr_y;
mouseXoffset *= sensitivity;
mouseYoffset *= sensitivity;
if (yaw != yaw + mouseXoffset && pitch == pitch + mouseYoffset) {
yaw += mouseXoffset;
}
else if (pitch != pitch + mouseYoffset && yaw == yaw + mouseXoffset) {
pitch += mouseYoffset;
}
front.x = 10.0f * cos(yaw);
front.y = 10.0f * sin(pitch);
front.z = sin(yaw) * cos(pitch) * 10.0f;
}
if (checkZoom) {
if (lastMouseY > curr_y) {
scale_by_y += 0.1f;
scale_by_x += 0.1f;
scale_by_z += 0.1f;
glutPostRedisplay();
}
else {
scale_by_y -= 0.1f;
scale_by_x -= 0.1f;
scale_by_z -= 0.1f;
if (scale_by_y < 0.2f) {
scale_by_y = 0.2f;
scale_by_x = 0.2f;
scale_by_z = 0.2f;
}
glutPostRedisplay();
}
lastMouseY = curr_y;
lastMouseX = curr_x;
}
}
void OnMouseClick(int button, int state, int x, int y) {
keymod = glutGetModifiers();
checkMotion = false;
if (button == GLUT_LEFT_BUTTON && keymod == GLUT_ACTIVE_ALT && state == GLUT_DOWN) {
checkMotion = true;
checkZoom = false;
}
else if (button == GLUT_RIGHT_BUTTON && keymod == GLUT_ACTIVE_ALT && state == GLUT_DOWN) {
checkMotion = false;
checkZoom = true;
}
}
Ive gone through line by line but for the life of me I cannot figure it out any help would be much appreicated!
The vertex shader does not compile, because there is missing a line:
const GLchar* vertexShaderSource = "#version 400 core\n"
"layout (location = 0) in vec3 position;"
"layout (location = 1) in vec3 color;"
"out vec3 mobileColor;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main()\n" // <--- MISSING
"{\n"
"gl_Position = projection * view * model * vec4(position, 1.0f);"
"mobileColor = color;"
"}\n";
I recommend to check if the shader compilation succeeded and if the program object linked successfully.
If the compiling of a shader succeeded can be checked by glGetShaderiv and the parameter GL_COMPILE_STATUS. e.g.:
#include <iostream>
#include <vector>
bool CompileStatus( GLuint shader )
{
GLint status = GL_TRUE;
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
if (status == GL_FALSE)
{
GLint logLen;
glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetShaderInfoLog( shader, logLen, &written, log.data() );
std::cout << "compile error:" << std::endl << log.data() << std::endl;
}
return status != GL_FALSE;
}
If the linking of a program was successful can be checked by glGetProgramiv and the parameter GL_LINK_STATUS. e.g.:
bool LinkStatus( GLuint program )
{
GLint status = GL_TRUE;
glGetProgramiv( program, GL_LINK_STATUS, &status );
if (status == GL_FALSE)
{
GLint logLen;
glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen );
GLsizei written;
glGetProgramInfoLog( program, logLen, &written, log.data() );
std::cout << "link error:" << std::endl << log.data() << std::endl;
}
return status != GL_FALSE;
}
Call CompileStatus and LinkStatus in UCreateShader:
void UCreateShader() {
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
CompileStatus(vertexShader);
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
CompileStatus(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
LinkStatus(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}

modern opengl textured pyramid

I think am missing something when drawing the textured pyramid. I can't get it to work properly since I am not good with 3d modern OpenGL. Somehow, I managed to draw a cube but seems am going wrong with the pyramid. I want the four sides to have 0.5(top), 0.0(left), 1,0 (right). and the bottom to be 0,1 (top left), 1,1 (top right), 0,0 (bottom left) and 1,0 (bottom right). I need these coordinates since am only getting flunky images with my coordinate. Any help would be appreciated.
here is my code with the vertices of a cube that I had created earlier and was running well.
/*Header Inclusions*/
#include <iostream>
#include <GL/Glew.h>
#include <GL/freeglut.h>
// GLM Math inclusions
#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; // Uses the standard namespace
#define WINDOW_TITLE "Modern OpenGL" // Macro for window title
//Vertex and fragment shader
#ifndef GLSL
#define GLSL(Version, source) "#version " #Version "\n" #source
#endif
// Variables for window width and height
GLint ShaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO, texture;
GLfloat degrees = glm::radians(-45.0f);
/* User-defined Function prototypes to:*/
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 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);
}
);
//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);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
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); // Enable z-depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen
glBindVertexArray(VAO); // Activate the Vertex Array Object before rendering and transforming them
// Transforms the object
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0, 0.0f, 0.0f)); // Place the object at the center of the 7i,p9rA
model = glm::rotate(model, degrees, glm::vec3(0.0, 1.0f, 0.0f)); // Rotate the object 45 degrees on the XYZ
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); // Increase the object size by a scale of 2
// Transforms the camera
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f,0.0f,-5.0f)); //Moves the world 0.5 units on X and -5 units in Z
// Creates a perspective projection
glm::mat4 projection;
projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
// Retrieves and passes transform matrices 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();
glBindTexture(GL_TEXTURE_2D, texture);
// Draws the triangles
glDrawArrays(GL_TRIANGLES,0, 36);
glBindVertexArray(0); // Deactivate the Vertex Array Object
glutSwapBuffers(); // Flips the the back buffer with the front buffer every frame. Similar to GL FLush
}
/*Creates 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 Vertex Shader
// Fragment Shader
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); // Creates the Fragment Shader
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);// Attaches the Fragment Shader to the 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 the Shader program
glAttachShader(ShaderProgram, fragmentShader);; // Attach Fragment Shader to the Shader program
glLinkProgram(ShaderProgram); //Link Vertex and Fragment shader, to Shader program
// Delete the Vertex and Fragment shaders once linked
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
/*creates the buffer and array object*/
void UCreateBuffers()
{
//position and color data
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
//Generate buffer id,
glGenVertexArrays(1, &VAO);
glGenBuffers(1,&VBO);
// Activate the Vertex Array Object before binding and setting any VB0s and Vertex Attribute Pointers.
glBindVertexArray(VAO);
// Activate the VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //Copy vertices to VBO
// Set attribute pointer 0 to hold Position data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0); // Enables vertex attribute
// Set attribute pointer 2 to hold Color data
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2); // Enables vertex attribute
glBindVertexArray(0); // Deactivates the VAC, which is good practice
}
/*Generate and load the texture*/
void UGenerateTexture(){
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);
}
I recommend to define a structure type for the attribute tuples:
struct TAttributeTuple
{
glm::vec3 v;
glm::vec2 uv;
};
Define the 5 corner points of the pyramid:
(Your explanation of the points is a bit unclear, so I am not sure if the points are according to your specification. Possibly you've modify them.)
glm::vec3 top( 0.5f, 0.5f, 0.5f );
glm::vec3 p01( -0.1f, 0.0f, 1.0f );
glm::vec3 p11( 1.1f, 0.0f, 1.0f );
glm::vec3 p00( 0.0f, 0.0f, 0.0f );
glm::vec3 p10( 1.0f, 0.0f, 0.0f );
Setup the array of vertex attributes:
TAttributeTuple vertices[]
{
{ p00, glm::vec2(0.0f, 0.0f) },
{ p10, glm::vec2(1.0f, 0.0f) },
{ p11, glm::vec2(1.0f, 1.0f) },
{ p00, glm::vec2(0.0f, 0.0f) },
{ p11, glm::vec2(1.0f, 1.0f) },
{ p01, glm::vec2(0.0f, 1.0f) },
{ p00, glm::vec2(0.0f, 0.0f) },
{ p10, glm::vec2(1.0f, 0.0f) },
{ top, glm::vec2(0.5f, 1.0f) },
{ p10, glm::vec2(0.0f, 0.0f) },
{ p11, glm::vec2(1.0f, 0.0f) },
{ top, glm::vec2(0.5f, 1.0f) },
{ p11, glm::vec2(0.0f, 0.0f) },
{ p01, glm::vec2(1.0f, 0.0f) },
{ top, glm::vec2(0.5f, 1.0f) },
{ p01, glm::vec2(0.0f, 0.0f) },
{ p00, glm::vec2(1.0f, 0.0f) },
{ top, glm::vec2(0.5f, 1.0f) }
};

OpenGL not drawing in Linux [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have been trying to fix this for days, but haven't come anywhere other then replacing sfml with glfw.
I tried using sfml first with OpenGL, but it didn't draw. That's why I tried using glfw instead.
There is no errors from the glsl files nor the main.cpp file.
This code (but less modified, because of replacing sfml with glfw) actually works on my other Windows PC.
Now I'm using Linux because my other PC needs a motherboard replacement.
Changing the color of the "background" works with glClearColor but drawing doesn't.
This program is supposed to have a cube that rotates and have textures.
I'm actually learning OpenGL atm.
I compiled and ran the program within the terminal instead of codeblocks.
Not that it would really help but just giving out the info.
Thank you for any help :)
main file
#include <GL/glew.h>
#include <glfw3.h>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <chrono>
#include <SOIL.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
void bindBufferData(GLfloat* vertices, GLuint& vbo);
GLuint createShaderProgam(const GLchar* vertexShaderText, const
GLchar* fragmentShaderText);
std::string loadFileContent(const std::string filepath);
static void glfwerror(int id, const char* description);
int main()
{
int width = 600, height = 600;
if(!glfwInit())
{
std::cout << "Initialization failed" << std::endl;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_DOUBLEBUFFER, 1);
glfwWindowHint(GLFW_DEPTH_BITS, 24);
glfwSetErrorCallback(&glfwerror);
GLFWwindow* window = glfwCreateWindow(width, height, "OpenGL",
NULL, NULL);
if(!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "failed to initialize: " <<
glewGetErrorString(glewInit()) << std::endl;
}
std::cout << "OpenGL ver: " << glGetString(GL_VERSION) <<
std::endl;
std::cout << "GLSL ver: " <<
glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
float vertices[] = {
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
};
std::string vertexShaderText =
loadFileContent("shader.vert.glsl");
std::string fragmentShaderText =
loadFileContent("shader.frag.glsl");
GLuint shaderProgram =
createShaderProgam(vertexShaderText.c_str(),
fragmentShaderText.c_str());
glUseProgram(shaderProgram);
GLuint ebo;
GLuint vbo;
GLuint vao;
GLuint tex;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
GL_STATIC_DRAW);
glGenBuffers(1, &ebo);
GLuint elements[]{
0, 1, 2,
2, 3, 0
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements,
GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glGenVertexArrays(0, &vao);
glBindVertexArray(vao);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float),
(GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float),
(GLvoid*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 *
sizeof(float), (GLvoid*)(6 * sizeof(float)));
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int S_width, S_height;
unsigned char* image = SOIL_load_image("unity_preview_02-.jpeg",
&S_width, &S_height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, S_width, S_height, 0,
GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glm::mat4 view = glm::lookAt(
glm::vec3(2.2f, 2.2f, 1.5f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f)
);
GLint uniView = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f /
600.0f, 1.0f, 20.0f);
GLuint uniProj = glGetUniformLocation(shaderProgram, "proj");
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
glm::mat4 transf = glm::mat4(1.0f);
GLint unitransf = glGetUniformLocation(shaderProgram, "transf");
glEnable(GL_DEPTH_TEST);
glm::vec4 resultRot = transf * glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
printf("%f, %f, %f\n", resultRot.x, resultRot.y, resultRot.z);
auto t_start = std::chrono::high_resolution_clock::now();
GLfloat angle = -60.0f;
GLfloat speed = 0.0f;
GLfloat y = 0.5f;
GLfloat x = 0.5f;
GLfloat z = 1.5f;
while(!glfwWindowShouldClose(window))
{
glLoadIdentity();
glClearColor(0.0f, 1.0f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Setup view
float Ratio;
glfwGetFramebufferSize(window, &width, &height);
Ratio = width / (float)height;
glViewport(0, 0, width, height);
auto t_now = std::chrono::high_resolution_clock::now();
float time =
std::chrono::duration_cast<std::chrono::duration<float>>(t_now
- t_start).count();
t_start = t_now;
transf = glm::rotate(transf, time * glm::radians(angle),
glm::vec3(x, y, z));
glUniformMatrix4fv(unitransf, 1, GL_FALSE,
glm::value_ptr(transf));
z += 10 * time;
x += 5 * time;
y -= 5 * time;
//std::cout << x << ", " << y << ", " << z << std::endl;
glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36);
//swap buffer and check for events
glfwSwapBuffers(window);
glfwPollEvents();
}
//sf::sleep(sf::milliseconds(2));
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
static void glfwerror(int id, const char* description)
{
std::cout << description << std::endl;
}
void bindBufferData(GLfloat* vertices, GLuint& vbo)
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GLuint createShaderProgam(const GLchar* vertexShaderText, const
GLchar* fragmentShaderText)
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
GLuint shaderProgram = glCreateProgram();
glShaderSource(vertexShader, 1, &vertexShaderText, nullptr);
glCompileShader(vertexShader);
GLint succes;
GLchar infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &succes);
if(!succes)
{
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "failed to compile vertex shader: " << infoLog <<
std::endl;
}
glShaderSource(fragmentShader, 1, &fragmentShaderText, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &succes);
if (!succes)
{
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "failed to compile fragment shader: " << infoLog
<< std::endl;
}
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &succes);
if(!succes)
{
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cout << "failed to link program: " << infoLog <<
std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
std::string loadFileContent(const std::string filepath)
{
std::ifstream file(filepath);
std::stringstream sstream;
if(!file.is_open())
{
std::cout << "could not find file: " << filepath << std::endl;
}
sstream << file.rdbuf();
return sstream.str();
}
fragment shader
#version 450 core
in vec3 vertexColor;
in vec2 Texcoord;
out vec4 outcolor;
uniform sampler2D tex;
void main()
{
outcolor = texture(tex, Texcoord) * vec4(vertexColor, 1.0);
}
vertex shader
#version 450 core
layout (location = 0)
in vec3 pos;
layout (location = 1)
in vec3 col;
in vec2 texcoord;
out vec3 vertexColor;
out vec2 Texcoord;
uniform mat4 transf;
uniform mat4 proj;
uniform mat4 view;
void main()
{
Texcoord = texcoord;
vertexColor = col;
gl_Position = proj * view * transf * vec4(pos, 1.0);
}
typing glxinfo | grep 'version' in Linux terminal
Output:
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
Max core profile version: 4.
Max compat profile version: 3.0
Max GLES1 profile version: 1.1
Max GLES[23] profile version: 3.1
OpenGL core profile version string: 4.5 (Core Profile) Mesa 17.2.4
OpenGL core profile shading language version string: 4.50
OpenGL version string: 3.0 Mesa 17.2.4
OpenGL shading language version string: 1.30
OpenGL ES profile version string: OpenGL ES 3.1 Mesa 17.2.4
OpenGL ES profile shading language version string: OpenGL ES GLSL ES
3.10
Ubuntu version
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
Your code shouldn't really work on a conforming GL implementation. Adding glGetError() as #HolyBlackCat sugggested will result in a couple of errors, for example, you are using deprecated GL calls like glEnableClientState(), glLoadIdentity(), and so on - which all are not available in a core profile.
Actually I would not recommend adding glGetError() calls. Since you use GL 4.5, you can directly use a debug context. You will get human-readbale text messages for every error that occurs (and some more or less helpful hints beyond that).
One of the main issues I'm seeing in your code is this one:
glGenVertexArrays(0, &vao);
You are actually rendering without VAOs - which are mandatory in core profiles. Asking the GL to create zero VAOs won't get you anything, and then just binding some uninitialized variable as VAO isn't a good idea either.

OpenGL not rendering colors

For some reason the colors are not rendering when I run my program. With the addition of glm, I have been running into issues with some strange images rendering on run. It might be a library, but it is highly doubtful. I have checked and rechecked my includes and libraries. I am using Eclipse.
Here is my code
/*
* Module5.cpp
*
* Created on: Aug 21, 2017
* Author:
*/
#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>
using namespace std;
#define WINDOW_TITLE "Window"
#ifndef GLSL
#define GLSL(Version, Source) "#version " #Version "\n" #Source
#endif
GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO; //Global variables for Buffer Object etc.
GLfloat cameraSpeed = 0.0005f;
GLchar currentKey; //will store key pressed
glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, 5.0f);
glm::vec3 CameraUpY = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 CameraForwardZ = glm::vec3(0.0f, 0.0f, -1.0f);
void UResizeWindow(int, int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
void UKeyboard(unsigned char key, int x, int y);
void UKeyReleased(unsigned char key, int x, int y);
const GLchar * vertexShaderSource = GLSL(330,
layout(location=0) in vec3 position; //incoming data
layout(location=1) in vec3 color;
out vec4 mobileColor; //Attrib pointer 0
//out vec4 colorFromVShader;
//uniform mat4 primitiveTransform; //transform for shape
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
//gl_Position = primitiveTransform * vertex_Position; //move object on y-axis .5
gl_Position = projection * view * model * vec4(position, 1.0f); //move object on y-axis .5
//colorFromVShader = colorFromVBO;
mobileColor = color;
}
);
const GLchar * fragmentShaderSource = GLSL(440,
in vec3 mobileColor;
out vec4 gpuColor;
void main(){
// gl_FragColor= vec4(1.0, 0.5, 0.0, 1.0);
gpuColor= vec4(mobileColor, 1.0);
}
);
//Main
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)
{
cout << "Failed to initialize glew!" << endl;
return -1;
}
UCreateShader();
UCreateBuffers();
glUseProgram(shaderProgram);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(URenderGraphics);
glutKeyboardFunc(UKeyboard);
glutKeyboardUpFunc(UKeyReleased);
glutMainLoop();
glDeleteVertexArrays(1, &VAO);//cleanup
glDeleteBuffers(1, &VBO);//cleanup
return 0;
}
void UResizeWindow(int w, int h)
{
WindowWidth = w;
WindowHeight = h;
glViewport(0, 0, WindowWidth, WindowHeight);
}
void URenderGraphics(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clears screen
glBindVertexArray(VAO); //activate vertex array to render the vertices that render our shape
if(currentKey == 'w')
cameraPosition += cameraSpeed * CameraForwardZ;
if(currentKey == 's')
cameraPosition -= cameraSpeed * CameraForwardZ;
if(currentKey == 'a')
cameraPosition -= cameraSpeed * CameraForwardZ;
if(currentKey == 'd')
cameraPosition += cameraSpeed * CameraForwardZ;
glm::mat4 model;
model = glm::translate(model,glm::vec3(0.0f, 0.0f, 0.0));
model = glm::rotate(model, glm::radians(-45.0f), glm::vec3(0.0f, 1.0f, 0.0f)); //rotate shape x-axis by 1.0f
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f)); //scale shape
glm::mat4 view; //camera
view = glm::lookAt(cameraPosition, cameraPosition + CameraForwardZ, CameraUpY); //move camera back by 5 (z)
glm::mat4 projection;
projection = glm::perspective(45.0f, (GLfloat)WindowWidth / (GLfloat)WindowHeight, 0.1f, 100.0f);
//projection = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f);
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));
//apply projection matrix
/*
glm::mat4 newTransform; //references 4 x 4 matrix
newTransform = glm::translate(newTransform, glm::vec3(0.0f, 0.5f, 0.0)); //make square move up y-axis
newTransform = glm::rotate(newTransform, glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); //rotate shape
//newTransform = glm::scale(newTransform, glm::vec3(0.5f, 0.5f, 0.5f)); //rotate shape
GLuint transformInfo = glGetUniformLocation(ProgramId, "primitiveTransform"); //id for shader, name of variable shader
glUniformMatrix4fv(transformInfo, 1, GL_FALSE, glm::value_ptr(newTransform));
*/
glutPostRedisplay();
glDrawArrays(GL_TRIANGLES, 0 , 36);
glBindVertexArray(0);
glutSwapBuffers();
}
void UCreateShader()
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
/*void applyDepthSettings() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}*/
void UCreateBuffers()
{
//specify coords for creating square
// Positon and Color data
GLfloat vertices[] = {
// Vertex Positions // Colors
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Top Right Vertex 0
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Bottom Right Vertex 1
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Bottom Left Vertex 2
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // Top Left Vertex 3
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top Right Vertex 0
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Bottom Right Vertex 1
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Bottom Left Vertex 2
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top Left Vertex 3
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Top Right Vertex 0
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom Left Vertex 2
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Top Left Vertex 3
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, // Top Right Vertex 0
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // Bottom Right Vertex 1
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // Bottom Left Vertex 2
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // Top Left Vertex 3
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, // Top Right Vertex 0
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, // Bottom Right Vertex 1
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // Bottom Left Vertex 2
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // Top Left Vertex 3
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, // Top Right Vertex 0
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, // Bottom Left Vertex 2
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, // Top Left Vertex 3
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f,
};
//generate id's for buffer object
glGenVertexArrays(1, &VAO); //generate for Vertex Array Object
glGenBuffers(1, &VBO); //generate for Vertex Buffer Object
glBindVertexArray(VAO); //activate text array object
glBindBuffer(GL_ARRAY_BUFFER, VBO); //activating VBO buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //pass in size of array from line 128
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
glEnableVertexAttribArray(0);//enable vertex attribute pointer, starting position of x,y,z
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));//send data to shader (accepts 6 arguments)GL_FALSE=not using normalization
glEnableVertexAttribArray(1);//specify color starting point
glBindVertexArray(0); //deactivate vertex array object (VBO)
}
void UKeyboard(unsigned char key, GLint x, GLint y)
{
switch(key)
{
case'w':
cout<<"You pressed W!" <<endl;
break;
case 's':
cout<<"You pressed S!"<<endl;
break;
case'a':
cout<<"You pressed A!"<<endl;
break;
case 'd':
cout<<"You pressed D!"<<endl;
break;
default:
cout<<"Press a key!"<<endl;
}
}
/*Implements the UKeyReleased function*/
void UKeyReleased(unsigned char key, GLint x, GLint y)
{
cout<<"Key released"<<endl;
}
Your vertex shader does not compile, because mobileColor is of type vec4 and color is of type vec3.
Change:
mobileColor = color;
to:
mobileColor = vec4(color, 1.0);
Note, your shader program was not used, because it was not successfully built. Everything you have drawn was drawn by default OpenGL with the currently set glColor, which is by default white (1,1,1,1).
Whether the compilation of a shader succeeded can be checked by glGetShaderiv, and the error message can be retrieved with glGetShaderInfoLog:
GLint status = GL_TRUE;
glCompileShader( shaderStage );
glGetShaderiv( shaderStage, GL_COMPILE_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetShaderiv( shaderStage, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen+1 );
GLsizei written;
glGetShaderInfoLog( shaderStage, logLen, &written, log.data() );
std::cout << "compile error:" << std::endl << log.data() << std::endl;
}
Whether a program was linked successfully can be checked by glGetProgramiv, and the error message can be retrieved with glGetProgramInfoLog:
GLint status = GL_TRUE;
glLinkProgram( shaderProgram );
glGetProgramiv( shaderProgram, GL_LINK_STATUS, &status );
if ( status == GL_FALSE )
{
GLint logLen;
glGetProgramiv( shaderProgram, GL_INFO_LOG_LENGTH, &logLen );
std::vector< char >log( logLen+1 );
GLsizei written;
glGetProgramInfoLog( shaderProgram, logLen, &written, log.data() );
std::cout << "link error:" << std::endl << log.data() << std::endl;
}