首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用OpenGL中以像素为单位的坐标绘制直线

用OpenGL中以像素为单位的坐标绘制直线
EN

Stack Overflow用户
提问于 2014-01-29 20:45:31
回答 1查看 2.9K关注 0票数 1

我正在尝试创建一个400x300屏幕,其中有一条从点(180,15)到点(10,145)的线。下面是我创建行的显示函数:

代码语言:javascript
复制
@Override
public void display(GLAutoDrawable drawable) {
  GL2 gl = drawable.getGL().getGL2();  // get the OpenGL 2 graphics context
  gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
  gl.glLoadIdentity();  // reset the model-view matrix


  gl.glColor3f(1.0f, 0.0f, 0.0f);  //set color of line
  gl.glTranslatef(0.0f, 0.0f, -6.0f); // translate into the screen
  gl.glBegin(GL_LINES); 
    gl.glVertex2f(180, 15); //specify line-segment geometry must use relative values
    gl.glVertex2f(10,145);
  gl.glEnd();

  gl.glFlush();
}

这是init函数,我首先定义屏幕的正交参数:

代码语言:javascript
复制
@Override
public void init(GLAutoDrawable drawable) {
    GL2 gl = (GL2) drawable.getGL();    // get the OpenGL graphics context
    drawable.setGL(new DebugGL2(gl));   //set debugger. This will come in handy.
    glu = new GLU();                         // get GL Utilities
    gl.glClearDepth(1.0f);      // set clear depth value to farthest
    gl.glEnable(GL_DEPTH_TEST); // enables depth testing
    gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // sets base color for glClear()
    gl.glDepthFunc(GL_LEQUAL);  // the type of depth test to do
    gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
    gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting

    //orthographic parameters set here
    glu.gluOrtho2D(0, 200, 0, 150);

}

这是一种重塑功能(据我所知),每当调整屏幕大小时,就会调用它。

代码语言:javascript
复制
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();  // get the OpenGL 2 graphics context

    if (height == 0) height = 1;   // prevent divide by zero
    float aspect = (float)width / height;
    glu = new GLU();

    // Set the view port (display area) to cover the entire window
    gl.glViewport(0, 0, width, height);

    // Setup perspective projection, with aspect ratio matches viewport
    glu.gluOrtho2D(0, 200, 0, 150); //set 2-D orthographic context

    glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar


    // Enable the model-view transform
    gl.glMatrixMode(GL_MODELVIEW);
    gl.glLoadIdentity(); // reset

}

我也曾尝试使用:

代码语言:javascript
复制
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(0,200,150,0,-1,1);

代码语言:javascript
复制
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(-1,1,-1,1,-1,1);

使用相对比例尺然而,我所做的一切似乎都没有用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-30 08:21:46

  1. 将要绘制的窗口中的区域或视图设置为 gl.glViewport(0,0,宽度,高度);//单位像素
  2. 设置投影,使您的OpenGL坐标以像素为单位(匹配视口的宽度/高度) //我们希望修改投影矩阵(如果没有这种情况,网格法线将打破) gl.glMatrixMode(GL_PROJECTION);//清除以前任何投影矩阵可能包含的转换(否则它将与下面的glOrtho矩阵组合) gl.glLoadIdentity();//设置投影(可以使用glTranslate/glScale,但这个实用函数更简单) gl.glOrtho(0、宽度0、0、高度-1、1);//左、右、下、上、前、后/将模型视图作为当前编辑gl.glMatrixMode(GL_MODELVIEW)的矩阵; 现在,查看卷是由glOrtho定义的框。 注意,(0, 0)当前位于左下角。如果您正在做任何GUI内容,那么在我们从左到右、从下读时,翻转这个并使原点左上角非常有用。没有什么能阻止你在绘制过程中改变一半--用一些任意的坐标系做一些3D的事情,然后画一个HUD并设置投影来匹配像素。
  3. 在你的显示功能中划出你的界线..。 gl.glBegin(GL_LINES);gl.glVertex2f(180,15);gl.glVertex2f(10,145);gl.glEnd(); 或者,演示翻译的使用 gl.glLoadIdentity();//从前面的gl.glTranslatef调用gl.glTranslatef(180,15)中删除模型视图矩阵中的以下转换;//修改模型视图矩阵gl.glBegin(GL_LINES);gl.glVertex2f(0,0);gl.glVertex2f(-170,130);gl.glEnd(); 您可以认为转换将视图向下和向左移动,或者将模型(行)向上和向右移动。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21442242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档