Simple OpenGL-based 3D CAD framework (1) copengldc

Source: Internet
Author: User

Copengldc. Similar to the CDC class, you can temporarily Add the vertex function and line function, or add other functions.

 

 

Class copengldc: Public cobject
{
Public:
Copengldc (hwnd );
Virtual ~ Copengldc ();

PRIVATE:
Hwnd m_hwnd;
Hglrc m_hrc;
HDC m_hdc;

Public:
Gcamera m_camera;

Cvector3d m_veclight;

Colorref m_mat_clr;

Colorref m_bk_clr;

 

Public:
// Initialize
Bool initdc ();
Void glresize (int cx, int CY );
Void glsetuprc ();

Void ready (); // start the scenario
Void finish (); // Force Execution and cache Switching

Void lighting (bool blighting );
Bool islighting ();
Void setlightdirection (float * l_direction );
// Specify the background color and object color
Void setmaterialcolor (colorref CLR );
Void setbkcolor (colorref CLR );
Void getbkcolor (colorref & CLR );
Void clearbkground ();
Void setcolor (colorref CLR );

Void drawline (const cpoint3d & p_begin, const cpoint3d & p_end); // draw a line
Void drawpoint (const cpoint3d & P); // painting point
Void drawelement (element * Ele); // draw an octal element.
 
};

 

The implementation is as follows:

 

 

Copengldc: copengldc (hwnd): m_hwnd (hwnd)
{
}

Copengldc ::~ Copengldc ()
{

}

Bool copengldc: initdc ()
{
If (m_hwnd = NULL) return false;
 
M_camera.init ();

M_hdc =: getdc (m_hwnd); // get the device context

Pixelformatdescriptor pfdwnd =
{
Sizeof (pixelformatdescriptor), // structure size.
1, // structure version number.
Pfd_draw_to_window | // property flags.
Pfd_support_opengl |
Pfd_doublebuffer,
Pfd_type_rgba,
24, // 24-bit color.
0, 0, 0, 0, 0, 0, // not concerned with these.
0, 0, 0, 0, 0, 0, 0, // No Alpha or accum buffer.
32, // 32-bit depth buffer.
0, 0, // No stencel or aux buffer.
Pfd_main_plane, // main layer type.
0, // reserved.
0, 0, 0 // unsupported.
};

Int pixelformat;
If (pixelformat = choosepixelformat (m_hdc, & pfdwnd) = 0)
{
Afxmessagebox ("choosepixelformat to WND failed ");
Return false;
}

If (setpixelformat (m_hdc, pixelformat, & pfdwnd) = false)
Afxmessagebox ("setpixelformat failed ");

M_hrc = wglcreatecontext (m_hdc );

Verify (wglmakecurrent (m_hdc, m_hrc ));
Glsetuprc ();
Wglmakecurrent (null, null );
Return m_hrc! = 0;
}

Void copengldc: glresize (int w, int H)
{
Wglmakecurrent (m_hdc, m_hrc );

// Prevent a divide by zero
If (H = 0) H = 1;
If (W = 0) W = 1;
M_camera.set_screen (W, H );
Wglmakecurrent (null, null );
}

Void copengldc: glsetuprc ()
{
Glenable (gl_depth_test); // Hidden Surface Removal
Glable (gl_color_material );

Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f); // background color
// Default color
Glfloat lightpos [] = {0.0, 0.0, 1000.0, 1.0 };
Glfloat specular [] = {0.4, 0.6, 0.8, 1.0 };
Glfloat matspecular [] = {1.0, 1.0, 1.0, 1.0 };
Glable (gl_lighting );
Glable (gl_light0 );

Glmaterialfv (gl_front, gl_specular, matspecular );
Gllightfv (gl_light0, gl_specular, specular );
Gllightfv (gl_light0, gl_position, lightpos );

Glcolor3ub (0, 0,255 );

/* Glpolygonmode (gl_front, gl_line );
Glpolygonmode (gl_back, gl_line );
Glshademodel (gl_flat );
Glable (gl_normalize );

// Set lighting and material attributes
Glfloat ambientproperties [] = {0.f, 0.f, 0.f, 1.0f };
Glfloat diffuseproperties [] = {0.f, 0.f, 0.f, 1.0f };
Glfloat specularproperties [] = {0.f, 0.f, 0.f, 1.0f };
 
Glcleardepth (1.0f );

 
Gllightfv (gl_light0, gl_ambient, ambientproperties );
Gllightfv (gl_light0, gl_diffuse, diffuseproperties );
Gllightfv (gl_light0, gl_specular, specularproperties );
Gllightmodelf (gl_light_model_two_side, 1.0f );

// Add illumination by default
Glable (gl_light0 );
Glable (gl_lighting );

Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
*/
}

Void copengldc: Ready ()
{
Wglmakecurrent (m_hdc, m_hrc );
M_camera.projection ();
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
}

Void copengldc: Finish ()
{
Glflush ();
Swapbuffers (m_hdc );
Wglmakecurrent (null, null );
}

Void copengldc: Lighting (bool blighting) // controls the illumination.
{
If (blighting)
{
Glable (gl_lighting );
}
Else
{
Gldisable (gl_lighting );
}
}

Bool copengldc: islighting () // determines the lighting Status
{
Glboolean blighting;
Glgetbooleanv (gl_lighting, & blighting );
Return blighting;

}

Void setlightdirection (float * l_direction) // four values, same coordinates
{
// Cvector3d m_veclight = cvector3d (l_direction); // automatically calls the constructor.
// Glmaterialfv (gl_front, gl_ambient_and_diffuse, l_direction );
}

Void copengldc: setmaterialcolor (colorref CLR)
{

M_mat_clr = CLR;

Glclampf R, G, B;
R = getrvalue (CLR );
G = getgvalue (CLR );
B = getbvalue (CLR );

Glfloat mat_a_d [] = {(glfloat) r/255, (glfloat) g/255, (glfloat) B/255 };
Glmaterialfv (gl_front, gl_ambient_and_diffuse, mat_a_d );
}

Void copengldc: setbkcolor (colorref CLR)
{
M_bk_clr = CLR;
}

Void copengldc: getbkcolor (colorref & CLR)
{
CLR = m_bk_clr;
}

Void copengldc: clearbkground ()
{
Glclampf R, G, B;
R = (glclampf) getrvalue (m_bk_clr)/255;
G = (glclampf) getgvalue (m_bk_clr)/255;
B = (glclampf) fig (m_bk_clr)/255;

Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
Glclearcolor (R, G, B, 1.0 );
}

Void copengldc: setcolor (colorref CLR)
{

Glcolor3ub (getrvalue (CLR), getgvalue (CLR), getbvalue (CLR ));
}

Void copengldc: drawline (const cpoint3d & p_begin, const cpoint3d & p_end)
{
Glbegin (gl_lines );
Glvertex3f (p_begin.x, p_begin.y, p_begin.z );
Glvertex3f (p_end.x, p_end.y, p_end.z );
Glend ();
}

Void copengldc: drawpoint (const cpoint3d & P)
{
Glbegin (gl_points );
Glvertex3f (P. X, p. Y, P. z );
Glend ();
}

Void copengldc: drawelement (element * Ele)
{

 

Glbegin (gl_quads );
Glvertex3f (ele-> num [1], ele-> num [2], ele-> num [3]);
Glvertex3f (ele-> num [4], ele-> num [5], ele-> num [6]);
Glvertex3f (ele-> num [7], ele-> num [8], ele-> num [9]);
Glvertex3f (ele-> num [10], ele-> num [11], ele-> num [12]);
Glend ();

Glbegin (gl_quads );
Glvertex3f (ele-> num [13], ele-> num [14], ele-> num [15]);
Glvertex3f (ele-> num [16], ele-> num [17], ele-> num [18]);
Glvertex3f (ele-> num [19], ele-> num [20], ele-> num [21]);
Glvertex3f (ele-> num [22], ele-> num [23], ele-> num [24]);
Glend ();

Glbegin (gl_quads );
Glvertex3f (ele-> num [13], ele-> num [14], ele-> num [15]);
Glvertex3f (ele-> num [16], ele-> num [17], ele-> num [18]);
Glvertex3f (ele-> num [4], ele-> num [5], ele-> num [6]);
Glvertex3f (ele-> num [1], ele-> num [2], ele-> num [3]);
Glend ();

Glbegin (gl_quads );
Glvertex3f (ele-> num [13], ele-> num [14], ele-> num [15]);
Glvertex3f (ele-> num [1], ele-> num [2], ele-> num [3]);
Glvertex3f (ele-> num [10], ele-> num [11], ele-> num [12]);
Glvertex3f (ele-> num [22], ele-> num [23], ele-> num [24]);
Glend ();

Glbegin (gl_quads );
Glvertex3f (ele-> num [22], ele-> num [23], ele-> num [24]);
Glvertex3f (ele-> num [19], ele-> num [20], ele-> num [21]);
Glvertex3f (ele-> num [7], ele-> num [8], ele-> num [9]);
Glvertex3f (ele-> num [10], ele-> num [11], ele-> num [12]);
Glend ();

Glbegin (gl_quads );
Glvertex3f (ele-> num [16], ele-> num [17], ele-> num [18]);
Glvertex3f (ele-> num [4], ele-> num [5], ele-> num [6]);
Glvertex3f (ele-> num [7], ele-> num [8], ele-> num [9]);
Glvertex3f (ele-> num [19], ele-> num [20], ele-> num [21]);
Glend ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.