我正在使用Lazy Foo的教程.我发现glPushMatrix()和glPopMatrix()函数非常混乱.我需要有人来解释如何存储矩阵,如何激活其中一个(如果这就是你所说的)以及来自Lazy Foo网站的代码:
void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );
//Pop default matrix onto current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
//Save default matrix again
glPushMatrix();
Run Code Online (Sandbox Code Playgroud)
"打开"然后恢复矩阵服务的目的是什么?
我正在开发一款3D游戏,但已经进行了首次测试之一,只有很少的计算,我得到3或4 fps左右.以下是我的完整代码:http://pastebin.com/j2DWPS6Z 这是我在主代码中使用的Terrain.cpp文件:http://pastebin.com/d1gnE5KH
看看我用于绘图的代码,我只绘制了400个多边形.据我所知,不应该将fps降低到3到4 fps左右.
我使用的计算机是HP Elitebook 8570w,8GB内存和Intel内核i7,所以这不是问题所在.
有谁知道我做错了导致fps这么低?
我试图将 GLSurfaceView 与其他 UI 元素一起设置在 xml 布局上,但在 LogCat 中不断出现错误,导致类 com.vi.cubo01.MyGLSurfaceView 膨胀。
这是java代码:
super.onCreate(savedInstanceState);
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie)
setContentView(R.layout.activity_main);
}
@Override
protected void onResume()
{
super.onResume();
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie);
mGLSurfaceView.onResume();
}
@Override
protected void onPause()
{
super.onPause();
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.vistaGLSuperficie);
mGLSurfaceView.onPause();
}
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
setEGLContextClientVersion(2);
setRenderer(new CustomRenderer());
}
}
Run Code Online (Sandbox Code Playgroud)
和 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" …Run Code Online (Sandbox Code Playgroud) 我正在使用 GLEW 和 GLFW。我的程序构建成功,但是当它运行时,我得到的只是黑屏。我知道这不是着色器代码的问题,因为我已经在 QT 中测试了 installShaders 函数并且它运行良好,所以我假设我错误地使用了 GLFW。
#include <stdio.h>
#include <stdlib.h>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
GLFWwindow* window;
extern const char* vertexShaderCode;
extern const char* fragmentShaderCode;
void sendDataToOpenGL()
{
GLfloat verts[] =
{
+0.0f, +1.0f,
+1.0f, +0.0f, +0.0f,
-1.0f, -1.0f,
+1.0f, +0.0f, +0.0f,
+1.0f, -1.0f,
+1.0f, +0.0f, +0.0f,
};
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (char*)(sizeof(float) * 2)); …Run Code Online (Sandbox Code Playgroud) 所以Oculus Rift SDK接受使用glFramebufferTexture函数创建的帧缓冲纹理ID .
例:
[...]
GLuint l_FBOId;
glGenFramebuffers(1, &l_FBOId);
glBindFramebuffer(GL_FRAMEBUFFER, l_FBOId);
// The texture we're going to render to...
GLuint l_TextureId;
glGenTextures(1, &l_TextureId);
// "Bind" the newly created texture : all future texture functions will modify this texture...
glBindTexture(GL_TEXTURE_2D, l_TextureId);
// Give an empty image to OpenGL (the last "0")
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, l_TextureSize.w, l_TextureSize.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
// Linear filtering...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Create Depth Buffer...
GLuint l_DepthBufferId;
glGenRenderbuffers(1, &l_DepthBufferId);
glBindRenderbuffer(GL_RENDERBUFFER, …Run Code Online (Sandbox Code Playgroud) 我有点惊讶.我一直在调试我的代码几个小时,而GLM似乎放弃了我.我正在努力完成以下两个实例:
....
cout << "multiplying A:" << endl;
displayMatrix(node->wMatrix);
cout << "and B:" << endl;
displayMatrix((node->children)[i]->wMatrix);
//switch order!
mat4 temp = (node->children)[i]->wMatrix * node->wMatrix;
cout << "Get result as:" << endl;
displayMatrix(temp);
...
Run Code Online (Sandbox Code Playgroud)
displayMatrix方法如下:
void displayMatrix(mat4 &m)
{
cout << m[0][0] << " " << m[0][1] << " " << m[0][2] << " " << m[0][3] << endl;
cout << m[1][0] << " " << m[1][1] << " " << m[1][2] << " " << m[1][3] << endl;
cout << …Run Code Online (Sandbox Code Playgroud) 我刚开始在我的项目中使用调试扩展,但是 glObjectLabel 在与 GL_BUFFER 一起使用时会产生错误。
显卡是带有 340.82 驱动程序的 nVidia Quadro 600
这个简单的测试是用 32 位的 MSVC 2010 构建的:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
void GLAPIENTRY ogl_cb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
using namespace std;
cout << "message: "<< message << endl;
}
int main()
{
using namespace std;
if (!glfwInit())
{
cerr << "Error initializing GLFW" << endl;
return 1;
}
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
GLFWwindow *window = glfwCreateWindow(1, 1, "", …Run Code Online (Sandbox Code Playgroud) 我按照指南绘制了2D的Lorenz系统.
我现在想要扩展我的项目并从2D切换到3D.据我所知,我必须用gluPerspective或glFrustum替换gluOrtho2D调用.不幸的是,无论我尝试什么都没用.这是我的初始化代码:
// set the background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
/// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);*/
// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 0.02f);
// enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// enable point smoothing
glEnable(GL_POINT_SMOOTH);
glPointSize(1.0f);
// set up the viewport
glViewport(0, 0, 400, 400);
// set up the projection matrix (the camera)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);
gluPerspective(45.0f, 1.0f, 0.1f, 100.0f); //Sets the frustum to perspective mode
// set …Run Code Online (Sandbox Code Playgroud) 我很确定这是因为我对GL_MODELVIEW矩阵的工作方式缺乏了解.这是一个屏幕录制正在发生的事情:http://youtu.be/3F7FLkVI7kA
如您所见,最底部的三角形是第一个绘制的三角形,并且随着我预期其他两个三角形移动而移动.第二个三角形相对于第一个三角形移动和旋转,第三个三角形相对于该组合移动和旋转.
我想要的是所有三个三角形在3D空间中是静止的,但是旋转(就像第一个三角形).
资源:
// Main loop
do {
// Clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update camera
glfwGetCursorPos(window, &cursorX, &cursorY);
cam.update(0.001f, (int)cursorX, (int)cursorY);
// Reset Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// move camera
glRotatef(cam.rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(cam.rotation.y, 0.0f, 1.0f, 0.0f);
// translate modelview matrix to position of the camera - everything should now draw relative to camera position
glTranslatef(-cam.position.x, cam.position.y, -cam.position.z);
// Draw ground
drawGroundGrid(-25.0f);
drawSpinningTriangle(0.0f, 0.0f, -5.0f);
drawSpinningTriangle(3.14f, 3.0f, -6.0f);
drawSpinningTriangle(-6.0f, 12.0f, -5.0f);
// Swap buffers …Run Code Online (Sandbox Code Playgroud) 我试图实现位图字体渲染.唯一令我困惑的是,我必须在这里问一下,当我为每个焦点绘制四边形时,每次渲染一个新的四边形时,UV都绝对会被破坏.如果我只渲染一个四边形,一切都OK.
(瓦片x = 3,y = 4)
这是多少字母的样子.

这是我的代码来渲染这些东西:
...
rc.translate(getTransform().getPos());
drawTileAt(3, 4);
rc.translate(100, 0, 0);
drawTileAt(5, 5);
rc.translate(200, 0, 0);
drawTileAt(2,6);
rc.translate(300,0,0);
drawTileAt(1, 7);
...
public void drawTileAt(int x, int y)
{
float w = 1f / 16f;
float _x = 1f / 16f * x;
float _y = 1f / 16f * y;
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(0, 100);GL11.glTexCoord2f(_x,_y);
GL11.glVertex2f(0, 0);GL11.glTexCoord2f(_x + w,_y);
GL11.glVertex2f(100,0);GL11.glTexCoord2f(_x + w,_y + w);
GL11.glVertex2f(100, 100);
GL11.glTexCoord2f(_x,_y + w);
GL11.glEnd();
}
Run Code Online (Sandbox Code Playgroud)
我也用tex-coords绘制VBO四边形,一切看起来都很好......