Hello. I am learning to use opengl and i am using glfw and glew for setting it up. When i animate a single triangle with quaternions i get darker edges and something like small dents when i rotate on the z axis.
Here is the code:
#include <iostream>
#include "dep/GLEW/glew.h"
#include "dep/GLFW/glfw3.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void character_callback(GLFWwindow* window, unsigned int codepoint);
void processInput(GLFWwindow* window);
int main()
{
glfwInit();
GLFWwindow *window;
//window = glfwCreateWindow(800, 600, "der", glfwGetPrimaryMonitor(), NULL);
window=glfwCreateWindow(800, 600, "der", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
glfwSwapInterval(1);
float positions[9] =
{ -0.5f,-0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
0.5f,-0.5f, 0.0f };
unsigned int indices[] = { 0,1,2 };
unsigned int myvertexarrayobject;
glGenVertexArrays(1, &myvertexarrayobject);
glBindVertexArray(myvertexarrayobject);
unsigned int myvertexbufferobject;
glGenBuffers(1, &myvertexbufferobject);
glBindBuffer(GL_ARRAY_BUFFER, myvertexbufferobject);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), positions, GL_DYNAMIC_DRAW);
//glBindVertexArray(0);
unsigned int ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
//glBindBuffer(GL_ARRAY_BUFFER, myvertexbufferobject);
const char* vertex_shader =
"#version 440 core\n"
"in vec3 vp;"
"uniform vec4 qtr;"
"vec4 pt=vec4(vp,1.0);"
"vec4 npt=vec4(-qtr[0],-qtr[1],-qtr[2],qtr[3]);"
"vec4 sqtr=vec4((qtr[3]*pt[0]+qtr[0]*pt[3]+qtr[1]*pt[2]-qtr[2]*pt[1]),(qtr[3]*pt[1]-qtr[0]*pt[2]+qtr[1]*pt[3]+qtr[2]*pt[0]),(qtr[3]*pt[2]+qtr[0]*pt[1]-qtr[1]*pt[0]+qtr[2]*pt[3]),(qtr[3]*pt[3]-qtr[0]*pt[0]-qtr[1]*pt[1]-qtr[2]*pt[2]));"
"vec4 npoint=vec4((sqtr[3]*npt[0]+sqtr[0]*npt[3]+sqtr[1]*npt[2]-sqtr[2]*npt[1]),(sqtr[3]*npt[1]-sqtr[0]*npt[2]+sqtr[1]*npt[3]+sqtr[2]*npt[0]),(sqtr[3]*npt[2]+sqtr[0]*npt[1]-sqtr[1]*npt[0]+sqtr[2]*npt[3]),(sqtr[3]*npt[3]-sqtr[0]*npt[0]-sqtr[1]*npt[1]-sqtr[2]*npt[2]));"
"void main() {"
" gl_Position =npoint;"
"}";
const char* fragment_shader =
"#version 440 core\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4(0.2, 0.5, 1.0, 1.0);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glDeleteShader(vs);
glDeleteShader(fs);
int countt = 0;
float i = 0.0f;
float ki(sin((i / 2)));
float kj(0.0f);
float kl(cos((i / 2)));
glEnable(GL_DEPTH_TEST);
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_VENDOR) << std::endl;
std::cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
std::cout << glGetString(GL_RENDERER) << std::endl;
while (!glfwWindowShouldClose(window))
{
if (countt < 180 && countt >= 0)
{
i = i + 0.1f;
ki = sin(i / 2);
kl = cos(i / 2);
countt++;
}
else if (countt >= 180 && countt < 360)
{
i = i - 0.1f;
ki = sin(i / 2);
kl = cos(i / 2);
countt++;
}
else
{
countt = 0;
}
int widt;
int hei;
glfwGetFramebufferSize(window, &widt, &hei);
glViewport(0, 0, widt, hei);
glfwSetCharCallback(window, character_callback);
processInput(window);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgram(shader_programme);
glBindVertexArray(myvertexarrayobject);
GLuint transformLoc = glGetUniformLocation(shader_programme, "qtr");
glUniform4f(transformLoc, kj, kj, ki, kl);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
//glBindVertexArray(0);
//swap front and back buffers
glfwSwapBuffers(window);
//poll for and procces events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
std::cout << "up pressed\n";
}
}
void character_callback(GLFWwindow* window, unsigned int codepoint)
{
std::cout << (char)codepoint << std::endl;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
I think the math is correct for the rotations. I can’t understand why the Y axis rotation seems to work fine but the Z axis has this artifacts(darker edges and dented triangles). I am sure i am doing something wrong but i cant figure it out . In the first iteration of the while loop the draw call doesn’t draw anything. It draws after the swap buffers command is executed and i can’t understand why it does that.