graphics-snippets

StackOverflow


Legacy OpenGL - immediate mode and fixed function pipeline

OpenGL Context
Legacy OpenGL
Fixed Function Pipeline

What is fixed function pipeline(PyOpenGL)?

The Fixed Function Pipeline is deprecated since decades and must not to be use any more. It is not flexible enough for a modern render algorithms and technologies.

See Vertex Specification and Shader for a flexible state of the art way of rendering.

The key points of the fixed function pipeline are:

To the use the fixed function pipeline a compatibility profile OpenGL Context must be used.
See OpenGL 4.6 API Compatibility Profile Specification, where the difference between a core profile and a compatibility profile a marked in red.

Why I’m getting access violation errors with my Vertex Buffer Object?

Begin/End sequences

Note, that drawing by glBegin/glEnd sequences, the fixed function matrix stack and fixed function, per vertex light model, is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.
Read about Vertex Specification and Shader for a state of the art way of rendering.

OpenGL 3D model texture mapping
Cube map/ frame buffer problems
Cannot impose texture to object

Primitives

Issue converting legacy pipeline to modern openGL

[…] is there a way to combine all of the data and send it as a single call?

If you want to render multiple GL_LINE_LOOP primitives with on draw call, then I recommend to use Primitive Restart:

Primitive restart functionality allows you to tell OpenGL that a particular index value means, not to source a vertex at that index, but to begin a new Primitive

Enable GL_PRIMITIVE_RESTART and define the restart index by glPrimitiveRestartIndex (e.g. 0xFFFFFFFF):

glEnable( GL_PRIMITIVE_RESTART );
glPrimitiveRestartIndex( 0xFFFFFFFF );

e.g. If you want to draw 2 line loops (with one draw call) with the indices (0, 1, 2) and (3, 4, 5, 6) then you have to define the following index array:

std::vector<uint32_t> indices{ 0, 1, 2, 0xFFFFFFFF, 3, 4, 5, 6 };

OpenGL Display List

Unexpected gluLookAt behavior with a point draw on OpenGL

Raster operation

Issues with the color comand in OpenGL

Fixed pipeline Shader

How can I draw gluQuadric with color?
OpenGL gluLookat not working with shaders on
OpenGL: glColor3f() and glVertex3f() with shader
Vertex/fragment shaders for a OpenGL firsrt-person shooter view?

You cannot mix fixed function attributes and the fixed function matrix stack with a version 3.30 shader program.
You have to use the built in attributes such as gl_Vertex and gl_MultiTexCoord0 (see Vertex Attributes).
You have to use the the built in uniform variables like gl_ModelViewProjectionMatrix. In legacy OpenGL (GLSL 1.20) there are provided a bunch of Built-In Uniforms. See OpenGL Shading Language 1.20 Specification; 7.5 Built-In Uniform State.
One of them is gl_ModelViewProjectionMatrix of type mat4, which provides the transformation by the model view and projection matrix. Separated gl_ModelViewMatrix and gl_ProjectionMatrix exists as well.

Vertex shader:

#version 120

varying vec2 texcoord;

void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  texcoord = gl_MultiTexCoord0.xy;
} 

Fragment shader:

#version 120

varying vec2 texcoord;

uniform sampler2D texture;

void main()
{
    gl_FragColor = texture2D(texture, texcoord);
}

Fixed-Function Attributes

OpenGL 4.6 API Compatibility Profile Specification; 10.3.3 Specifying Arrays for Fixed-Function Attributes; page 402

The commands

void VertexPointer( int size, enum type, sizei stride, const void *pointer );
void NormalPointer( enum type, sizei stride, const void *pointer );
void ColorPointer( int size, enum type, sizei stride, const void *pointer );
void SecondaryColorPointer( int size, enum type, sizei stride, c onst void *pointer );
void IndexPointer( enum type, sizei stride, const void *pointer );
void EdgeFlagPointer( sizei stride, const void *pointer );
void FogCoordPointer( enum type, sizei stride, const void *pointer );
void TexCoordPointer( int size, enum type, sizei stride, const void *pointer );

specify the location and organization of arrays to store vertex coordinates, normals, colors, secondary colors, color indices, edge flags, fog coordinates. … An individual array is enabled or disabled by calling one of

void EnableClientState( enum array );
void DisableClientState( enum array );

with array set to VERTEX_ARRAY, NORMAL_ARRAY, COLOR_ARRAY, SECONDARY_COLOR_ARRAY, INDEX_ARRAY, EDGE_FLAG_ARRAY, FOG_COORD_ARRAY, or TEXTURE_COORD_ARRAY, for the vertex, normal, color, secondary color, color index, edge flag, fog coordinate, or texture coordinate array, respectively.

Trying to draw a basic shape with LWJGL3
VBO and VAO not drawing opengl and LWJGL3
LWJGL 3.1.6 crashes on Win 10

The modern way of rendering in OpenGL, would be to use a Shader program.

If you don’t use a shader program, than you have to define the array of vertex data using the deprecated way by glVertexPointer and you have to enable the client-side capability for vertex coordinates by glEnableClientState( GL_VERTEX_ARRAY ).

What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?

If the OpenGL extension ARB_vertex_program; Modify Section 2.7, Vertex Specification is valid, then there is a mapping between Fixed function attributes and attribute indices:

Setting generic vertex attribute zero specifies a vertex; the four vertex coordinates are taken from the values of attribute zero. A Vertex2, Vertex3, or Vertex4 command is completely equivalent to the corresponding VertexAttrib command with an index of zero. Setting any other generic vertex attribute updates the current values of the attribute. There are no current values for vertex attribute zero.

Implementations may, but do not necessarily, use the same storage for the current values of generic and certain conventional vertex attributes. When any generic vertex attribute other than zero is specified, the current values for the corresponding conventional attribute in Table X.1 become undefined. Additionally, when a conventional vertex attribute is specified, the current values for the corresponding generic vertex attribute in Table X.1 become undefined. For example, setting the current normal will leave generic vertex attribute 2 undefined, and vice versa.

Generic Attribute Conventional Attribute Conventional Attribute Command
0 vertex position Vertex
1 vertex weights 0-3 WeightARB, VertexWeightEXT
2 normal Normal
3 primary color Color
4 secondary color SecondaryColorEXT
5 fog coordinate FogCoordEXT
6 - -
7 - -
8 texture coordinate set 0 MultiTexCoord(TEXTURE0, …
   

This means there is a “mapping” between vertex attribute 0 and the fixed function attribute GL_VERTEX_ARRAY, but not necessarily a mapping for any other vertex attribute.

Nvidia goes a step ahead as specified in Release Notes for NVIDIA OpenGL Shading Language Support; November 9, 2006; - pp. 7-8.
There is an actual mapping between the fixed function attributes and vertex attribute indices, as specified in the table above.
See also the answer to What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?

drawing several triangles with different colours with one glDrawArrays command

Why doesn’t OpenGL draw a polygon in this code?

Translation, Rotation and Scale - Fixed function pipeline

Drawing cubes with stacked matrix**
Drawing cubes with stacked matrix

OpenGL translation before and after a rotation

OpenGL Arm that rotates shoulder and elbow:
arms fixed pipeline

OpenGL: Objects rotating strangely Rotating a multipart object
how to use glTranslatef,glScalef,glRotatef in openGL
Move Object in Circle using OpenGL?
OpenGL movement object and rotate in same axis
OpenGL movement object and rotate in same axis OpenGL movement object and rotate in same axis

Solar System in OpenGL, Camera position
solarsystem

How to rotate 2 objects independently in pygame and pyopengl

Animate cube inside an animation
![Animate cube inside an animation

Write to Framebuffer

Using gl_FragColor vs. out vec4 color?

OpenGL Shading Language 4.60 Specification - 7.1.7. Compatibility Profile Built-In Language Variables; page 144 OpenGL Shading Language 3.30 Specification - 7.2 Fragment Shader Special Variables; page 71

The following fragment output variables are available in a fragment shader when using the compatibility profile:

out vec4 gl_FragColor;
out vec4 gl_FragData[gl_MaxDrawBuffers];

Writing to gl_FragColor specifies the fragment color that will be used by the subsequent fixed functionality pipeline. If subsequent fixed functionality consumes fragment color and an execution of the fragment shader executable does not write a value to gl_FragColor then the fragment color consumed is undefined.

This means you can’t use gl_FragColor, in an OpenGL Core profile Context, because it is deprecated, but it would still be available in a compatibility profile.

The modern way of writing to the output buffers from a fragment shader, is to declare user-defined output variables and to use Layout Qualifiers.

Fixed function pipeline (deprecated) Light model

glEnable(GL_LIGHTING)
glEnable(GL_LIGHTING)

Basic OpenGL Lighting
Per Fragment Lighting

legacy lighting flowchart

OpenGL lighting position is fixed at undesired location

When the light position is set by glLightfv(GL_LIGHT0, GL_POSITION, pos), then pos is multiplied by the current model view matrix.
This means if the position is set before the view matrix is set (gluLookAt), then the light position is relative to the camera (view space position).
If it is set after the view matrix was set, then the light position has to be in world coordinates, becaus it is transformed by the view matrix.

This means if the position is set before the model view matrix is set, then the light position is placed in world coordiantes. It is transformed by the view matrix but not by the model matrix.
If it is set after the model view matrix was set, then the light position can be set in the coordinate system of the model.

When the spot light direction is set by glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir), then the direction is multiplied by the upper 3x3 of the modelview matrix. The parameter GL_SPOT_DIRECTION is a direction vector and not a position.

The OpenGL fixed function pipeline calculates the light per vertex: Gouraud shading.
This may cause, that you can’t “see” the light, if the light vector hits the vertex coordinate by a acute angle relative to the plane of the surface or if the light cone of the spotlight does not hit any vertex position. You are in danger of that behavior, because your geometry consists of “large” primitives and the cone opening angle of the spot light is very small (15°).

See OpenGL Lighting on texture plane is not working

How to make opengl text visible/change color?
OpenGL object no lighting
Inter dependency between gl_color and material
Adding a Light Source to 3D objects in OpenGL

When lighting (GL_LIGHTING) is enabled, the render color is taken from the material parameters (glMaterial).

If you still want to use the current color attribute (set by glColor), you need to enable GL_COLOR_MATERIAL and to set the color material parameters (glColorMaterial):

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

See also Basic OpenGL Lighting.

The standard OpenGL Blinn-Phong light model is calcualted like this:

OBJ file, after read mtl can not show the colors
opengl light reduce reflection
OpenGL import PNG darker

Ka ... ambient material
Kd ... difusse material
Ks ... specular material

La ... ambient light
Ld ... diffuse light
Ls ... specular light
sh ... shininess

N  ... norlmal vector 
L  ... light vector (from the vertex position to the light) 
V  ... view vector (from the vertex position to the eye)

Id    = max(dot(N, L), 0.0);

H     = normalize(V + L);
NdotH = max(dot(N, H), 0.0);
Is    = pow(NdotH, sh);

fs    = Ka*La + Id*Kd*Ld + Is*Ks*Ls;
Is    = (sh + 2.0) * pow(NdotH, sh) / (2.0 * 3.14159265);

Color and movement of OpenGL object does not alternate properly
How can I make my light stay fixed relative to my scene (in the cube)?
glColor3f in OpenGl have little brightness
OpenGL: Updating shading with several movable lights
How do i make OpenGL Specular Light work?
Texture Mapping disappeared when added 3D models
How to stop positional light moving with camera
OpenGL doesn’t consider distance of GL_DIFFUSE positional light

OpenGL: How do I render individual RGB values given a cross product that contains the total light?
OpenGL: How do I render individual RGB values given a cross product that contains the total light?
OpenGL: How do I render individual RGB values given a cross product that contains the total light?

Fixed function pipeline (deprecated) Gouraud versus Phong

OpenGL Lighting on texture plane is not working

Gouraud versus Phong

what the difference between phong shading and gouraud shading?

GLSL fixed function fragment program replacement
How to add lighting to shader of instancing
How do you incorporate per pixel lighting in shaders with LIBGDX?
Per Vertex Diffuse and Specular Shader
Phong and Gouraud Shading WebGL

In the case of specular highlights, the light distribution is not linear and cannot be calculated with linear interpolation. The effect is distorted or is completely lost.

Drawing geometry and mesh

GLU

“invalid value” when calling gluNewQuadric

texturing a moving sphere in OpenGL C++
sphere texture

glColor3f(1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);

glPushMatrix();
glRotatef(angle_degree, 1.0f, 0.0f, 0.0f);

GLUquadric *qobj = gluNewQuadric();
gluQuadricTexture( qobj, GL_TRUE );
gluSphere( qobj, 0.1, 10, 10 );
gluDeleteQuadric( qobj );

glPopMatrix();

glDisable(GL_TEXTURE_2D);

Particles

OpenGL - Stop spray and stay in its position
spray

Map Buffer

OpenGL NURBS surfaces
nurbs