帮助纠正Android OpenGL 2.0 w / QCAR中的行缩放

时间:2011-08-02 13:34:17

标签: android opengl opengl-es augmented-reality

我正在使用Android中的QCAR AR SDK,它使用OpenGL 2.0,我是2.0的新手。 QCAR SDK适用于基于CV的AR应用程序,并利用OpenGL渲染到图像上。

我只想在屏幕中央绘制一个小X,并使用以下代码。但是,不是将X绘制到正确的坐标,而是延伸到屏幕的边缘。无论我分配给顶点的值是什么,都会发生这种情况。我无法弄清楚这是一个缩放问题还是我正在使用的坐标系中的一些混乱。

关于为什么没有正确绘制这些线条的任何想法? - 我知道这在1.1中会更容易,但我必须使用2.0。

日Thnx

// Clear color and depth buffer 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);


    GLfloat diagVertices[12];

    diagVertices[0] = -10;
    diagVertices[1] = -10;
    diagVertices[2] = 0.0f;

    diagVertices[3] = 10;
    diagVertices[4] = 10;
    diagVertices[5] = 0.0f;

    diagVertices[6] = -10;
    diagVertices[7] = 10;
    diagVertices[8] = 0.0f;

    diagVertices[9] = 10;
    diagVertices[10] = -10;
    diagVertices[11] = 0.0f;

    glUseProgram(diagonalShaderProgramID);
// map the border vertices
    glVertexAttribPointer(diagVertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &diagVertices[0]);
// draw it
glEnableVertexAttribArray(diagVertexHandle);

glLineWidth(3.0f);
glDrawArrays(GL_LINES, 0, 4);
glDisableVertexAttribArray(diagVertexHandle);

这是我正在使用的着色器..

static const char* diagLineMeshVertexShader = " \
  \
attribute vec4 vertexPosition; \
 \
void main() \
{ \
   gl_Position = vertexPosition; \
} \
";

static const char* diagLineFragmentShader = " \
 \
precision mediump float; \
 \
void main() \
{ \
   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \
} \
";

更新

所以我使用Eclipse和Cygwin在Windows 7(64)中建立了一个构建环境,并测试了相同的方法 - 绘制顶点属性数组。代码库源自演示GSLS的简单lighthouse3D示例。我编译并运行该示例以确认它是按预期呈现的。然后我实现了顶点数组,如上所述。我看到了完全相同的问题。无论顶点值如何,这些线都会延伸到窗口的边缘。

这适用于GL_VERSION 2.1.2。顶点属性数组的实现以及渲染它们的方法看起来与我通过参考资源找到的其他示例相同。

这是代码.. - 我已经注释掉了我改变过的lights3d代码部分。

#define WIN32

#include <stdio.h>
#include <stdlib.h>

#include <GL/Glee.h>
#include <GL/glut.h>
#include "textfile.h"


GLuint v,f,f2,p;
float lpos[4] = {1,0.5,1,0};
GLfloat crossVertices[12];
GLint lineVertexHandle = 0;

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
    h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
//gluPerspective(45,ratio,1,1000);
gluPerspective(45,ratio,1,10);
glMatrixMode(GL_MODELVIEW);


}


void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
          0.0,0.0,0.0,
          0.0f,1.0f,0.0f);

glLightfv(GL_LIGHT0, GL_POSITION, lpos);
//glutSolidTeapot(1);

// map the border vertices
glVertexAttribPointer(lineVertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &crossVertices[0]);
glEnableVertexAttribArray(lineVertexHandle);


glLineWidth(1.0f);
glDrawArrays(GL_LINES, 0, 4);
glDisableVertexAttribArray(lineVertexHandle);
glutSwapBuffers();


}

void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27) 
    exit(0);
}


void setShaders() {

char *vs = NULL,*fs = NULL,*fs2 = NULL;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
f2 = glCreateShader(GL_FRAGMENT_SHADER);


vs = textFileRead("toon.vert");
fs = textFileRead("toon.frag");
fs2 = textFileRead("toon2.frag");

const char * ff = fs;
const char * ff2 = fs2;
const char * vv = vs;

glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
glShaderSource(f2, 1, &ff2,NULL);

free(vs);free(fs);

glCompileShader(v);
glCompileShader(f);
glCompileShader(f2);

p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,f2);
glAttachShader(p,v);

glLinkProgram(p);
glUseProgram(p);
}

void defineVertices(){
crossVertices[0]=       10.0f;
crossVertices[1]=0.0f;
crossVertices[2]=0.0f;
crossVertices[3]=       -1 * 10.0f;
crossVertices[4]=0.0f;
crossVertices[5]=0.0f;
crossVertices[6]=0.0f;
crossVertices[7]=       10.0f;
crossVertices[8]=0.0f;
crossVertices[9]=0.0f;
crossVertices[10]=      -1 * 10.0f;
crossVertices[11]=0.0f;
}


int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("MM 2004-05");

glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(processNormalKeys);

glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glEnable(GL_CULL_FACE);
    /*
glewInit();
if (glewIsSupported("GL_VERSION_2_0"))
    printf("Ready for OpenGL 2.0\n");
else {
    printf("OpenGL 2.0 not supported\n");
    exit(1);
}
*/
setShaders();
defineVertices();

glutMainLoop();

// just for compatibiliy purposes
return 0;
}

这里是顶点着色器,来自lighthouse3D示例......

varying vec3 normal, lightDir;

void main()
{   
lightDir = normalize(vec3(gl_LightSource[0].position));
normal = normalize(gl_NormalMatrix * gl_Normal);

gl_Position = ftransform();
}

关于可能造成这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

在顶点着色器中,您只需将顶点位置传递到光栅化器,而无需通过模型视图或投影矩阵对其进行变换。虽然这是完全有效的,但您仍需要关注坐标所在的范围。

在顶点处理阶段之后,你的坐标必须在[-1,1] - 立方体中,那里的所有东西都被剪掉,然后这个立方体被视网膜变换转换成屏幕空间,例如: [0,w]x[0,h]x[0,1]。所以你的坐标范围从-10到10,所以你的线实际上是屏幕尺寸的10倍。如果您的意思是像素,则应该在顶点着色器中将x,y值从[-w/2,w/2]x[-h/2,h/2]缩放到[-1,1]

这是您提供的桌面GL项目中的相同问题,您在着色器中调用ftransform,但您的投影矩阵是一个简单的透视矩阵,不会大幅缩小您的坐标。因此,如果您希望线坐标为像素,则在此项目中将gluPerspective的调用替换为glOrtho(-0.5*w, 0.5*w, -0.5*h, 0.5*h, -1.0, 1.0)

并且还要记住,默认情况下,OpenGL中的y从下到上。因此,如果您希望它的行为方式不同(许多图像处理框架都会这样做),那么您也必须在顶点着色器中否定y坐标(同样在glOrtho调用中交换第3和第4个值。另一个项目)。但请记住,这将反转您渲染的任何三角形的方向(如果有的话)。

例如,在您的顶点着色器中,只需执行以下操作:

uniform vec2 screenSize;           //contains the screen size in pixels
attribute vec2 vertexPosition;     //why take 4 if you only need 2?

void main()
{
    gl_Position = vec4(2.0*vertexPosition/screenSize, 0.0, 1.0);
}

这为您提供了一个以像素为单位的坐标系,原点位于中心,y轴从底部到顶部。如果这不是你想要的,那么随意玩转换(它也可以通过CPU上的预先计算2.0/screenSize进行优化。但请记住,在顶点着色器之后,屏幕空间实际上是[-1,1] - 立方体,然后通过视口转换将其转换为实际屏幕空间(以glViewport为准) )。