Sign in to follow this  
JustusAa

OpenGL Glut change Xcord and Y cord

Recommended Posts

JustusAa    122
Hey I would love some help, been stuck with this for a while. I spent 16h myself to solve the problem so dont think I havnt put heart into it. My grafics engine will get cords x and y (its 2d) and draw objekt there. Problem is I cant change the cords I set, I am sure I made some kind of error but cant find what. Hoping for you guys.. Code , some is in other modules but I will try to explain so I wont waste your time.
<<<<<<<<<<<<<<<<<<<<<
// These variables set the dimensions of the rectanglar region we wish to view.
const double Xmin = 0.0, Xmax = 1066.0;
const double Ymin = 0.0, Ymax = 600.0;
GLint littleMen_display_list;
float oldPosX=0, oldPosY=0, posX=0, posY=0;
int startGraficsEngine(int argc, char** argv) 
{
    	glutInit(&argc, argv);
        // The image is not animated so single buffering is OK. 
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
        // Window position (from top corner), and size (width and hieght)
	glutInitWindowPosition(200,200);
	glutInitWindowSize(1066,600);
	glutCreateWindow("EH");
        // Initialize OpenGL as we like it..
        initRendering();  
        startKeyboard(); //works as intended
        glutReshapeFunc( resizeWindow );
        glutDisplayFunc(renderScene);

        // here is the setting of the idle function
	glutIdleFunc(renderScene);
        
        /* set clear color */
	glClearColor (1.0, 1.0, 1.0, 1.0);
	/* set fill  color */
	glColor3f(1.0, 1.0, 0.0);
	glutMainLoop();
  
    return (EXIT_SUCCESS);
}
void glutExit()
{
    exit(1);
}
void initRendering()
{
        glEnable ( GL_DEPTH_TEST );
       littleMen_display_list = createDL();
        
	// Uncomment out the first block of code below, and then the second block,
	//		to see how they affect line and point drawing.

	// The following commands should cause points and line to be drawn larger
	//	than a single pixel width.
	glPointSize(1);
	glLineWidth(1);


	// The following commands should induce OpenGL to create round points and 
	//	antialias points and lines.  (This is implementation dependent unfortunately).
	glEnable(GL_POINT_SMOOTH);
	glEnable(GL_LINE_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);	// Make round points, not square points
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);		// Antialias the lines
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void renderScene(void) 
{
   sleepTime.tv_sec = 0;              //set to 0
   sleepTime.tv_nsec = 33000000;      //set how much time that is minimum for loop to take
   clock_gettime(CLOCK_THREAD_CPUTIME_ID  , &startTime); 
   ///SET RENDER CODE BETWEEN HERE

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
   glPushMatrix();
   glTranslatef(returnPos(1), returnPos(2), 0.0);
   drawLittleMan();
   glPopMatrix();   

    glPushMatrix();
    glTranslatef(returnPos(1)+20, returnPos(2), 0.0);
    glCallList(littleMen_display_list);
    glPopMatrix();

   
    glPushMatrix();
    glTranslatef(555, 555, 0.0);
    glCallList(littleMen_display_list);
    glPopMatrix();
   
  ///AND HERE
  glutSwapBuffers();
   

  clock_gettime(CLOCK_THREAD_CPUTIME_ID , &endTime);
  someTime.tv_sec = endTime.tv_sec - startTime.tv_sec;
  someTime.tv_nsec = endTime.tv_nsec - startTime.tv_nsec;
  sleepTime.tv_sec -= someTime.tv_sec;
  sleepTime.tv_nsec -= someTime.tv_nsec;
  nanosleep(&sleepTime, NULL);

}
void resizeWindow(int w, int h)
{
	double scale, center;
	double windowXmin, windowXmax, windowYmin, windowYmax;

	// Define the portion of the window used for OpenGL rendering.
	glViewport( 0, 0, w, h );	// View port uses whole window

	// Set up the projection view matrix: orthographic projection
	// Determine the min and max values for x and y that should appear in the window.
	// The complication is that the aspect ratio of the window may not match the
	//		aspect ratio of the scene we want to view.
	w = (w==0) ? 1 : w;
	h = (h==0) ? 1 : h;
	if ( (Xmax-Xmin)/w < (Ymax-Ymin)/h ) {
		scale = ((Ymax-Ymin)/h)/((Xmax-Xmin)/w);
		center = (Xmax+Xmin)/2;
		windowXmin = center - (center-Xmin)*scale;
		windowXmax = center + (Xmax-center)*scale;
		windowYmin = Ymin;
		windowYmax = Ymax;
	}
	else {
		scale = ((Xmax-Xmin)/w)/((Ymax-Ymin)/h);
		center = (Ymax+Ymin)/2;
		windowYmin = center - (center-Ymin)*scale;
		windowYmax = center + (Ymax-center)*scale;
		windowXmin = Xmin;
		windowXmax = Xmax;
	}
	
	// Now that we know the max & min values for x & y that should be visible in the window,
	//		we set up the orthographic projection.
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho( windowXmin, windowXmax, windowYmin, windowYmax, -1, 1 );

}
GLuint createDL()
{
	GLuint littleManDL;
	// Create the id for the list
	littleManDL = glGenLists(1);
	// start list
	glNewList(littleManDL,GL_COMPILE);
	// call the function that contains 
	// the rendering commands
	drawLittleMan();
	// endList
	glEndList();
	return(littleManDL);
}
void changePos(float f_posx, float f_posy)//funktion keybord presses uses
{
    posX =f_posx;
    posY = f_posx;
    printf("changePos\n");
 //   posX = oldPosX - f_posx;
 //   posY = oldPosY - f_posy;
 //   oldPosX = posX;
 //   oldPosY = posY;
    return ;
}
int returnPos(int val)// use it to change pos to draw men at.. works but no differnce..
{
    int x=0, y=0;
    x = posX - x ;
    y = posY - y;
    if(val==1)
    {printf("X\n");
     return x;}
    if(val==2){printf("Y\n");
        return y;}
    else
    return 0;
}
void drawLittleMan() { //my patetented man withouth his head ^^
    
    //Sets startingpoint of little man
    float posX=0.0, posY=0.0;

    //Set leglenght of little man
    float leftLeg=8.0, rightLeg=8.0;
    //Set Bodylenght of little man
    float bodyLeangth=8.0;
    float neckLeanght=4.0;
    float headSize=4.0;
    //Set armlenght of little man
    float armLeft=8.0, armRight=8.0;

        // Set drawing color 
        glColor3f(1.0f, 0.0f, 1.0f);
        
         //Draws little man
        glBegin(  GL_LINES );
        glVertex2f( posX             ,    posY );
        glVertex2f( posX-leftLeg     ,    posY-leftLeg );

        glVertex2f( posX             ,    posY );
        glVertex2f( posX+rightLeg    ,    posY-rightLeg );

        glVertex2f( posX             ,    posY);
        glVertex2f( posX             ,    posY+bodyLeangth );

        glVertex2f( posX             ,    posY+bodyLeangth );
        glVertex2f( posX-armLeft     ,    posY+bodyLeangth );

        glVertex2f( posX             ,    posY+bodyLeangth );
        glVertex2f( posX+armRight    ,    posY+bodyLeangth );

        glVertex2f( posX             ,    posY+bodyLeangth );
        glVertex2f( posX             ,    posY+bodyLeangth+neckLeanght );

        glVertex2f( posX             ,    posY+bodyLeangth+neckLeanght );
        glVertex2f( posX-headSize    ,    posY+bodyLeangth+neckLeanght );


        glEnd();
        return ;
}

<<<<<<<<<<<<<<<<<<<<<


Thanx first edit source tags, second want to tell you I think its a problem with double buffert or idlefunk thats a part of glut. I dont know though. I dont have any compilation problems and work in linux. [Edited by - JustusAa on April 23, 2008 7:04:46 AM]

Share this post


Link to post
Share on other sites
BionicBytes    154
The code is a bit hard to read - TIP: use the
 tag.

Anyway two areas that might the cause of your issue:
This routine:


int returnPos(int val)// use it to change pos to draw men at.. works but no differnce..
{
int x=0, y=0;
x = posX - x ; // ARE YOU SURE MEAN THIS - ITS WAYS GOING TO SUBTRACT 0
y = posY - y;
if(val==1)
{printf("X\n");
return x;}
if(val==2){printf("Y\n");
return y;}
else
return 0;
}



Will always do nothing of any use. Local variables x and y are always going to be posX and posY.

More worrying is this line in the render loop

//Sets startingpoint of little man
float posX=0.0, posY=0.0;

Here you hiding your globals vars with local vars. I suggest you don't do this.
remove the float declaration from the above line and see if this helps.


Share this post


Link to post
Share on other sites
JustusAa    122
I am sorry i just edited the text with code snippets so it looks nicer.
Forgot to say that void drawLittleMan() is in a completely different module called drawThings with .c and .h.
The posX and posY starts at 0,0 but I call changePos() when an button is pressed and sends new positions 300,300 for ex. the happenings is in another module for keyboardpresses that I didnt add but looks like this:


void keyboardFunc( unsigned char key, int x, int y )
{
switch ( key ) {
case 'w':
posy=400.0;
posx=400.0;
changePos(posx, posy);
break;


left out end of it as I can test it and it works..

That changes posX and posY, then I return it, the kode snippet you complain abouth is more of testing in progress.

In short I can set the littleman anywhere on screen to start but not move him once I did. I can change posX and posY how much I want then with no change of the little men on screen. Two of them are supposed to move ^^

[Edited by - JustusAa on April 23, 2008 7:12:11 AM]

Share this post


Link to post
Share on other sites
BionicBytes    154
In your startup code instead of:

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

use

glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);

Since, I bet every new project you will use the lines you have here. Anyway this wont solve the issue.

What happens if you try and simple put a specific set of transalate commands in before you call drawLittleGreenMen in the renderloop. Just want to check that you get expected values for that specific debug case.

Once your satisfied with the result when revert back to the use of your global vars. In your render loop - you still declare local vars by the same name. This is bad practice - remove or rename.

Share this post


Link to post
Share on other sites
JustusAa    122
Added GLUT_STENCIL (gonna read what it does now.. ;) )
Doesnt solve issue, abouth alfa channel I may add that later.

"What happens if you try and simple put a specific set of transalate commands in before you call drawLittleGreenMen in the renderloop."

I am atm drawing three little men at different positions, two very close to eatch other. This are the ones that should be possible to move by pressing keys.

Added
glTranslatef(0.01, 0.01, 0.0);




before the first push matrix and the three men started to move slowly to the right and up aswell. I must press I dont care much if the move around every update, I want to set the objects at specific cords every update, information that I get from a server.

"Once your satisfied with the result when revert back to the use of your global vars. In your render loop - you still declare local vars by the same name. This is bad practice - remove or rename."
I presume u talking abouth drawLittleMan() thats is a member of drawItems.h so they dont share global variables. Sure can change the names for variables. Other code is graficsengineglut.c and .h to explain.

forget abouth
void changePos(float f_posx, float f_posy)
int returnPos(int val)
I will design another system to change posX and Y. If it works I will hit myself with a book.

[Edited by - JustusAa on April 23, 2008 8:55:51 AM]

Share this post


Link to post
Share on other sites
JustusAa    122
SOLVED

I am sorry for taking up your time. Problem was this:
My keyboard reader was supposed to send a value:

case 'w':
posy=400.0;
posx=400.0;
printf("hej\n");
changePos(posx, posy);

for ex. 400 to my module that draw the objects. By the help of BionicBytes I got in the right direction with my debugging.

void changePos(float f_posx, float f_posy)
{
printf("%f %f\n", f_posx, f_posx);


printed 0.000 0.0000 all the time. So I found my error somewhere I didnt expect, guess it is sometimes like this when you are new to an api. Its strange though that changePos was run from the keyboard reader and no values was sent. Although thats a feature i dont need.

Share this post


Link to post
Share on other sites
BionicBytes    154
Software dev is always like this. I always try to create simple UnitTest() methods on new software modules so i have a degree of confidence before i move on to the next bit.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now