Click here to Skip to main content
15,908,254 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralHandle system menu (custom) Pin
alex.barylski14-Jan-04 14:46
alex.barylski14-Jan-04 14:46 
GeneralRe: Handle system menu (custom) Pin
David Crow15-Jan-04 8:03
David Crow15-Jan-04 8:03 
GeneralCPU usage per process. (Multiprocess System) Pin
ham744614-Jan-04 13:46
ham744614-Jan-04 13:46 
GeneralSimple Tutorial Pin
merlin37114-Jan-04 13:39
merlin37114-Jan-04 13:39 
GeneralRe: Simple Tutorial Pin
Prakash Nadar14-Jan-04 19:38
Prakash Nadar14-Jan-04 19:38 
QuestionActiveX Controls in VC.NET 2003? Pin
LunaticFringe14-Jan-04 13:01
LunaticFringe14-Jan-04 13:01 
Generalclistctrl, and images Pin
pnpfriend14-Jan-04 12:06
pnpfriend14-Jan-04 12:06 
GeneralCode Crash Pin
Member 81979414-Jan-04 11:33
Member 81979414-Jan-04 11:33 
Hey all,

I am encountering the following problem.
My program crashes with I try to read a netCDF file and display it in VC++ with OpenGL.
I have created a Win32 Application, and it works just fine and displays any other graphics code. I am a new VC++ user and hence dont know much stuff in it. I would highly appreciate if anyone could help me out. I would like to know at which point should I read the netcdf file the following is my code


#include <windows.h> /* must include this before GL/gl.h */
#include <GL/gl.h> /* OpenGL header file */
#include <GL/glu.h> /* OpenGL utilities header file */
#include <stdio.h>
#include <math.h>

#include <netcdf.h>
#include <stdio.h>
#include <string.h>

// Variable definitions. The comments contain the dimension names.
long lat[11]; // lat
char lat_units[6];
long lon[11]; // lon
char lon_units[6];
long depth[11]; // depth
char depth_units[6];
long time[60]; // time
char time_units[7];
float magnitude[60][11][11][11]; // time, lat, lon, depth
char magnitude_units[10];
double magnitude_valid_range[2];


void
read_netCDF_file ()
{
//# func_description
// This routine reads the data from plume1.nc into memory.

int fd;
long start[MAX_NC_VARS];
long end[MAX_NC_VARS];

// Open the netCDF file.
fd = ncopen("plume1.nc", NC_NOWRITE);

// Initialize start so all elements are zero.
// bzero(start, MAX_NC_VARS * sizeof(long));

memset(start,0, MAX_NC_VARS * sizeof(long));

// Read in the variables and their attributes.
// Read: lat.
end[0] = 11;
ncvarget(fd, 0, start, end, lat);
// Read: lat_units.
ncattget(fd, 0, "units", lat_units);

// Read: lon.
end[0] = 11;
ncvarget(fd, 1, start, end, lon);
// Read: lon_units.
ncattget(fd, 1, "units", lon_units);

// Read: depth.
end[0] = 11;
ncvarget(fd, 2, start, end, depth);
// Read: depth_units.
ncattget(fd, 2, "units", depth_units);

// Read: time.
end[0] = 60;
ncvarget(fd, 3, start, end, time);
// Read: time_units.
ncattget(fd, 3, "units", time_units);

// Read: magnitude.
end[0] = 60; end[1] = 11; end[2] = 11; end[3] = 11;
ncvarget(fd, 4, start, end, magnitude);
// Read: magnitude_units.
ncattget(fd, 4, "units", magnitude_units);
// Read: magnitude_valid_range.
ncattget(fd, 4, "valid_range", magnitude_valid_range);

// Read in the Global Attributes.
// Close the netCDF file.
ncclose(fd);
}


void
display()
{

GLint x = 0; // latitude variable
GLint y = 0; // longitude variable
GLint z = 0; // depth variable
GLint t = 0; // time variable
float mag ; //magnitude at pt [x,y,z,t]
int color_code ; // color code which will be displayed
// so that accordingly the point can be colored

glClear(GL_COLOR_BUFFER_BIT);
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
// glClearDepth(1.0f); // Depth Buffer Setup
// glEnable(GL_DEPTH_TEST); // Enables Depth Testing
// glDepthFunc(GL_LEQUAL);
glColor3f(1.0f,0.0f,0.0f);
// glBegin(GL_POINTS);

/* for(t=0;t<2;t++) {
for(x=0;x<11;x++) {
for(y=0;y<11;y++) {
for(z=0;z<11;z++) {
mag = magnitude[t][x][y][z];
//color_code = getcolor(mag);
glVertex3i(x,y,z);
}
}
}
}*/

glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();

// glEnd();
glFlush();
}


LONG WINAPI
WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PAINTSTRUCT ps;

switch(uMsg) {
case WM_PAINT:
display();
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0;

case WM_SIZE:
glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
PostMessage(hWnd, WM_PAINT, 0, 0);
return 0;

case WM_CHAR:
switch (wParam) { // wParam is the parameter which is passed in from the window which is
// finally displayed here we can take in the key sequences from the
// screen
case 27: /* ESC key */
PostQuitMessage(0);
break;
}
return 0;

case WM_CLOSE:
PostQuitMessage(0);
return 0;
}

return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

HWND
CreateOpenGLWindow(char* title, int x, int y, int width, int height,
BYTE type, DWORD flags)
{
int pf;
HDC hDC;
HWND hWnd;
WNDCLASS wc;
PIXELFORMATDESCRIPTOR pfd;
static HINSTANCE hInstance = 0;

/* only register the window class once - use hInstance as a flag. */
if (!hInstance) {
hInstance = GetModuleHandle(NULL);
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";

if (!RegisterClass(&wc)) {
MessageBox(NULL, "RegisterClass() failed: "
"Cannot register window class.", "Error", MB_OK);
return NULL;
}
}

hWnd = CreateWindow("OpenGL", title, WS_OVERLAPPEDWINDOW |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
x, y, width, height, NULL, NULL, hInstance, NULL);

if (hWnd == NULL) {
MessageBox(NULL, "CreateWindow() failed: Cannot create a window.",
"Error", MB_OK);
return NULL;
}

hDC = GetDC(hWnd);

/* there is no guarantee that the contents of the stack that become
the pfd are zeroed, therefore _make sure_ to clear these bits. */
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
pfd.iPixelType = type;
pfd.cColorBits = 32;

pf = ChoosePixelFormat(hDC, &pfd);
if (pf == 0) {
MessageBox(NULL, "ChoosePixelFormat() failed: "
"Cannot find a suitable pixel format.", "Error", MB_OK);
return 0;
}

if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
MessageBox(NULL, "SetPixelFormat() failed: "
"Cannot set format specified.", "Error", MB_OK);
return 0;
}

DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

ReleaseDC( hWnd, hDC );

return hWnd;
}

int APIENTRY
WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,
LPSTR lpszCmdLine, int nCmdShow)
{
HDC hDC; /* device context */
HGLRC hRC; /* opengl context */
HWND hWnd; /* window */
MSG msg; /* message */

read_netCDF_file();

hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
if (hWnd == NULL)
exit(1);

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

ShowWindow(hWnd, nCmdShow);

while(GetMessage(&msg, hWnd, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

wglMakeCurrent(NULL, NULL);
ReleaseDC( hWnd, hDC );
wglDeleteContext(hRC);
DestroyWindow(hWnd);

return msg.wParam;
}


I am new user of VC++ and Win32 Programming.

Regards,
kris


Help Required ASAP !!!
GeneralRe: Code Crash Pin
Christian Graus14-Jan-04 11:57
protectorChristian Graus14-Jan-04 11:57 
GeneralRe: Code Crash Pin
Member 81979414-Jan-04 12:16
Member 81979414-Jan-04 12:16 
GeneralOverlapped Connect Pin
JT Anderson14-Jan-04 11:25
JT Anderson14-Jan-04 11:25 
GeneralRe: Overlapped Connect Pin
valikac14-Jan-04 16:21
valikac14-Jan-04 16:21 
GeneralRe: Overlapped Connect Pin
JT Anderson15-Jan-04 4:12
JT Anderson15-Jan-04 4:12 
GeneralRe: Overlapped Connect Pin
valikac15-Jan-04 11:30
valikac15-Jan-04 11:30 
GeneralRe: Overlapped Connect Pin
JT Anderson15-Jan-04 11:48
JT Anderson15-Jan-04 11:48 
Generalexecute a binary resource without first saving it to disk Pin
Diddy14-Jan-04 11:12
Diddy14-Jan-04 11:12 
GeneralRe: execute a binary resource without first saving it to disk Pin
valikac14-Jan-04 16:29
valikac14-Jan-04 16:29 
GeneralRe: execute a binary resource without first saving it to disk Pin
Diddy14-Jan-04 22:09
Diddy14-Jan-04 22:09 
Generalmultiple languages w/o separate builds Pin
KeyserSoze0014-Jan-04 10:00
KeyserSoze0014-Jan-04 10:00 
GeneralRe: multiple languages w/o separate builds Pin
l a u r e n14-Jan-04 10:05
l a u r e n14-Jan-04 10:05 
QuestionWhere can I learn DevStudio? Pin
BrockVnm14-Jan-04 9:40
BrockVnm14-Jan-04 9:40 
AnswerRe: Where can I learn DevStudio? Pin
l a u r e n14-Jan-04 9:45
l a u r e n14-Jan-04 9:45 
GeneralRe: Where can I learn DevStudio? Pin
BrockVnm14-Jan-04 9:59
BrockVnm14-Jan-04 9:59 
GeneralRe: Where can I learn DevStudio? Pin
l a u r e n14-Jan-04 10:01
l a u r e n14-Jan-04 10:01 
GeneralRe: Where can I learn DevStudio? Pin
BrockVnm14-Jan-04 10:12
BrockVnm14-Jan-04 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.