gpt4 book ai didi

android - 帮助纠正 Android OpenGL 2.0 w/QCAR 中的线缩放

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:55 25 4
gpt4 key购买 nike

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

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

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

谢谢

// 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。顶点属性数组的实现以及渲染它们的方法似乎与我通过引用资源找到的其他示例相同。

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

#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();
}

关于可能导致此问题的原因有什么想法吗?

最佳答案

在您的顶点着色器中,您只需将顶点位置传递给光栅化器,而无需通过模型 View 或投影矩阵来转换它们。尽管这是完全有效的,但您仍然需要注意坐标所在的范围。

在顶点处理阶段之后,你的坐标必须在 [-1,1]-立方体中,那里的所有东西都被剪掉,然后这个立方体被视口(viewport)变换转换到屏幕空间,例如[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]-立方体,然后通过视口(viewport)转换将其转换为以像素为单位的真实屏幕空间(无论哪个值你给了 glViewport)。

关于android - 帮助纠正 Android OpenGL 2.0 w/QCAR 中的线缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6912918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com