| |

VerySource

 Forgot password?
 Register
Search
View: 1130|Reply: 7

VC ++ and OPENGL developmentOne

[Copy link]

3

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-16 12:30:01
| Show all posts |Read mode
The establishment of OpenGL application framework
#include <gl\gl.h> // Header file for OpenGL32 library
#include <gl\glu.h> // Header file for GLu32 library
#include <gl\glaux.h> // Header file for the GLaux library

Initialize OpenGL
SetOpenGLInterface ()
PIXELFORMATDESCRIPTOR pfd = {
// Initialize the pixel storage format
Sizeof (PIXELFORMATDESCRIPTOR), // size of pfd
1, // version number
PFD_DRAW_TO_WINDOW | // Support window
PFD_SUPPORT_OPENGL | // Support OpenGL
PFD_DOUBLEBUFFER, // support double buffer
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // each color bit (ignored)
0, // no alpha cache
0, // ignore conversion bits
0, // no accumulation
0, 0, 0, 0,
32, // 32-bit depth buffer
0, // no template cache
0, // no auxiliary cache
PFD_MAIN_PLANE, // main drawing layer
0, // reserved
0, 0, 0 // ignored layer mask
};
m_pDC = GetDC (); // Get device environment handle
int iFormat = ChoosePixelFormat (m_pDC-> m_hDC,&pfd); // Set the pixel format
SetPixelFormat (m_pDC-> m_hDC, iFormat,&pfd);
m_hGlrc = wglCreateContext (m_pDC-> m_hDC); // Create a rendering context
wglMakeCurrent (m_pDC-> m_hDC, m_hGlrc); // Set the current drawing description table of a thread
DEM data structure definition

ReleaseDC (m_pDC); // Release DC
if (m_hGlrc! = NULL) // release RC
wglDeleteContext (m_hGlrc);

Set up the scene
InitOpenGL ()
GLfloat light_position [] = {0.0, 0.0, 1.0, 0.0}; // define the position coordinates of the light source
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
GLfloat light_ambient [] = {0.0, 0.0, 0.0, 1.0}; // define ambient reflected light
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
GLfloat light_diffuse [] = {1.0, 1.0, 1.0, 1.0}; // define diffuse reflection light
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
GLfloat light_specular [] = {1.0, 1.0, 1.0, 1.0}; // define specular reflection light
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
GLfloat light_model_ambient [] = {0.4f, 0.4f, 0.4f, 1.0f}; // define light model parameters
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
GLfloat local_view [] = {0.0};
glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);


glEnable (GL_LIGHTING); // GL_LIGHTING is valid
glEnable (GL_LIGHT0); // GL_LIGHT0 is valid
glEnable (GL_DEPTH_TEST); // Allow deep comparison
glDepthFunc (GL_LESS); // activate depth comparison
glClearColor (0.1f, 0.1f, 0.5f, 0.0f); // Set the blue background
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE); // Trade-off image quality and drawing speed

Define the viewport
InitViewPort ()
CRect rect; // Get the size of the drawing client area
GetClientRect (rect);
glMatrixMode (GL_PROJECTION); // Set the projection mode
glLoadIdentity (); // Load identity matrix
if (m_nViewMode == 0) // establish a perspective projection matrix
GluPerspective (90.0, rect.Width () / rect.Height (), 1.0, 10000.0);
if (m_nViewMode == 1) // establish an orthographic projection matrix
GlOrtho (-0.5 * 10000.0, 0.5 * 10000.0, -0.5 * 10000.0, 0.5 * 10000.0, 1.0, 10000.0); glViewport (0, 0, rect.Width (), rect.Height ()); // Reset viewport
GlMatrixMode (GL_MODELVIEW); // determine the current matrix mode
GlLoadIdentity (); // load identity matrix

This is the code to find online, I don't know much, I hope to know how to help heroes debug.
Reply

Use magic Report

0

Threads

55

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-4-28 08:45:01
| Show all posts
First compile and link to see if there is any problem.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-12 15:00:01
| Show all posts
First make sure the connection is clear, and the settings must be made well, huh, huh
Reply

Use magic Report

3

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-7-12 21:15:01
| Show all posts
There are still many mistakes in the connection. I think the key is that I am not familiar with the operation mode of VC++, and the location of the code may not be placed properly, so I hope that friends who have seen similar things will give pointers.
Thank you
Reply

Use magic Report

0

Threads

14

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-7-13 17:30:01
| Show all posts
just for reference
BOOL CSingleView::PreCreateWindow(CREATESTRUCT&cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
////////////////////////////////////////////////// //////////////
//Set the window type
cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
////////////////////////////////////////////////// //////////////
return CView::PreCreateWindow(cs);
}

////////////////////////////////////////////////// ///////////////////////////
// CSingleView drawing
void CSingleView::OnDraw(CDC* pDC)
{
CSingleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
////////////////////////////////////////////////// ////////////////
RenderScene(); //Render the scene
////////////////////////////////////////////////// ////////////////

}
int CSingleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
The
// TODO: Add your specialized creation code here
////////////////////////////////////////////////// ////////////////
//Initialize OpenGL and set timer
m_pDC = new CClientDC(this);
SetTimer(1, 20, NULL);
InitializeOpenGL(m_pDC);
////////////////////////////////////////////////// ////////////////
Init();
return 0;
}
void CSingleView::OnDestroy()
{
CView::OnDestroy();
The
// TODO: Add your message handler code here
////////////////////////////////////////////////// ///////////////
//Delete palette and rendering context, timer
::wglMakeCurrent(0,0);
::wglDeleteContext( m_hRC);
if (m_hPalette)
DeleteObject(m_hPalette);
if (m_pDC)
{
delete m_pDC;
}
KillTimer(1);
////////////////////////////////////////////////// ///////////////
The
}

void CSingleView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
The
// TODO: Add your message handler code here
////////////////////////////////////////////////// ///////////////
//Add graphics transformation function when the window is zoomed
glViewport(0,0,cx,cy);
////////////////////////////////////////////////// ///////////////
GLdouble aspect_ratio;
aspect_ratio = (GLdouble)cx/(GLdouble)cy;
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
gluPerspective(40.0F, aspect_ratio, 1.0F, 10000.0F);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
The
}
void CSingleView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
////////////////////////////////////////////////// ///////////////
//Add timer response function and scene update function
Invalidate(FALSE);
////////////////////////////////////////////////// ///////////////
The
CView::OnTimer(nIDEvent);
}

////////////////////////////////////////////////// ///////////////////
// Set logical palette
////////////////////////////////////////////////// ////////////////////
void CSingleView::SetLogicalPalette(void)
{
    struct
    {
        WORD Version;
        WORD NumberOfEntries;
        PALETTEENTRY aEntries[256];
    } logicalPalette = {0x300, 256 };

BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE blues[] = {0, 85, 170, 255};

    for (int colorNum=0; colorNum<256; ++colorNum)
    {
        logicalPalette.aEntries[colorNum].peRed =
            reds[colorNum&0x07];
        logicalPalette.aEntries[colorNum].peGreen =
            greens[(colorNum >> 0x03)&0x07];
        logicalPalette.aEntries[colorNum].peBlue =
            blues[(colorNum >> 0x06)&0x03];
        logicalPalette.aEntries[colorNum].peFlags = 0;
    }

    m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
}


////////////////////////////////////////////////// ////////
// Initialize the openGL scene
////////////////////////////////////////////////// ////////
BOOL CSingleView::InitializeOpenGL(CDC* pDC)
{
m_pDC = pDC;
SetupPixelFormat();
//Generate drawing description table
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
//Set the current drawing description table
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

return TRUE;
}

////////////////////////////////////////////////// ////////
// Set the pixel format
////////////////////////////////////////////////// ////////
BOOL CSingleView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of pfd structure
1, // version number
PFD_DRAW_TO_WINDOW | // Support drawing in the window
PFD_SUPPORT_OPENGL | // Support OpenGL
PFD_DOUBLEBUFFER, // Double buffer mode
PFD_TYPE_RGBA, // RGBA color mode
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // ignore color bits
0, // No non-transparency cache
0, // Ignore shift bits
0, // No accumulation buffer
0, 0, 0, 0, // ignore accumulated bits
32, // 32-bit depth cache
0, // No template cache
0, // No auxiliary cache
PFD_MAIN_PLANE, // Main layer
0, // reserved
0, 0, 0 // Ignore layer, visibility and damage mask
};
int pixelformat;
pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd);//Select the pixel format
::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat,&pfd); //Set pixel format
if(pfd.dwFlags&PFD_NEED_PALETTE)
SetLogicalPalette(); //Set logical palette
return TRUE;
}


////////////////////////////////////////////////// ////////
// Scene drawing and rendering
////////////////////////////////////////////////// ////////
BOOL CSingleView::RenderScene()
{
...............
...............
}
void CSingleView::Init(GLvoid)
{
.............
.............
}
Reply

Use magic Report

3

Threads

7

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-7-21 11:00:01
| Show all posts
Thank you "addforever" for your advice.
I hope to talk about the process that the program goes through when drawing an opengl image. I am rather stupid. Only in this way can I understand where the OpenGL drawing function should be placed.
Hope to attach a small sample code.
Function is
There are several space points defined (7 or 8 are enough) (X, Y, Height) to draw a surface. I wonder if it's OK?
Following
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-7-29 18:30:01
| Show all posts
To learn OPENGL, you can see NEHE's tutorial. I encapsulated an OCX control for OPENGL, which can be easily used in dialog boxes and other programs. It is very convenient and can be sent to you.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-29 20:15:01
| Show all posts
It's nice to have someone give you some pointers. I figured it out all by myself. Now it feels like one word: simple.
Under MFC, it is nothing more than setting the pixel format, testing the pixel format, DC RC want to connect, this is the initialization of the program. There is also the initialization of the scene by looking at the camera model (it includes a lot of things, but it's actually very simple), and other settings like blanking, anti-aliasing, lighting and texture mapping are not difficult. I feel that the most difficult part is the modeling part, until the best programming problem has become a mathematical problem related to my major.


Don’t forget to release the memory at the end~~
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list