我已经从 developer.Apple 网站下载了示例代码GLPaint,以使用 OpenGL 在 Canvas 上绘制图片。
我对 GLPaint 应用程序进行了许多更改以满足我的要求。现在,我想将绘制的项目作为图像保存到照片库中。
我知道在照片库中保存图像的方法。所以,我尝试在绘制图片后创建相应的图像文件。你知道这样做的好方法是什么吗?对此的任何帮助表示高度赞赏。
代码细节描述如下。
绘画视图.h
EAGLContext *context;
// OpenGL names for the renderbuffer and framebuffers used to render to this view
GLuint viewRenderbuffer, viewFramebuffer;
// OpenGL name for the depth buffer that is attached to viewFramebuffer, if it exists (0 if it does not exist)
GLuint depthRenderbuffer;
GLuint brushTexture;
CGPoint location;
CGPoint previousLocation;
Run Code Online (Sandbox Code Playgroud)
PaintView.m
// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect bounds = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将自透明纹理渲染到帧缓冲区,但我没有得到我猜到的东西:先前在帧缓冲区上渲染的所有东西都被忽略了,这个纹理与我清理主画布的颜色混合在一起.
这就是我想要的,但不使用framebuffers:
package test;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
public class GdxTest extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
@Override
public void create () {
batch = new SpriteBatch();
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(1, 1, 1, 1);
pixmap.fillRectangle(0, 0, 1, 1);
// Generating a simple 1x1 white texture
img = new Texture(pixmap);
pixmap.dispose();
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setColor(1, 1, 1, 1);
batch.draw(img, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setColor(0, …Run Code Online (Sandbox Code Playgroud) 我有一个GLKMatrix4变量,并希望使用它的函数GLfloat *array值glUniformMatrix4fv.我用Google搜索但没有找到任何有用的信息.有一个CC3Matrix4x4PopulateFromGLKMatrix4在cocos3d SDK中调用的函数,但它回复的文件太多了.我真的不想用它.
有没有简单的方法来投GLKMatrix4来GLfloat* array?
所以我在手机上设置了一个简单的模拟.目标是在屏幕上显示带有各种透明度的红色,白色和蓝色圆圈.我有大部分工作,除了一件事,而透明度的工作,唯一的混合发生在黑色背景.结果,中心的圆圈显示为深红色,而不是在其下方显示白色圆圈.我究竟做错了什么?
注意我正在使用正交2d投影矩阵.所有对象z位置都相同,并以特定顺序呈现.
以下是我如何设置透明度的方法:
glEnable(GLenum(GL_DEPTH_TEST))
glEnable(GLenum(GL_POINT_SIZE));
glEnable(GLenum(GL_BLEND))
glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))
glEnable(GLenum(GL_POINT_SMOOTH))
//Note some of these things aren't compatible with OpenGL-es but they can hurt right?
Run Code Online (Sandbox Code Playgroud)
这是片段着色器:
precision mediump float;
varying vec4 outColor;
varying vec3 center;
varying float o_width;
varying float o_height;
varying float o_pointSize;
void main()
{
vec4 fc = gl_FragCoord;
vec3 fp = vec3(fc);
vec2 circCoord = 2.0 * gl_PointCoord - 1.0;
if (dot(circCoord, circCoord) > 1.0) {
discard;
}
gl_FragColor = outColor;//colorOut;
}
Run Code Online (Sandbox Code Playgroud)
以下是我将每个圆圈传递给着色器的方法:
func drawParticle(part: Particle,color_loc: GLint, size_loc: GLint) …Run Code Online (Sandbox Code Playgroud) 我是OpenGL的新手,我很期待比较两种纹理,以了解它们彼此之间的相似程度。我知道如何使用两个位图图像来实现此目的,但我确实需要使用一种方法来比较两个纹理。
问题是:当我们比较两个图像时,有什么方法可以比较两个纹理吗?像一个像素一个像素地比较两个图像?
我是一个在Swift中开发的iOS开发人员(非常少的经验).我的下一个项目是使用Open G1 ES制作一个双人tic tac toe游戏.Swift不是OpenGl ES开发的最佳语言选择,因此我开始学习Objective C.我正在阅读一本专门针对iOS的Open GL ES的书,目标C.在阅读本书时,他们有以下数据结构存储顶点,他们说这是一个数组.这是代码......
static const SceneVertex vertices[] = {{{-0.5f, -0.5f, 0.0}},{{ 0.5f, -0.5f, 0.0}},{{-0.5f, 0.5f, 0.0}}};
Run Code Online (Sandbox Code Playgroud)
这看起来不像Objective-C中的数组.这是在C?另外为什么数组的初始化有点奇怪.为什么用花括号而不是方括号来初始化数组?
我最近制作了第一个渲染立方体的OpenGL程序,我将其扩展到包括一些其他基本功能(旋转,缩放,平移,鼠标选择顶点).但是,我认为该程序使用的是某些版本的OpenGL ES xx,因为我无法使用glReadPixels和gl_depth_component来启用鼠标选择仅可见顶点.
为了解决这个问题,我修改了我的旧程序,在我的main函数中手动指定默认的表面格式为OpenGL 3.0版,但是现在程序在尝试创建GLWidget实例时会抛出以下错误.
宣扬: "QOpenGLFunctions ::将IsInitialized(应将d_ptr)"
我的主要功能(main.cpp):
#include "mainwindow.h"
#include <QApplication>
#include <QOpenGLFunctions>
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setSamples(16);
format.setDepthBufferSize(24);
format.setVersion(3,0);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat( format);
QApplication a(argc, argv);
MainWindow w; //breakpoint here - step into
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
进入这个断点会引导我进入我的MainWindow构造函数(mainwindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); //breakpoint here - step into
ui->GLwidget2->getmainwindowpointer(this);
connect(ui->actionTop,SIGNAL(triggered()),ui->GLwidget2,SLOT(settopview()));
connect(ui->actionRight, SIGNAL(triggered()), ui->GLwidget2, SLOT(setrightview()));
connect(ui->actionIso,SIGNAL(triggered()),ui->GLwidget2,SLOT(setisoview()));
//connect(ui->checkBox_perspective,SIGNAL(released()),ui->GLwidget2,SLOT(setperspective()));
connect(ui->checkBox_legend, SIGNAL(released()), ui->GLwidget2, SLOT(setlegend()));
connect(ui->checkBox_edges, SIGNAL(released()), ui->GLwidget2, SLOT(setedges()));
connect(ui->checkBox_faces, SIGNAL(released()), ui->GLwidget2, …Run Code Online (Sandbox Code Playgroud) 我正在使用OpenGL ES 2.0.显然,您可以使用glGetAttribLocation获取任何着色器变量的位置.这意味着您不需要将glGetUniformLocation用于统一变量,但是您不能以相反的方式执行此操作.为什么是这样?
编辑:
VS:统一mat4 uMvpMat; 属性vec4 vPos; void main(){gl_Position = uMvpMat*vPos; }
FS:精密介质浮动; 均匀的vec4 vColor; void main(){gl_FragColor = vColor; }
例如,glGetUniformLocation(programSolidHandle, "vColor")似乎相当于glGetAttribLocation(programSolidHandle, "vColor").但是,这不适用glGetUniformLocation(programSolidHandle, "vPos"),但不起作用.
我正在使用以下着色器源代码来实现顶点和片段.
顶点着色器源:
#define highp
attribute highp vec3 position;
uniform highp mat4 mvp;
void main(void)
{
gl_Position = mvp * vec4(position, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
片段着色器源:
#define highp
uniform highp vec3 color;
void main(void)
{
gl_FragColor = vec4(color, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
但是,着色器无法正常工作.如下面的屏幕截图所示,3D对象的颜色就像2D对象一样:
glGetShaderiv带有GL_COMPILE_STATUS返回success == TRUE,因此没有着色器编译错误.
glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
Run Code Online (Sandbox Code Playgroud)
我没有尝试glGetError()代码.我要去尝试一下.但我怀疑我没有收到任何OpenGL错误.
我相信我需要调整color顶点和片段着色器.我应该如何调整着色器源中的颜色?任何人都可以通过暗示来帮助我.到目前为止,我无法通过修改源来解决问题.
在@ Rabbid76 的帮助下,3D对象看起来很不错:
我正在使用@ Rabbid76代码进行一些小修改:将#version 130顶点和片段着色器源添加到顶部.看起来我的英特尔显卡需要该#version 130指令,否则会引发一些警告和错误:
警告:片段着色器中不支持扩展"GL_OES_standard_derivative"
该#version 130指令解决了上述警告及其后续错误.
正如我在opengl中看到的那样,坐标在-1和1之间,我想使用opengl渲染对象,我在像素坐标中渲染对象的第一个位置,但是我不知道如何将像素坐标转换为opengl坐标。我正在使用Java开发Android应用程序。
公共类FirstOpenGLProjectRenderer实现GLSurfaceView.Renderer {
private static final String A_POSITION = "a_Position";
private static final String A_COLOR = "a_Color";
private static final int POSITION_COMPONENT_COUNT = 4;
private static final int COLOR_COMPONENT_COUNT = 3;
private static final int BYTES_PER_FLOAT = 4;
private static final int STRIDE =
(POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT;
private final FloatBuffer vertexData;
private final Context context;
private int program;
private int aPositionLocation;
private int aColorLocation;
private static final String U_MATRIX = "u_Matrix";
private final float[] projectionMatrix = new float[16]; …Run Code Online (Sandbox Code Playgroud)