hi all,
i am trying to use glvertex array to load in the data, and sending it to cuda.
i was testing just the drawing hte particles command, and it crashes sometimes, sometimes it works fine.dont knwo what the problem is
iam reading line of data from a text file. which has >20000 lines, each line is a vertex point
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, GL
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// includes
#include <cutil.h>
#include <cutil_gl_error.h>
#include <cuda_gl_interop.h>
////////////////////////////////////////////////////////////////////////////////
// constants
const unsigned int window_width = 800;
const unsigned int window_height = 600;
const unsigned int mesh_width = 256;
const unsigned int mesh_height = 256;
// vbo variables
float * m_d_position, *m_d_velocity;
const int M_SIZE=3000; //milky way particle size . 2000 is the size limit for malloc for my system
float scale=0.0;
float mouseWindowX = 0.0;
float mouseWindowY = 0.0;
GLuint vbo;
float anim = 0.0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
////////////////////////////////////////////////////////////////////////////////
// kernels
#include <simpleGL_kernel.cu>
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void textFileRead(char *);
void runTest( int argc, char** argv);
// GL functionality
CUTBoolean initGL();
void createVBO( GLuint* vbo);
void deleteVBO( GLuint* vbo);
// rendering callbacks
void display();
void keyboard( unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
// Cuda functionality
void runCuda( GLuint vbo);
void checkResultCuda( int argc, char** argv, const GLuint& vbo);
void atextFileRead(char *fn,float * position_arr,float *velocity_arr);
////////////////////////////////////////////////////////////////////////////////
// Reading text file - data set
////////////////////////////////////////////////////////////////////////////////
void textFileRead(char *fn)
{
FILE *fp;
char *p;
float value;
char line[100];
int i=0,j=0,count=0;
j++;//delete later
m_d_position = (float *) malloc (3 * M_SIZE * sizeof(float));
m_d_velocity = (float *) malloc (3 * M_SIZE* sizeof(float));
int TOTAL=M_SIZE*3;
if (fn != NULL) {
fp = fopen(fn,"rt"); //open in read mode
if (fp != NULL) {
while (fgets(line,100,fp)!=NULL)
{
count=0;
p = strtok(line," ");
do {
p = strtok('
#ifdef _WIN32
define WINDOWS_LEAN_AND_MEAN
define NOMINMAX
include <windows.h>
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, GL
#include <GL/glew.h>
#if defined (APPLE) || defined(MACOSX)
#include <GLUT/glut.h>
#include <GL/glut.h>
// includes
#include <cutil.h>
#include <cutil_gl_error.h>
#include <cuda_gl_interop.h>
////////////////////////////////////////////////////////////////////////////////
// constants
const unsigned int window_width = 800;
const unsigned int window_height = 600;
const unsigned int mesh_width = 256;
const unsigned int mesh_height = 256;
// vbo variables
float * m_d_position, *m_d_velocity;
const int M_SIZE=3000; //milky way particle size . 2000 is the size limit for malloc for my system
float scale=0.0;
float mouseWindowX = 0.0;
float mouseWindowY = 0.0;
GLuint vbo;
float anim = 0.0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
////////////////////////////////////////////////////////////////////////////////
// kernels
#include <simpleGL_kernel.cu>
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void textFileRead(char *);
void runTest( int argc, char** argv);
// GL functionality
CUTBoolean initGL();
void createVBO( GLuint* vbo);
void deleteVBO( GLuint* vbo);
// rendering callbacks
void display();
void keyboard( unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
// Cuda functionality
void runCuda( GLuint vbo);
void checkResultCuda( int argc, char** argv, const GLuint& vbo);
void atextFileRead(char *fn,float * position_arr,float *velocity_arr);
////////////////////////////////////////////////////////////////////////////////
// Reading text file - data set
////////////////////////////////////////////////////////////////////////////////
void textFileRead(char *fn)
{
FILE *fp;
char *p;
float value;
char line[100];
int i=0,j=0,count=0;
j++;//delete later
m_d_position = (float *) malloc (3 * M_SIZE * sizeof(float));
m_d_velocity = (float ) malloc (3 * M_SIZE sizeof(float));
int TOTAL=M_SIZE*3;
if (fn != NULL) {
fp = fopen(fn,“rt”); //open in read mode
if (fp != NULL) {
while (fgets(line,100,fp)!=NULL)
{
count=0;
p = strtok(line," ");
do {
p = strtok(’
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, GL
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// includes
#include <cutil.h>
#include <cutil_gl_error.h>
#include <cuda_gl_interop.h>
////////////////////////////////////////////////////////////////////////////////
// constants
const unsigned int window_width = 800;
const unsigned int window_height = 600;
const unsigned int mesh_width = 256;
const unsigned int mesh_height = 256;
// vbo variables
float * m_d_position, *m_d_velocity;
const int M_SIZE=3000; //milky way particle size . 2000 is the size limit for malloc for my system
float scale=0.0;
float mouseWindowX = 0.0;
float mouseWindowY = 0.0;
GLuint vbo;
float anim = 0.0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
////////////////////////////////////////////////////////////////////////////////
// kernels
#include <simpleGL_kernel.cu>
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void textFileRead(char *);
void runTest( int argc, char** argv);
// GL functionality
CUTBoolean initGL();
void createVBO( GLuint* vbo);
void deleteVBO( GLuint* vbo);
// rendering callbacks
void display();
void keyboard( unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
// Cuda functionality
void runCuda( GLuint vbo);
void checkResultCuda( int argc, char** argv, const GLuint& vbo);
void atextFileRead(char *fn,float * position_arr,float *velocity_arr);
////////////////////////////////////////////////////////////////////////////////
// Reading text file - data set
////////////////////////////////////////////////////////////////////////////////
void textFileRead(char *fn)
{
FILE *fp;
char *p;
float value;
char line[100];
int i=0,j=0,count=0;
j++;//delete later
m_d_position = (float *) malloc (3 * M_SIZE * sizeof(float));
m_d_velocity = (float *) malloc (3 * M_SIZE* sizeof(float));
int TOTAL=M_SIZE*3;
if (fn != NULL) {
fp = fopen(fn,"rt"); //open in read mode
if (fp != NULL) {
while (fgets(line,100,fp)!=NULL)
{
count=0;
p = strtok(line," ");
do {
p = strtok('\0', ", ");
if( p){
sscanf(p,"%f", &value);
if(count<3){
m_d_position[i++] = value;
}
else if(count<6){
m_d_velocity[i++]=value;
}
count++;
}
} while(p);
if(i>TOTAL){break;} //16384-1
}//while fgets
fclose(fp);
}
}
}
/******************************* DRAW POINTS ***************************************/
void drawPoints() // each time trough the draw loop we do this
{
glPointSize(1.0);
glVertexPointer(3, GL_FLOAT, 0, m_d_position); //3= x,y,z
glEnableClientState(GL_VERTEX_ARRAY); // initial particle position
glDrawArrays(GL_POINTS, 0, M_SIZE); // 100 by 100
glDisableClientState(GL_VERTEX_ARRAY);
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
textFileRead("particles.txt");
runTest( argc, argv);
CUT_EXIT(argc, argv);
}
/******************************* MOUSE X,Y DETECTION ***************************************/
void PassiveMouseMotion(int x, int y)
{
if (x<0)
mouseWindowX = -1;
else if (x>window_width)
mouseWindowX = 1;
else
mouseWindowX = 2* (x / (float) window_width) - 1.0;
if (y<0)
mouseWindowY = -1;
else if (y>window_height)
mouseWindowY = 1;
else
mouseWindowY = 2* (y / (float) window_height) - 1.0;
}
void processSpecialKeys(int key, int x, int y) {
if (key == GLUT_KEY_PAGE_UP )
scale+=1.0;
if (key == GLUT_KEY_PAGE_DOWN )
scale-=1.0;
printf("%f,",scale);
}
void reshape(int w,int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float) w / (float) h, 0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest( int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
// Create GL context
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( window_width, window_height);
glutCreateWindow( "CS 522-Sujatha project2");
// initialize GL
if( CUTFalse == initGL()) {
return;
}
// register callbacks
glutDisplayFunc( display);
glutReshapeFunc(reshape);
glutKeyboardFunc( keyboard);
glutMouseFunc( mouse);
glutMotionFunc( motion);
glutSpecialFunc(processSpecialKeys);
glutPassiveMotionFunc(PassiveMouseMotion);
// create VBO
//createVBO( &vbo);
// run the cuda part
//runCuda( vbo);
// check result of Cuda step
//checkResultCuda( argc, argv, vbo);
// start rendering mainloop
glutMainLoop();
}
////////////////////////////////////////////////////////////////////////////////
//! Run the Cuda part of the computation
////////////////////////////////////////////////////////////////////////////////
void runCuda( GLuint vbo)
{
// map OpenGL buffer object for writing from CUDA
float4 *dptr;
CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&dptr, vbo));
// execute the kernel
dim3 block(8, 8, 1);
dim3 grid(mesh_width / block.x, mesh_height / block.y, 1);
kernel<<< grid, block>>>(dptr, mesh_width, mesh_height, anim);
// unmap buffer object
CUDA_SAFE_CALL(cudaGLUnmapBufferObject( vbo));
}
////////////////////////////////////////////////////////////////////////////////
//! Initialize GL
////////////////////////////////////////////////////////////////////////////////
CUTBoolean initGL()
{
// initialize necessary OpenGL extensions
glewInit();
if (! glewIsSupported( "GL_VERSION_2_0 "
"GL_ARB_pixel_buffer_object"
)) {
fprintf( stderr, "ERROR: Support for necessary OpenGL extensions missing.");
fflush( stderr);
return CUTFalse;
}
// default initialization
glClearColor( 0.0, 0.0, 0.0, 1.0);
glDisable( GL_DEPTH_TEST);
CUT_CHECK_ERROR_GL();
return CUTTrue;
}
////////////////////////////////////////////////////////////////////////////////
//! Create VBO
////////////////////////////////////////////////////////////////////////////////
void createVBO(GLuint* vbo)
{
// create buffer object
glGenBuffers( 1, vbo);
glBindBuffer( GL_ARRAY_BUFFER, *vbo);
// initialize buffer object
// unsigned int size = mesh_width * mesh_height * 4 * sizeof( float);
glBufferData( GL_ARRAY_BUFFER, sizeof(m_d_position), 0, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_d_position), m_d_position); // copy vertices starting from 0 offest
glBindBuffer( GL_ARRAY_BUFFER, 0);
// register buffer object with CUDA
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(*vbo));
if(glIsBuffer(*vbo)){
printf("yes buffer");}
else{
printf("no buffer");
}
CUT_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Delete VBO
////////////////////////////////////////////////////////////////////////////////
void deleteVBO( GLuint* vbo)
{
glBindBuffer( 1, *vbo);
glDeleteBuffers( 1, vbo);
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(*vbo));
*vbo = 0;
}
float along_z=0.0;
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
// run CUDA kernel to generate vertex positions
//runCuda(vbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,20.0,30.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);
//glScalef(scale,scale,scale);
//glTranslatef(0.0, 0.2,0.0 );
along_z+=0.2;
//glRotatef(mouseWindowX*360,0,1,0);
// glRotatef(mouseWindowY*60,0,0,1);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.74,-19.02,6.79);
glEnd();
glColor3f(0.0, 1.0, 0.0);
drawPoints();
/* // render from the vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1.0, 0.0, 0.0);
glDrawArrays(GL_LINES,0,int(M_SIZE/3));
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
*/
glutSwapBuffers();
glutPostRedisplay();
anim += 0.01;
}
////////////////////////////////////////////////////////////////////////////////
//! Keyboard events handler
////////////////////////////////////////////////////////////////////////////////
void keyboard( unsigned char key, int /*x*/, int /*y*/)
{
switch( key) {
case( 27) :
deleteVBO( &vbo);
exit( 0);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Mouse event handlers
////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
float dx, dy;
dx = x - mouse_old_x;
dy = y - mouse_old_y;
if (mouse_buttons & 1) {
rotate_x += dy * 0.2;
rotate_y += dx * 0.2;
} else if (mouse_buttons & 4) {
translate_z += dy * 0.01;
}
mouse_old_x = x;
mouse_old_y = y;
}
////////////////////////////////////////////////////////////////////////////////
//! Check if the result is correct or write data to file for external
//! regression testing
////////////////////////////////////////////////////////////////////////////////
void checkResultCuda( int argc, char** argv, const GLuint& vbo)
{
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(vbo));
// map buffer object
glBindBuffer( GL_ARRAY_BUFFER_ARB, vbo );
float* data = (float*) glMapBuffer( GL_ARRAY_BUFFER, GL_READ_ONLY);
// check result
if( cutCheckCmdLineFlag( argc, (const char**) argv, "regression")) {
// write file for regression test
CUT_SAFE_CALL( cutWriteFilef( "./data/regression.dat",
data, mesh_width * mesh_height * 3, 0.0));
}
// unmap GL buffer object
if( ! glUnmapBuffer( GL_ARRAY_BUFFER)) {
fprintf( stderr, "Unmap buffer failed.\n");
fflush( stderr);
}
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(vbo));
CUT_CHECK_ERROR_GL();
}
', ", ");
if( p){
sscanf(p,“%f”, &value);
if(count<3){
m_d_position[i++] = value;
}
else if(count<6){
m_d_velocity[i++]=value;
}
count++;
}
} while(p);
if(i>TOTAL){break;} //16384-1
}//while fgets
fclose(fp);
}
}
}
/******************************* DRAW POINTS ***************************************/
void drawPoints() // each time trough the draw loop we do this
{
glPointSize(1.0);
glVertexPointer(3, GL_FLOAT, 0, m_d_position); //3= x,y,z
glEnableClientState(GL_VERTEX_ARRAY); // initial particle position
glDrawArrays(GL_POINTS, 0, M_SIZE); // 100 by 100
glDisableClientState(GL_VERTEX_ARRAY);
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
textFileRead(“particles.txt”);
runTest( argc, argv);
CUT_EXIT(argc, argv);
}
/******************************* MOUSE X,Y DETECTION ***************************************/
void PassiveMouseMotion(int x, int y)
{
if (x<0)
mouseWindowX = -1;
else if (x>window_width)
mouseWindowX = 1;
else
mouseWindowX = 2* (x / (float) window_width) - 1.0;
if (y<0)
mouseWindowY = -1;
else if (y>window_height)
mouseWindowY = 1;
else
mouseWindowY = 2* (y / (float) window_height) - 1.0;
}
void processSpecialKeys(int key, int x, int y) {
if (key == GLUT_KEY_PAGE_UP )
scale+=1.0;
if (key == GLUT_KEY_PAGE_DOWN )
scale-=1.0;
printf(“%f,”,scale);
}
void reshape(int w,int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float) w / (float) h, 0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest( int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
// Create GL context
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( window_width, window_height);
glutCreateWindow( “CS 522-Sujatha project2”);
// initialize GL
if( CUTFalse == initGL()) {
return;
}
// register callbacks
glutDisplayFunc( display);
glutReshapeFunc(reshape);
glutKeyboardFunc( keyboard);
glutMouseFunc( mouse);
glutMotionFunc( motion);
glutSpecialFunc(processSpecialKeys);
glutPassiveMotionFunc(PassiveMouseMotion);
// create VBO
//createVBO( &vbo);
// run the cuda part
//runCuda( vbo);
// check result of Cuda step
//checkResultCuda( argc, argv, vbo);
// start rendering mainloop
glutMainLoop();
}
////////////////////////////////////////////////////////////////////////////////
//! Run the Cuda part of the computation
////////////////////////////////////////////////////////////////////////////////
void runCuda( GLuint vbo)
{
// map OpenGL buffer object for writing from CUDA
float4 *dptr;
CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&dptr, vbo));
// execute the kernel
dim3 block(8, 8, 1);
dim3 grid(mesh_width / block.x, mesh_height / block.y, 1);
kernel<<< grid, block>>>(dptr, mesh_width, mesh_height, anim);
// unmap buffer object
CUDA_SAFE_CALL(cudaGLUnmapBufferObject( vbo));
}
////////////////////////////////////////////////////////////////////////////////
//! Initialize GL
////////////////////////////////////////////////////////////////////////////////
CUTBoolean initGL()
{
// initialize necessary OpenGL extensions
glewInit();
if (! glewIsSupported( "GL_VERSION_2_0 "
“GL_ARB_pixel_buffer_object”
)) {
fprintf( stderr, “ERROR: Support for necessary OpenGL extensions missing.”);
fflush( stderr);
return CUTFalse;
}
// default initialization
glClearColor( 0.0, 0.0, 0.0, 1.0);
glDisable( GL_DEPTH_TEST);
CUT_CHECK_ERROR_GL();
return CUTTrue;
}
////////////////////////////////////////////////////////////////////////////////
//! Create VBO
////////////////////////////////////////////////////////////////////////////////
void createVBO(GLuint* vbo)
{
// create buffer object
glGenBuffers( 1, vbo);
glBindBuffer( GL_ARRAY_BUFFER, *vbo);
// initialize buffer object
// unsigned int size = mesh_width * mesh_height * 4 * sizeof( float);
glBufferData( GL_ARRAY_BUFFER, sizeof(m_d_position), 0, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_d_position), m_d_position); // copy vertices starting from 0 offest
glBindBuffer( GL_ARRAY_BUFFER, 0);
// register buffer object with CUDA
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(*vbo));
if(glIsBuffer(*vbo)){
printf(“yes buffer”);}
else{
printf(“no buffer”);
}
CUT_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Delete VBO
////////////////////////////////////////////////////////////////////////////////
void deleteVBO( GLuint* vbo)
{
glBindBuffer( 1, *vbo);
glDeleteBuffers( 1, vbo);
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(*vbo));
*vbo = 0;
}
float along_z=0.0;
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
// run CUDA kernel to generate vertex positions
//runCuda(vbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,20.0,30.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);
//glScalef(scale,scale,scale);
//glTranslatef(0.0, 0.2,0.0 );
along_z+=0.2;
//glRotatef(mouseWindowX*360,0,1,0);
// glRotatef(mouseWindowY*60,0,0,1);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.74,-19.02,6.79);
glEnd();
glColor3f(0.0, 1.0, 0.0);
drawPoints();
/* // render from the vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1.0, 0.0, 0.0);
glDrawArrays(GL_LINES,0,int(M_SIZE/3));
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
*/
glutSwapBuffers();
glutPostRedisplay();
anim += 0.01;
}
////////////////////////////////////////////////////////////////////////////////
//! Keyboard events handler
////////////////////////////////////////////////////////////////////////////////
void keyboard( unsigned char key, int /x/, int /y/)
{
switch( key) {
case( 27) :
deleteVBO( &vbo);
exit( 0);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Mouse event handlers
////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
float dx, dy;
dx = x - mouse_old_x;
dy = y - mouse_old_y;
if (mouse_buttons & 1) {
rotate_x += dy * 0.2;
rotate_y += dx * 0.2;
} else if (mouse_buttons & 4) {
translate_z += dy * 0.01;
}
mouse_old_x = x;
mouse_old_y = y;
}
////////////////////////////////////////////////////////////////////////////////
//! Check if the result is correct or write data to file for external
//! regression testing
////////////////////////////////////////////////////////////////////////////////
void checkResultCuda( int argc, char** argv, const GLuint& vbo)
{
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(vbo));
// map buffer object
glBindBuffer( GL_ARRAY_BUFFER_ARB, vbo );
float* data = (float*) glMapBuffer( GL_ARRAY_BUFFER, GL_READ_ONLY);
// check result
if( cutCheckCmdLineFlag( argc, (const char**) argv, “regression”)) {
// write file for regression test
CUT_SAFE_CALL( cutWriteFilef( “./data/regression.dat”,
data, mesh_width * mesh_height * 3, 0.0));
}
// unmap GL buffer object
if( ! glUnmapBuffer( GL_ARRAY_BUFFER)) {
fprintf( stderr, “Unmap buffer failed.\n”);
fflush( stderr);
}
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(vbo));
CUT_CHECK_ERROR_GL();
}
', ", ");
if( p){
sscanf(p,"%f", &value);
if(count<3){
m_d_position[i++] = value;
}
else if(count<6){
m_d_velocity[i++]=value;
}
count++;
}
} while(p);
if(i>TOTAL){break;} //16384-1
}//while fgets
fclose(fp);
}
}
}
/******************************* DRAW POINTS ***************************************/
void drawPoints() // each time trough the draw loop we do this
{
glPointSize(1.0);
glVertexPointer(3, GL_FLOAT, 0, m_d_position); //3= x,y,z
glEnableClientState(GL_VERTEX_ARRAY); // initial particle position
glDrawArrays(GL_POINTS, 0, M_SIZE); // 100 by 100
glDisableClientState(GL_VERTEX_ARRAY);
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
textFileRead("particles.txt");
runTest( argc, argv);
CUT_EXIT(argc, argv);
}
/******************************* MOUSE X,Y DETECTION ***************************************/
void PassiveMouseMotion(int x, int y)
{
if (x<0)
mouseWindowX = -1;
else if (x>window_width)
mouseWindowX = 1;
else
mouseWindowX = 2* (x / (float) window_width) - 1.0;
if (y<0)
mouseWindowY = -1;
else if (y>window_height)
mouseWindowY = 1;
else
mouseWindowY = 2* (y / (float) window_height) - 1.0;
}
void processSpecialKeys(int key, int x, int y) {
if (key == GLUT_KEY_PAGE_UP )
scale+=1.0;
if (key == GLUT_KEY_PAGE_DOWN )
scale-=1.0;
printf("%f,",scale);
}
void reshape(int w,int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float) w / (float) h, 0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest( int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
// Create GL context
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( window_width, window_height);
glutCreateWindow( "CS 522-Sujatha project2");
// initialize GL
if( CUTFalse == initGL()) {
return;
}
// register callbacks
glutDisplayFunc( display);
glutReshapeFunc(reshape);
glutKeyboardFunc( keyboard);
glutMouseFunc( mouse);
glutMotionFunc( motion);
glutSpecialFunc(processSpecialKeys);
glutPassiveMotionFunc(PassiveMouseMotion);
// create VBO
//createVBO( &vbo);
// run the cuda part
//runCuda( vbo);
// check result of Cuda step
//checkResultCuda( argc, argv, vbo);
// start rendering mainloop
glutMainLoop();
}
////////////////////////////////////////////////////////////////////////////////
//! Run the Cuda part of the computation
////////////////////////////////////////////////////////////////////////////////
void runCuda( GLuint vbo)
{
// map OpenGL buffer object for writing from CUDA
float4 *dptr;
CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&dptr, vbo));
// execute the kernel
dim3 block(8, 8, 1);
dim3 grid(mesh_width / block.x, mesh_height / block.y, 1);
kernel<<< grid, block>>>(dptr, mesh_width, mesh_height, anim);
// unmap buffer object
CUDA_SAFE_CALL(cudaGLUnmapBufferObject( vbo));
}
////////////////////////////////////////////////////////////////////////////////
//! Initialize GL
////////////////////////////////////////////////////////////////////////////////
CUTBoolean initGL()
{
// initialize necessary OpenGL extensions
glewInit();
if (! glewIsSupported( "GL_VERSION_2_0 "
"GL_ARB_pixel_buffer_object"
)) {
fprintf( stderr, "ERROR: Support for necessary OpenGL extensions missing.");
fflush( stderr);
return CUTFalse;
}
// default initialization
glClearColor( 0.0, 0.0, 0.0, 1.0);
glDisable( GL_DEPTH_TEST);
CUT_CHECK_ERROR_GL();
return CUTTrue;
}
////////////////////////////////////////////////////////////////////////////////
//! Create VBO
////////////////////////////////////////////////////////////////////////////////
void createVBO(GLuint* vbo)
{
// create buffer object
glGenBuffers( 1, vbo);
glBindBuffer( GL_ARRAY_BUFFER, *vbo);
// initialize buffer object
// unsigned int size = mesh_width * mesh_height * 4 * sizeof( float);
glBufferData( GL_ARRAY_BUFFER, sizeof(m_d_position), 0, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(m_d_position), m_d_position); // copy vertices starting from 0 offest
glBindBuffer( GL_ARRAY_BUFFER, 0);
// register buffer object with CUDA
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(*vbo));
if(glIsBuffer(*vbo)){
printf("yes buffer");}
else{
printf("no buffer");
}
CUT_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Delete VBO
////////////////////////////////////////////////////////////////////////////////
void deleteVBO( GLuint* vbo)
{
glBindBuffer( 1, *vbo);
glDeleteBuffers( 1, vbo);
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(*vbo));
*vbo = 0;
}
float along_z=0.0;
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
// run CUDA kernel to generate vertex positions
//runCuda(vbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,20.0,30.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);
//glScalef(scale,scale,scale);
//glTranslatef(0.0, 0.2,0.0 );
along_z+=0.2;
//glRotatef(mouseWindowX*360,0,1,0);
// glRotatef(mouseWindowY*60,0,0,1);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.74,-19.02,6.79);
glEnd();
glColor3f(0.0, 1.0, 0.0);
drawPoints();
/* // render from the vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1.0, 0.0, 0.0);
glDrawArrays(GL_LINES,0,int(M_SIZE/3));
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
*/
glutSwapBuffers();
glutPostRedisplay();
anim += 0.01;
}
////////////////////////////////////////////////////////////////////////////////
//! Keyboard events handler
////////////////////////////////////////////////////////////////////////////////
void keyboard( unsigned char key, int /*x*/, int /*y*/)
{
switch( key) {
case( 27) :
deleteVBO( &vbo);
exit( 0);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Mouse event handlers
////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
float dx, dy;
dx = x - mouse_old_x;
dy = y - mouse_old_y;
if (mouse_buttons & 1) {
rotate_x += dy * 0.2;
rotate_y += dx * 0.2;
} else if (mouse_buttons & 4) {
translate_z += dy * 0.01;
}
mouse_old_x = x;
mouse_old_y = y;
}
////////////////////////////////////////////////////////////////////////////////
//! Check if the result is correct or write data to file for external
//! regression testing
////////////////////////////////////////////////////////////////////////////////
void checkResultCuda( int argc, char** argv, const GLuint& vbo)
{
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(vbo));
// map buffer object
glBindBuffer( GL_ARRAY_BUFFER_ARB, vbo );
float* data = (float*) glMapBuffer( GL_ARRAY_BUFFER, GL_READ_ONLY);
// check result
if( cutCheckCmdLineFlag( argc, (const char**) argv, "regression")) {
// write file for regression test
CUT_SAFE_CALL( cutWriteFilef( "./data/regression.dat",
data, mesh_width * mesh_height * 3, 0.0));
}
// unmap GL buffer object
if( ! glUnmapBuffer( GL_ARRAY_BUFFER)) {
fprintf( stderr, "Unmap buffer failed.\n");
fflush( stderr);
}
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(vbo));
CUT_CHECK_ERROR_GL();
}
please help me out, its driving me crazy. where does the problem lie?