- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 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/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!