I am writing CUDA C codes and trying to use OpenGL for the visualization of simulation data.
I am using Visual Studio 2010 for development.
I was going through the “CUDA by example” book and tried to reproduce the examples in the book. So in the CUDA OpenGL interoperability section everything did work fine until I began making use of glGenBuffers. Other functions related to buffer objects, like glBindBuffer did not identify during compilation either. I googled a lot about this and there are many suggestions like including “GL/glext.h”, which I all tried.
I would also like to mention that all of the headers (glut.h, glew.h, glxew.h and wglew.h) and lib files (glut.lib, glew32.lib, glew32s.lib) are present in the Visual Studio SDK lib and inc directories. In the VS 2010 Project → properties → linker → input → additional dependencies I added the library files in the following order:
cudart.lib
glut32.lib
glu32.lib
glew32.lib
glew32s.lib
freeglut.lib
opengl32.lib
Here’s my program’s source core for reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <GL/glew.h>
#include <GL/glext.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cutil.h>
#include <cudagl.h>
#include <cuda_gl_interop.h>
#define DIM 512;
GLuint bufferObj;
cudaGraphicsResource *resource;
void display (void)
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glFlush();
}
void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
cudaDeviceProp prop;
int dev;
memset(&prop,0,sizeof(cudaDeviceProp));
prop.major = 1;
prop.minor = 0;
cudaChooseDevice(&dev,&prop);
cudaGLSetGLDevice(dev);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitWindowPosition (100, 100);
glutCreateWindow("Bitmap");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
glGenBuffers(1,&bufferObj);
//glBindBuffer()
}
When compiling, I’m presented with the following errors:
1> C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc\GL/glut.h(574) : see previous definition of 'GLUT_GAME_MODE_DISPLAY_CHANGED'
1>C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc\GL/glew.h(84): fatal error C1189: #error : gl.h included before glew.h
1>
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizatio ns\CUDA 4.2.targets(361,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2008 -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin" -I"C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include" -G --keep-dir "Debug" -maxrregcount=0 --machine 32 --compile -g -Xcompiler "/EHsc /nologo /Od /Zi /MDd " -o "Debug\OPENGL_PRAC_1.cu.obj" "C:\Users\umdutta\Desktop\SANKHA_ALL_MATERIALS\PRO GRAMMING_FOLDER\CUDA_OPENGL_C\2_PRACTICE_PROG_FOLD ER_(OPENGL_PRAC)\PROJ_NUM_1_OPENGL\PROJ_NUM_1_OPEN GL\OPENGL_PRAC_1.cu"" exited with code 2.
1>
1>Build FAILED.
It really helps to read the error messages. Your problem is, that you mindlessly include a bunch of headers. First, you need only
glew.h, and don’t useglext.h. Then it’s also important to include the headers in the right order.This should work:
BTW:
cudagl.his for the old, deprecated GL interaction API. If you usecuda_gl_interop.hyou should not usecudagl.h.