- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我遵循了谷歌的这个教程:http://developer.android.com/resources/tutorials/opengl/opengl-es20.html
三角形应该旋转以响应触摸事件,但没有任何反应。当它应该自己旋转时,它在前面的步骤中也没有旋转。不确定是什么问题。我只是复制了他们的代码。
整个项目只有两个文件:
HelloOpenGLES20Activity.java
package com.opengl.es20;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
public class HelloOpenGLES20Activity extends Activity {
private GLSurfaceView mGLView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity
mGLView = new HelloOpenGLES20SurfaceView(this);
setContentView(mGLView);
}
@Override
protected void onPause() {
super.onPause();
// The following call pauses the rendering thread.
// If your OpenGL application is memory intensive,
// you should consider de-allocating objects that
// consume significant memory here.
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
// The following call resumes a paused rendering thread.
// If you de-allocated graphic objects for onPause()
// this is a good place to re-allocate them.
mGLView.onResume();
}
}
class HelloOpenGLES20SurfaceView extends GLSurfaceView {
private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private HelloOpenGLES20Renderer mRenderer;
private float mPreviousX;
private float mPreviousY;
public HelloOpenGLES20SurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(2);
// set the mRenderer member
mRenderer = new HelloOpenGLES20Renderer();
setRenderer(mRenderer);
// Render the view only when there is a change
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
// reverse direction of rotation above the mid-line
if (y > getHeight() / 2) {
dx = dx * -1 ;
}
// reverse direction of rotation to left of the mid-line
if (x < getWidth() / 2) {
dy = dy * -1 ;
}
mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR;
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
}
HelloRendererOpenGLES20Renderer.java
package com.opengl.es20;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;
public class HelloOpenGLES20Renderer implements GLSurfaceView.Renderer {
private FloatBuffer triangleVB;
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix; \n" +
"attribute vec4 vPosition; \n" +
"void main(){ \n" +
" gl_Position = uMVPMatrix * vPosition; \n" +
"} \n";
private final String fragmentShaderCode =
"precision mediump float; \n" +
"void main(){ \n" +
" gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
"} \n";
private int mProgram;
private int maPositionHandle;
private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mMMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
public float mAngle;
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
// Set the background frame color
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
initShapes();
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // creates OpenGL program executables
// get handle to the vertex shader's vPosition member
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// Prepare the triangle data
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, triangleVB);
GLES20.glEnableVertexAttribArray(maPositionHandle);
// Create a rotation for the triangle
// long time = SystemClock.uptimeMillis() % 4000L;
// float angle = 0.090f * ((int) time);
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
// Apply a ModelView Projection transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width/height;
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
}
private void initShapes(){
float triangleCoords[] = {
// X, Y, Z
-0.5f, -0.25f, 0,
0.5f, -0.25f, 0,
0.0f, 0.559016994f, 0
};
// initialize vertex Buffer for triangle
ByteBuffer vbb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
triangleCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
triangleVB = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer
triangleVB.put(triangleCoords); // add the coordinates to the FloatBuffer
triangleVB.position(0); // set the buffer to read the first coordinate
}
private int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
最佳答案
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
// Apply a ModelView Projection transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
我相信这个最后的 multiplyMM 是你的问题。您正在将 mMVPMatrix 重置为mMVPMatrix = mProjMatrix * mVMatrix,并且您正在丢失将应用旋转的模型转换矩阵 mMMatrix。此外,验证 mAngle 是否为非零。
关于android - 三角形不会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9725688/
SDL3 提供了 SDL_RenderGeometry 函数绘制几何图形,用法和 OpenGL 差不多,先定义顶点数据,然后根据顶点数据绘制几何图形。 绘制三角形的代码如下: std::array
我想在图像上使用三角形类型按钮,但我无法执行此操作... 如何做到这一点? 最佳答案 这个project可以帮你。您可以自定义 UIButton 的形状。 关于iphone - 定制非矩形按钮 三角形
我一直在尝试找出如何使用 Python 制作彩虹三角螺旋。我可以制作一个方形螺旋,但它不会导入颜色。而且它不使用三角形。 输出应该是什么样子: 我取得的成就: 我的代码: import tur
我正在使用 this 研究三角形检测算法文章。我编写了这段代码,但不幸的是,当三角形之间存在交集时,该方法返回 false。 private boolean checkTriangleCollisio
我在资源文件中找到了几个关于如何在 Android 中绘制三角形的答案。但是我没有找到任何可以解释如何更改三角形旋转的内容。 我找到的例子:
对于编码类(class)中的作业,我应该找到一种方法让 Python 制作星号三角形,如下所示: x xx xxx 但是,无论我用我的代码做什么,我都无法做到这一点。最好的我可以得到的是:
我在绘制两个多边形时遇到问题。我想填充两个三角形,但一个大于第二个。我在 winforms 中使用 UserControl。代码: Point[] DOWN = new Point[] {new Po
如何测试三角形和正方形是否相交? 当我们知道它是正方形而不是矩形时,有什么方法可以优化它吗?此外,正方形是轴对齐的,这样应该可以进一步提升性能? 或者我应该把正方形分成三角形,然后对三角形-三角形相交
我有一个方法是画一个多边形,然后将多边形向右旋转90度,使其原来的顶点现在指向右边。 这是绘制多边形(三角形)的代码,但我不知道如何旋转它。 Point[] points = new Point[3]
我知道有高效的多边形裁剪算法(例如 Maillot、Vatti、Greiner-Hormann)。然而,这些算法适用于任意多边形,尽管它们适合我的情况,但在我看来,对像我这样的简单情况使用这种通用算法
我的问题可能很愚蠢,但我没有找到三角形 strip 使用的好例子: http://i.stack.imgur.com/KL8jk.png 像这样的顶点: A: -0.5f, -0.5f, // Bo
我正在尝试创建一个等边三角形,您可以在 fiddle 中看到它: 我的想法是,我将笔放在 (0, 0) 处,然后在 (20, 11) 处画线,但三角形看起来不正确。 最佳答案 您的三角形已被
通过编写一些逻辑代码,只是无法弄清楚如何以所需的形式获得 01 三角形的输出,三角形确实打印出来,但不是根据要求的输出。 import java.util.Scanner; import java
我一直在尝试制作一个简单的 pygame 程序来检查光标是否在三角形内部或外部。我通过找到较大三角形的面积,然后从鼠标位置到所有三个点制作三个内部三角形并找到它们的面积来完成此操作。 根据我的理解,如
我有一个方法 drawTriangle,它在 JAVA 中的 OpenGL 程序的 display() 方法中被引用。 public void drawTriangle(GL gl, int x1,
我正在尝试用 C++ 创建一个程序,该程序将数字的三角形模式放入二维数组中。 示例: 1 3 4 5 9 2 9 4 6 1 顶行是一个数字(整数),三角形的每一行比它上面的行多一
所以我最近一直在尝试学习 OpenGL,遵循了几个文本和视频教程。 我无法绘制三角形,我已经双重和三次检查我是否以正确的顺序执行了所有必要的步骤,但我显然遗漏了一些东西 在添加一些代码之前,我应该声明
我遇到了一个用递归绘制谢尔宾斯基三角形的程序。我如何解释这段代码是调用 sierpinski1 直到 n == 0,然后只绘制 3 个小三角形(每次调用一个三角形),因为 n == 0 是绘制某些东西
我有一个需要 3 个点的函数,我将使用这些点来绘制一个三角形,就好像我在使用 glVertex 函数一样。 但由于我想在避免透视变形的同时对这个三角形进行纹理贴图,我必须对其进行 segmentati
下面的代码应该为三角形添加一个 3d 对象,但我收到错误 Assets/Scripts/MakeTriangle.cs(6,28):错误 CS0120:需要对象引用才能访问非静态成员 `UnityEn
我是一名优秀的程序员,十分优秀!