gpt4 book ai didi

java - 我如何从一个文件中的类调用以使用着色器显示在另一个包含 gl 代码的文件中(这两个文件都在 jni 文件夹中)?

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:12 27 4
gpt4 key购买 nike

如何在 .cpp 文件的 glVertexArttribPointer 中添加我的立方体?

我有一个类 Cube 和另一个类 Group,它们是另一个类 Mesh 的子类。我的多维数据集类在另一个文件->group.cpp 中,我们在其中添加多维数据集。 Group是Mesh类的子类,Cube也是Mesh类的子类。gl 代码在 newproj.cpp 中,其中行-> glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);显示三角形,因为三角形的顶点在同一个文件中指定。那么如何通过 gl 代码显示立方体的顶点和索引呢?我是着色器新手,所以我不太了解如何使用着色器进行显示。请指导我如何处理需要添加和删除多维数据集的 Group 类。

附言我正在 Eclipse + Ubuntu 上使用 OpenGL ES 2.0 开发项目。我想做的是转换我在本教程中找到的代码 (http://blog.jayway.com/2010/02/15/opengl-es-tutorial-for-android-%E2%80%93 -part-v/) 使用 JNI 转换为 C++

这是我在 jni 中 newproj.cpp 中的代码:

#include<string.h>
#include<jni.h>

#include<myheader.h>
#include<android/log.h>

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define LOG_TAG "libgl2jni"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

//Cube c;
//Mesh m;
struct SMatrixStack {
float lpfMatrix[16];
float lpfInverse[16];
bool bInverseUpToDate;
SMatrixStack *lpsmsNext;
};
class CMatrix {
public:
CMatrix():lpsmsNext(NULL), bInverseUpToDate(false), bNormalUpToDate(false), bInverseNormalUpToDate(false) {LoadIdentity();};
~CMatrix() {SMatrixStack *tmp; while (lpsmsNext) {tmp = lpsmsNext; lpsmsNext = lpsmsNext->lpsmsNext; delete tmp;}};

void LoadIdentity();
void Rotate(float angle, float x, float y, float z);
void Translate(float x, float y, float z);

private:
float lpfMatrix[16];
float lpfInverse[16];
bool bInverseUpToDate;
bool bNormalUpToDate;
bool bInverseNormalUpToDate;
SMatrixStack *lpsmsNext;
void Concatenate(float *m);
};

void CMatrix::LoadIdentity()
{
for(int i=0; i<16; i++)
{
lpfMatrix[i] = lpfInverse[i] = 0.0;
}

lpfMatrix[0] = lpfInverse[0] = 1.0;
lpfMatrix[5] = lpfInverse[5] = 1.0;
lpfMatrix[10]= lpfInverse[10]= 1.0;
lpfMatrix[15]= lpfInverse[15]= 1.0;

bInverseUpToDate = true;
}

void CMatrix::Concatenate(float *m)
{
float tm[16];
int j;

for(j=0; j<16; j++) tm[j] = 0.0f;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0f;

for (int i=0; i<4; i++)
{
j = 4*i;
tm[j+0] = m[j]*lpfMatrix[0] + m[j+1]*lpfMatrix[4] + m[j+2]*lpfMatrix[8] + m[j+3]*lpfMatrix[12];
tm[j+1] = m[j]*lpfMatrix[1] + m[j+1]*lpfMatrix[5] + m[j+2]*lpfMatrix[9] + m[j+3]*lpfMatrix[13];
tm[j+2] = m[j]*lpfMatrix[2] + m[j+1]*lpfMatrix[6] + m[j+2]*lpfMatrix[10]+ m[j+3]*lpfMatrix[14];
tm[j+3] = m[j]*lpfMatrix[3] + m[j+1]*lpfMatrix[7] + m[j+2]*lpfMatrix[11]+ m[j+3]*lpfMatrix[15];
}
for (j=0;j<16;j++) lpfMatrix[j] = tm[j];
bInverseUpToDate = false;
}
void CMatrix::Rotate(float angle, float x, float y, float z)
{
float tm[16];
int j;
for(j=0; j<16; j++) tm[j] = 0.0f;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0f;

float c = cos(angle);
float s = sin(angle);
float v = 1.0f - c;


tm[0] = x*x*v + c;
tm[1] = x*y*v - z*s;
tm[2] = x*z*v + y*s;
tm[4] = x*y*v + z*s;
tm[5] = y*y*v + c;
tm[6] = y*z*v - x*s;
tm[8] = x*z*v - y*s;
tm[9] = y*z*v + x*s;
tm[10]= z*z*v + c;

Concatenate(tm);
}
void CMatrix::Translate(float x, float y, float z)
{
float tm[16];
int j;
for(j=0; j<16; j++) tm[j] = 0.0;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0;

tm[3] = x;
tm[7] = y;
tm[11]= z;

Concatenate(tm);
}

CMatrix cmobj1;

static void printGLString(const char *name, GLenum s) {
const char *v = (const char *) glGetString(s);
LOGI("GL %s = %s\n", name, v);
}

static void checkGlError(const char* op) {
for (GLint error = glGetError(); error; error
= glGetError()) {
LOGI("after %s() glError (0x%x)\n", op, error);
}
}

static const char gVertexShader[] =
"attribute vec4 vPosition;\n"
"void main() {\n"
" gl_Position = vPosition;\n"
"}\n";

static const char gFragmentShader[] =
"precision mediump float;\n"
"void main() {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
"}\n";

GLuint loadShader(GLenum shaderType, const char* pSource) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &pSource, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen) {
char* buf = (char*) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
LOGE("Could not compile shader %d:\n%s\n",
shaderType, buf);
free(buf);
}
glDeleteShader(shader);
shader = 0;
}
}
}
return shader;
}

GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
if (!vertexShader) {
return 0;
}

GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
if (!pixelShader) {
return 0;
}

GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char* buf = (char*) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
LOGE("Could not link program:\n%s\n", buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
}
}

glDeleteShader(vertexShader);
glDeleteShader(pixelShader);

return program;

}

GLuint gProgram;
GLuint gvPositionHandle;


bool setupGraphics(int w, int h) {
printGLString("Version", GL_VERSION);
printGLString("Vendor", GL_VENDOR);
printGLString("Renderer", GL_RENDERER);
printGLString("Extensions", GL_EXTENSIONS);

LOGI("setupGraphics(%d, %d)", w, h);
gProgram = createProgram(gVertexShader, gFragmentShader);
if (!gProgram) {
LOGE("Could not create program.");
return false;
}
gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
checkGlError("glGetAttribLocation");
LOGI("glGetAttribLocation(\"vPosition\") = %d\n", gvPositionHandle);

glViewport(0, 0, w, h);
checkGlError("glViewport");
return true;
}

const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f,0.5f, -0.5f };
//const GLfloat cubearray[]=c.Cube(2,2,2);

void renderFrame() {
static float grey;
grey += 0.01f;

glClearColor(grey, grey, grey, 1.0f);
checkGlError("glClearColor");
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
checkGlError("glClear");

// Replace the current matrix with the identity matrix
cmobj1.LoadIdentity();

// Translates 4 units into the screen.
cmobj1.Translate(0.0,0.0,-4.0);

glUseProgram(gProgram);
checkGlError("glUseProgram");
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
//glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, cubearray);
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(gvPositionHandle);
checkGlError("glEnableVertexAttribArray");
glDrawArrays(GL_TRIANGLES, 0, 3);
checkGlError("glDrawArrays");
// c.Cube(2,2,2);
//root.draw();
}

extern "C"
{
JNIEXPORT void JNICALL Java_newproj_pack_Tutcppmain_init(JNIEnv * env, jobject obj, jint width, jint height);
JNIEXPORT void JNICALL Java_newproj_pack_Tutcppmain_step(JNIEnv * env, jobject obj);

};

JNIEXPORT void Java_newproj_pack_Tutcppmain_init(JNIEnv * env, jobject obj, jint width, jint height)
{
setupGraphics(width, height);
}

JNIEXPORT void Java_newproj_pack_Tutcppmain_step(JNIEnv * env, jobject obj)
{
//OpenGLRenderer Renderobj();
renderFrame();
}

这是我的代码 group.cpp,它也在我的 jni 中:

#include<iostream>
#include <stdio.h> // Header File For Standard Input/Output

#include <stdlib.h>
#include <math.h>

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "string.h"
#include<vector>
using namespace std;
//#include<mesh.cpp>
//#include<myheader.h>

#include<iostream>
#include <stdio.h> // Header File For Standard Input/Output

#include <stdlib.h>
#include <math.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include "string.h"

struct SMatrixStack1 {
float lpfMatrix[16];
float lpfInverse[16];
bool bInverseUpToDate;
SMatrixStack1 *lpsmsNext;
};
class CMatrix1 {
public:
CMatrix1():lpsmsNext(NULL), bInverseUpToDate(false), bNormalUpToDate(false), bInverseNormalUpToDate(false) {LoadIdentity();};
~CMatrix1() {SMatrixStack1 *tmp; while (lpsmsNext) {tmp = lpsmsNext; lpsmsNext = lpsmsNext->lpsmsNext; delete tmp;}};

void LoadIdentity();
void Rotate(float angle, float x, float y, float z);
void Translate(float x, float y, float z);

private:
float lpfMatrix[16];
float lpfInverse[16];
bool bInverseUpToDate;
bool bNormalUpToDate;
bool bInverseNormalUpToDate;
SMatrixStack1 *lpsmsNext;
void Concatenate(float *m);
};

void CMatrix1::LoadIdentity()
{
for(int i=0; i<16; i++)
{
lpfMatrix[i] = lpfInverse[i] = 0.0;
}

lpfMatrix[0] = lpfInverse[0] = 1.0;
lpfMatrix[5] = lpfInverse[5] = 1.0;
lpfMatrix[10]= lpfInverse[10]= 1.0;
lpfMatrix[15]= lpfInverse[15]= 1.0;

bInverseUpToDate = true;
}

void CMatrix1::Concatenate(float *m)
{
float tm[16];
int j;

for(j=0; j<16; j++) tm[j] = 0.0f;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0f;

for (int i=0; i<4; i++)
{
j = 4*i;
tm[j+0] = m[j]*lpfMatrix[0] + m[j+1]*lpfMatrix[4] + m[j+2]*lpfMatrix[8] + m[j+3]*lpfMatrix[12];
tm[j+1] = m[j]*lpfMatrix[1] + m[j+1]*lpfMatrix[5] + m[j+2]*lpfMatrix[9] + m[j+3]*lpfMatrix[13];
tm[j+2] = m[j]*lpfMatrix[2] + m[j+1]*lpfMatrix[6] + m[j+2]*lpfMatrix[10]+ m[j+3]*lpfMatrix[14];
tm[j+3] = m[j]*lpfMatrix[3] + m[j+1]*lpfMatrix[7] + m[j+2]*lpfMatrix[11]+ m[j+3]*lpfMatrix[15];
}
for (j=0;j<16;j++) lpfMatrix[j] = tm[j];
bInverseUpToDate = false;
}
void CMatrix1::Rotate(float angle, float x, float y, float z)
{
float tm[16];
int j;
for(j=0; j<16; j++) tm[j] = 0.0f;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0f;

float c = cos(angle);
float s = sin(angle);
float v = 1.0f - c;

tm[0] = x*x*v + c;
tm[1] = x*y*v - z*s;
tm[2] = x*z*v + y*s;
tm[4] = x*y*v + z*s;
tm[5] = y*y*v + c;
tm[6] = y*z*v - x*s;
tm[8] = x*z*v - y*s;
tm[9] = y*z*v + x*s;
tm[10]= z*z*v + c;

Concatenate(tm);
}
void CMatrix1::Translate(float x, float y, float z)
{
float tm[16];
int j;
for(j=0; j<16; j++) tm[j] = 0.0;
tm[0] = tm[5] = tm[10] = tm[15] = 1.0;

tm[3] = x;
tm[7] = y;
tm[11]= z;

Concatenate(tm);
}


class Mesh
{
private:

// Flat Color
float rgba[4];
int isize;
int vsize;
int csize;

//CMatrix cmobj;
GLuint program;


public:

Mesh();
void draw();
float *IBuffer;
float *VBuffer;
float *CBuffer;

float x;
float y;
float z;

CMatrix1 cmobj;
protected:
void setVertices(float &vertices, int vsize);
void setIndices(short &indices, int isize);
void setColor(float red, float green, float blue, float alpha);
void setColors(float &colors, int csize);

};

Mesh::Mesh(){
int isize=-1;
float *p = rgba;

// Smooth Colors
*CBuffer = NULL;
*VBuffer=NULL;
*IBuffer=NULL;
float rgba[]={ 1.0f, 1.0f, 1.0f, 1.0f };
GLuint program = glCreateProgram();
}

void Mesh:: draw()
{
float rgba[]={ 1.0f, 1.0f, 1.0f, 1.0f };
float *vertices;
// Rotate params.
float rx = 0;

float ry = 0;

float rz = 0;

// Counter-clockwise winding.
glFrontFace(GL_CCW);
// Enable face culling.
glEnable(GL_CULL_FACE);
// What faces to remove with the face culling.
glCullFace(GL_BACK);

/*
static const char gVertexShader[] =
"attribute vec4 a_Color;\n"
"glVertexAttrib4f(a_Color, rgba[0], rgba[1], rgba[2], rgba[3]);"
"varying vec4 v_Color;\n"
"attribute vec4 a_Position;"
"void main() {\n"
" v_Color = a_Color;\n"
"a_Position = Projection * Modelview * Position;"
"gl_Position=a_Position;"

"}\n";
static const char gFragmentShader[] =
"precision mediump float;\n"
"varying vec4 v_Color;\n"
"void main() {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
"gl_FragColor=v_Color;\n"
"}\n";
*/
static const char gVertexShader[] =
"attribute vec4 vPosition;\n"
"void main() {\n"
" gl_Position = vPosition;\n"
"}\n";

static const char gFragmentShader[] =
"precision mediump float;\n"
"void main() {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
"}\n";
GLuint positionSlot = glGetAttribLocation(program, "vPosition");
glVertexAttrib4fv(0, rgba);

glEnableVertexAttribArray(positionSlot);

GLsizei stride = sizeof(vertices);
glVertexAttribPointer(positionSlot, 2, GL_FLOAT, GL_FALSE, stride, VBuffer);

//glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);

cmobj.Translate(x, y, z);
cmobj.Rotate(rx, 1, 0, 0);
cmobj.Rotate(ry, 0, 1, 0);
cmobj.Rotate(rz, 0, 0, 1);

// Point out the where the color buffer is.
glDrawElements(GL_TRIANGLES, isize,GL_UNSIGNED_SHORT, IBuffer);
// Disable the vertices buffer.
glDisableVertexAttribArray(positionSlot);
// Disable face culling.
glDisable(GL_CULL_FACE);

}


void Mesh::setVertices(float &vertices, int vsize)
{
for(int v=0;v<vsize;v++)
{
VBuffer[v]=vertices;
}
}

void Mesh::setIndices(short &indices, int isize)
{

for(int i=0;i<isize;i++)
{
IBuffer[i]=indices;
}
}

void Mesh::setColor(float red, float green, float blue, float alpha)
{
// Setting the flat color.
rgba[0] = red;
rgba[1] = green;
rgba[2] = blue;
rgba[3] = alpha;
}

void Mesh::setColors(float &colors, int csize)
{

for(int c=0; c<csize;c++){
CBuffer[c]=colors;
}
}


class Cube : Mesh
{
public:
Cube(float width, float height, float depth);
};


Cube::Cube(float width, float height, float depth) {
width /= 2;
height /= 2;
depth /= 2;

float vertices[] =
{
-width, -height, -depth, // 0
width, -height, -depth, // 1
width, height, -depth, // 2
-width, height, -depth, // 3
-width, -height, depth, // 4
width, -height, depth, // 5
width, height, depth, // 6
-width, height, depth, // 7
};

short indices[] = { 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7,
3, 3, 7, 4, 3, 4, 0, 4, 7, 6, 4, 6, 5, 3, 0, 1, 3, 1, 2, };



int vsize=sizeof(vertices);
int isize=sizeof(indices);

setIndices(*indices,isize);
setVertices(*vertices,vsize);


}

class Group : Mesh {
public:
void AddCube() {
Cube c;
c.Cube(2,2,2);

}
void drawobj(){
Mesh m;
m.draw();
}
private:

};
vector<Group> grpobj;

最佳答案

这是一个彻头彻尾的困惑。您需要将几何体与着色器分开。所以你的几何类应该只导出它的顶点和索引列表并且能够

a) 调用 glVertexAttribPointer() 和 glEnableVertexAttribArray() 准备渲染,以及
b) 使用 glDrawElements() 渲染它们。

这很重要,因为您可能想在多个地方渲染一个对象,然后您需要执行 a) 一次和 b) 多次。

着色器完全不同。你应该有一个带有着色器的类,它能够编译着色器源代码,然后使用着色器。您可能希望在您的应用程序中拥有多个着色器,因此您将在启动时编译一次着色器,然后在绘制时在它们之间切换。

我不会修复您的代码。您最好编写一个简单的应用程序(例如,通常称为“第一个三角形”或“彩色三角形”),然后从那里改进您的对象设计。

关于java - 我如何从一个文件中的类调用以使用着色器显示在另一个包含 gl 代码的文件中(这两个文件都在 jni 文件夹中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8967873/

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