Hi,
I try to compile example like this:
#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>
#include <GL/glut.h>
// includes
#include <cutil.h>
#include <cutil_gl_error.h>
#include <cuda_gl_interop.h>
////////////////////////////////////////////////////////////////////////////////
// constants
const unsigned int window_width = 512;
const unsigned int window_height = 512;
const unsigned int mesh_width = 256;
const unsigned int mesh_height = 256;
// vbo variables
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>
__global__ void kernel(float4* pos, unsigned int width, unsigned int height, float time)
{
unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
// calculate uv coordinates
float u = x / (float) width;
float v = y / (float) height;
u = u*2.0f - 1.0f;
v = v*2.0f - 1.0f;
// calculate simple sine wave pattern
float freq = 4.0f;
float w = sinf(u*freq + time) * cosf(v*freq + time) * 0.5f;
// write output vertex
pos[y*width+x] = make_float4(u, w, v, 1.0f);
}
void launch_kernel(float4* pos, unsigned int mesh_width, unsigned int mesh_height, float time)
{
// execute the kernel
dim3 block(8, 8, 1);
dim3 grid(mesh_width / block.x, mesh_height / block.y, 1);
kernel<<< grid, block>>>(pos, mesh_width, mesh_height, time);
}
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
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);
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
runTest( argc, argv);
CUT_EXIT(argc, argv);
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest( int argc, char** argv)
{
//CUT_DEVICE_INIT();
// Create GL context
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( window_width, window_height);
glutCreateWindow( "Cuda GL interop");
// initialize GL
if( CUTFalse == initGL()) {
return;
}
// register callbacks
glutDisplayFunc( display);
glutKeyboardFunc( keyboard);
glutMouseFunc( mouse);
glutMotionFunc( motion);
// 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);
// viewport
glViewport( 0, 0, window_width, window_height);
// projection
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0);
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, size, 0, GL_DYNAMIC_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, 0);
// register buffer object with CUDA
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(*vbo));
CUT_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Delete VBO
////////////////////////////////////////////////////////////////////////////////
void deleteVBO( GLuint* vbo)
{
glBindBuffer( 1, *vbo);
glDeleteBuffers( 1, vbo);
CUDA_SAFE_CALL(cudaGLUnregisterBufferObject(*vbo));
*vbo = 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();
glTranslatef(0.0, 0.0, translate_z);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
// 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_POINTS, 0, mesh_width * mesh_height);
glDisableClientState(GL_VERTEX_ARRAY);
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();
}
but thore are erross, plenty of them
Error 1 error LNK2019: unresolved external symbol glutMainLoop referenced in function “void __cdecl runTest(int,char * *)” (?runTest@@YAXHPEAPEAD@Z) sample.cu.obj
…
Error 1 error LNK2019: unresolved external symbol glutMotionFunc referenced in function “void __cdecl runTest(int,char * *)” (?runTest@@YAXHPEAPEAD@Z) sample.cu.obj
…
this happens every time when I try with OpenGL/Cuda sample. Just CUDA sample runs ok.
any help pelase
kind regards
milan