c++ - opengl draw in 2D coordinates instead of vertex coordinate system -
how can draw in 2d coordinates instead of vertex coordinate system, =>
drawpoint(50 , 100 , 0.01f);
this code , background texture , point
static void draw(void) { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); double w = glutget( glut_window_width ) / 300.0; double h = glutget( glut_window_height ) / 300.0; glmatrixmode(gl_projection); glloadidentity(); glmatrixmode(gl_modelview); glloadidentity(); glortho( -1 * w/2, 1 * w/2, -1 * h/2, 1 * h/2, w/2, -h/2); glbegin(gl_polygon); gltexcoord2f(0.0f, 0.0f); glvertex3f(-w/2.f, -h/2.f, 0.0f); gltexcoord2f(1.0f, 0.0f); glvertex3f( w/2.f, -h/2.f, 0.0f); gltexcoord2f(1.0f, 1.0f); glvertex3f( w/2.f, h/2.f, 0.0f); gltexcoord2f(0.0f, 1.0f); glvertex3f(-w/2.f, h/2.f, 0.0f); glend(); drawpoint(50 , 100 , 0.01f); glutswapbuffers(); }
the function drawpoint => draws circles
void drawpoint(glfloat x, glfloat y, glfloat radius){ gldisable(gl_lighting); gldisable(gl_texture_2d); int i; int triangleamount = 20; //# of triangles used draw circle //glfloat radius = 0.8f; //radius glfloat twicepi = 2.0f * 3.1415; glbegin(gl_triangle_fan); glcolor3f(1.0, 0.0, 0.0); glvertex2f(x, y); // center of circle for(i = 0; <= triangleamount;i++) { glvertex2f( x + (radius * cos(i * twicepi / triangleamount)), y + (radius * sin(i * twicepi / triangleamount)) ); } glend(); glenable(gl_lighting); glenable(gl_texture_2d); }
so don't know if have change drawpoint function.
update : main source
glutinitdisplaymode(glut_double | glut_rgb); glutinit(&argc, argv); glutinitwindowsize(widthx, heighty); glutcreatewindow("prg"); glutreshapefunc(resize); glutdisplayfunc(draw); glutkeyboardfunc(keypressed); glutkeyboardupfunc(keyup); texture[0] = soil_load_ogl_texture ( "img.jpg", soil_load_auto, soil_create_new_id, soil_flag_invert_y | soil_flag_ntsc_safe_rgb | soil_flag_compress_to_dxt ); glbindtexture(gl_texture_2d, texture[0]); glenable(gl_texture_2d); glclearcolor(0.0f, 0.0f, 0.0f, 0.5f); glenable(gl_point_smooth); glhint(gl_point_smooth_hint, gl_nicest); glhint(gl_perspective_correction_hint, gl_nicest); glutmainloop();
update 2 :
if in way impossible, there method transform x , y vector ?, example :
drawpoint(vectconvert(50),vectconvert(100),0.01f);
i not sure if right answer. edited code, try this:
static void draw(void) { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); // divided window width , height 300 seems wrong double w = glutget( glut_window_width ); double h = glutget( glut_window_height ); glmatrixmode(gl_projection); glloadidentity(); glmatrixmode(gl_modelview); glloadidentity(); // create ortho 2d // last 2 parameters near , far planes // since using 2d should keep them @ 0.0 , 1.0 glortho( -1 * w/2.0, 1 * w/2.0, -1 * h/2.0, 1 * h/2.0, 0.0f, 1.0f); // mirror y axis glscalef(1, -1, 1); // coordinate system starts @ center of window // need move left top corner gltranslatef(-(w/2.0f), -(h/2.0f), 0.0f); texture[0] = soil_load_ogl_texture // load image file directly new opengl texture ( "img.jpg", soil_load_auto, soil_create_new_id, soil_flag_invert_y | soil_flag_ntsc_safe_rgb | soil_flag_compress_to_dxt ); glbindtexture(gl_texture_2d, texture[0]); glbegin(gl_polygon); // need mirror y coordinates texture gltexcoord2f(0.0f, 1.0f); glvertex3f(0.0f , 0.0f, 0.0f); gltexcoord2f(1.0f, 1.0f); glvertex3f(w , 0.0f, 0.0f); gltexcoord2f(1.0f, 0.0f); glvertex3f(w , h , 0.0f); gltexcoord2f(0.0f, 0.0f); glvertex3f(0.0f , h , 0.0f); glend(); drawpoint(50 , 100 , 0.01f); glutswapbuffers(); }
also, not load texture file every frame. overkill gpu.
Comments
Post a Comment