I have a problem with glViewport. In my liitle programm i have two viewports. I can draw a form (with the motionfunc) in one of those viewports and in the other a line is automatically drawn.
So far so good..
When i try to draw something with mousefunc the viewport is in a total different place. And it is very difficult to find the new correct coordinates for that viewport.
Is there a possibility to reset the coordinates..
I cant use glLoadIdentity in mouse or motion because then nothing is displayed.
I hope you understand what i mean. It is a bit difficult to explain.
OK here a codesnippet....
void mouse (int button, int state, int mx, int my)
{
if (modus == 0 && button==GLUT_LEFT_BUTTON && state != GLUT_DOWN)
{
...
}
else if (modus == 1 && button==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
**glViewport(10,10 , sw_w1, sw_h1);**
//the drawing is much higher than in the first viewport in motion.
//But it should be the same viewport like the first in motion.
glBegin()...glEnd()
glFlush();
}
}
void motion(int mousex,int mousey)
{
GLdouble h=12;
GLdouble winkel=360/h;
Line liste[num];
liste[++last].x =(mousex)-((sw_w1+2*GAP)/2);
liste[last].y =(mousey)-((sw_h1+2*GAP)/2);
if (modus==0 && gruppe == 0) {
if (last>=1)
{
glViewport(10, 10, sw_w1, sw_h1); //works fine
glColor3d(R, G, B);
for(h+1;h>0;h--){
glRotated(winkel, 0, 0, 1);
glBegin(GL_LINE_STRIP);
for(int i=last-1;i<=last;i++){
glVertex2i(liste[i].x,liste[i].y);
}
glEnd();
}
glLineWidth(linewidth+0.5);
glColor3f(1, 0, 0);
glBegin(GL_LINE_STRIP);
for(int i=last-1;i<=last;i++){
glVertex2i(liste[i].x,liste[i].y);
}
glEnd();
glViewport(1020,10 , sw_w2, sw_h2); //works fine
glColor3f(1, 0, 0);
glBegin(GL_LINE_STRIP);
for(int i=last-1;i<=last;i++){
glVertex2i(liste[i].x,liste[i].y);
}
glEnd();
}
glFlush();
}
}
The second and third viewport works fine. The first one is the same as the second but the picture is displayed much higher.Why is that so?
And how could i change it so that i get the same viewport like the second one.
I hope you now understand what i mean.
You should check your modelview/projection matrices and see if they are what you expect them to be in each function.
Also, as Christian's commented, it is not necessary, nor recommended, to draw in the motion func. Update your application state per the input and call glutPostRedisplay to signal that you want to redraw your window. That way, your application will have a cleaner design and it will be easier to make it behave consistently.
(added my comment as answer since that was the problem, and added Christian's comment, since that is the proper solution. Don't draw in motionfunc!.)
Related
A Project I am working on involves me using glScissor, in some cases i need to perform a scissor on an area twice (or more), with the goal of only rendering what is within both scissor boxes.
The issue im running into is that the second scissor box just overrides the previous one, meaning only the last box set is used instead of both.
I have tried existing solutions such as setting scissor1, push matrix, enable scissor_test, set scissor2, disable scissor_test, popmatrix, disable scissor_test. As proposed here: glScissor() call inside another glScissor()
I could not get these to produce any difference, I had also tried glPushAttrib instead of matrix but still no difference.
Here is an example program I wrote for scissor testing, its compiled by g++ and uses freeglut, the scissoring takes place in display():
/*
Compile: g++ .\scissor.cpp -lglu32 -lfreeglut -lopengl32
*/
#include <GL/gl.h>//standard from mingw, already in glut.h - header library
#include <GL/glu.h>//standard from mingw, already in glut.h - utility library
#include <GL/glut.h>//glut/freeglut - more utilities, utility tool kit
void display();
void reshape(int, int);
void timer(int);
void init(){
glClearColor(0, 0, 0, 1);
}
int main(int argc, char **argv){
glutInit(&argc, argv);//init glut
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);//init display mode, add double buffer mode
//init window
glutInitWindowPosition(200, 100);//if not specified, it will display in a random spot
glutInitWindowSize(500, 500);//size
//create window
glutCreateWindow("Window 1");
//give glut a function pointer so it can call that function later
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);//call certain function after a specified amount of time
init();
glutMainLoop();//once this loop runs your program has started running, when the loop ends the program terminates
}
float xPos = -10;
int state = 1;//1 = right, -1 = left
//our rendering happens here
void display(){
//clear previous frame
glClear(GL_COLOR_BUFFER_BIT);//pass in flag of frame buffer
//draw next frame below
glLoadIdentity();//reset rotations, transformations, ect. (resets coordinate system)
//we are using a model view matrix by default
//TEST
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 100, 1000);
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(50, 0, 1000, 1000);
//assuming both scissors intersect, we should only see the square between 50 and 100 pixels
//draw
glBegin(GL_QUADS);//every set of 3 verticies is a triangle
//GL_TRIANGLES = 3 points
//GL_QUADS = 4 points
//GL_POLYGON = any amount of points
glVertex2f(xPos, 1);//the 2 is the amount of args we pass in, the f means theyr floats
glVertex2f(xPos, -1);
glVertex2f(xPos+2, -1);
glVertex2f(xPos+2, 1);
glEnd();//tell opengl your done drawing verticies
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
glDisable(GL_SCISSOR_TEST);
//display frame buffer on screen
//glFlush();
glutSwapBuffers();//if double buffering, call swap buffers instead of flush
}
//gets called when window is reshaped
void reshape(int width, int hight){
//set viewport and projection
//viewport is a rectangle where everything is drawn, like its the window
glViewport(0, 0, width, hight);
//matrix modes: there is model view and projection, projection has depth
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//reset current matrix after changing matrix mode
gluOrtho2D(-10, 10, -10, 10);//specify 2d projection, set opengl's coordinate system
glMatrixMode(GL_MODELVIEW);//change back to model view
}
//this like makes a loop
void timer(int a){
glutPostRedisplay();//opengl will call the display function the next time it gets the chance
glutTimerFunc(1000/60, timer, 0);
//update positions and stuff
//this can be done here or in the display function
switch(state){
case 1:
if(xPos < 8)
xPos += 0.15;
else
state = -1;
break;
case -1:
if(xPos > -10)
xPos -= 0.15;
else
state = 1;
break;
}
}
I tried following example solutions, such as push/pop matrix/attrib, but couldnt get anything to work
There is no first or second scissor box. There is just the scissor box. You can change the scissor box and that change will affect subsequent rendering. But at any one time, there is only one.
What you want is to use the stencil buffer to discard fragments outside of an area defined by rendering certain values into the stencil buffer.
I have an interesting bug that has been "bugging" me for a few days now.
I am currently using OpenGL to draw text on a screen. I am utilizing the OGLFT library to assist the drawing. This library actually uses the freetype2 library. I am actually not doing anything special with the text. I am only looking for monochromatic text.
Anyways, after implementing the library, I noticed that the text is only drawn correct when I have glStipple enabled. I believe that there is some interference issue between the OGLFT library and what I am enabling.
I was wondering if there is anyone out there with some experience on using the OGLFT library. I am posting a minimalist example of my code to demonstrate what is going on:
(Please note that there are some variables that are used to st the zoom factor of my glCanvas and the position of the camera and that this is only for 2D)
double _zoomX = 1.0;
double _zoomY = 1.0;
double _cameraX = 0;
double _cameraY = 0;
/* This function gets called everytime a draw routine is needed */
void modelDefinition::onPaintCanvas(wxPaintEvent &event)
{
wxGLCanvas::SetCurrent(*_geometryContext);// This will make sure the the openGL commands are routed to the wxGLCanvas object
wxPaintDC dc(this);// This is required for drawing
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
updateProjection();
OGLFT::Monochrome *testface = new OGLFT::Monochrome( "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 8);
testface->draw(0, 0, "test");
glEnable(GL_LINE_STIPPLE);// WHen I comment out this line, the text is unable to be drawn
glLineStipple(1, 0b0001100011000110);
glBegin(GL_LINES);
glVertex2d(_startPoint.x, _startPoint.y);
glVertex2d(_endPoint.x, _endPoint.y);
glEnd();
glDisable(GL_LINE_STIPPLE);
SwapBuffers();
}
void modelDefinition::updateProjection()
{
// First, load the projection matrix and reset the view to a default view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-_zoomX, _zoomX, -_zoomY, _zoomY, -1.0, 1.0);
//Reset to modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, (double)this->GetSize().GetWidth(), (double)this->GetSize().GetHeight());
/* This section will handle the translation (panning) and scaled (zooming).
* Needs to be called each time a draw occurs in order to update the placement of all the components */
if(_zoomX < 1e-9 || _zoomY < 1e-9)
{
_zoomX = 1e-9;
_zoomY = _zoomX;
}
if(_zoomX > 1e6 || _zoomY > 1e6)
{
_zoomX = 1e6;
_zoomY = _zoomX;
}
glTranslated(-_cameraX, -_cameraY, 0.0);
}
Also one thing to note is that the code below the glEnable(GL_LINE_STIPPLE); is required. It is as if the glStipple needs to be drawn correctly for the text to be displayed correctly.
Looking through your code, I believe that your intention is to render it as a greyscale? If so, then you can simply use the OGLFT::Grayscale *testface = new OGLFT::Grayscale( "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 8);
This will get what you need without having to worry about the issue that you posted. In fact, I recommend doing it this way too.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I have been searching all day for a tutorial or example code for a simple program - click on object (like a 2D rectangle for example) then as you hold and move the mouse the object follows the mouse, then on mouse release the object remains in new location. In other words, I want to understand how to drag and drop an object with the mouse events.
Could anyone help to point me in the right direction of any useful sources of information relating to this problem?
Thanks for all the responses so far.
I have worked out how to do it, so I will go ahead an answer my own question.
I am using GLUT as a mouse handler:
When the mouse is clicked and moving (glutMotionFunc) the drag function is called.
In the drag function the mouse coordinates (x,y) are converted to a Points struct while being converted into window coordinates.
If the mouse is within the square then drag the square by changing it's coordinates and redisplay.
I am still very new to OpenGL and C++ so I do apologize for the messy coding. I am a bit frustrated in doing it this way as the redrawn square makes it seem the cursor snaps to the center. I welcome alternative solutions to this problem and criticism of my code, for learning purposes.
CODE (included glut and using namespace std):
// points structure made of two coordinates; x and y
struct Points
{
float x,y; // initializor
Points() { x = 0.0; y = 0.0; } // constructor
Points(float _x, float _y) : x(_x), y(_y) {}
};
// square made of 4 points
class Square
{
public:
Points pts[4]; // square structure
Square(); // initialize constructor
void draw(Square *sqr); // draw square
Points mouse(int x, int y); // get mouse coordintaes
Square* drag(Square *sqr, Points *mouse); // change points of sqr
};
// square constructor
Square::Square()
{
pts[0] = Points(0.2,0.2);
pts[1] = Points(0.4,0.2);
pts[2] = Points(0.4,0.4);
pts[3] = Points(0.2,0.4);
};
// draw function
void Square::draw(Square *sqr)
{
// draw square fill
int i;
glColor3f(0.2, 0.2, 0.2);
glBegin(GL_QUADS);
for (i = 0; i < 4; ++i)
{
glVertex2f(sqr->pts[i].x, sqr->pts[i].y);
}
glEnd();
// draw square points
i = 0;
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; ++i)
{
glVertex2f(sqr->pts[i].x, sqr->pts[i].y);
}
glEnd();
}
// mouse function
Points Square::mouse(int x, int y)
{
int windowWidth = 400, windowHeight = 400;
return Points(float(x)/windowWidth, 1.0 - float(y)/windowHeight);
}
// drag function
Square* Square::drag(Square *sqr, Points *mouse)
{
sqr->pts[0].x = mouse->x - 0.1;
sqr->pts[0].y = mouse->y - 0.1;
sqr->pts[1].x = mouse->x + 0.1;
sqr->pts[1].y = mouse->y - 0.1;
sqr->pts[3].x = mouse->x - 0.1;
sqr->pts[3].y = mouse->y + 0.1;
sqr->pts[2].x = mouse->x + 0.1;
sqr->pts[2].y = mouse->y + 0.1;
return sqr;
}
// GLOBAL
// create square object
Square* sqr = new Square;
// display at start
void display() {
glClear(GL_COLOR_BUFFER_BIT);
sqr->draw(sqr);
glFlush();
}
// drag function
void drag (int x, int y)
{
// int x and y of mouse converts to screen coordinates
// returns the point as mousePt
Points mousePt = sqr->mouse(x,y);
//create pointer to window point coordinates
Points* mouse = &mousePt;
// if the mouse is within the square
if (mouse->x > sqr->pts[0].x && mouse->y > sqr->pts[0].y)
{
if (mouse->x < sqr->pts[2].x && mouse->y < sqr->pts[2].y)
{
// then drag by chaning square coordinates relative to mouse
sqr->drag(sqr,mouse);
glutPostRedisplay();
}
}
}
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("Move Box");
glutMotionFunc(drag);
Initialize();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
OpenGL is only concerned with the drawing process. Everything else (mouse input, object picking, scene management/alterations, etc.) is completely up to you to implement.
Here's a rough outline:
Install a mouse click event handler (the exact method to use depends on the framework used and/or the operating system)
In the mouse click event handler perform a picking operation. This usually involves unprojecting the mouse window position into the world space (see gluUnproject) resulting in a ray. Test each object in the scene if it intersects with the ray; you'll have to implement this yourself, because OpenGL just draws thing (there is no such thing as a "scene" in OpenGL).
If a object has been picked register it to be manipulated in the mouse drag handler
everytime a mouse drag event happens adjust the object's position data and trigger of the OpenGL display (you always redraw the whole thing in OpenGL).
When the mouse is released unregister the object from the drag handler.
As mentioned by others, OpenGL does not handle user input. You want to use a library for that. If you want a more all-around solution, you can even use a more complete render or physics engine.
For simple user input, you can use SDL (e.g. this is for mouse input).
For more complete 2D stuff, you can just use Box2D. Here are a whole bunch of tutorials.
The heavy-weight solution is a complete render engine, such as Ogre3D or CrystalSpace.
As mentioned by others, you need to get a mouse handler to get the mouse position first. Then you need a way to pick an object. You have a few options to do the picking in OpenGL.
If you are using classic OpenGL, you can use the select buffer. The following link is a good tutorial
http://www.lighthouse3d.com/opengl/picking/index.php3?openglway
If you are using modern opengl, which is shader based, you can use FBO based picking.
http://ogldev.atspace.co.uk/www/tutorial29/tutorial29.html
You can always implement a ray tracking picking yourself in both cases. The gluUnproject can help a lot in the implementation.
http://schabby.de/picking-opengl-ray-tracing/
After that, you just need to update the object position according to the mouse movement or acceleration.
So a subset of my code (within a rendering loop of course) is:
for(int x=0;x<data.length;x++)
for(int y=0;y<data[0].length;y++) {
float r = colorData[x][y][0], g = colorData[x][y][1], b = colorData[x][y][2];
glGetError();
glColor3f(r,g,b);
int xCoord = x*100;
int yCoord = y*100;
int height = (int) Math.round(data[y][x]*25);
glBegin(GL_QUADS);
//Top
glVertex3f(xCoord,yCoord,height);
glVertex3f(xCoord+100,yCoord,height);
glVertex3f(xCoord+100,yCoord+100,height);
glVertex3f(xCoord,yCoord+100,height);
//*/
//Sides
glColor3f(r/2,g/2,b/2);
glVertex3f(xCoord,yCoord,height);
glVertex3f(xCoord,yCoord,0);
glVertex3f(xCoord+100,yCoord,0);
glVertex3f(xCoord+100,yCoord,height);
glVertex3f(xCoord,yCoord+100,height);
glVertex3f(xCoord+100,yCoord+100,height);
glVertex3f(xCoord+100,yCoord+100,0);
glVertex3f(xCoord,yCoord+100,0);
glVertex3f(xCoord,yCoord+100,height);
glVertex3f(xCoord,yCoord+100,0);
glVertex3f(xCoord,yCoord,0);
glVertex3f(xCoord,yCoord,height);
glVertex3f(xCoord+100,yCoord+100,height);
glVertex3f(xCoord+100,yCoord,height);
glVertex3f(xCoord+100,yCoord,0);
glVertex3f(xCoord+100,yCoord+100,0);
//Bottom
glColor3f(r/4,g/4,b/4);
glVertex3f(xCoord,yCoord,0);
glVertex3f(xCoord,yCoord+100,0);
glVertex3f(xCoord+100,yCoord+100,0);
glVertex3f(xCoord+100,yCoord,0);
glEnd();
int err = glGetError();
if(err != GL_NO_ERROR)
System.out.println("An error has occured: #"+err);
glColor3f(1,0,0);
glBegin(GL_LINES);
//Top
glVertex3f(xCoord,yCoord,height);
glVertex3f(xCoord+100,yCoord,height);
glVertex3f(xCoord+100,yCoord+100,height);
glVertex3f(xCoord,yCoord+100,height);
glEnd();
}
The code works correctly, drawing a two dimensional grid of boxes, with the sides of the box being slightly darker than the top. (Poor man's lighting)
Just for curiosity I took out the first call to glColor3f, expecting the top of the box to become colorless, and the sides to remain the same; however the entire grid of boxes goes white. Why is this? Is it something in this code here? Or is some other part of my render loop causing this?
(If it makes a difference, I'm using LWJGL's Java implementation of OpenGL1.1)
OpenGL immediate mode works a bit like this (pseudocode)
glColor3f(r,g,b){
state.color.{r,g,b} = r,g,b;
}
glNormal(x,y,z){
state.normal.{x,y,z} = x,y,z;
}
/* and so on */
glVertex3f(x,y,z){
send_vertex_to_rasterizer(state.color, state.normal, ..., {x,y,z})
}
I.e. OpenGL remembers the last color you've set and will it apply to all following vertices until you change it to another color, which then will apply.
I have a function Drwa() this is rendering a triangle on screen.and also i have another Draw_poly() which is rendering a Rectangle on screen. And i also i m rotating rectangle and triangle both simultaneously.I want to keep speed of rotation different for both how will i do ?
Let suppose i am moving an object on screen and another i m rotating then how will i do ? That's why i m looking for function moving of object will keep time limited and rotating object will not keep time.So rotation will be fast and moving of object will be slow
First, define your rotation as angle per second. Then in your main draw function, compute the elapsed time in second, multiply by the angular speed, and you're done.
I would like to partecipate with a an answer of mine.
The answer of genpfault could be good as much as you need, but if you would like to produce a good animation you need to design a better software.
Here, look at my answer. However, reading another your question, I think you are missing some fundamental point: learn OpenGL architecture, practice on each OpenGL entry point, read books.
At last, but not least, I would you suggest to search for answer already told on stackoverflow. This is supposed to be a question & answer site...
Rotate one less/slower than the other:
static float rot_a = 0.0;
static float rot_b = 0.0;
rot_a += 1.0;
rot_b += 0.5;
glPushMatrix();
glRotatef( rot_a, 0, 0, 1 );
Draw_A();
glPopMatrix();
glPushMatrix();
glRotatef( rot_b, 0, 0, 1 );
Draw_B();
glPopMatrix();
Alternatively you can spin up some threads that modify your object positions and sleep() without blocking the render thread.
Position obj_a;
Position obj_b;
void thread_1()
{
while( !done )
{
sleep(1);
modify_pos( obj_a );
}
}
void thread_2()
{
while( !done )
{
sleep(2);
modify_pos( obj_b );
}
}
void draw()
{
glPushMatrix();
position_object( obj_a );
Draw_A();
glPopMatrix();
glPushMatrix();
position_object( obj_b );
Draw_B();
glPopMatrix();
}
int main()
{
...
launch_thread( thread_1 );
launch_thread( thread_2 );
...
return 0;
}