GLSL shaders not working. No compile errors, and used glUseProgram - c++

I came up with code to render a rectangle, but the shaders won't work. It still has the blank white color.
Here I will include the important code
Main:
float verts[] = {
-.5f, -.5f, .0f,
-.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, .5f, .0f,
.5f, -.5f, .0f,
-.5f, -.5f, .0f
};
Shader shader("basicVert.glsl", "basicFrag.glsl");
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
shader.enable();
Shader.cpp (class functions)
Shader::Shader(const string vpath, const string fpath) {
Shader();
current_vpath = vpath;
current_fpath = fpath;
shaderID = init();
}
Shader::Shader(const char *vpath, const char *fpath) {
Shader(string(vpath), string(fpath));
}
Shader::~Shader() {
shaderID = NULL;
glDeleteProgram(shaderID);
}
void Shader::enable() {
glUseProgram(shaderID);
}
GLuint Shader::makeVertextShader(const char* source) {
GLuint vertShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShaderID, 1, &source, NULL);
glCompileShader(vertShaderID);
GLint r;
glGetShaderiv(vertShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(vertShaderID, GL_INFO_LOG_LENGTH, &l);
cout << l << endl;
char *bfer = new char[l];
glGetShaderInfoLog(vertShaderID, l, &l, bfer);
cerr << "Failed to compile VERTEXT SHADER! FILE NAME: " <<
current_vpath << endl;
cerr << bfer << endl;
glDeleteShader(vertShaderID);
delete[] bfer;
return NULL;
}
return vertShaderID;
}
GLuint Shader::makeFragmentShader(const char* source) {
GLuint fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShaderID, 1, &source, NULL);
glCompileShader(fragShaderID);
GLint r;
glGetShaderiv(fragShaderID, GL_COMPILE_STATUS, &r);
if (r == GL_FALSE) {
GLint l;
glGetShaderiv(fragShaderID, GL_INFO_LOG_LENGTH, &l);
char *bfer = new char[l];
glGetShaderInfoLog(fragShaderID, l, &l, bfer);
cerr << "Failed to compile FRAGMENT SHADER! FILE NAME: " <<
current_fpath << endl;
cerr << bfer << endl;
glDeleteShader(fragShaderID);
delete[] bfer;
return NULL;
}
return fragShaderID;
}
GLuint Shader::init() {
GLuint program = glCreateProgram();
const string vs = readFile(current_vpath);
const string vf = readFile(current_fpath);
const char *vertexsrc = vs.c_str();
const char *fragmentsrc = vf.c_str();
GLuint vertShaderID = this->makeVertextShader(vertexsrc);
GLuint fragShaderID = this->makeFragmentShader(fragmentsrc);
glAttachShader(program, vertShaderID);
glAttachShader(program, fragShaderID);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vertShaderID);
glDeleteShader(fragShaderID);
return program;
}
GLSL Vertex Shader
#version 330 core
layout(location = 0) in vec3 position;
void main(){
gl_Position = position;
}
GLSL Fragment Shader
#version 330 core
layout(location = 0) out vec4 color;
void main(){
color = vec4(1.0, 0.0, 1.0, 1.0);
gl_FragColor = color;
}

gl_FragColor is no longer supported in modern versions of GLSL
so it will be in Vertex Shader,
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
in FS,
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
//gl_FragColor is no longer supported in modern versions of GLSL
}

The vertex and fragment shader have to look like this:
#version 330 core
layout(location = 0) in vec3 position;
void main()
{
gl_Position = vec4( position.xyz, 1.0 );
}
#version 330 core
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 1.0, 1.0);
}
Explanation:
There are 2 issues in your code:
1.) While the vertex attribute position, in the Vertex Shader has the type vec3, the Built-in Variable (GLSL) gl_Position has the type vec4.
Either the type of the vertex attribute has to be changed:
layout(location = 0) in vec4 position;
or the assignment to gl_Position has to be adapted:
gl_Position = vec4( position.xyz, 1.0 );
2.) In the Fragment Shader either can be used the Built-in output Variable (GLSL) gl_FragColor:
void main()
{
gl_FragColor = [...];
}
or an explicit output variable has to be declared:
out vec4 color;
void main()
{
color = [...];
}

The problem seems to lie in the vertex shader: You pass a vec3 position to a vec4 gl_Position which effectively sets the w-coordinate to 0. What you need for proper rendering is a homogeneous coordinate of 1 (otherwise the division by w is a division by 0). Try to change your code to
gl_Position = vec4(position, 1.0);

Related

How to display vertices normal in opengl

I'm trying to show the normals of some vertices of the mesh when I select them but accordingly to the position of the cam these normals are displayed or not and the big problem is that they are displayed when they shouldn't (i.e. when i select vertices of the tibia but i'm looking at the calf so they should be hidden by the leg) and not displayed when they should be displayed.
I could be wrong but it's like some geometry of the model is drawn above the line. Moreover, if before calling the draw calls i disable the depth test it works (even if the problem of being showed when they shouldn't isn't resolved obviously). That's some images to give you an idea of what I mean.
WITH DEPTH TEST OFF:
DEPT TEST ON:
That's the code:
void StatusManager::Render() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (!animatedModel)
return;
Update();
// draw wireframe if enabled
if (wireframeEnabled)
DrawWireframe();
if (visualMode == Mode_Texture) {
// draw model
if (lightingMode == Mode_Flat)
DrawModel(modelFlatShader);
else if (lightingMode == Mode_Smooth)
DrawModel(modelSmoothShader);
else
DrawModel(modelNoLightShader);
}
else if (visualMode == Mode_CurrentBoneIDInfluence) {
DrawModel(currentBoneShader);
}
else if (visualMode == Mode_NumBones)
{
DrawModel(numBonesShader);
}
else
{
DrawModel(modelGreyShader);
}
// render selected vertices
DrawSelectedVertices();
if (!info.hitPoint)
return;
//DrawHotPoint();
if (selectionMode == Mode_Vertex)
DrawHoveredPoint();
if (selectionMode == Mode_Edge)
DrawHoveredLine();
if (selectionMode == Mode_Face)
DrawHoveredFace();
}
void StatusManager::DrawWireframe() {
wireframeShader.use();
glLineWidth(1.0f);
// model/view/projection transformations
glm::mat4 modelView = camera.viewMatrix;
wireframeShader.setMat4("modelView", modelView);
wireframeShader.setMat4("projection", projection);
// pass bones matrices to the shader
auto transforms = animator.GetFinalBoneMatrices();
for (int i = 0; i < transforms.size(); ++i)
wireframeShader.setMat4("finalBonesMatrices[" + std::to_string(i) + "]", transforms[i]);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
animatedModel.value().Draw(wireframeShader);
}
void StatusManager::DrawModel(Shader& modelShader) {
modelShader.use();
// model/view/projection transformations
modelShader.setMat4("modelView", camera.viewMatrix);
modelShader.setMat4("projection", projection);
if (visualMode == Mode_Texture)
modelShader.setVec3("light_pos", lightPos);
else if (visualMode == Mode_CurrentBoneIDInfluence)
modelShader.setInt("currentBoneID", currentBoneID);
// pass bones matrices to the shader
auto transforms = animator.GetFinalBoneMatrices();
for (int i = 0; i < transforms.size(); ++i)
modelShader.setMat4("finalBonesMatrices[" + std::to_string(i) + "]", transforms[i]);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
/*if (pause)
bakedModel.value().Draw(modelShader);
else*/
animatedModel.value().Draw(modelShader);
}
void StatusManager::DrawHoveredFace() {
assert(bakedModel.has_value());
assert(info.hitPoint.has_value());
Mesh& m = bakedModel.value().meshes[info.meshIndex];
Face& f = info.face.value();
//vertex
float hoveredVertices[9] = {
m.vertices[f.indices[0]].Position.x, m.vertices[f.indices[0]].Position.y, m.vertices[f.indices[0]].Position.z,
m.vertices[f.indices[1]].Position.x, m.vertices[f.indices[1]].Position.y, m.vertices[f.indices[1]].Position.z,
m.vertices[f.indices[2]].Position.x, m.vertices[f.indices[2]].Position.y, m.vertices[f.indices[2]].Position.z,
};
hoverShader.use();
glBindVertexArray(HVAO);
glBindBuffer(GL_ARRAY_BUFFER, HVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(hoveredVertices), &hoveredVertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.0, 0.0);
// model/view/projection transformations
glm::mat4 modelView = camera.viewMatrix;
hoverShader.setMat4("modelView", modelView);
hoverShader.setMat4("projection", projection);
glDrawArrays(GL_TRIANGLES, 0, 3);
//unbind
glBindVertexArray(0);
}
void StatusManager::DrawHoveredPoint() {
assert(bakedModel.has_value());
assert(info.hitPoint.has_value());
Mesh& m = bakedModel.value().meshes[info.meshIndex];
Face& f = info.face.value();
int index = getClosestVertexIndex(info.hitPoint.value(), m, f);
//vertex
float hoveredVertices[3] = { m.vertices[index].Position.x, m.vertices[index].Position.y, m.vertices[index].Position.z };
hoverShader.use();
glBindVertexArray(HVAO);
glBindBuffer(GL_ARRAY_BUFFER, HVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(float), &hoveredVertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.0, 0.0);
// model/view/projection transformations
glm::mat4 modelView = camera.viewMatrix;
hoverShader.setMat4("modelView", modelView);
hoverShader.setMat4("projection", projection);
glPointSize(8.0f);
glDrawArrays(GL_POINTS, 0, 1);
//unbind
glBindVertexArray(0);
}
void StatusManager::DrawHotPoint()
{
float hotVertices[3] = { hotPoint.x, hotPoint.y, hotPoint.z };
hoverShader.use();
glBindVertexArray(HVAO);
glBindBuffer(GL_ARRAY_BUFFER, HVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(float), &hotVertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.0, 0.0);
// model/view/projection transformations
glm::mat4 modelView = camera.viewMatrix;
hoverShader.setMat4("modelView", modelView);
hoverShader.setMat4("projection", projection);
glPointSize(8.0f);
glDrawArrays(GL_POINTS, 0, 1);
//unbind
glBindVertexArray(0);
}
void StatusManager::UpdateSelectedVertices()
{
selectedVertices.clear();
for (Vertex* v : selectedVerticesPointers)
selectedVertices.push_back(*v);
}
void StatusManager::DrawHoveredLine() {
assert(bakedModel.has_value());
assert(info.hitPoint.has_value());
Mesh& m = bakedModel.value().meshes[info.meshIndex];
Face& f = info.face.value();
auto line = getClosestLineIndex(info.hitPoint.value(), m, f);
//vertex
float hoveredVertices[6] = {
m.vertices[line.v1].Position.x, m.vertices[line.v1].Position.y, m.vertices[line.v1].Position.z,
m.vertices[line.v2].Position.x, m.vertices[line.v2].Position.y, m.vertices[line.v2].Position.z
};
hoverShader.use();
glBindVertexArray(HVAO);
glBindBuffer(GL_ARRAY_BUFFER, HVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(hoveredVertices), &hoveredVertices);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.0, 0.0);
// model/view/projection transformations
glm::mat4 modelView = camera.viewMatrix;
hoverShader.setMat4("modelView", modelView);
hoverShader.setMat4("projection", projection);
glLineWidth(3.0f);
glDrawArrays(GL_LINES, 0, 2);
glLineWidth(1.0f);
//unbind
glBindVertexArray(0);
}
normalShader just for completeness
VERTEX SHADER:
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 norm;
layout(location = 2) in ivec4 boneIds;
layout(location = 3) in vec4 weights;
layout(location = 4) in int numBones;
uniform mat4 projection;
uniform mat4 modelView;
uniform mat4 finalBonesMatrices[100];
out VS_OUT {
vec4 normal;
} vs_out;
void main()
{
mat4 cumulativeMatrix = mat4(1.0);
if (numBones>0)
cumulativeMatrix = mat4(0.0);
for(int i = 0 ; i < numBones ; i++)
{
cumulativeMatrix += (finalBonesMatrices[boneIds[i]] * weights[i]);
}
gl_Position = projection * modelView * cumulativeMatrix * vec4(pos, 1.0);
vs_out.normal = normalize(modelView * cumulativeMatrix * vec4(norm, 0.0));
}
GEOMETRY SHADER:
#version 330 core
layout (points) in;
layout (line_strip, max_vertices = 2) out;
uniform float normal_length;
in VS_OUT {
vec4 normal;
} gs_in[];
void main() {
gl_Position = gl_in[0].gl_Position;
EmitVertex();
gl_Position = gl_in[0].gl_Position + gs_in[0].normal * normal_length;
EmitVertex();
EndPrimitive();
}
FRAGMENT SHADER:
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1, 0, 0, 1);
}
If you need other piece of code tell me and I'll provide it, but I think I posted all the code and images you need.
Ok I don't know how I didn't noticed it, but yes, the problem was that I wasn't applying the projection matrix to the normal vector. Thanks Spektre to point this out.

How I can avoid in OpenGL disappearing lines using a shader?

So I'm drawing some lines by just using a fragment shader. The vertex it's just an empty quad.
The issue I have is that when I zoom out the camera and the lines get smaller they sometimes appear and disappear and I don't understand why.
This is how it looks without zooming
and this is how it looks when the camera is far away from them
The more far away I get from them the more artefacts appear.
This is how my vertex shader looks
#version 330 core
layout(location = 0) in vec2 _position;
out vec2 position;
uniform mat4 uCameraView;
void main() {
gl_Position = uCameraView * vec4(_position.x, _position.y, 0.0f, 1.0f);
position = _position;
}
And this is the fragment
#version 330 core
in vec2 position;
uniform vec4 uGridColor;
uniform float uTileSize;
uniform float uGridBorderSize;
out vec4 fragColor;
void main() {
vec2 uv = mod(position, uTileSize);
vec2 border = mod(uv + (uGridBorderSize / 2.0), uTileSize);
border -= mod(uv - (uGridBorderSize / 2.0), uTileSize);
if (length(border) > uTileSize - uGridBorderSize) {
fragColor = uGridColor;
} else {
fragColor = vec4(0.0);
}
}
Why it's this happening? maybe is something related to antialiasing? My OpenGL setup it's just the default one.
Your current code is making a binary decision "yes line" / "no line". However beyond a certain point (line width < pixel width) you're effectivly dealing with spatial frequencies above the Nyquist limit.
Instead of using a binary "yes"/"no" you need to calculate the pixel coverage, i.e. how much line is inside a pixel. For that you'd normally use a unsigned distance function (UDF). Here's some GLSL code for UDF lines in pixel space (you can also use them in normalized space, but then you'll have to adjust the smoothstep parameters). Try this on https://shadertoy.com
float lsd(vec2 a, vec2 b, vec2 p, float w){
w *= 0.5;
vec2 n = normalize(b-a);
float l = length(b-a);
float t = dot((p-a),n);
float d = length((a-p)+t*n);
float e = min(length(p-a)+w, length(p-b)+w);
return (t > w && t < l-w) ? d : e;
}
float line(vec2 a, vec2 b, float width, vec2 fragcoord){
return max(0., 1.-smoothstep(0., 1., lsd(a, b, fragcoord, width)-0.5*width));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float l =
line(vec2(8.,8.), vec2(128.,33.), 1., fragCoord)
+ line(vec2(33.,220.), vec2(260.,20.), 4., fragCoord);
fragColor = vec4(l,l,l,1.0);
}
It is hard to tell based on your shaders alone, so here is an example of how to zoom and pan around a line grid. It uses a projection matrix to zoom which looks slightly different to how you implemented the zoom, but the important bit is that it doesn't have any artefacts of lines thinning when zooming/panning.
Here is a demonstration, hopefully the GIF shows it, but the grid lines are constant thickness as you zoom in and out:
#include <iostream>
#include <vector>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/string_cast.hpp>
using std::vector;
using glm::mat4;
using glm::vec3;
using glm::vec4;
void processInput(GLFWwindow *window);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// timing
float deltaTime = 0.0f;
float lastFrame = 0.0f;
vec3 rayCast(double xpos, double ypos, mat4 projection, mat4 view) {
// converts a position from the 2d xpos, ypos to a normalized 3d direction
float x = (2.0f * xpos) / SCR_WIDTH - 1.0f;
float y = 1.0f - (2.0f * ypos) / SCR_HEIGHT;
float z = 1.0f;
vec3 ray_nds = vec3(x, y, z);
vec4 ray_clip = vec4(ray_nds.x, ray_nds.y, -1.0f, 1.0f);
// eye space to clip we would multiply by projection so
// clip space to eye space is the inverse projection
vec4 ray_eye = inverse(projection) * ray_clip;
// convert point to forwards
ray_eye = vec4(ray_eye.x, ray_eye.y, -1.0f, 0.0f);
// world space to eye space is usually multiply by view so
// eye space to world space is inverse view
vec4 inv_ray_wor = (inverse(view) * ray_eye);
vec3 ray_wor = vec3(inv_ray_wor.x, inv_ray_wor.y, inv_ray_wor.z);
ray_wor = normalize(ray_wor);
return ray_wor;
}
class Line {
int shaderProgram;
unsigned int VBO, VAO;
vector<float> vertices;
vec3 startPoint;
vec3 endPoint;
mat4 MVP;
vec3 lineColor;
public:
Line(vec3 start, vec3 end) {
startPoint = start;
endPoint = end;
lineColor = vec3(1,1,1);
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"uniform mat4 MVP;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec3 color;\n"
"void main()\n"
"{\n"
" FragColor = vec4(color, 1.0f);\n"
"}\n\0";
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// link shaders
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
vertices = {
start.x, start.y, start.z,
end.x, end.y, end.z,
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
int setMVP(mat4 mvp) {
MVP = mvp;
}
int setColor(vec3 color) {
lineColor = color;
}
int draw() {
glUseProgram(shaderProgram);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "MVP"), 1, GL_FALSE, &MVP[0][0]);
glUniform3fv(glGetUniformLocation(shaderProgram, "color"), 1, &lineColor[0]);
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, 2);
return 0;
}
~Line() {
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
}
};
vec3 cameraPos = glm::vec3(0.0f, 0.0f, 15.0f);
vec3 cameraFront = glm::vec3(0,0,-1);
mat4 model = mat4(1.0);
glm::mat4 view;
glm::mat4 projection;
float scrollSpeed = 2.0f;
float fov = 45.0f;
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "grid", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
Line x(vec3(0,0,0), vec3(1,0,0));
x.setColor(vec3(1,0,0));
Line y(vec3(0,0,0), vec3(0,1,0));
y.setColor(vec3(0,1,0));
std::vector<Line*> grid = {};
for (int i = -5; i < 6; i++) {
grid.push_back(new Line(vec3(-5, i, 0), vec3(5,i, 0)));
}
for (int j = -5; j < 6; j++) {
grid.push_back(new Line(vec3(j, -5, 0), vec3(j,5, 0)));
};
while (!glfwWindowShouldClose(window))
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
view = glm::lookAt(cameraPos, cameraPos + cameraFront, vec3(0,1,0));
projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
for (int i = 0; i < grid.size(); i++) {
grid[i]->setMVP(projection * view * model);
grid[i]->draw();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
for (int i = 0; i < grid.size(); i++) {
delete grid.at(i);
}
glfwTerminate();
return 0;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE);
if (state == GLFW_PRESS)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
cameraPos -= scrollSpeed * glm::vec3(xoffset/(float)SCR_WIDTH, yoffset/(float)SCR_WIDTH, 0);
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
firstMouse = true;
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
cameraPos += (float)yoffset * rayCast(lastX, lastY, projection, view);
}

Why is my triangle not displayed after tessellation? OpenGL

I started learning opengl and I'm at the stage of tessellation and I have problem, my triangle won't displaying when I attach tesselation shaders to program. Shaders compile correctly and there is no linking error. Everything seems to be fine from the code side. When i only use vertex shader and fragment shader everything works good. What could be the reason?
Shaders::Shaders(const char* vertexPath, const char* fragmentPath, const char* tessControlPath, const char* tessEvaluationPath)
{
std::string vs_str = load_file(vertexPath);
std::string fs_str = load_file(fragmentPath);
std::string tc_str = load_file(tessControlPath);
std::string te_str = load_file(tessEvaluationPath);
const char* vs_src = vs_str.c_str();
const char* fs_src = fs_str.c_str();
const char* tc_src = tc_str.c_str();
const char* te_src = te_str.c_str();
create_shader(GL_VERTEX_SHADER, vertexShader, vs_src);
create_shader(GL_FRAGMENT_SHADER, fragmentShader, fs_src);
create_shader(GL_TESS_CONTROL_SHADER, tessControlShader, tc_src);
create_shader(GL_TESS_EVALUATION_SHADER, tessEvaluationShader, te_src);
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glAttachShader(program, tessControlShader);
glAttachShader(program, tessEvaluationShader);
glLinkProgram(program);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteShader(tessControlShader);
glDeleteShader(tessEvaluationShader);
glCreateVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
}
void Shaders::create_shader(GLenum type, GLuint& shader, const char* src)
{
GLint isCompiled = 0;
shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
for (int i = 0; i < errorLog.size(); i++)
{
std::cerr << errorLog[i];
}
glDeleteShader(shader);
}
}
int main()
{
Window* window = new Window(1200, 1000);
Shaders* shader = new Shaders("shaders/vertex.glsl", "shaders/fragment.glsl", "shaders/tess_control.glsl", "shaders/tess_evaluation.glsl");
while (glfwWindowShouldClose(window->get_window()) == false)
{
window->loop();
const GLfloat color[]{1.0f, 0.5f, 0.5f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
const GLfloat attrib[]{0.0f, 0.0f, 1.0f, 1.0f};
glUseProgram(shader->get_program());
glVertexAttrib4fv(0, attrib);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArrays(GL_PATCHES, 0, 3);
}
delete window;
delete shader;
glfwTerminate();
}
vertex.glsl
#version 450 core
layout (location = 0) in vec4 color;
out VS_OUT
{
vec4 color;
} vs_out;
void main(void)
{
const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0), vec4(-0.25, -0.25, 0.5, 1.0), vec4(0.25, 0.25, 0.5, 1.0));
gl_Position = vertices[gl_VertexID];
vs_out.color = color;
}
fragment.glsl
#version 450 core
in VS_OUT
{
vec4 color;
} fs_in;
out vec4 color;
void main(void)
{
color = fs_in.color;
}
tess_control.glsl
#version 450 core
layout (vertices = 3) out;
void main(void)
{
if (gl_InvocationID = 0)
{
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 5.0;
gl_TessLevelOuter[2] = 5.0;
}
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
tess_evaluation.glsl
#version 450 core
layout (triangles, equal_spacing, ccw) in;
void main(void)
{
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position + gl_TessCoord.y * gl_in[1].gl_Position + gl_TessCoord.z * gl_in[2].gl_Position);
}
You have specifies the number of vertices that will be used to make up a single patch primitive, by glPatchParameteri:
glPatchParameteri(GL_PATCH_VERTICES, 3);
glDrawArrays(GL_PATCHES, 0, 3);
Further more you have to pass the color all the way, through all the shader stages, from the vertex shader to the fragment shader:
The input to the Tessellation Control Shader is a patch and the output is patch too. But the input patch size can be different of the output patch size. While the input patch size is defined by GL_PATCH_VERTICES, the output patch size is define by layout (vertices = 3) out. Thus you have to specify, in the shader program, how the attributes of the input patch are mapped to the attributes of the output patch.
#version 450 core
layout (vertices = 3) out;
in VS_OUT
{
vec4 color;
} in_data[];
out VS_OUT
{
vec4 color;
} out_data[];
void main(void)
{
out_data[gl_InvocationID].color = in_data[gl_InvocationID].color;
if (gl_InvocationID = 0)
{
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 5.0;
gl_TessLevelOuter[2] = 5.0;
}
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
The Tessellation Evaluation Shader performs the interpolation. The attributes of the output patch of the tessellation control shader are interpolated dependent on gl_TessCoord.
You have to implement the algorithm which interpolates the attributes in the shader program.
#version 450 core
layout (triangles, equal_spacing, ccw) in;
in VS_OUT
{
vec4 color;
} in_data[];
out VS_OUT
{
vec4 color;
} out_data;
void main(void)
{
out_data.color = in_data[0].color * gl_TessCoord.x + in_data[1].color * gl_TessCoord.y + in_data[2].color * gl_TessCoord.z;
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position + gl_TessCoord.y * gl_in[1].gl_Position + gl_TessCoord.z * gl_in[2].gl_Position);
}

Attribute being removed by compiler, but I can't see why

I'm trying to write a set of shaders to do per-vertex lighting with multiple lights, and textures. I think I'm running into the shader compiler optimizing out 'v_texture', one of my attribute variables, because glVertexAttribLocation is failing to find it.
Here's my vertex shader code:
attribute vec3 v_position;
attribute vec3 v_normal;
attribute vec2 v_texture;
varying vec4 color;
varying vec2 texCoord;
const int MAX_LIGHTS = 8;
uniform struct lightSource
{
vec4 position;
vec4 color;
vec3 coneDirection;
float coneAngle;
float ambientFactor;
} sceneLights[MAX_LIGHTS];
uniform mat4 modelView;
uniform mat4 Projection;
uniform float shininess;
uniform int numLights;
vec4 applyLight(lightSource light)
{
vec4 outColor;
float attenuation;
vec3 surfacePos = (modelView * vec4(v_position.xyz, 1.0)).xyz;
vec3 toLight;
if(light.position.w == 0.0)
{
toLight = normalize(light.position.xyz);
attenuation = 1.0;
}
else
{
toLight = normalize(light.position.xyz - surfacePos);
float distanceToLight = length(light.position.xyz - surfacePos);
float lightAngle = degrees(acos(dot(-surfacePos, normalize(light.coneDirection))));
if(lightAngle > light.coneAngle)
{
attenuation = 0.0;
}
else
{
attenuation = 1.0/(1.0 + (0.1 * pow(distanceToLight, 2.0)));
}
}
vec3 Eye = normalize(-surfacePos);
vec3 Halfway = normalize(toLight + Eye);
vec3 worldNormal = normalize(modelView * vec4(v_normal, 0.0)).xyz;
vec4 ambient = vec4(light.ambientFactor * vec3(light.color.xyz), 1.0);
float Kd = max(dot(toLight, worldNormal), 0.0);
vec4 diffuse = Kd * light.color;
float Ks = pow(max(dot(worldNormal, Halfway), 0.0), shininess);
vec4 specular = Ks * light.color;
if(dot(toLight, worldNormal) < 0.0)
{
specular = vec4(0.0, 0.0, 0.0, 0.0);
}
outColor = ambient + (attenuation * (diffuse + specular));
outColor.a = 1.0;
return outColor;
}
void main(void)
{
vec4 colorSum;
colorSum = vec4(0, 0, 0, 0);
for(int i = 0; i < MAX_LIGHTS; i++)
{
if(i >= numLights)
{
break;
}
colorSum += applyLight(sceneLights[i]);
}
colorSum.xyzw = normalize(colorSum.xyzw);
vec3 gammaCorrection = vec3(1.0/2.2);
color = vec4(pow(vec3(colorSum.xyz), gammaCorrection), 1.0);
texCoord = v_texture;
gl_Position = Projection * modelView * vec4(v_position.xyz, 1.0);
}
And the fragment shader:
varying vec4 color;
varying vec2 texCoord;
uniform sampler2D texSampler;
void main(void)
{
gl_FragColor = (color * texture2D(texSampler, texCoord.xy));
}
I've been over the code top to bottom but I just can't see where my problem is. I'm assigning v_texture to the texCoord, and passing it to the frag shader, where I'm using it together with the final lighting result from the vertex shader to yield the final color. I'm checking for shader errors when I compile them, and I'm not getting anything. My only guess at the moment is that maybe gl_FragColor = (color * texture2D(texSampler, texCoord.xy)) isn't a valid statement, but then shouldn't this have caught it?
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(fragment_shader, sizeof(InfoLog), NULL, InfoLog);
fprintf(stderr, "Fragment Shader %d: '%s'\n", fragment_shader, InfoLog);
}
Edit: I probably should have put this in here in the first place, but this snippet is the shader section from my C++ program's initialize() function. The only one of the glGetAttribLocation calls to fail is for v_texture.
// Begin shader loading.
//compile the shaders
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
GLint shader_status;
pass=new char [2048];
unsigned int len;
//Give a maximum number of attempts to load the vertex shader
int attempts=10;
//Load the vertex shader
do
{
loader.load("lighting.vsh");
shaderCode = loader.hold.c_str();
len=loader.length;
pass=shaderCode;
attempts-=1;
}
while(len!=pass.length()&& attempts>0);
//Pass the temperary variable to a pointer
tmp = pass.c_str();
// Vertex shader first
glShaderSource(vertex_shader, 1,&tmp, NULL);
glCompileShader(vertex_shader);
//check the compile status
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(vertex_shader, sizeof(InfoLog), NULL, InfoLog);
fprintf(stderr, "Vertex Shader %d: '%s'\n", vertex_shader, InfoLog);
}
const char *fs=loader.load("lighting.fsh");
// Now the Fragment shader
glShaderSource(fragment_shader, 1, &fs, NULL);
glCompileShader(fragment_shader);
//check the compile status
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(fragment_shader, sizeof(InfoLog), NULL, InfoLog);
fprintf(stderr, "Fragment Shader %d: '%s'\n", fragment_shader, InfoLog);
}
//Now we link the 2 shader objects into a program
//This program is what is run on the GPU
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
//check if everything linked ok
glGetProgramiv(program, GL_LINK_STATUS, &shader_status);
if(!shader_status)
{
std::cerr << "[F] THE SHADER PROGRAM FAILED TO LINK" << std::endl;
return false;
}
//Now we set the locations of the attributes and uniforms
//this allows us to access them easily while rendering
loc_position = glGetAttribLocation(program,
const_cast<const char*>("v_position"));
if(loc_position == -1)
{
std::cerr << "Error: POSITION NOT FOUND IN SHADER" << std::endl;
return false;
}
loc_normals = glGetAttribLocation(program, const_cast<const char*>("v_normal"));
if(loc_normals == -1)
{
std::cerr << "Error: NORMALS NOT FOUND IN SHADER" << std:: endl;
return false;
}
loc_texture = glGetAttribLocation(program, const_cast<const char*>("v_texture"));
if(loc_texture == -1)
{
std::cerr << "[F] TEXTURE NOT FOUND IN SHADER" << std::endl;
return false;
}
// Begin light initialization.

Create a triangle that continuously rotates around it’s y-axis in OpenGl

Im wondering how to rotate a OpenGL triangle around its Y-Axis in OpenGL,
Ive been able to translate the triangle from object space to clip space.
Im wondering if anyone has any experience with this issue and may be able to lend a hand.
Main.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <Windows.h>
using namespace std;
using namespace glm;
const int windowWidth = 1024;
const int windowHeight = 768;
GLuint VBO;
const int NUMVERTS = 3;
GLuint gModelToWorldTransformLocation;
GLuint gWorldToViewTransformLocation;
GLuint gProjectionTransformLocation;
struct SimpleVertex
{
vec3 pos;
vec4 colour;
};
static void renderSceneCallBack()
{
static mat4 modelToWorldTransform = mat4(1.0f);
static mat4 worldToViewTransform = lookAt(
vec3(0.0f,0.0f,3.0f), // position of your camera, in world space
vec3(0.0f,0.0f,0.0f), // look at in world space
vec3(0.0f,1.0f,0.0f) // Camera up direction (set to 0,-1,0 to look upside-down)
);
static mat4 projectionTransform = perspective(45.0f, (float)windowWidth / (float)windowHeight, 1.0f, 100.0f);
glUniformMatrix4fv(gModelToWorldTransformLocation, 1, GL_FALSE, &modelToWorldTransform[0][0]);
glUniformMatrix4fv(gWorldToViewTransformLocation, 1, GL_FALSE, &worldToViewTransform[0][0]);
glUniformMatrix4fv(gProjectionTransformLocation, 1, GL_FALSE, &projectionTransform[0][0]);
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (const GLvoid*)12);
glDrawArrays(GL_TRIANGLES, 0, NUMVERTS);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glutSwapBuffers();
}
static void initializeGlutCallbacks()
{
glutDisplayFunc(renderSceneCallBack);
glutIdleFunc(renderSceneCallBack);
}
static void createVertexBuffer()
{
// Create some vertices to put in our VBO.
// Create vertex buffer
SimpleVertex vertices[] =
{
{vec3(-0.5f, -0.5f, 0.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f)},
{vec3(0.5f, -0.5f, 0.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f)},
{vec3( 0.0f, 0.5f, 0.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f)}
};
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(SimpleVertex) * 3, vertices, GL_STATIC_DRAW);
}
static void addShader(GLuint shaderProgram, const char* pShaderText, GLenum shaderType)
{
GLuint shaderObj = glCreateShader(shaderType);
if (shaderObj == 0)
{
cerr<<"Error creating shader type "<<shaderType<<endl;
exit(0);
}
const GLchar* p[1];
p[0] = pShaderText;
GLint Lengths[1];
Lengths[0]= strlen(pShaderText);
glShaderSource(shaderObj, 1, p, Lengths);
glCompileShader(shaderObj);
GLint success;
glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);
cerr<<"Error compiling shader type "<<shaderType<<": "<<InfoLog<<endl;
exit(1);
}
glAttachShader(shaderProgram, shaderObj);
}
const string readFileToString(char* filename)
{
ifstream file (filename, ios::in);
if (file.is_open())
{
stringstream continut;
continut << file.rdbuf();
continut << '\0';
return continut.str();
}
return "";
}
static void buildShaders()
{
GLuint shaderProgram = glCreateProgram();
if (shaderProgram == 0)
{
cerr<<"Error creating shader program\n";
exit(1);
}
string VS = readFileToString("vertexShader.glsl");
string FS = readFileToString("fragmentShader.glsl");
addShader(shaderProgram, VS.c_str(), GL_VERTEX_SHADER);
addShader(shaderProgram, FS.c_str(), GL_FRAGMENT_SHADER);
GLint success = 0;
GLchar errorLog[1024] = { 0 };
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (success == 0)
{
glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
cerr<<"Error linking shader program: "<<errorLog<<endl;
exit(1);
}
glValidateProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_VALIDATE_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
cerr<<"Error linking shader program: "<<errorLog<<endl;
exit(1);
}
glUseProgram(shaderProgram);
gModelToWorldTransformLocation = glGetUniformLocation(shaderProgram, "gModelToWorldTransform");
//assert(gModelToWorldTransformLocation != 0xFFFFFFFF);
gWorldToViewTransformLocation = glGetUniformLocation(shaderProgram, "gWorldToViewTransform");
//assert(gWorldToViewTransformLocation != 0xFFFFFFFF);
gProjectionTransformLocation = glGetUniformLocation(shaderProgram, "gProjectionTransform");
//assert(gProjectionTransformLocation != 0xFFFFFFFF);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow("Transformations");
initializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK)
{
cerr<<"Error: "<<glewGetErrorString(res)<<"\n";
return 1;
}
buildShaders();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Create a vertex buffer
createVertexBuffer();
glutMainLoop();
return 0;
}
VertexShader
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec4 Colour;
out vec4 Colour0;
uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewTransform;
uniform mat4 gProjectionTransform;
void main()
{
vec4 vertexPositionInModelSpace = vec4(Position, 1);
vec4 vertexInWorldSpace = gModelToWorldTransform * vertexPositionInModelSpace;
vec4 vertexInViewSpace = gWorldToViewTransform * vertexInWorldSpace;
vec4 vertexInHomogeneousClipSpace = gProjectionTransform * vertexInViewSpace;
gl_Position = vertexInHomogeneousClipSpace;
Colour0 = Colour;
}
FragmentShader
#version 330
in vec4 Colour0;
out vec4 FragColor;
void main()
{
FragColor = Colour0;
}
You can have a float variable for rotation and create rotation matrix from it once then store it. Every frame you should have the world matrix updated like this: modelToWorldTransform = rotationTransform * modelToWorldTransform; See wikipedia for creating the rotation matrix around y: http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
EDIT: I suppose your having trouble with affine transformations. To move (no matter how - translate, rotate) an object in the world, you have to apply a transformation to it.
Currently, you are applying one transformation, which is translation to the position 0, 0, 3.
So far, so good. But your triangle is static(it doesn't move). So to rotate it, you need to apply another transformation (represented by a matrix, again). But if you always apply one and the same rotation, the triangle will be rotated against its original transform but it won't move once again. So you need to apply a rotation every frame, store the result in the modeltoworldtransform and then on next frame repeat the step. e.g in the renderCallback:
static mat4 modelToWorldTransform = mat4(1.0f);
static mat4 rotationTransform = <rotation by the described in wiki way>;
modelToWorldTransform = rotationTransform * modelToWorldTransform;
Also, order of multiplying matrices matters, because their multiplication (which we use to get transform from object to camera space) is not commutative and A * B is different from B * A. I highly recommend you to read more about linear algebra and it will help you understand what happens "behind the scenes". Many books about graphics for game devs explain it in a pretty good and understandable way. "Mathematics for 3D Game Programming and Computer Graphics" by Eric Lengyel kicks ass. It even helped me when I studied maths in university ;).