## Convert Quaternion rotation to rotation matrix?

The following code is based on a quaternion (qw, qx, qy, qz), where the order is based on the Boost quaternions: boost::math::quaternion<float> quaternion; float qw = quaternion.R_component_1(); float qx = quaternion.R_component_2(); float qy = quaternion.R_component_3(); float qz = quaternion.R_component_4(); First you have to normalize the quaternion: const float n = 1.0f/sqrt(qx*qx+qy*qy+qz*qz+qw*qw); qx *= n; qy … Read more

## What are shaders in OpenGL and what do we need them for? [closed]

The OpenGL wiki gives a good definition: A Shader is a user-defined program designed to run on some stage of a graphics processor. History lesson In the past, graphics cards were non-programmable pieces of silicon which performed a set of fixed algorithms: inputs: 3D coordinates of triangles, their colors, light sources output: a 2D image … Read more

## how to enable vertical sync in opengl?

On Windows there is OpenGL extension method wglSwapIntervalEXT. From the post by b2b3 http://www.gamedev.net/community/forums/topic.asp?topic_id=360862: If you are working on Windows you have to use extensions to use wglSwapIntervalExt function. It is defined in wglext.h. You will also want to download glext.h file. In wglext file all entry points for Windows specific extensions are declared. All … Read more

## glVertexAttribPointer clarification

Some of the terminology is a bit off: A Vertex Array is just an array (typically a float[]) that contains vertex data. It doesn’t need to be bound to anything. Not to be confused with a Vertex Array Object or VAO, which I will go over later A Buffer Object, commonly referred to as a … Read more

## Frequency of shader invocations in rendering commands

Each shader stage has its own frequency of invocations. I will use the OpenGL terminology, but D3D works the same way (since they’re both modelling the same hardware relationships). Vertex Shaders These are the second most complicated. They execute once for every input vertex… kinda. If you are using non-indexed rendering, then the ratio is … Read more

## How to cast int to const GLvoid*?

Like this: reinterpret_cast <GLvoid *> (start * sizeof(unsigned int));

## OpenGL stretched shapes – aspect ratio

glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); float aspect = (float)width / (float)height; glOrtho(-aspect, aspect, -1, 1, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Update: Explanation what happens OpenGL is a state machine and in the case of OpenGL-2.1 and below maintains a set of transformation matrices. A vertex ↑v first gets multiplied with the modelview matrix to yield a … Read more

## GLSL – Using custom output attribute instead of gl_Position

This was mostly covered by the comments, but in the interest of having an answer in place, let me summarize which pre-defined GLSL variables are gone in the core profile, and which are still there. In most cases, pre-defined variables were removed from core profile shaders as a direct consequence of large parts of the … Read more

## GLSL, semaphores?

Basically, you’re just implementing a spinlock. Only instead of one lock variable, you have an entire texture’s worth of locks. Logically, what you’re doing makes sense. But as far as OpenGL is concerned, this won’t actually work. See, the OpenGL shader execution model states that invocations execute in an order which is largely undefined relative … Read more

## Is it possible to rotate an object around its own axis and not around the base coordinate’s axis?

First thing you should know is that in OpenGL, transformation matrices are multiplied from right. What does it mean? It means that the last transformation you write gets applied to the object first. So let’s look at your code: gl.glScalef(0.8f, 0.8f, 0.8f); gl.glTranslatef(0.0f, 0.0f, -z); gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y … Read more