Code covered by the BSD License  

Highlights from
OpenGL .NET Examples

image thumbnail
from OpenGL .NET Examples by Dirk-Jan Kroon
Matlab OpenGL .NET code and examples for R2009a and above

Polygon_Mesh_Painters(id)
function Polygon_Mesh_Painters(id)
% Function is written by D.Kroon University of Twente (April 2009)

% From id get the render data
StoreName=['OpenGL' num2str(id)];
data=getappdata(0,[StoreName '_data']);

% Allow to use GL functions without prefixing with Tao.Opengl.
import Tao.OpenGl.*

if(data.FirstRender)
    % Load the Vessel Volume data
    load('ExampleData/Vessel_Scan.mat');
    % Smooth the Vessel volume data, to remove noise
    V=smooth3(V);
    % Triangulated an iso surface, of iso value 26500;
    FV=isosurface(V,26500);
    % Calculate Normals from the gradient in the volume data
    Normals=isonormals(V,FV.vertices);
    
    % Fit the vertice coordinates to range -1 and 1
    Vertices=FV.vertices*(2/max(FV.vertices(:)))-1;
 
    % Transpose, because of later use OpenGL style arrays.
    Faces=FV.faces';
    Vertices=Vertices';
    Normals=Normals';
    
    % Get a DisplayList id
    data.VesselMeshOpenGLID=Gl.glGenLists (1);

    % Render the vessel polygons to a display list object , so the display
    % list object can be called to display the vessel mesh.
    Gl.glNewList(data.VesselMeshOpenGLID, Gl.GL_COMPILE);

    % Convert vertex data to System arrays (Equal to GL_FLOAT and GL_UNSIGNED_INT)
    VertexArray=NET.convertArray(Vertices(:), 'System.Single', numel(Vertices));
    NormalArray=NET.convertArray(Normals(:), 'System.Single', numel(Normals));
    FacesArray=NET.convertArray(Faces(:)-1, 'System.UInt32', numel(Faces));
    
    % Enable opengl array input
    Gl.glEnableClientState(Gl.GL_NORMAL_ARRAY);
    Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY );
    
    % Set input arrays
    Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, VertexArray);
    Gl.glNormalPointer(Gl.GL_FLOAT, 0, NormalArray);
 
    % Draw the triangles
    Gl.glDrawElements( Gl.GL_TRIANGLES, numel(Faces), Gl.GL_UNSIGNED_INT, FacesArray );

    % Disable opengl array input
    Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
    Gl.glDisableClientState(Gl.GL_NORMAL_ARRAY);

    % End display list
    Gl.glEndList();
    
    % Store the data structure
    setappdata(0,[StoreName '_data'],data);
end

% Set background clear color to blue            
Gl.glClearColor(0.2, 0.2, 0.2, 0.0);  

% Clear the color and depth buffer
Gl.glClear(int32(bitor(uint32(Gl.GL_COLOR_BUFFER_BIT),uint32(Gl.GL_DEPTH_BUFFER_BIT)))); 

% Set depth testing enabled
Gl.glEnable(Gl.GL_DEPTH_TEST)  

% Enable Light
Gl.glEnable(Gl.GL_LIGHTING);
% Vector for light position (directional light - try a positional one)

% Get Projection matrix in array of length 16
projmatrix = NET.createArray('System.Single', 16);
Gl.glGetFloatv(Gl.GL_PROJECTION_MATRIX,projmatrix);
% Reshape to 4x4 matrix
projmatrix=reshape(double(projmatrix),[4 4]);
% Use the inverse projection matrix to get the light fixed with the camera
LightPosCam=(projmatrix)\[0.3 0.5 -0.6 0]';

LightPos= NET.convertArray(LightPosCam, 'System.Single', 4);
Ambient =  NET.convertArray([0.1 0.1 0.1 1], 'System.Single', 4); 
Diffuse=  NET.convertArray([0.6 0.6 0.6 1], 'System.Single', 4); 
Specular=  NET.convertArray([0.3 0.3 0.3 1], 'System.Single', 4); 

% Turn on Lighting
Gl.glEnable(Gl.GL_LIGHTING);
Gl.glEnable(Gl.GL_LIGHT0);

Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_POSITION,LightPos);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_AMBIENT,Ambient);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_DIFFUSE,Diffuse);
Gl.glLightfv(Gl.GL_LIGHT0,Gl.GL_SPECULAR,Specular);

% Constant attenuation (for distance, etc.)
% Only works for fixed light locations!  Otherwise disabled
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_CONSTANT_ATTENUATION, 1.0);
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_LINEAR_ATTENUATION, 0.0);
Gl.glLightf(Gl.GL_LIGHT0, Gl.GL_QUADRATIC_ATTENUATION, 0.0);
Gl.glLightModeli(Gl.GL_LIGHT_MODEL_LOCAL_VIEWER, Gl.GL_TRUE);

% Normalize vectors
Gl.glEnable(Gl.GL_NORMALIZE);

% Enable ColorMaterial
Gl.glEnable (Gl.GL_COLOR_MATERIAL ) ;
% Set the Material Properties
floor_ambient = NET.convertArray([0, 0, 0.3 1.0 ], 'System.Single', 4); 
floor_diffuse = NET.convertArray([0.3, 0.3, 0.3, 1.0 ], 'System.Single', 4); 
floor_specular= NET.convertArray([2,2,2, 1 ], 'System.Single', 4);
floor_emission= NET.convertArray([0.2,0,0, 1 ], 'System.Single', 4);
floor_shininess = 10;
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, floor_ambient);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, floor_diffuse);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, floor_specular);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SHININESS, floor_shininess);
Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_EMISSION, floor_emission);

% Gourang shanding
Gl.glShadeModel(Gl.GL_SMOOTH);

% Specular color enable
Gl.glLightModeli(Gl.GL_LIGHT_MODEL_COLOR_CONTROL,Gl.GL_SEPARATE_SPECULAR_COLOR);

% Display the vessels
Gl.glCallList(data.VesselMeshOpenGLID);


% Disable Light
Gl.glDisable(Gl.GL_COLOR_MATERIAL ) ;
Gl.glDisable(Gl.GL_LIGHT0);
Gl.glDisable(Gl.GL_LIGHTING);


% Flush the pipeline, update the graphics buffer
Gl.glFlush();


Contact us at files@mathworks.com