I am writing a mini OpenGL program with instancing in it. But when I ran the code, only one instance was shown. I thought that it should be easy, but I failed. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <GL/glut.h>
#include <vector>
#include <fstream>
#include <string>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
#define WIDTH 1280
#define HEIGHT 720
static const std::string vertexShader =
"#version 330 core\n"
"in vec3 position;\n"
"in vec3 color;\n"
"in ivec3 Glob_pos;\n"
"uniform mat4 MVP;\n"
"out vec3 ToFragColor;\n"
"void main(){\n"
" gl_Position = MVP * vec4(vec3(Glob_pos) + position,1.0);\n"
" ToFragColor = color;\n"
"}\n";
static const std::string fragmentShader =
"#version 330 core\n"
"in vec3 ToFragColor;\n"
"uniform vec3 Cam;\n"
"out vec3 out_color;\n"
"void main(){\n"
" out_color = ToFragColor;\n"
"}\n";
static const GLfloat CUBE_VERT[12*3*2*3] = {
-1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f,
-1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 0.0f,
1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 1.0f,
-1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 0.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, 0.0f,
-1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
-1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f,-1.0f, 0.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
//TOP
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 0.0f, -1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
-1.0f, 1.0f,-1.0f, 0.0f, 0.0f, -1.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f
};
GLuint LoadShaders();
void setup(GLuint programID);
void render(GLuint programID);
void computeMatricesFromInputs(GLFWwindow* window);
GLuint vao, vbo, ib;
GLuint MatrixID;
GLuint CamID;
//For model view matrix calculation
glm::vec3 CamLoc = glm::vec3(0,0,-5);
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float) WIDTH / (float)HEIGHT, 1.0f, 1000.0f);
glm::mat4 View = glm::lookAt(
CamLoc,
glm::vec3(0,0,0),
glm::vec3(0,1,0)
);
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 mvp = Projection * View * Model;
glm::vec3 direction;
float horizontalAngle = 3.14f;
float verticalAngle = 0.0f;
float initialFoV = 45.0f;
float speed = 3.0f;
float mouseSpeed = 0.005f;
float mtimeScale = 0.1f;
float ktimeScale = 1.0f;
float FoV = initialFoV; //- 5 * glfwGetMouseWheel();
double lastPos[] = { 0,0 };
const glm::vec3 null = glm::vec3(0,0,0);
//For model view matrix calculation -- end
std::vector<int> instanceObj;
int main(int argc, char **argv){
glutInit(&argc, argv);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window;
window = glfwCreateWindow(WIDTH, HEIGHT, "DearDaniel's OpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
glewExperimental = true;
glewInit();
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
GLuint programID = LoadShaders();
MatrixID = glGetUniformLocation(programID, "MVP");
CamID = glGetUniformLocation(programID, "Cam");
glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS);
glEnable(GL_CULL_FACE); glCullFace(GL_BACK);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
setup(programID);
do{
glUseProgram(programID);
computeMatricesFromInputs(window);
render(programID);
glfwSwapBuffers(window);
glfwPollEvents();
} while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
glDeleteProgram(programID);
return 0;
}
GLuint LoadShaders(){
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
char const * VertexSourcePointer = vertexShader.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
char const * FragmentSourcePointer = fragmentShader.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
glDetachShader(ProgramID, VertexShaderID);
glDetachShader(ProgramID, FragmentShaderID);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
void setup(GLuint programID) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 12*3*2*3*sizeof(int), CUBE_VERT, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glGenBuffers(1, &ib);
glBindBuffer(GL_ARRAY_BUFFER, ib);
glVertexAttribPointer(2, 3, GL_INT, GL_FALSE, 3 * sizeof( int), (void*) 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribDivisor(2, 1);
glBindVertexArray(0);
}
void render(GLuint programID) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, ib);
instanceObj.clear();
for (int i = 0; i < 32; i+=2)
for (int j = 0; j < 32; j +=2)
for (int k = 0; k < 32; k +=2) {
instanceObj.push_back(i);
instanceObj.push_back(j);
instanceObj.push_back(k);
}
glBufferData(GL_ARRAY_BUFFER, sizeof(int)*instanceObj.size(), &instanceObj[0], GL_STATIC_DRAW);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
glUniform3f(CamID, CamLoc.x, CamLoc.y, CamLoc.z);
glDrawArraysInstanced(GL_TRIANGLES, 0, 36, 16*16*16);
glBindVertexArray(0);
}
void computeMatricesFromInputs(GLFWwindow* window){
static double lastTime = glfwGetTime();
double currentTime = glfwGetTime();
float deltaTime = float(currentTime - lastTime);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
float hori = horizontalAngle + mouseSpeed * float( xpos - lastPos[0] ) * mtimeScale;
float vert = verticalAngle + mouseSpeed * float( ypos - lastPos[1] ) * mtimeScale;
horizontalAngle = hori;
if (vert > -3.14f/2.0f && vert < 3.14f/2.0f) verticalAngle = vert;
lastPos[0] = xpos;
lastPos[1] = ypos;
direction = glm::vec3 (
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);
glm::vec3 up = glm::cross( right, direction );
if (glfwGetKey( window, GLFW_KEY_UP ) == GLFW_PRESS) CamLoc += direction * deltaTime * speed * ktimeScale;
if (glfwGetKey( window, GLFW_KEY_DOWN ) == GLFW_PRESS) CamLoc -= direction * deltaTime * speed * ktimeScale;
if (glfwGetKey( window, GLFW_KEY_RIGHT ) == GLFW_PRESS) CamLoc += right * deltaTime * speed * ktimeScale;
if (glfwGetKey( window, GLFW_KEY_LEFT ) == GLFW_PRESS) CamLoc -= right * deltaTime * speed * ktimeScale;
Projection = glm::perspective(glm::radians(FoV), 4.0f / 3.0f, 0.1f, 100.0f);
View = glm::lookAt(
CamLoc, // Camera is here
CamLoc+direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
);
lastTime = currentTime;
mvp = Projection * View;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
printf("%f ", mvp[i][j]);
}
printf("\n");
}
}
I used the following command to compile my code:
g++ miniGL.cpp -Wall -std=c++11 -L/usr/local/lib -pthread -lGLEW -lGLU -lGL -lglut -lglfw3 -ldl -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lXcursor -lglfw -o ogl
I was meant to render 16*16*16 cubes onto the screen, but only one has shown. I spent more than ten hours for debugging. Now I gave up and shamelessly seeking for help.
You have to use glVertexAttribIPointer, when defining the array of generic vertex attribute data, for the vertex attrbute in ivec3 Glob_pos;.
glVertexAttribPointer is for floating point attributes only (integral data will be converted to floating point).
See the Khronos group reference page for glVertexAttribPointer:
For glVertexAttribPointer, if normalized is set to GL_TRUE, it indicates that values stored in an integer format are to be mapped to the range [-1,1] (for signed values) or [0,1] (for unsigned values) when they are accessed and converted to floating point. Otherwise, values will be converted to floats directly without normalization.
For glVertexAttribIPointer, only the integer types GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT are accepted. Values are always left as integer values.
Note, probably Glob_pos is never set and has always the same, but undefined value.
Since you don't use glGetAttribLocation to get the attribute indices for the vertex attributes (position, color, Glob_pos), you should use Layout Qualifiers:
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in ivec3 Glob_pos;
Related
I'm trying to create a toggle to change between ortho and perspective views. I have successfully coded buttons to initiate camera movement but can't figure out how to code a button to perform this change.
How I would toggle the projection modes here?
My code so far:
#include <iostream> // cout, cerr
#include <cstdlib> // EXIT_FAILURE
#include <GL/glew.h> // GLEW library
#include <GLFW/glfw3.h> // GLFW library
// GLM Math Header inclusions
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//Camera class
#include <learnOpengl/camera.h>
using namespace std; // Standard namespace
/*Shader program Macro*/
#ifndef GLSL
#define GLSL(Version, Source) "#version " #Version " core \n" #Source
#endif
// Unnamed namespace
namespace
{
const char* const WINDOW_TITLE = "Milestone 4-5"; // Macro for window title
// Variables for window width and height
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
// Stores the GL data relative to a given mesh
struct GLMesh
{
GLuint vao; // Handle for the vertex array object
GLuint vbo; // Handles for the vertex buffer objects
GLuint nVertices; // Number of indices of the mesh
};
// Main GLFW window
GLFWwindow* gWindow = nullptr;
// Triangle mesh data
GLMesh gMesh;
// Shader program
GLuint gProgramId;
// camera
Camera gCamera(glm::vec3(0.0f, 0.0f, 3.0f));
float gLastX = WINDOW_WIDTH / 2.0f;
float gLastY = WINDOW_HEIGHT / 2.0f;
bool gFirstMouse = true;
glm::vec3 gCameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 gCameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 gCameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// timing
float gDeltaTime = 0.0f; // time between current frame and last frame
float gLastFrame = 0.0f;
}
/* User-defined Function prototypes to:
* initialize the program, set the window size,
* redraw graphics on the window when resized,
* and render graphics on the screen
*/
bool UInitialize(int, char* [], GLFWwindow** window);
void UResizeWindow(GLFWwindow* window, int width, int height);
void UProcessInput(GLFWwindow* window);
void UMousePositionCallback(GLFWwindow* window, double xpos, double ypos);
void UMouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void UMouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void UCreateMesh(GLMesh& mesh);
void UDestroyMesh(GLMesh& mesh);
void URender();
bool UCreateShaderProgram(const char* vtxShaderSource, const char* fragShaderSource, GLuint& programId);
void UDestroyShaderProgram(GLuint programId);
/* Vertex Shader Source Code*/
const GLchar* vertexShaderSource = GLSL(440,
layout(location = 0) in vec3 position; // Vertex data from Vertex Attrib Pointer 0
layout(location = 1) in vec4 color; // Color data from Vertex Attrib Pointer 1
out vec4 vertexColor; // 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
vertexColor = color; // references incoming color data
}
);
/* Fragment Shader Source Code*/
const GLchar* fragmentShaderSource = GLSL(440,
in vec4 vertexColor; // Variable to hold incoming color data from vertex shader
out vec4 fragmentColor;
void main()
{
fragmentColor = vec4(vertexColor);
}
);
int main(int argc, char* argv[])
{
if (!UInitialize(argc, argv, &gWindow))
return EXIT_FAILURE;
// Create the mesh
UCreateMesh(gMesh); // Calls the function to create the Vertex Buffer Object
// Create the shader program
if (!UCreateShaderProgram(vertexShaderSource, fragmentShaderSource, gProgramId))
return EXIT_FAILURE;
// Sets the background color of the window to black (it will be implicitly used by glClear)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// render loop
// -----------
while (!glfwWindowShouldClose(gWindow))
{
// per-frame timing
// --------------------
float currentFrame = glfwGetTime();
gDeltaTime = currentFrame - gLastFrame;
gLastFrame = currentFrame;
// input
// -----
UProcessInput(gWindow);
// Render this frame
URender();
glfwPollEvents();
}
// Release mesh data
UDestroyMesh(gMesh);
// Release shader program
UDestroyShaderProgram(gProgramId);
exit(EXIT_SUCCESS); // Terminates the program successfully
}
// Initialize GLFW, GLEW, and create a window
bool UInitialize(int argc, char* argv[], GLFWwindow** window)
{
// GLFW: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// GLFW: window creation
// ---------------------
* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
if (*window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return false;
}
glfwMakeContextCurrent(*window);
glfwSetFramebufferSizeCallback(*window, UResizeWindow);
glfwSetCursorPosCallback(*window, UMousePositionCallback);
glfwSetScrollCallback(*window, UMouseScrollCallback);
glfwSetMouseButtonCallback(*window, UMouseButtonCallback);
// tell GLFW to capture our mouse
glfwSetInputMode(*window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// GLEW: initialize
// ----------------
// Note: if using GLEW version 1.13 or earlier
glewExperimental = GL_TRUE;
GLenum GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult)
{
std::cerr << glewGetErrorString(GlewInitResult) << std::endl;
return false;
}
// Displays GPU OpenGL version
cout << "INFO: OpenGL Version: " << glGetString(GL_VERSION) << endl;
return true;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
void UProcessInput(GLFWwindow* window)
{
static const float cameraSpeed = 2.5f;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
gCamera.ProcessKeyboard(FORWARD, gDeltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
gCamera.ProcessKeyboard(BACKWARD, gDeltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
gCamera.ProcessKeyboard(LEFT, gDeltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
gCamera.ProcessKeyboard(RIGHT, gDeltaTime);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
gCamera.ProcessKeyboard(UP, gDeltaTime);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
gCamera.ProcessKeyboard(DOWN, gDeltaTime);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
void UResizeWindow(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void UMousePositionCallback(GLFWwindow* window, double xpos, double ypos)
{
if (gFirstMouse)
{
gLastX = xpos;
gLastY = ypos;
gFirstMouse = false;
}
float xoffset = xpos - gLastX;
float yoffset = gLastY - ypos; // reversed since y-coordinates go from bottom to top
gLastX = xpos;
gLastY = ypos;
gCamera.ProcessMouseMovement(xoffset, yoffset);
}
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void UMouseScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
gCamera.ProcessMouseScroll(yoffset);
}
// glfw: handle mouse button events
// --------------------------------
void UMouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
switch (button)
{
case GLFW_MOUSE_BUTTON_LEFT:
{
if (action == GLFW_PRESS)
cout << "Left mouse button pressed" << endl;
else
cout << "Left mouse button released" << endl;
}
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
{
if (action == GLFW_PRESS)
cout << "Middle mouse button pressed" << endl;
else
cout << "Middle mouse button released" << endl;
}
break;
case GLFW_MOUSE_BUTTON_RIGHT:
{
if (action == GLFW_PRESS)
cout << "Right mouse button pressed" << endl;
else
cout << "Right mouse button released" << endl;
}
break;
default:
cout << "Unhandled mouse button event" << endl;
break;
}
}
// Functioned called to render a frame
void URender()
{
// Enable z-depth
glEnable(GL_DEPTH_TEST);
// Clear the frame and z buffers
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 1. Scales the object by 0.5
glm::mat4 scale = glm::scale(glm::vec3(0.5f, 0.5f, 0.5f));
// 2. Rotates shape by 37 degrees
glm::mat4 rotation = glm::rotate(37.0f, glm::vec3(1.0, 0.4f, 1.0f));
// 3. Place object at the origin
glm::mat4 translation = glm::translate(glm::vec3(0.0f, 0.0f, 0.0f));
// Model matrix: transformations are applied right-to-left order
glm::mat4 model = translation * rotation * scale;
// camera/view transformation
glm::mat4 view = gCamera.GetViewMatrix();
// Creates a perspective projection
glm::mat4 projection = glm::perspective(glm::radians(gCamera.Zoom), (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, 0.1f, 100.0f);
// Set the shader to be used
glUseProgram(gProgramId);
// Retrieves and passes transform matrices to the Shader program
GLint modelLoc = glGetUniformLocation(gProgramId, "model");
GLint viewLoc = glGetUniformLocation(gProgramId, "view");
GLint projLoc = glGetUniformLocation(gProgramId, "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));
// Activate the VBOs contained within the mesh's VAO
glBindVertexArray(gMesh.vao);
// Draws the triangles
glDrawArrays(GL_TRIANGLES, 0, gMesh.nVertices);
// Deactivate the Vertex Array Object
glBindVertexArray(0);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
glfwSwapBuffers(gWindow); // Flips the the back buffer with the front buffer every frame.
}
// Implements the UCreateMesh function
void UCreateMesh(GLMesh& mesh)
{
// Position and Color data
GLfloat verts[] = {
// Vertex Positions // Colors (r,g,b,a)
2.8f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top right 0
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
-0.8f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top left 3
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
-0.8f, 0.2f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom left 2
-0.8f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top left 3
2.8f, 2.0f, -1.0f, 0.5f, 0.5f, 1.0f, 1.0f, // 4 back top right
2.8f, 0.2f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // 5 back bot right
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
2.8f, 0.2f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // 5 back bot right
-0.8f, 0.2f, -1.0f, 0.2f, 0.2f, 0.5f, 1.0f, // 6 back bot left
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
2.8f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top right 0
2.8f, 2.0f, -1.0f, 0.5f, 0.5f, 1.0f, 1.0f, // 4 back top right
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
2.8f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top right 0
-0.8f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top left 3
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
2.8f, 0.2f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // 5 back bot right
-0.8f, 0.2f, -1.0f, 0.2f, 0.2f, 0.5f, 1.0f, // 6 back bot left
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
-0.8f, 0.2f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom left 2
-0.8f, 0.2f, -1.0f, 0.2f, 0.2f, 0.5f, 1.0f, // 6 back bot left
-0.8f, 0.2f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom left 2
-0.8f, 2.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // top left 3
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
-0.8f, 0.2f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // bottom left 2
-0.8f, 0.2f, -1.0, 0.2f, 0.2f, 0.5f, 1.0f, // 6 back bot left
-0.8f, 2.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7 back top left
2.8f, 2.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // top right 0
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
2.8f, 2.0f, -1.0f, 0.5f, 0.5f, 1.0f, 1.0f, // 4 back top right
2.8f, 0.2f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f, // bottom right 1
2.8f, 2.0f, -1.0f, 0.5f, 0.5f, 1.0f, 1.0f, // 4 back top right
2.8f, 0.2f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // 5 back bot right
1.6f, 1.8f, 0.1f, 0.0f, 1.0f, 1.0f, 1.0f, // 8 top of vent pyramid
2.2f, 1.0f, 0.1f, 1.0f, 0.0f, 1.0f, 1.0f, // 9 right side of vent pyramid
1.5f, 1.2f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f, // 11 center apex
1.6f, 0.2f, 0.1f, 1.0f, 1.0f, 0.0f, 1.0f, // 10 bottom of vent pyramid
2.2f, 1.0f, 0.1f, 1.0f, 0.0f, 1.0f, 1.0f, // 9 right side of vent pyramid
1.5f, 1.2f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f, // 11 center apex
1.6f, 0.2f, 0.1f, 1.0f, 1.0f, 0.0f, 1.0f, // 10 bottom of vent pyramid
1.1f, 1.0f, 0.1f, 0.0f, 1.0f, 0.0f, 1.0f, // 12 left side of vent pyramid
1.5f, 1.2f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f, // 11 center apex
1.1f, 1.0f, 0.1f, 0.0f, 1.0f, 0.0f, 1.0f, // 12 left side of vent pyramid
1.5f, 1.2f, 0.1f, 0.0f, 0.0f, 0.0f, 0.0f, // 11 center apex
1.6f, 1.8f, 0.1f, 0.0f, 1.0f, 1.0f, 1.0f, // 8 top of vent pyramid
-4.0f, 4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
4.0f, 4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
-4.0f, -4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
4.0f, 4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
4.0f, -4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
-4.0f, -4.0f, -1.0f, 0.0f, 0.25f, 0.25f, 1.0f,
};
const GLuint floatsPerVertex = 3;
const GLuint floatsPerColor = 4;
mesh.nVertices = sizeof(verts) / (sizeof(verts[0]) * (floatsPerVertex + floatsPerColor));
glGenVertexArrays(1, &mesh.vao); // we can also generate multiple VAOs or buffers at the same time
glBindVertexArray(mesh.vao);
// Create VBO
glGenBuffers(1, &mesh.vbo);
glBindBuffer(GL_ARRAY_BUFFER, mesh.vbo); // Activates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU
// Strides between vertex coordinates
GLint stride = sizeof(float) * (floatsPerVertex + floatsPerColor);
// Create Vertex Attribute Pointers
glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride, (char*)(sizeof(float) * floatsPerVertex));
glEnableVertexAttribArray(1);
}
void UDestroyMesh(GLMesh& mesh)
{
glDeleteVertexArrays(1, &mesh.vao);
glDeleteBuffers(1, &mesh.vbo);
}
// Implements the UCreateShaders function
bool UCreateShaderProgram(const char* vtxShaderSource, const char* fragShaderSource, GLuint& programId)
{
// Compilation and linkage error reporting
int success = 0;
char infoLog[512];
// Create a Shader program object.
programId = glCreateProgram();
// Create the vertex and fragment shader objects
GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
// Retrive the shader source
glShaderSource(vertexShaderId, 1, &vtxShaderSource, NULL);
glShaderSource(fragmentShaderId, 1, &fragShaderSource, NULL);
// Compile the vertex shader, and print compilation errors (if any)
glCompileShader(vertexShaderId); // compile the vertex shader
// check for shader compile errors
glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShaderId, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
return false;
}
// compile the fragment shader
glCompileShader(fragmentShaderId);
// check for shader compile errors
glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShaderId, sizeof(infoLog), NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
return false;
}
// Attached compiled shaders to the shader program
glAttachShader(programId, vertexShaderId);
glAttachShader(programId, fragmentShaderId);
glLinkProgram(programId); // links the shader program
// check for linking errors
glGetProgramiv(programId, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(programId, sizeof(infoLog), NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
return false;
}
glUseProgram(programId); // Uses the shader program
return true;
}
void UDestroyShaderProgram(GLuint programId)
{
glDeleteProgram(programId);
}
Simply change your
glm::mat4 projection = glm::perspective(glm::radians(gCamera.Zoom), (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, 0.1f, 100.0f);
to use glm::ortho depending on your game state.
You'll likely have to tinker a bit with the view projection as well.
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);
}
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'm currently learning OpengGL and i can't seem to resolve this "void" error in my vertexshader. No issue with my fragmentshader or main.cpp file thus far. any input is greatly appreciated...............................................................................
Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include GLEW
#include <GL/glew.h>
#include <vector>
#include <iostream>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <common/skyboxtex.hpp>
#include <common/shader.hpp>
#include <common/texture.hpp>
#include <common/controls.hpp>
#include <glm/gtc/type_ptr.hpp>
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Tutorial 0 - Keyboard and Mouse", 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 we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Hide the mouse and enable unlimited mouvement
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set the mouse at the center of the screen
glfwPollEvents();
glfwSetCursorPos(window, 1024/2, 768/2);
// Dark blue background
//glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader");
GLuint skyboxShader = LoadShaders("skybox.FragmentShader", "skybox.VertexShader");
// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
GLuint ModelMatrixID = glGetUniformLocation(programID, "M");
GLuint ViewMatrixID = glGetUniformLocation(programID, "V");
// Load the texture
GLuint Texture = loadDDS("uvtemplate.DDS");
// Get a handle for our "myTextureSampler" uniform
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
//static GLuint terrainVbo = 0, terraininVboNorm = 0, terrainEbo = 0;
//static int terrainCount = 0;
//skyboxvertieces
float skyboxVertices[] = {
// positions
-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
};
// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
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
};
// Two UV coordinatesfor each vertex. They were created with Blender.
static const GLfloat g_uv_buffer_data[] = {
0.000059f, 0.000004f,
0.000103f, 0.336048f,
0.335973f, 0.335903f,
1.000023f, 0.000013f,
0.667979f, 0.335851f,
0.999958f, 0.336064f,
0.667979f, 0.335851f,
0.336024f, 0.671877f,
0.667969f, 0.671889f,
1.000023f, 0.000013f,
0.668104f, 0.000013f,
0.667979f, 0.335851f,
0.000059f, 0.000004f,
0.335973f, 0.335903f,
0.336098f, 0.000071f,
0.667979f, 0.335851f,
0.335973f, 0.335903f,
0.336024f, 0.671877f,
1.000004f, 0.671847f,
0.999958f, 0.336064f,
0.667979f, 0.335851f,
0.668104f, 0.000013f,
0.335973f, 0.335903f,
0.667979f, 0.335851f,
0.335973f, 0.335903f,
0.668104f, 0.000013f,
0.336098f, 0.000071f,
0.000103f, 0.336048f,
0.000004f, 0.671870f,
0.336024f, 0.671877f,
0.000103f, 0.336048f,
0.336024f, 0.671877f,
0.335973f, 0.335903f,
0.667969f, 0.671889f,
1.000004f, 0.671847f,
0.667979f, 0.335851f
};
//Skybox
GLuint skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1,&skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT,GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
//skybox image faces
std::vector <std::string>faces;
{
"right.BMP",
"left.BMP",
"top.BMP",
"bottom.BMP",
"front.BMP",
"back.BMP";
};
unsigned int CubemapTexture = loadCubemap(faces);
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);
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);
do{
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our shader
glUseProgram(programID);
glUseProgram(skyboxShader);
// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glm::mat4 view = glm::mat4(glm::mat3(getViewMatrix()));
// skybox Uniform INCLUDE glm value_PTR.hpp
glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "projection"), 1, GL_FALSE, glm::value_ptr(ProjectionMatrix));
// Send our transformation to the currently bound shader,
// in the "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 our "myTextureSampler" sampler to use Texture Unit 0
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the 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, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size : U+V => 2
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
glm::mat4 ModelMatrix2 = glm::mat4(1.0);
ModelMatrix2 = glm::translate(ModelMatrix2, glm::vec3(2.0f, 0.0f, 0.0f));
glm::mat4 MVP2 = ProjectionMatrix * ViewMatrix * ModelMatrix2;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP2[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix2[0][0]);
// 2nd object
glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
//newshit
glBindVertexArray(skyboxVAO);
glDepthFunc(GL_LEQUAL);
glUseProgram(skyboxShader);
glBindTexture(GL_TEXTURE_CUBE_MAP, CubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
// 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, &TextureID);
glDeleteVertexArrays(1, &VertexArrayID);
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
This is the skybox vertexshader
#version 330 core
layout (location = 0) in vec3 position;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
out gl_PerVertex
{
vec4 gl_position;
}
ERROR: 0:22: 'void' : syntax error syntax error
void main() <--?? error
{
vec4 pos = projection * view * vec4(position, 1.0);
gl_Position = pos.xyww;
TexCoords = position;
}
skybox fragmentshader
#version 330 core
in vec3 TexCoords;
out vec4 color;
uniform samplerCube skybox;
void main()
{
color = texture(skybox, TexCoords);
}
You can avoid using block syntax to simplify your shader. I noticed a number of potential issues/syntax errors with your vertex shader (eg the redundant use of the gl_PerVertex block, missing semicolons, etc).
Also wasn't sure if you intended to pass pos.xyww or pos.xyzw to gl_Position as this looked non-standard to me.
Try this:
#version 330 core
layout (location = 0) in vec3 position;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
vec4 pos = projection * view * vec4(position.xyz, 1.0);
gl_Position = pos;
TexCoords = position;
}
I have a script that I am unable to make fully 3D. So far on ortho the background shows up only. In perspective, almost nothing shows up but when I move the camera it does change color.
projection = glm::perspective(glm::radians(44.0f), static_cast<GLfloat>(Width) / static_cast<GLfloat>(Height), 0.1f, 100.0f);
projection = glm::ortho(0.0f, static_cast<GLfloat>(Width), static_cast<GLfloat>(Height), 0.0f, -1.0f, 1.0f);
See below for images:
Image 1:
3D version - Ortho
Image 2:
3D version - Ortho
Image 3:
2D version - Stable and works perfectly
Here is my code that creates and renders sprites:
#ifndef SPRITE_H
#define SPRITE_H
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "ShaderClass.h"
#include "TextureClass.h"
class Sprite
{
public:
Shader shader;
// Set up Vertex Array Object && Vertex Buffer Object
GLuint VAO,
VBO;
// Constructor
Sprite(Shader &shader){ this->shader = shader; setBuffers(); }
// Deconstructor
~Sprite(){ glDeleteVertexArrays(1, &VAO); }
// Initializes and configures the buffers and vertex attributes
void setBuffers()
{
// Define vertices for the sprite
GLfloat vertices[] = {
// x y z u v
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f
};
//
// OpenGL buffers
// Initialization code using Vertex Array Object (VAO) (done once (unless the object frequently changes))
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Bind Vertex Array Object
glBindVertexArray(VAO);
//Attributes
glEnableVertexAttribArray(0); // location = 0
glVertexAttribPointer(
0, // location = 0
3, // 3 components (x, y, z)
GL_FLOAT, GL_FALSE,
5 * sizeof(GLfloat), // stride: 5 * float (x, y, z, u, v)
(GLvoid*)0); // offset: 0
glEnableVertexAttribArray(1); // location = 1
glVertexAttribPointer(
1, // location = 1
2, // 2 components (u, v)
GL_FLOAT, GL_FALSE,
5 * sizeof(GLfloat), // stride: 5 * float (x, y, z, u, v)
(GLvoid*)(3 * sizeof(GLfloat))); // offset: 3 * float (x, y, z)
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Unbind the VAO
glBindVertexArray(0);
}
// Renders the sprite
void DrawSprite(Texture &texture, glm::vec2 position, glm::vec2 size = glm::vec2(10, 10), GLfloat rotate = 0.0f, glm::vec3 colour = glm::vec3(1.0f))
{
// Apply the shader
this->shader.Use();
// Transform the object
glm::mat4 transformObject;
transformObject = glm::translate(transformObject, glm::vec3(position, 0.0f));
transformObject = glm::translate(transformObject, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
transformObject = glm::rotate(transformObject, rotate, glm::vec3(0.0f, 0.0f, 1.0f));
transformObject = glm::translate(transformObject, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
transformObject = glm::scale(transformObject, glm::vec3(size, 1.0f));
this->shader.SetVector3f("spriteColour", colour);
this->shader.SetMatrix4("transformObject", transformObject);
glActiveTexture(GL_TEXTURE0);
// Bind the appropriate texture to *this* model
texture.Bind();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36 * 5);
glBindVertexArray(0);
}
};
#endif
These are the shaders that I am using:
Vertex shader:
#version 440 core
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 texCoord;
out vec2 TexCoords;
uniform mat4 transformObject;
uniform mat4 view;
uniform mat4 projection;
void main()
{
TexCoords = vec2(texCoord.x, texCoord.y);
gl_Position = projection * view * transformObject * vec4(Position.x, Position.y, Position.z, 1.0);
}
Fragment shader:
#version 440 core
in vec2 TexCoords;
out vec4 colour;
uniform sampler2D tex;
uniform vec3 spriteColour;
void main()
{
colour = vec4(spriteColour, 1.0) * texture(tex, TexCoords);
}
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;
}