OpenGL window not responding when performing computation on GPU - c++

It is a CUDA Code which performs ray tracing. The OpenGL window is used to display the output of the ray tracing performed. Since, the RayTrace is quite slow, I am not particularly concerned about OpenGL performace etc.
But when the ray-tracing (startRayTrace()) is called, the OpenGL window just goes to "Not Responding" State, and displays the output once the ray tracing is finished.
I can't figure out how to prevent it going into the not-respond state. When it goes in not respond state, I cannot minimize the window etc. but the rendered image is still shown as it is.
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glLoadIdentity();
float image[768][1024][3] = { 0 };
for (int i = 0; i < 768; ++i) {
for (int j = 0; j < 1024; ++j) {
int idx = (767 - i) * 1024 + j;
image[i][j][0] = host_c[idx].x;
image[i][j][1] = host_c[idx].y;
image[i][j][2] = host_c[idx].z;
}
}
glRasterPos2i(-1, -1);
glDrawPixels(1024, 768, GL_RGB, GL_FLOAT, image);
glEnd();
glutSwapBuffers();
}
void winResize(int w, int h) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport( (w>1024)?((w-1024)/2):(0), (h>768)?((h-768)/2):(0), w, h);
glMatrixMode(GL_MODELVIEW);
}
void startRayTrace() {
cudaMemcpyToSymbol(cam_offset, &cam_offset_global, sizeof(double), 0, cudaMemcpyHostToDevice);
init <<< 1, 1 >>> ();
cudaDeviceSynchronize();
char title[35];
//rayTrace <<<48, 16 >>> ();
//cudaDeviceSynchronize();
for (int i = 0; i < 24; ++i) {
rayTrace <<< 1, 32 >>> (); //Overcome Watchdog timer on Windows without disabling TDR
cudaDeviceSynchronize();
sprintf(title, "Ray Tracing | Rendering %.2f%%...", ((i + 1) / 24.f) * 100);
glutSetWindowTitle(title);
}
copyToHost <<< 1, 1 >>> (dev_c);
cudaMemcpy(host_c, dev_c, WIDTH * HEIGHT * sizeof(vector), cudaMemcpyDeviceToHost);
}
void keyPress(unsigned char key, int x, int y) {
if (key == 'd') {
cam_offset_global += 10;
}
if (key == 'a') {
cam_offset_global -= 10;
}
}
void keyUp(unsigned char key, int x, int y) {
if (key == 'd' || key == 'a') {
startRayTrace();
}
//cudaDeviceSynchronize();
glutPostRedisplay();
}
int main(int argc, char * argv[]) {
cudaMalloc(&dev_c, WIDTH * HEIGHT * sizeof(vector));
//OpenGL Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(1024, 768);
glutCreateWindow("Ray Tracing | Rendering 0%...");
//Ray Tracing
startRayTrace();
cudaDeviceSynchronize();
const GLubyte* ren = glGetString(GL_RENDERER);
printf("\n\n\n OpenGL Renderer : %s \n\n", ren);
//Register Callbacks
glutDisplayFunc(display);
glutReshapeFunc(winResize);
glutKeyboardFunc(keyPress);
glutKeyboardUpFunc(keyUp);
glutMainLoop();
delete[] host_c;
cudaFree(dev_c);
return 0;
}
Once the ray-trace is done, the host_c[] stores the image data, which I use to display the output on glWindow. The glutPostRedisplay() is supposed to re-render the output once ray trace finishes and host_c[] is updated, but the glWindow hangs until the ray-tracing finishes.

GLUT is not going to read further events, while it's being right inside an event hand.er You're calling startRayTrace right from the keyboard callback. However startRayTrace doesn't just start the raytrace, it also waits for completion. And because of that, GLUT will be stuck until the raytrace finishes.
CUDA kernels are executed asynchronously. To get note of when the kernel finishes, add a cudaEvent right after the kernel in the CUDA stream. Then register a GLUT idle callback function. In that function, poll if the event did complete, and when the event did complete, issue a glutPostRedisplay.
Do not cudaSync… inside the keyboard function, because that will stall.

Related

glutPostRedisplay() causing extreme lag when drawing in GLUT

I am using GLUT to draw a sequence of lines and my glutPassiveMotionFunc(passiveMotion); (no button press mouse motion) draws a line from the end of the line sequence to wherever the cursor is. This project is intended to be a bezier curve tool so I also render tangents with glutMotionFunc(motion); (button hold mouse motion). At this stage there are drawLine() calls at every place the cursor is in the window. I call glutPostRedisplay();to refresh the screen at the end of each movement to get rid of the history of drawLine() calls.
The introduction of glutPostRedisplay(); makes the program lag more and more each time my display function glutDisplayFunc(myDisplay); renders a line segment.
void myMouse(int button, int state, int x, int y)
{
// If left button was clicked
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
Point point;
point.setxy(x, SCREEN_HEIGHT - y);
// Draw the red dot.
glColor3f(255, 0, 0);
drawDot(point);
pointsVector[0] = point;
points++;
pointsVector[points].setxy((float)x, (float)(SCREEN_HEIGHT - y));
glColor3f(0, 0, 0);
drawLine(pointsVector[points - 1], pointsVector[points]);
}
else if (state == GLUT_UP)
{
Handle.setxy(x, SCREEN_HEIGHT - y);
controlPoints[0] = pointsVector[0];
controlPoints[points].setxy(Handle.x, Handle.y);
glColor3f(0, 255, 0);
drawDot(Handle);
reverseControlPoints[0] = pointsVector[0];
ReverseHandle.x = (2 * pointsVector[points].x) - Handle.x;
ReverseHandle.y = (2 * pointsVector[points].y) - Handle.y;
reverseControlPoints[points].setxy(ReverseHandle.x, ReverseHandle.y);
glColor3f(0, 255, 0);
drawDot(ReverseHandle);
glColor3f(0, 0, 255);
drawLine(Handle, ReverseHandle);
}
}
}
void passiveMotion(int x, int y)
{
Point Cursor;
Cursor.setxy(x, SCREEN_HEIGHT - y);
if ((pointsVector[0].x != NULL) && (pointsVector[0].y != NULL))
{
drawLine(pointsVector[points], Cursor);
glutPostRedisplay();
}
}
void motion(int x, int y)
{
Point Cursor;
Cursor.setxy(x, SCREEN_HEIGHT - y);
//drawLine(pointsVector[points], Cursor);
Point reverseCursor;
reverseCursor.x = (2 * pointsVector[points].x) - Cursor.x;
reverseCursor.y = (2 * pointsVector[points].y) - Cursor.y;
drawLine(pointsVector[points], reverseCursor);
glutPostRedisplay();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 1; i < points; i++)
{
// draw main line & dot
glColor3f(255, 0, 0);
drawDot(pointsVector[i]);
glColor3f(0, 0, 0);
drawLine(pointsVector[i], pointsVector[i + 1]);
// draw tangent lines and tangent dots
glColor3f(0, 255, 0);
drawLine(controlPoints[i + 1], reverseControlPoints[i + 1]);
glColor3f(0, 0, 255);
drawDot(controlPoints[i + 1]);
drawDot(reverseControlPoints[i + 1]);
}
glFlush();
}
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 500);
glutInitWindowPosition(100, 150);
glutCreateWindow("Bezier Curve");
glutDisplayFunc(myDisplay);
glutMouseFunc(myMouse);
glutPassiveMotionFunc(passiveMotion);
glutMotionFunc(motion);
myInit();
glutMainLoop();
return 0;
}
Hard to say for sure, however this snippet inside your display function looks suspicious.
controlPoints.push_back(Handle);
reverseControlPoints.push_back(ReverseHandle);
I don't know what Handle is exactly, however it seems like you're inserting new copies of it into your vectors with every frame, and I don't see you ever clearing them out. Also in myMouse() you're incrementing points which you then use to iterate through that ever-expanding vector. From my memory of how GLUT works, unless you explicitly set it to not repeat button presses, myMouse() won't fire just once when you click a mouse button, it will instead keep triggering the callback while you have it held down. Which means more increments to points, more eventual iterations through the vectors, and slower and slower code.

Movement of '2D' camera in OpenGL/C++

I've checked every other related question on this site but none of the solutions have worked for me. I'm simply trying to follow my rectangle, which moves left and right with key presses in OpenGL. Here's my very simple program:
/*Begin useful backend functions/vars*/
/*************************************/
//Window size and refresh rate (60 fps)
int width = 500;
int height = 500;
int interval = 1000 / 60;
//Used to draw rectangles
void drawRect(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
/***********************************/
/*End useful backend functions/vars*/
/*Game vars*/
/***********/
//keycodes
#define keyA 0x41
#define keyD 0x44
//player
int playerWidth = 30;
int playerHeight = 50;
int playerSpeed = 3;
//player starting position
float playerX = width / 2;
float playerY = 25.0f;
/***************/
/*End game vars*/
/*Game specific functions*/
/*************************/
void keyboard() {
//Move player (and camera) on key presses
if (GetAsyncKeyState(keyA)) {
playerX -= playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
}
if (GetAsyncKeyState(keyD)) {
playerX += playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerSpeed, 0, 0);
}
}
/********************/
/*End game functions*/
/*Draw and update for window*/
/****************************/
void draw() {
//Initialliy clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Draw player
drawRect(playerX, playerY, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
void update(int value) {
// input handling
keyboard();
// Call update() again in 'interval' milliseconds
glutTimerFunc(interval, update, 0);
// Redisplay frame
glutPostRedisplay();
}
/*****************/
/*End draw/update*/
/*Main function*/
/***************/
int main(int argc, char** argv) {
// initialize opengl (via glut)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("My Game");
// Register callback functions
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
// setup scene to be 2d
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set draw color to white
glColor3f(1.0f, 1.0f, 1.0f);
//start the whole thing
glutMainLoop();
return 0;
}
/*************/
/*End of main*/
The keyboard movement works perfectly, however:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
inside my keyboard() function, does nothing. And if I try it with GL_PROJECTION, it turns my screen black.
First of all note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.
Simplify things.
Add a the keyboard events for key up and down (glutKeyboardFunc / glutKeyboardUpFunc). This functions only modifies the speed of the player. The speed is set when a button is pressed and is set 0, when a button is release:
int playerSpeed = 0;
void keyboardDown(unsigned char key, int x, int y)
{
if (key == 'a')
playerSpeed -= 3;
else if (key == 'd')
playerSpeed = 3;
}
void keyboardUp( unsigned char key, int x, int y )
{
playerSpeed = 0;
}
The timer event just modifies the position of the player:
void update(int value)
{
playerX += playerSpeed;
glutTimerFunc(interval, update, 0);
glutPostRedisplay();
}
In draw the model matrix is set and the rectangle is drawn:
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerX, playerY, 0);
//Draw player
drawRect(0, 0, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
Set the callback functions in main:
int main(int argc, char** argv)
{
// ...
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
glutKeyboardFunc( keyboardDown );
glutKeyboardUpFunc( keyboardUp );
// ...
}

OpenGl glutIdleFunc Animation

My problem
I am still learning opengl and I was working on a game similar to chicken invaders. What I am trying to do is to have a background similar to that of chicken invaders with some stars and galaxy. I did that using a quad with the size of the screen that I added texture to. Up until now all is good but I am now trying to translate this galaxy background downward so that the spaceship and the chicken seem as if they are moving forward through space. My idea is to used glutIdleFunc() function and on every iteration, I should sleep first for 30 milliseconds then update the galaxy background so that every row of pixels is moved downward by 1 row and the last row is copied to the first row. The problem is that this approach works fine for the about 15 seconds then the program stops completely and my computer hangs and freezes to the extent that I need to restart my pc. I think my approach requires ridiculously many operations every time glutIdleFunc() is called. I searched for people who faced similar situations online but I found no similar post.
code:
main function
int main(int argc, char** argr) {
glutInit(&argc, argr);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_WIDTH);
// background is a global variable that contains the bitmap array
int err = load_image("Art/background.bmp", &background);
if(err)
{
fprintf(stderr, "%d\n", err);
exit(1);
}
glutCreateWindow("OpenGL - 2D Template");
glutDisplayFunc(Display);
glutMotionFunc(mo);
glutMouseFunc(mou);
glutIdleFunc(Animate);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
gluOrtho2D(0.0, 1000, 0.0, 600);
glutMainLoop();
}
Animation
void Animate(void)
{
msleep(300);
translate_image(&background, 30);
glutPostRedisplay();
}
void translate_image(image_t* img, const int c)
{
uint32_t copy[img->height][img->width];
for(int i = 0; i < img->height; i++)
{
for(int j = 0; j < img->width; j++)
{
copy[i][j] = img->data[i * img->width + j];
}
}
for(int i = 0; i < img->height; i++)
{
for(int j = 0; j < img->width; j++)
{
int index = (i + c) % img->height;
img->data[index * img->width + j] = copy[i][j];
}
}
}
Display Function
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
texture = glInitTexture(background);
drawImage(texture, 0.0f, 0.0f, background.width, background.height);
glDisable(GL_TEXTURE_2D);
drawSpaceShip();
glFlush();
}
Main question
is There a more efficient way to do the same thing I am trying to do with freezing my computer every time I run it ?
You don't have to copy around the picture contents. It suffices to animate the texture coordinates, which you use to map the picture to the quad.
Also you must call glutInitDisplayMode before glutCreateWindow for it to have an effect. Also you don't want a single buffered framebuffer, but a double buffered one GLUT_DOUBLE instead of GLUT_SINGLE and call glutSwapBuffers instead of glFinish.

opengl code compilation issue

After drawing a shape am not been able to draw any more shapes in my display function. But it is being drawn perfectly in other methods but not in my glutidlefunction(myDispalay). Stuck here for about two days. can anyone help me out from here ?
Here is the code :
include < stdio.h>
include< math.h>
include < GL/glut.h>
define PI 3.1416
float x,y,r,y2,r2;
void drawCircle(float x, float y, float radius)
{
int i;
int triangleAmount = 1000; //# of triangles used to draw circle
float twicePi = 2.0f * PI;
glBegin(GL_TRIANGLE_FAN);
for(i = 0; i <= triangleAmount;i++)
{
glVertex2f( x + (radius * cos(i * twicePi / triangleAmount)),
y + (radius * sin(i * twicePi / triangleAmount)));
}
glEnd();
}
void drawthrower()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
drawCircle(x,y,r);
glBegin(GL_LINES);
glVertex2i(x-12,y-28);
glVertex2i(x-12,y-70);
glVertex2i(x+12,y-28);
glVertex2i(x+12,y-70);
glEnd();
drawCircle(x,y2,r2);
glutSwapBuffers();
}
void myDisplay()
{
while ((x+r)<=1024)
{
x+=8;
drawthrower();
}
while ((x-r)>=0)
{
x-=8;
drawthrower();
}
}
void myInit()
{
glClearColor(1,1,1,1);
x=50,y=693,r=30,x=50,y2=623,r2=12;
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1024.0, 0.0, 768.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1024,768);
glutInitWindowPosition(0, 0);
glutCreateWindow("Graphics_Project");
myInit();
glutReshapeFunc(reshape);
glutIdleFunc(myDisplay);
glutMainLoop();
return 0;
}
When using GLUT drawing code should be isolated to the display callback function. You're currently have the display function registered as idle function. To make the program call the display function continuously call glutPostRedisplay from the idle function (or register glutPostRedisplay as the idle function). The reason why you should limit drawing code to the display callback is, that depending on the OS when the redraw event is sent certain special state may be set.
Also, while not technically wrong, but a stylistic issue is, that the glutSwapBuffers should be moved into the display function, so that when maintaining the program one does not go hunting for details like in which subroutine the buffer swap is carried out.

glDrawPixels/glCopyPixels to get a magnified view producing severely clamped image

Despite an earlier question (asked here), our project is constrained to using glDrawPixels, so we have to do some hackery.
One of the feature requirements is to be able to have a magnified view show up on a clicked region of an image; so, looking at an image, I want to click the mouse, and have a 200% image window show up where the mouse is. As I drag my cursor, the window should follow the cursor.
The context is set up like:
The Big Red Book has code that looks like this:
Gl.glShadeModel(Gl.GL_FLAT);
Gl.glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
Gl.glPixelStorei(Gl.GL_UNPACK_ALIGNMENT, 2);
Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE);
Gl.glDisable(Gl.GL_SCISSOR_TEST);
Gl.glDisable(Gl.GL_ALPHA_TEST);
Gl.glDisable(Gl.GL_STENCIL_TEST);
Gl.glDisable(Gl.GL_DEPTH_TEST);
Gl.glDisable(Gl.GL_BLEND);
Gl.glDisable(Gl.GL_DITHER);
Gl.glDisable(Gl.GL_LOGIC_OP);
Gl.glDisable(Gl.GL_LIGHTING);
Gl.glDisable(Gl.GL_FOG);
Gl.glDisable(Gl.GL_TEXTURE_1D);
Gl.glDisable(Gl.GL_TEXTURE_2D);
Gl.glPixelTransferi(Gl.GL_MAP_COLOR, Gl.GL_TRUE);
Gl.glPixelTransferf(Gl.GL_RED_SCALE, 1.0f);
Gl.glPixelTransferi(Gl.GL_RED_BIAS, 0);
Gl.glPixelTransferf(Gl.GL_GREEN_SCALE, 1.0f);
Gl.glPixelTransferi(Gl.GL_GREEN_BIAS, 0);
Gl.glPixelTransferf(Gl.GL_BLUE_SCALE, 1.0f);
Gl.glPixelTransferi(Gl.GL_BLUE_BIAS, 0);
Gl.glPixelTransferi(Gl.GL_ALPHA_SCALE, 1);
Gl.glPixelTransferi(Gl.GL_ALPHA_BIAS, 0);
And then the call to make the smaller-but-zoomed image looks like
int width = (int)((this.Width * 0.2)/2.0);
Gl.glReadBuffer(Gl.GL_FRONT_AND_BACK);
Gl.glRasterPos2i(0, 0);
Gl.glBitmap(0, 0, 0, 0, mStartX - (width*2), mStartY, null);
Gl.glPixelZoom(2.0f, 2.0f);
Gl.glCopyPixels(mStartX - width, mStartY, width, width, Gl.GL_COLOR);
where mStartY and mStartX are the points where the click happened.
Problem is, the window that shows up is really mangling the lookup tables, and really clamping the image down to essentially a black-and-white binary image (ie, no shades of grey).
The data is a black-and-white unsigned short array, and is set with this code:
float step = (65535.0f / (float)(max - min));
mColorTable = new ushort[65536];
int i;
for (i = 0; i < 65536; i++)
{
if (i < min)
mColorTable[i] = 0;
else if (i > max)
mColorTable[i] = 65535;
else
mColorTable[i] = (ushort)((float)(i - min) * step);
}
.... //some irrelevant code
Gl.glPixelMapusv(Gl.GL_PIXEL_MAP_R_TO_R, 65536, mColorTable);
Gl.glPixelMapusv(Gl.GL_PIXEL_MAP_G_TO_G, 65536, mColorTable);
Gl.glPixelMapusv(Gl.GL_PIXEL_MAP_B_TO_B, 65536, mColorTable);
Now, according to this documentation, I should use GL_PIXEL_MAP_I_TO_I and set INDEX_SCALE and INDEX_BIAS to zero, but doing that does not change the result, that the image is severely clamped. And by 'severely clamped' I mean it's either black or white, with very few shades of grey, but the original non-magnified image looks like what's expected.
So, how do I avoid the clamping of the magnified view? Should I make a second control that follows the cursor and gets filled in with data from the first control? That approach seems like it would take the array copies outside of the graphics card and into C#, which would almost by definition be slower, and so make the control nonresponsive.
Oh, I'm using C# and the Tao framework, if that matters.
Here's the answer. The problem is that the LUT is being applied twice, so before calling the copy, call:
Gl.glPixelTransferi(Gl.GL_MAP_COLOR, Gl.GL_FALSE);
Then, once done, call:
Gl.glPixelTransferi(Gl.GL_MAP_COLOR, Gl.GL_TRUE);
That way, the 'extreme clamping' I was describing is removed.
#thing2k-- your solution causes the copy to happen outside the graphics card, so slows down the drawing on mouse drag, but doesn't fix the double clamp.
Please, pretty please with loads of sugar, molasses, sprinkles and a mist of high-fructose corn syrup on top and all over, explain why you cannot just use texture-mapping to draw this imagery.
Texture-mapping is a core, basic, everyday, run of the mill, garden-variety, standard, typical, expected, and just generally nice feature of OpenGL. It is in version 1.4. Why not use it as a starting point?
If I understand you correctly then this should be close to what you after, using glReadPixels and glDrawPixels.
Sorry it's C++ not C# but the OpenGL function should still be the same.
// main.cpp
// glut Text
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <GL/glut.h>
#include <cstdio>
int WIDTH = 800;
int HEIGHT = 600;
int MouseButton, MouseY = 0, MouseX = 0;
const int size = 80;
char *image, rect[size*size*3];
int imagewidth, imageheight;
bool Init()
{
int offset;
FILE* file = fopen("image.bmp", "rb");
if (file == NULL)
return false;
fseek(file, 10, SEEK_SET);
fread(&offset, sizeof(int), 1, file);
fseek(file, 18, SEEK_SET);
fread(&imagewidth, sizeof(int), 1, file);
fread(&imageheight, sizeof(int), 1, file);
fseek(file, offset, SEEK_SET);
image = new char[imagewidth*imageheight*3];
if (image == NULL)
return false;
fread(image, 1, imagewidth*imageheight*3, file);
fclose(file);
return true;
}
void Reshape(int width, int height)
{
WIDTH = width;
HEIGHT = height;
glViewport(0 , 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
}
void Display()
{
int size2 = size/2;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRasterPos2i(0,0);
glPixelZoom(1.f, 1.f);
glDrawPixels(imagewidth, imageheight, 0x80E0/*GL_RGB*/, GL_UNSIGNED_BYTE, image);
glReadPixels(MouseX-size2, MouseY-size2, size, size, GL_RGB, GL_UNSIGNED_BYTE, rect);
glPixelZoom(2.f, 2.f);
glRasterPos2i(MouseX-size, MouseY-size);
glDrawPixels(size, size, GL_RGB, GL_UNSIGNED_BYTE, rect);
glFlush();
glutSwapBuffers();
}
void Mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
MouseButton &= (1<<button);
else
MouseButton &= ~(1<<button);
}
void MouseMove(int x, int y)
{
MouseX = x;
MouseY = HEIGHT - y;
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
if (Init() == false)
return 1;
glutInitWindowSize(WIDTH, HEIGHT);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("glut_Text");
glClearColor(0.25, 0.25, 0.25, 1.0);
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutMouseFunc(Mouse);
glutMotionFunc(MouseMove);
glutPassiveMotionFunc(MouseMove);
glutMainLoop();
return 0;
}
Hope this helps.