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.
Related
I tried to scroll text horizontally in a OpenGL app. The text comes perfect and smooth part by part at the screen when starting from the right side. When the first letter then touches the side of the window at the left side, the whole text disappears. I want that the text goes over the border, so that it vanished smoothly and start again from the right side. It's a sort of banner i try to make. Someone who knows how to solve this problem?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glut.h>
using namespace std;
//static int font_index = 0;
int state = 1;
float xsize = 800;
float ysize = 300;
void print_bitmap_string(/*void* font,*/ const char* s)
{
while (*s) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *s);
s++;
}
}
void reshape(int w, int h)
{
GLdouble size;
GLdouble aspect;
/* Use the whole window. */
glViewport(0, 0, w, h);
/* We are going to do some 2-D orthographic drawing. */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
size = (GLdouble)((w >= h) ? w : h) / 2.0;
if (w <= h) {
aspect = (GLdouble)h / (GLdouble)w;
glOrtho(-size, size, -size * aspect, size * aspect, -100000.0, 100000.0);
}
else {
aspect = (GLdouble)w / (GLdouble)h;
glOrtho(-size * aspect, size * aspect, -size, size, -100000.0, 100000.0);
}
/* Make the world and window coordinates coincide so that 1.0 in */
/* model space equals one pixel in window space. */
glScaled(aspect, aspect, 1.0);
/* Now determine where to draw things. */
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
}
float yild;
float ystep;
float x_pos = xsize/2;
float y_pos = 70;
void draw()
{
const char* bitmap_font_names[7] = { "Testing train application for windows!!##" };
glPushMatrix();
/* Draw the strings, according to the current mode and font. */
glTranslatef(0.5, -100, 0);
//set the text color
glColor4f(0.0f, 173.0f, 115.0f, 1.0f);
//ystep = 100.0;
//yild = 20.0;
glRasterPos2f(x_pos, y_pos /* + 1.25 * yild*/);
print_bitmap_string(bitmap_font_names[0]);
glPopMatrix();
}
void display(void)
{
//change background color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
draw();
glColor3f(0.0, 255.0, 0.0);
/*glBegin(GL_TRIANGLES);
glVertex2f(20 + x_pos, 0 + y_pos);
glVertex2f(50 + x_pos, 10 + y_pos);
glVertex2f(20 + x_pos, 20 + y_pos);
glEnd();*/
glutSwapBuffers();
}
void timer(int) {
glutPostRedisplay();
glutTimerFunc(1000 / 90, timer, 0);
switch (state) {
case 1:
if (x_pos > (-xsize / 2) - 200) {
x_pos -= 1;
}
else {
state = -1;
}
break;
case -1:
x_pos = (xsize / 2);
state = 1;
break;
}
cout << x_pos << endl;
}
int main(int argc, char** argv)
{
glutInitWindowSize(xsize, ysize);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Train Display");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(1000, timer, 0);
glutMainLoop();
return 0;
}
It looks like OpenGL may not allow you to explicitly draw bitmaps outside of the viewport.
From the documentation on glRasterPos,
To set a valid raster position outside the viewport, first set a valid raster position, then call glBitmap with NULL as the bitmap parameter.
The documentation on glBitmap elaborates on this aspect:
To set a valid raster position outside the viewport, first set a valid raster position inside the viewport, then call glBitmap with NULL as the bitmap parameter and with xmove and ymove set to the offsets of the new raster position. This technique is useful when panning an image around the viewport.
This question already has an answer here:
grid not showing (it is all black)
(1 answer)
Closed 2 years ago.
I am trying to make an option pop-up menu to be able to choose if I want the grid on or off as an option to add to the game I am trying to make. I have been able to display the pop-up menu which has two options called "grid on" and "grid off", however when I click on it, the grid doesn't show at all for me which I am struggling to do.
void myGrid(GLint i) {
glEnable(GL_LINE_STIPPLE); //Activates the line-style feature
glLineStipple(1, 0xAAAA); // Plots a dashed polyline
glColor3f(0.8, 0.8, 0.8);
glBegin(GL_LINES);
for (i = 2; i <= 9; i++)
{
glVertex3f(i * 0.1 * w, 0.0, 0.0);
glVertex3f(i * 0.1 * w, 0.9 * h, 0.0);
}
for (i = 1; i <= 9; i++)
{
glVertex3f(0.1 * w, i * 0.1 * h, 0.0);
glVertex3f(w, i * 0.1 * h, 0.0);
}
glEnd();
glDisable(GL_LINE_STIPPLE);
}
int main(int argc, char* argv[])//standard c entry signature
{
//cout << "Player Controls:\n Up Key - move forward\n Down Key - move backward\n Left/Right key - move left or right\n R/r - rotate";
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use double-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
/* window width = 400 pixels, height = 400 pixels */
glutInitWindowSize(500, 500);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window and set its title bar*/
glutCreateWindow("Coursework 1");
init();
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
//glutSpecialFunc(specialKeys);
// glutSpecialUpFunc(keyGoneUp);
glutReshapeFunc(reshape);
//Create a grid turning it on and off
glutCreateMenu(myGrid);
glutAddMenuEntry("Grid on", 1);
glutAddMenuEntry("Grid off", 2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// SetupRC();
glutIdleFunc(IdleFunc);
glutMainLoop();
return 0;
}
Toggle global (or window-specific, up to you) boolean in menu callback
Issue a glutPostRedisplay() in menu callback
Use global boolean to draw/don't draw grid in display callback
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 );
// ...
}
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.
I am fairly new to using GLUT, and I have been attempting to compile a program (which I found here, first response) that uses the mouse to draw a rectangle by recording the starting and ending points of a click-and-drag.
As a clean copy/paste, it will compile but not draw anything. It just displays a white screen, even after changing the background color to black (in the setup() function). I've read several sources to verify that this program doesn't miss anything in its draw and reshape functions, and it's all there.
I create a window, set the viewport to the window dimensions, and then use the gluOrtho2D function to set the mapping (since the window and viewport are the same dimensions, I set the mapping to the window dimensions). The mouse callback records where I left-click, and where I release left-click, then calls the glutPostRedisplay() function to redraw the window with the new coordinates. After a bit of debugging, I discovered the coordinates are recorded and saved appropriately, and are measured in pixels (x and y are integers between 0 and window dimension), so I should be able to draw a rectangle from one vertex to the other vertex using the coordinates. But, like I said, it only displays a white screen.
So, is there something wrong with the way I am drawing the rectangle? Am I mapping the window incorrectly? I am seriously lost, and any feedback would be greatly appreciated.
EDIT2: I changed the glutInitDisplayMode from GLUT_SINGLE to GLUT_DOUBLE, and that fixed the whole non-interactive white screen thing. Now it will draw a rectangle with the mouse with a flipped y-coordinate (which I fixed), and it works great now. Thank you very much for the suggestion.
Here is my program (EDIT1: added comments):
#include <cstdlib>
#include <GL/glut.h>
using namespace std;
GLsizei width, height;
struct Position
{
Position() : x(0), y(0) {}
float x;
float y;
};
Position start; // Records left-click location
Position finish; // Records left-click release location
void display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear window
glColor3ub(rand()%256, rand()%256, rand()%256); // generates random color
glBegin(GL_QUADS);
glVertex2f(start.x,start.y);
glVertex2f(finish.x,start.y);
glVertex2f(finish.x,finish.y);
glVertex2f(start.x,finish.y);
glEnd();
glutSwapBuffers(); // display newly drawn image in window
}
void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h ); // set to size of window
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0.0, (float)w, 0.0, (float)h );
width = w; // records width globally
height = h; // records height globally
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_DOWN)
{
start.x = x; //x1
start.y = y; //y1
}
if(state==GLUT_UP)
{
finish.x = x; //x2
finish.y = y; //y2
}
break;
glutPostRedisplay();
}
}
void motion( int x, int y )
{
finish.x = x;
finish.y = y;
glutPostRedisplay();
}
void setup()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // *should* display black background
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("");
setup();
// initializing callbacks
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}
As my comment suggested:
change:
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
to:
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);