OpenGl texturing ........ ppm background - c++

i am using a ppm loader to set image as a background , but there is a problem
in colors here is the code and the image that i am use .
http://imgur.com/w732d6j
http://imgur.com/mJr26Ik
here is the code .....
texture.h
#ifndef TEXTURE_H
#define TEXTURE_H
struct Image
{
unsigned char* pixels;
int width;
int height;
int numChannels;
};
class Texture
{
public:
Texture ();
void Prepare (int texN);
void ReadPPMImage (char *fn);
GLuint texName;
Image image;
};
#endif
texture.cpp
#include <fstream>
#include <glut.h>
#pragma warning (disable : 4996)
#include "Texture.h"
Texture::Texture ()
{
}
void Texture::Prepare (int texN)
{
texName = texN;
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture (GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width,
image.height, 0, GL_RGB, GL_UNSIGNED_BYTE,
image.pixels);
}
void Texture::ReadPPMImage (char* fn)
{
int tmpint;
char str[100];
FILE* inFile = fopen (fn,"rb");
if (inFile == NULL)
{
printf ("Can't open input file %s. Exiting.\n",fn);
exit (1);
}
fscanf (inFile,"P%d\n", &tmpint);
if (tmpint != 6)
{
printf ("Input file is not ppm. Exiting.\n");
exit (1);
}
// skip comments embedded in header
fgets (str,100,inFile);
while (str[0]=='#')
fgets(str,100,inFile);
// read image dimensions
sscanf (str,"%d %d",&image.width, &image.height);
fgets (str,100,inFile);
sscanf (str,"%d",&tmpint);
if (tmpint != 255)
printf("Warning: maxvalue is not 255 in ppm file\n");
image.numChannels = 3;
image.pixels = (unsigned char*) malloc (image.numChannels * image.width * image.height * sizeof (unsigned char));
if (image.pixels == NULL)
{
printf ("Can't allocate image of size %dx%d. Exiting\n", image.width, image.height);
exit (1);
}
else
printf("Reading image %s of size %dx%d\n", fn, image.width, image.height);
fread (image.pixels, sizeof (unsigned char), image.numChannels * image.width * image.height, inFile);
fclose (inFile);
}
Main.cpp
#include <glut.h>
#include "Texture.h"
#pragma warning (disable : 4996)
const float fMinX = -5.0, fMinY = -5.0, fNearZ = 1.0,
fMaxX = 5.0 , fMaxY = 5.0 , fFarZ = 10.0;
Texture ImageOne ;
void Init ()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable (GL_DEPTH_TEST);
glGenTextures (1, &ImageOne.texName);
ImageOne.ReadPPMImage("wood_1.ppm");
ImageOne.Prepare(1) ;
}
void Reshape (int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (fMinX, fMaxX, fMinY, fMaxY, fNearZ, fFarZ);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void Display ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_TEXTURE_2D);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBindTexture (GL_TEXTURE_2D, ImageOne.texName);
glBegin(GL_QUADS);
glTexCoord2f(0,1);
glVertex3f(-5.5,5,-6);
glTexCoord2f(0,0);
glVertex3f(-5.5,-5,-6);
glTexCoord2f(1,0);
glVertex3f(5,-5,-6);
glTexCoord2f(1,1);
glVertex3f(5,5,-6);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers ();
glFlush ();
}
void main (int argc, char **argv)
{
// init GLUT and create window
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow ("OpenGL - Rotating Cubes");
Init ();
// register callbacks
glutDisplayFunc (Display);
glutReshapeFunc (Reshape);
glutIdleFunc (Display); // used in animation
// enter GLUT event processing cycle
glutMainLoop();
}

Why are you using
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
?
It does not make sense for your use case (and perfectly explays the "inversion" of the color values). You probably want GL_REPLACE or GL_MODULATE.

Related

Opengl texture cylinder trouble

I'm a little bit confused.
I want to texture a "cylinder", so my first approach was this (with n the number of faces and k the number of iteration)
void cylindrer(double r, int n,double h){
double x[n],y[n];
for(int k=0; k<n; k++){
x[k]=r*cos(2*k*M_PI/n);
y[k]=r*sin(2*k*M_PI/n);
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texObject[1]);
for(int k=0; k<n ;k++){
int m= (k+1)%n;
glBegin(GL_POLYGON);
glTexCoord2f(1.0/n*(k),0.0); glVertex3f( x[k], y[k], h/2);
glTexCoord2f(1.0/n*(k),1); glVertex3f( x[k], y[k], -h/2);
glTexCoord2f(1.0/n*(k+1),1); glVertex3f( x[m], y[m], -h/2);
glTexCoord2f(1.0/n*(k+1),0.0); glVertex3f( x[m], y[m], h/2);
glEnd();
}
glDisable(GL_TEXTURE_2D);
}
The texture is applied but reverse so I'd changed to
glBegin(GL_POLYGON);
glTexCoord2f(1.0/n*(n-k), 0.0); glVertex3f( x[k], y[k], h/2);
glTexCoord2f(1.0/n*(n-k), 1.0); glVertex3f( x[k], y[k], -h/2);
glTexCoord2f(1.0/n*(n-k-1), 1.0); glVertex3f( x[m], y[m], -h/2);
glTexCoord2f(1.0/n*(n-k-1), 0.0); glVertex3f( x[m], y[m], h/2);
glEnd();
And it works and look like this :
when I use this texture :
But now I want to rotate the texture of 90 degrees, so I create a new jpeg file and rotate it.
So the texture now look like this :
But this is the result when I use it :
The texture is twisted around the cylinder and I don't understand why.
Any idea ?
How I load texture :
#include <math.h>
#include <jpeglib.h>
#include <jerror.h>
GLuint texObject[2]; // textures
int main(int argc,char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(200,200);
glutInitWindowSize(1366,768);
glutCreateWindow("");
glClearColor(0.0,0.0,0.0,0.0);
//glColor3f(1.0,1.0,1.0);
glShadeModel(GL_FLAT);
glPointSize(2.0);
glEnable(GL_DEPTH_TEST);
glGenTextures(2, texObject);
loadJpegImage("./textureCorps5.jpg", 1);
glutMainLoop();
return 0;
}
void loadJpegImage(char *fichier, int i)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *file;
unsigned char *ligne;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
#ifdef __WIN32
if (fopen_s(&file,fichier,"rb") != 0)
{
fprintf(stderr,"Error\n");
exit(1);
}
#elif __GNUC__
if ((file = fopen(fichier,"rb")) == 0)
{
fprintf(stderr,"Error\n");
exit(1);
}
#endif
jpeg_stdio_src(&cinfo, file);
jpeg_read_header(&cinfo, TRUE);
unsigned char image[cinfo.image_width*cinfo.image_height*3];
jpeg_start_decompress(&cinfo);
ligne=image;
while (cinfo.output_scanline<cinfo.output_height)
{
ligne=image+3*cinfo.image_width*cinfo.output_scanline;
jpeg_read_scanlines(&cinfo,&ligne,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
glBindTexture(GL_TEXTURE_2D, texObject[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cinfo.image_width, cinfo.image_height, 0,
GL_RGB, GL_UNSIGNED_BYTE, image);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
GL_UNPACK_ALIGNMENTspecifies the alignment requirements for the start of each pixel row in memory. By default GL_UNPACK_ALIGNMENT is set to 4.
This means each row of the texture is supposed to have a lenght of 4*N bytes.
Your texture is an RGB texture, which needs 24 bits or 3 bytes for each texel and you tightly packed the texels and especially the lines of the texture. This means that you may disregard the alignment of 4 for the start of a line of the texture (Except 3 times the width of the texture is divisible by 4 without a remaining).
To deal with that you have to change the alignment to 1. This means you have to set glPixelStorei(GL_UNPACK_ALIGNMENT, 1); before glTexImage2D, for reading a tightly packed texture.
Otherwise you will get an offset per line of 0-3 bytes when reading the texture. This causes a continuously twisted texture.
Instead you can take care of the alignment and the aligned length of a line when you create the turned texture, too:
int bytesPerLine = cinfo.image_width * 3;
bytesPerLine += bytesPerLine % 4;
unsigned char image[bytesPerLine * cinfo.image_height];
jpeg_start_decompress(&cinfo);
while (cinfo.output_scanline<cinfo.output_height)
{
ligne = image + bytesPerLine*cinfo.output_scanline;
jpeg_read_scanlines(&cinfo,&ligne,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

Load jpg image as texture - freeimage, opengl

I tried to load jpg image with FreeImage Library. I used this code, but the result was only white window. I think to use this image like background and after that to load object file.
It`s the code, that i used:
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include <FreeImage.h>
FIBITMAP *loadImage(const char *filename)
{
FIBITMAP *dib1 = NULL;
FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilename(filename);
dib1 = FreeImage_Load(fif, filename, JPEG_DEFAULT);
if (!dib1)
{
std::cerr << "Erreur ouverture d\'image" << std::endl;
exit (0);
}
std::cerr << "Success" << std::endl;
return dib1;
}
GLuint loadTexture (FIBITMAP * dib1)
{
GLuint tex_id = 0;
int x, y;
int height, width;
RGBQUAD rgbquad;
FREE_IMAGE_TYPE type;
BITMAPINFOHEADER *header;
type = FreeImage_GetImageType(dib1);
height = FreeImage_GetHeight(dib1);
width = FreeImage_GetWidth(dib1);
header = FreeImage_GetInfoHeader(dib1);
int scanLineWidh = ((3*width)%4 == 0) ? 3*width : ((3*width)/4)*4+4;
unsigned char * texels= (GLubyte*)calloc(height*scanLineWidh, sizeof(GLubyte));
for (x=0 ; x<width ; x++)
for (y=0 ; y<height; y++)
{
FreeImage_GetPixelColor(dib1,x,y,&rgbquad);
texels[(y*scanLineWidh+3*x)]=((GLubyte*)&rgbquad)[2];
texels[(y*scanLineWidh+3*x)+1]=((GLubyte*)&rgbquad)[1];
texels[(y*scanLineWidh+3*x)+2]=((GLubyte*)&rgbquad)[0];
}
glGenTextures (1, &tex_id);
glBindTexture (GL_TEXTURE_2D, tex_id);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB,
width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, texels);
free(texels);
return tex_id;
}
void display(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers(); //swap the buffers
}
int main(int argc, char **argv) {
FIBITMAP *dib1 = loadImage("planina.jpg");
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(800,450);
glutInitWindowPosition(20,20);
glutCreateWindow("Loader");
//glutReshapeFunc(reshape);
//glutDisplayFunc(display);
loadTexture(dib1);
FreeImage_Unload(dib1);
glutMainLoop();
return 0;
}
What I am doing wrong?
This is your display function:
void display(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers(); //swap the buffers
}
And what's immediately apparent is, that the only thing it does is
setting a clear color
clear the window
load an identity matrix
displays the result
What's lacking is any kind of actually drawing something. You have to draw some triangles or quads with the texture applied for the texture to actually show up somehow.

Display a pgm image using openGl

So i am writing a program using openGl on C++.I want to load an image and then display it in a NxN grid form.
I loaded the image and stored its data in an array and then proceeded to use the following method to achieve my goal:
void fillGrid(){
ifstream myfile("paper.pgm");
ofstream otherfile;
otherfile.open("test.txt");
string line;
string buffer;
fstream afile;
if (myfile.is_open())
{
int counter=0;
while (getline(myfile, line))
{
if(counter>2){
buffer=buffer+line;
}
counter++;
}
pixels=new float[1600];
int i=0;
string delimiters = " ,";
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = buffer.find_first_of( delimiters, current );
if(i<=1600){
pixels[i]=myAtof (buffer.substr(current));
paper[i]=pixels[i];
}
i++;
}
while (next != string::npos);
for(int j=0;j<=1600;j++){
otherfile<<paper[j]<<" "<< j<<endl;
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glBindTexture(GL_TEXTURE_2D, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 140, 140, 0, GL_RGB, GL_FLOAT, paper);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
}
}
This function opens the file containing the image data, loads the image in the pixels array at first and then at the paper array that i use in glTexImage2D.MyAtof() is a function i built to convert a string into a float. The data is properly passed from the file to the array, i've tested it.
After ther fillGrid function the following function is called t do the repaint:
void drawGrid2(void)
{
for(int i=-240;i<240;i+=40){
for(int j=-300;j<=300;j+=40){
drawSquare2(i,j);
}
}
}
And also:
void drawSquare2(int x,int y)
{
glBindTexture(GL_TEXTURE_2D, 1);
glBegin(GL_QUADS); //Start drawing quad
glVertex2f(x,y); //first coordinate x y
glVertex2f(x+40,y); //second coordinate
glVertex2f(x+40,y+40); //third coordinate
glVertex2f(x,y+40); //last coordinate
glEnd(); //Stop drawing quads
glFlush ();
}
Main:
int main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (0, 0); // Set top-left display-window position.
glutInitWindowSize (600, 500); // Set display-window width and height.
glutCreateWindow ("Main"); // Create display window.
init (); // Execute initialization procedure.
glutDisplayFunc (display); // Send graphics to display window.
glutKeyboardFunc(processEscKey);
glutMainLoop (); // Display everything and wait.
return 0;
}
Other functions:
void init (void)
{
// glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glClearColor (0.0, 0.0, 0.0, 1.0);//black
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D(-300,300,-300,300);//0,width,0,height
}
void processEscKey(unsigned char key, int x, int y)
{
if(key==27){
exit(0);
}
else if(key==98){
fillGrid();
drawGrid2();
}
}
Is this the correct way to create a texture and display an image with openGl?
The file compiles but the result isn't what i want.
I wanted an 15x15 grid of squares, with each square containing the image.
Before the repaint the result was this.
After the repaint the result is this.
I used different functions for the first repaint and the the second one.
Since the first one is working i didnt post it.
OpenGL's default texture minification filter is GL_NEAREST_MIPMAP_LINEAR. Your texture is not mipmap complete, so texturing will not work in this mode. You should set glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); (or GL_LINEAR).
You also seem to try to set the texture maginification filter here:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
butt GL_REPEAT is not a valid filter mode at all.

Using CUDA, SFML, and OpenGL: Texture Refuses to Appear on Quad

Using various tutorials/examples/documentations/forums online, I have typed out code to allow CUDA to manipulate OpenGL textures such that it can be outputted to the screen. My method of displaying is to use PBO and an allocated texture image of uchar4 array. Despite all my attempts at fixing the problem, the texture would not show up on the 2D surface. I cannot seem to pinpoint the problem.
These are all the things I have checked/done thus far: I have created a PBO and registered it with CUDA, called cudaGraphicsResourceGetMappedPointer and the unmapping equivalent before and after the GPU function calls, made sure that glEnable is called for 2D_TEXTURE, glDisable called for any unnecessary values, unbinded textures/buffers when not in need. I have also reset SFML OpenGL states in case SFML was the cause. Square textures have also been employed. My OpenGL verision and CUDA version work for all function calls I use.
There did not seem to be any errors within the program when I checked cudaErrors and OpenGL Errors.
I am not sure if this has something to do with it but when I call:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
My quad does not seem to display.
I have mainly found inspiration from this website.
Thank you very much!
Here is my code:
Main.cpp
#include <GL/glew.h>
#include <windows.h>
#include <GL/GL.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "GeneralTypedef.h"
#include "OpenGLTest.cuh"
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1024, 1024), "OpenGL");
//window.setVerticalSyncEnabled(true);
sf::Vector2u windowSize;
windowSize = sf::Vector2u(window.getSize());
bool running = true;
glewInit();
window.resetGLStates();
std::printf("OpenGL: %s:", glGetString(GL_VERSION));
// We will not be using SFML's gl states.
OpenGLTest* test = new OpenGLTest(window.getSize());
sf::Time time;
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
// adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
windowSize = window.getSize();
}
}
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
test->createFrame(time.asMicroseconds());
test->drawFrame();
window.display();
}
// release resources...
delete test;
return 0;
}
OpenGLTest.cuh
#ifndef OPENGLTEST_CUH
#define OPENGLTEST_CUH
#include <GL/glew.h>
#include <windows.h>
#include <GL/GL.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "GeneralTypedef.h"
class OpenGLTest
{
public:
uchar4* image;
GLuint gltexture;
GLuint pbo;
cudaGraphicsResource_t cudaPBO;
uchar4* d_textureBufferData;
sf::Vector2u windowSize;
OpenGLTest(sf::Vector2u windowSize)
{
this->windowSize = sf::Vector2u(windowSize);
this->setupOpenGL();
};
~OpenGLTest()
{
delete image;
image == nullptr;
cudaFree(d_textureBufferData);
d_textureBufferData == nullptr;
glDeleteTextures(1, &gltexture);
}
void drawFrame();
void createFrame(float time);
private:
void setupOpenGL();
};
#endif //OPENGLTEST_CUH
OpenGLTest.cu
#include "OpenGLTest.cuh"
__global__ void createGPUTexture(uchar4* d_texture)
{
uint pixelID = blockIdx.x*blockDim.x + threadIdx.x;
d_texture[pixelID].x = 0;
d_texture[pixelID].y = 1;
d_texture[pixelID].z = 1;
d_texture[pixelID].w = 0;
}
__global__ void wow(uchar4* pos, unsigned int width, unsigned int height,
float time)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int x = index%width;
unsigned int y = index / width;
if (index < width*height) {
unsigned char r = (x + (int)time) & 0xff;
unsigned char g = (y + (int)time) & 0xff;
unsigned char b = ((x + y) + (int)time) & 0xff;
// Each thread writes one pixel location in the texture (textel)
pos[index].w = 0;
pos[index].x = r;
pos[index].y = g;
pos[index].z = b;
}
}
void OpenGLTest::drawFrame()
{
glColor3f(1.0f,1.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, gltexture);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, windowSize.x, windowSize.y, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, float(windowSize.y));
glTexCoord2f(1.0f, 0.0f);
glVertex2f(float(windowSize.x), float(windowSize.y));
glTexCoord2f(1.0f, 1.0f);
glVertex2f(float(windowSize.x), 0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex2f(0.0f, 0.0f);
glEnd();
glFlush();
// Release
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// Test Triangle
/*
glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex2f(0, 0);
glVertex2f(10, 0);
glVertex2f(0, 100);
glEnd();
*/
}
void OpenGLTest::createFrame(float time)
{
cudaGraphicsMapResources(1, &cudaPBO, 0);
size_t numBytes;
cudaGraphicsResourceGetMappedPointer((void**)&d_textureBufferData, &numBytes, cudaPBO);
int totalThreads = windowSize.x * windowSize.y;
int nBlocks = totalThreads/ 256;
// Run code here.
createGPUTexture << <nBlocks, 256>> >(d_textureBufferData);
//wow << <nBlocks, 256 >> >(d_textureBufferData, windowSize.x, windowSize.y, time);
// Unmap mapping to PBO so that OpenGL can access.
cudaGraphicsUnmapResources(1, &cudaPBO, 0);
}
void OpenGLTest::setupOpenGL()
{
image = new uchar4[1024*1024];
glViewport(0, 0, windowSize.x, windowSize.y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, windowSize.x, windowSize.y, 0.0, -1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
// Unbind any textures from previous.
glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
// Create new textures.
glGenTextures(1, &gltexture);
glBindTexture(GL_TEXTURE_2D, gltexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Create image with same resolution as window.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowSize.x , windowSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
// Create pixel buffer boject.
glGenBuffers(1, &pbo);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, windowSize.x * windowSize.y * sizeof(uchar4), image, GL_STREAM_COPY);
cudaGraphicsGLRegisterBuffer(&cudaPBO, pbo, cudaGraphicsMapFlagsNone);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
GeneralType
#ifndef GENERALTYPEDEF_CUH
#define GENERALTYPEDEF_CUH
typedef unsigned int uint;
#endif // GENERALTYPEDEF_CUH
After rewriting the entire code and understanding it more, I have figured out the reason. The color components for the uchar4 in the kernel function is mapped from 0-255. The w component is transparency. As such, it should be mapped to 255 for the image to show. I hope this helps for those who may have the same problem. Some sites have this value set very low as well.

Crash with CUDA/OGL interop

I am trying to setup a little CUDA/GL interop example. I have looked around in the internet, so I found some tutorials with some helpful stuff.
All I want is, is to produce a texture in CUDA and draw it with OpenGL.
The source I have now is crashing my Macbook Pro every time I run it, so I thought that if somebody could take an eye on it, that would be really helpful.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
// OpenGL Graphics includes
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif
// includes, cuda
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
// Utilities and timing functions
#include <helper_functions.h> // includes cuda.h and cuda_runtime_api.h
#include <timer.h> // timing functions
// CUDA helper functions
#include <helper_cuda.h> // helper functions for CUDA error check
#include <helper_cuda_gl.h> // helper functions for CUDA/GL interop
#include <vector_types.h>
const unsigned int window_width = 512;
const unsigned int window_height = 512;
GLuint viewGLTexture;
cudaGraphicsResource_t viewCudaResource;
void initGLandCUDA() {
int argc = 0;
char** argv = NULL;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("CUDA GL Interop");
glewInit();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &viewGLTexture);
glBindTexture(GL_TEXTURE_2D, viewGLTexture);
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
glBindTexture(GL_TEXTURE_2D, 0);
cudaGLSetGLDevice(gpuGetMaxGflopsDeviceId());
cudaGraphicsGLRegisterImage(&viewCudaResource, viewGLTexture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard);
}
__global__ void renderingKernel(cudaSurfaceObject_t image) {
unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
uchar4 color = make_uchar4(0.f, 0.f, 0.f, .3f);
//if I write in 0, 0 and not x,y, the computer is not crashing, but there is no black pixel at 0,0
surf2Dwrite(color, image, x, y, cudaBoundaryModeClamp);
}
void callCUDAKernel(cudaSurfaceObject_t image) {
dim3 block(8, 1, 1);
dim3 grid(8, 1, 1);
renderingKernel<<< grid, block>>>(image);
}
void renderFrame() {
cudaGraphicsMapResources(1, &viewCudaResource);
{
cudaArray_t viewCudaArray;
checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(&viewCudaArray, viewCudaResource, 0, 0));
cudaResourceDesc viewCudaArrayResourceDesc;
{
viewCudaArrayResourceDesc.resType = cudaResourceTypeArray;
viewCudaArrayResourceDesc.res.array.array = viewCudaArray;
}
cudaSurfaceObject_t viewCudaSurfaceObject;
checkCudaErrors(cudaCreateSurfaceObject(&viewCudaSurfaceObject, &viewCudaArrayResourceDesc));
callCUDAKernel(viewCudaSurfaceObject);
checkCudaErrors(cudaDestroySurfaceObject(viewCudaSurfaceObject));
}
checkCudaErrors(cudaGraphicsUnmapResources(1, &viewCudaResource));
checkCudaErrors(cudaStreamSynchronize(0));
glBindTexture(GL_TEXTURE_2D, viewGLTexture);
{
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(+1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(+1.0f, +1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, +1.0f);
}
glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
glFinish();
}
int main(int argc, char **argv)
{
initGLandCUDA();
glutDisplayFunc(renderFrame);
//glutKeyboardFunc(keyboard);
//glutMouseFunc(mouse);
glutMainLoop();
}
It seems like some kind of out-of-range error, but I am currently out of ideas (btw, this is cc 3.0, running to nVidia 650M).
Edit :
By crashing I mean : crashing. Computer freezes. I can't move my mouse and I have to reboot.
Yes, I have looked in all examples, they are not exactly what I want. Changing them to be want I want results in this problem. If there was any other help in the manual or anywhere else that would help me and I have found I would not bother asking for help. You need to link with cuda_runtime and glut libs
Below is a working version of your code. The issues in your code were:
Your kernel was depending on being launched with 512x512 threads, but you were only launching with 64x1 threads.
Your kernel was writing to unaligned addresses with surf2Dwrite().
You were setting up double buffering in OpenGL but you were not swapping the buffers. (glutSwapBuffers()).
You were initializing an uchar4 with floats.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
// OpenGL Graphics includes
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <vector_types.h>
const unsigned int window_width = 512;
const unsigned int window_height = 512;
GLuint viewGLTexture;
cudaGraphicsResource_t viewCudaResource;
#define check(ans) { _check((ans), __FILE__, __LINE__); }
inline void _check(cudaError_t code, char *file, int line)
{
if (code != cudaSuccess) {
fprintf(stderr,"CUDA Error: %s %s %d\n", cudaGetErrorString(code), file, line);
exit(code);
}
}
void initGLandCUDA() {
int argc = 0;
char** argv = NULL;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("CUDA GL Interop");
glewInit();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &viewGLTexture);
glBindTexture(GL_TEXTURE_2D, viewGLTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, window_width, window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
check(cudaGLSetGLDevice(0));
check(cudaGraphicsGLRegisterImage(&viewCudaResource, viewGLTexture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard));
}
__global__ void renderingKernel(cudaSurfaceObject_t image) {
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
uchar4 color = make_uchar4(x / 2, y / 2, 0, 127);
surf2Dwrite(color, image, x * sizeof(color), y, cudaBoundaryModeClamp);
}
void callCUDAKernel(cudaSurfaceObject_t image) {
dim3 block(256, 1, 1);
dim3 grid(2, 512, 1);
renderingKernel<<<grid, block>>>(image);
check(cudaPeekAtLastError());
check(cudaDeviceSynchronize());
}
void renderFrame() {
check(cudaGraphicsMapResources(1, &viewCudaResource));
cudaArray_t viewCudaArray;
check(cudaGraphicsSubResourceGetMappedArray(&viewCudaArray, viewCudaResource, 0, 0));
cudaResourceDesc viewCudaArrayResourceDesc;
memset(&viewCudaArrayResourceDesc, 0, sizeof(viewCudaArrayResourceDesc));
viewCudaArrayResourceDesc.resType = cudaResourceTypeArray;
viewCudaArrayResourceDesc.res.array.array = viewCudaArray;
cudaSurfaceObject_t viewCudaSurfaceObject;
check(cudaCreateSurfaceObject(&viewCudaSurfaceObject, &viewCudaArrayResourceDesc));
callCUDAKernel(viewCudaSurfaceObject);
check(cudaDestroySurfaceObject(viewCudaSurfaceObject));
check(cudaGraphicsUnmapResources(1, &viewCudaResource));
check(cudaStreamSynchronize(0));
glBindTexture(GL_TEXTURE_2D, viewGLTexture);
{
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(+1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(+1.0f, +1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, +1.0f);
}
glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
glFinish();
}
int main(int argc, char **argv)
{
initGLandCUDA();
glutDisplayFunc(renderFrame);
//glutKeyboardFunc(keyboard);
//glutMouseFunc(mouse);
glutMainLoop();
}
Output: