Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:06:50+00:00 2026-06-18T02:06:50+00:00

I have an opengl c++ code for reading .ply file. I want to read

  • 0

I have an opengl c++ code for reading .ply file. I want to read keyboard arrow keys(up,down,right,left) for move a point on the screen. I used glutKeyboardFunc function but i have an error:

Unhandled exception at 0x1000bb1e in opengl3.exe: 0xC0000005: Access violation reading location 0x00000070.

On this line:

glutKeyboardFunc(keyPressed);

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <string>
#include <conio.h>
HWND    hWnd;
HDC     hDC;
HGLRC   hRC;

// Set up pixel format for graphics initialization
void SetupPixelFormat()
{
    PIXELFORMATDESCRIPTOR pfd, *ppfd;
    int pixelformat;

    ppfd = &pfd;

    ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
    ppfd->nVersion = 1;
    ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    ppfd->dwLayerMask = PFD_MAIN_PLANE;
    ppfd->iPixelType = PFD_TYPE_COLORINDEX;
    ppfd->cColorBits = 16;
    ppfd->cDepthBits = 16;
    ppfd->cAccumBits = 0;
    ppfd->cStencilBits = 0;

    pixelformat = ChoosePixelFormat(hDC, ppfd);
    SetPixelFormat(hDC, pixelformat, ppfd);
}

void InitGraphics()
{
    hDC = GetDC(hWnd);

    SetupPixelFormat();

    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    glClearColor(0, 0, 0, 0.5);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
}

// Resize graphics to fit window
void ResizeGraphics()
{
    // Get new window size
    RECT rect;
    int width, height;
    GLfloat aspect;

    GetClientRect(hWnd, &rect);
    width = rect.right;
    height = rect.bottom;
    aspect = (GLfloat)width / height;

    // Adjust graphics to window size
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, aspect, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

// Draw frame
void DrawGraphics()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set location in front of camera
    glLoadIdentity();
    glTranslated(0, 0, -10);

    // Draw a square
    glBegin(GL_QUADS);
    glColor3d(100, 200, 30);
    /*
    glVertex3d(-2, 2, 0);
    glVertex3d(2, 2, 0);
    glVertex3d(2, -2, 0);
    glVertex3d(-2, -2, 0);
    */






    //FILE * file = fopen("D:\\dart.ply","r");
    FILE * file = fopen("mosalas.ply","r");
        if (file!=NULL)
  {
        fseek(file,0,SEEK_END);
        long fileSize = ftell(file);
        float* Vertex_Buffer;
        try
        {
        Vertex_Buffer = (float*) malloc (ftell(file));
        }
        catch (char* )
        {
            //return -1;
        }
        //if (Vertex_Buffer == NULL) return -1;
        fseek(file,0,SEEK_SET); 

       //Faces_Triangles = (float*) malloc(fileSize*sizeof(float));
       //Normals  = (float*) malloc(fileSize*sizeof(float));

       if (file)
       {
            int i = 0;   
            int temp = 0;
            int quads_index = 0;
            int triangle_index = 0;
            int normal_index = 0;
            char buffer[1000];


            fgets(buffer,300,file);         // ply


            // READ HEADER
            // -----------------
            int TotalConnectedPoints;
            // Find number of vertexes
            while (  strncmp( "element vertex", buffer,strlen("element vertex")) != 0  )
            {
                fgets(buffer,300,file);         // format
            }
            strcpy(buffer, buffer+strlen("element vertex"));
            sscanf(buffer,"%i", &TotalConnectedPoints);

            int TotalFaces;
            // Find number of face
            fseek(file,0,SEEK_SET);
            while (  strncmp( "element face", buffer,strlen("element face")) != 0  )
            {
                fgets(buffer,300,file);         // format
            }
            strcpy(buffer, buffer+strlen("element face"));
            sscanf(buffer,"%i", &TotalFaces);


            // go to end_header
            while (  strncmp( "end_header", buffer,strlen("end_header")) != 0  )
            {
                fgets(buffer,300,file);         // format
            }
            i =0;
            float* ver0,ver1,ver2,ver3;
            //char* key[100];

            for (int iterator = 0; iterator < TotalConnectedPoints; iterator++)
            {
                fgets(buffer,300,file);

                //sscanf(buffer,"%f %f %f", &Vertex_Buffer[i], &Vertex_Buffer[i+1], &Vertex_Buffer[i+2]);
                sscanf(buffer,"%f %f %f", &ver1, &ver2, &ver3);
                glNormal3f(ver1, ver2, ver3);
                glVertex3d(ver1, ver2, ver3);

                //glVertex3f(ver1, ver2, ver3);

                i += 3;
            }

       }

}



    glPushMatrix();
glBegin;
glTranslatef(-10,5,0);
glColor3f(1,1,1);
glRotatef(90,0,1,0); 
glutSolidCone(0.25, 15, 20, 20);
glEnd();
glPopMatrix();
    //glutMouseFunc(0,0,0);







    /*
    glVertex3d(1.000000, 1.000000, 6);
    glVertex3d(3.000000, 0.000000, 4);
    glVertex3d(0.000000, 0.000000, 4);
    glVertex3d(0.000000, 3.000000, 4);
    glVertex3d(1.500000, 0.500000, 0);*/





    glEnd();

    // Show the new scene
    SwapBuffers(hDC);/**/
}
void keyPressed (unsigned char key, int x, int y) {  
}  
// Handle window events and messages
LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM  lParam)
{
    switch (uMsg)
    {
    case WM_SIZE:
        ResizeGraphics();
        break;

    case WM_CLOSE: 
        DestroyWindow(hWnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    // Default event handler
    default: 
        return DefWindowProc (hWnd, uMsg, wParam, lParam); 
        break; 
    } 

    return 1; 
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

    const LPCWSTR appname = TEXT("OpenGL Sample");

    WNDCLASS wndclass;
    MSG      msg;

    // Define the window class
    wndclass.style         = 0;
    wndclass.lpfnWndProc   = (WNDPROC)MainWndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIcon(hInstance, appname);
    wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wndclass.lpszMenuName  = appname;
    wndclass.lpszClassName = appname;

    // Register the window class
    if (!RegisterClass(&wndclass)) return FALSE;

    // Create the window
    hWnd = CreateWindow(
            appname,
            appname,
            WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            800,
            600,
            NULL,
            NULL,
            hInstance,
            NULL);

    if (!hWnd) return FALSE;

    // Initialize OpenGL
    InitGraphics();

    // Display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    DrawGraphics();
    // Event loop
    int key;
    int asgh = 0;
    POINT pt;
    /**/while (1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
        {
            if (!GetMessage(&msg, NULL, 0, 0))
                return TRUE;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }



        glutKeyboardFunc(keyPressed);
        DrawGraphics();
    }

    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:06:51+00:00Added an answer on June 18, 2026 at 2:06 am

    The program you posted lacks a call to glutInit().

    Furthermore you’re doing all of the platform-specific window management and context creation that GLUT is supposed abstract away.

    Bottom line: If you want to use GLUT, use GLUT. If you want to use Win32, use Win32. Don’t try to mix-and-match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have some openGL code (such code for example) /* FUNCTION: YCamera ::
I am starting out with OpenGL ES. The example code that I am reading
I have the following OpenGL code in the display function: glLoadIdentity(); gluLookAt(eyex, eyey, eyez,
Due to performance issues, I have had to transfer my android opengl code from
I have OpenGL code like the following that I'd like to port to OpenGL
I have some simple OpenGL ES code in C++ that I've run on a
I'm working on some cross-platform code using OpenGL and SDL, but have immediately run
Using XCode 4.4.1 I have the following OpenGL code: //set the tex params glTexParameteri(GL_TEXTURE_2D,
I have optimized a function Render() by changing OpenGL code. I have then run
I have some OpenGL code that behaves inconsistently across different hardware. I've got some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.