In my app I am using OpenGL ES to render a file downloaded from the internet that is then parsed into a vertex array, so I have to input vertex and normals data after launch. I am new to OpenGL ES but I have been reading and learning. I have setup a vertex and normals buffer that seem to be working fine, but I think that I am putting the vertex and normals data into the buffers incorrectly because when I load the view there is an object there that vaguely resembles the shape that I want but with triangles veering off in various directions and parts of the shape missing. Here is my code for inputting my data into the buffers:
for (int i = 0; i < triangle_cnt; i++) {
int base = i * 18;
GLfloat x1 = vertices[base];
GLfloat y1 = vertices[base + 1];
GLfloat z1 = vertices[base + 2];
GLfloat x2 = vertices[base + 6];
GLfloat y2 = vertices[base + 7];
GLfloat z2 = vertices[base + 8];
GLfloat x3 = vertices[base + 12];
GLfloat y3 = vertices[base + 13];
GLfloat z3 = vertices[base + 14];
vector_t normal;
vector_t U;
vector_t V;
GLfloat length;
U.x = x2 - x1;
U.y = y2 - y1;
U.z = z2 - z1;
V.x = x3 - x1;
V.y = y3 - y1;
V.z = z3 - z1;
normal.x = U.y * V.z - U.z * V.y;
normal.y = U.z * V.x - U.x * V.z;
normal.z = U.x * V.y - U.y * V.x;
length = normal.x * normal.x + normal.y * normal.y + normal.z * normal.z;
length = sqrt(length);
base = i * 9;
verticesBuff[base] = x1;
verticesBuff[base + 1] = y1;
verticesBuff[base + 2] = z1;
normalsBuff[base] = normal.x;
normalsBuff[base + 1] = normal.y;
normalsBuff[base + 2] = normal.z;
verticesBuff[base + 3] = x2;
verticesBuff[base + 4] = y2;
verticesBuff[base + 5] = z2;
normalsBuff[base + 3] = normal.x;
normalsBuff[base + 4] = normal.y;
normalsBuff[base + 5] = normal.z;
verticesBuff[base + 6] = x3;
verticesBuff[base + 7] = y3;
verticesBuff[base + 8] = z3;
normalsBuff[base + 6] = normal.x;
normalsBuff[base + 7] = normal.y;
normalsBuff[base + 8] = normal.z;
fprintf(stderr, "%ff, %ff, %ff, %ff, %ff, %ff, \n", x1, y1, z1, normal.x, normal.y, normal.z);
fprintf(stderr, "%ff, %ff, %ff, %ff, %ff, %ff, \n", x2, y2, z2, normal.x, normal.y, normal.z);
fprintf(stderr, "%ff, %ff, %ff, %ff, %ff, %ff, \n", x3, y3, z3, normal.x, normal.y, normal.z);
}
And here is the code I use for using those buffers:
- (void)setupGL {
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
self.effect = [[GLKBaseEffect alloc] init];
self.effect.light0.enabled = GL_TRUE;
self.effect.light0.diffuseColor = GLKVector4Make(.05f, .55f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertCount*sizeof(verticesBuff)*3*2, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verticesBuff) * vertCount * 3, verticesBuff);
glBufferData(GL_ARRAY_BUFFER, vertCount*sizeof(normalsBuff)*3*2, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertCount * 3, sizeof(normalsBuff) * vertCount * 3, normalsBuff);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
glBindVertexArrayOES(0);
_rotMatrix = GLKMatrix4Identity;
_quat = GLKQuaternionMake(0, 0, 0, 1);
_quatStart = GLKQuaternionMake(0, 0, 0, 1);
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
glClearColor(0.78f, 0.78f, 0.78f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(_vertexArray);
// Render the object with GLKit
[self.effect prepareToDraw];
glVertexPointer(3, GL_FLOAT, 0, verticesBuff);
glNormalPointer(GL_FLOAT, 0, normalsBuff);
glDrawArrays(GL_TRIANGLES, 0, vertCount); //*******************************
// Render the object again with ES2
glUseProgram(_program);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
glDrawArrays(GL_TRIANGLES, 0, vertCount);
}
If I take those logs and paste them into the vertex array of a sample app using the code Apple supplies when creating an OpenGL ES app then the object renders beautifully, so I have deduced that I must just be putting the vertex data in wrong.
So can someone help me understand what I am doing wrong when entering the vertices and normals? Any help is appreciated.
Also this is what my render looks like:
And this is, in shape at least, what it should look like:
It's a bit messy and there might be quite a few problems. The one you are currently facing would seem to be:
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
The stride parameter (24) should be sizeof(GLfloat)*3 and same goes for normals. This is because you don't use interleaved vertex structure any more and now your position coordinates are tightly packed (that being said you can also use 0 as a stride parameter now). The result you produced with this bug is that only every second vertex was taken, others were dropped and when positions are all taken it starts drawing normals. Also if your normal pointer and offset are set correctly you are reading them beyond the buffer and should produce either crash or reading some other resources.
Related
CODE:
[lightRender beginWithClear:0 g:0 b:0 a:0];
self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];
CC_NODE_DRAW_SETUP();
CGPoint vertices[3];
ccColor4F colors[3];
CGPoint mid = BODY_POSITION(cursor.body);
float radius = LIGHT_RANGE * lightRadius; // - (CCRANDOM_0_1() * LIGHT_RANGE * .015);
float initialAlpha = .7f;
for (int i = 0; i < LIGHT_PRECISION; i++)
{
int nVertices = 0;
float angle = 2*M_PI/LIGHT_PRECISION * i;
float nextAngle = 2*M_PI/LIGHT_PRECISION * (i+1);
int n = i + 1;
if (n == LIGHT_PRECISION)
n = 0;
vertices[nVertices] = ccpAdd(mid, ccp(cosf(angle) * radius * lightLength[i], sinf(angle) * radius * lightLength[i]));
colors[nVertices++] = (ccColor4F){1, 1, .9, initialAlpha * (1 - lightLength[i])};
vertices[nVertices] = ccpAdd(mid, ccp(cosf(nextAngle) * radius * lightLength[n], sinf(nextAngle) * radius * lightLength[n]));
colors[nVertices++] = (ccColor4F){1, 1, .9, initialAlpha * (1 - lightLength[n])};
vertices[nVertices] = mid;
colors[nVertices++] = (ccColor4F){1, 1, .8, initialAlpha};
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
}
[lightRender end];
CC_NODE_DRAW_SETUP Code:
#define CC_NODE_DRAW_SETUP() \
do { \
ccGLEnable( _glServerState ); \
NSAssert1(_shaderProgram, #"No shader program set for node: %#", self); \
[_shaderProgram use]; \
[_shaderProgram setUniformsForBuiltins]; \
} while(0)
How to fix shader code in iOS12 with Cocos2d 2.2 Obj.C project ? If I run in iOS 7 simulator it works perfect..same code not working in iOS 12
Fixed Shaders Problem in Cocos2d.
In Above code, main problem is CGFloat...converted it to float for all vertices.
Also I took shaders related files from Cocos2d-x and converted it to obj.C for Cocos2d.
Now all shaders working Perfect in Cocos2d v2.2 Obj.C iOS 12, iPhoneX/XR/XSMax also..
Happy Coding! Cheers :)
The OpenCV docs give the following SVM kernel type example:
A comparison of different kernels on the following 2D test case with four classes. Four SVM::C_SVC SVMs have been trained (one against rest) with auto_train. Evaluation on three different kernels (SVM::CHI2, SVM::INTER, SVM::RBF). The color depicts the class with max score. Bright means max-score > 0, dark means max-score < 0.
Where can I find the sample code that generates this example?
Specifically, the SVM predict() method presumably returns a label value and not a max-score. How can it return a max-score?
Note that the quote states that it uses SVM::C_SVC which is a classification, not a regression, type.
You can get the score with 2-class SVM, and if you pass RAW_OUTPUT to predict:
// svm.cpp, SVMImpl::predict(...) , line 1917
bool returnDFVal = (flags & RAW_OUTPUT) != 0;
// svm.cpp, PredictBody::operator(), line 1896,
float result = returnDFVal && class_count == 2 ?
(float)sum : (float)(svm->class_labels.at<int>(k));
Then you need to train 4 different 2 class SVM, one against rest.
These are the result I get on these samples:
INTER with trainAuto
CHI2 with trainAuto
RBF with train (C = 0.1, gamma = 0.001) (trainAuto overfits in this case)
Here is the code. You can enable trainAuto with AUTO_TRAIN_ENABLED boolean variable, and you can set the KERNEL as well as images dimensions, etc.
#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;
using namespace cv::ml;
int main()
{
const int WIDTH = 512;
const int HEIGHT = 512;
const int N_SAMPLES_PER_CLASS = 10;
const float NON_LINEAR_SAMPLES_RATIO = 0.1;
const int KERNEL = SVM::CHI2;
const bool AUTO_TRAIN_ENABLED = false;
int N_NON_LINEAR_SAMPLES = N_SAMPLES_PER_CLASS * NON_LINEAR_SAMPLES_RATIO;
int N_LINEAR_SAMPLES = N_SAMPLES_PER_CLASS - N_NON_LINEAR_SAMPLES;
vector<Scalar> colors{Scalar(255,0,0), Scalar(0,255,0), Scalar(0,0,255), Scalar(0,255,255)};
vector<Vec3b> colorsv{ Vec3b(255, 0, 0), Vec3b(0, 255, 0), Vec3b(0, 0, 255), Vec3b(0, 255, 255) };
vector<Vec3b> colorsv_shaded{ Vec3b(200, 0, 0), Vec3b(0, 200, 0), Vec3b(0, 0, 200), Vec3b(0, 200, 200) };
Mat1f data(4 * N_SAMPLES_PER_CLASS, 2);
Mat1i labels(4 * N_SAMPLES_PER_CLASS, 1);
RNG rng(0);
////////////////////////
// Set training data
////////////////////////
// Class 1
Mat1f class1 = data.rowRange(0, 0.5 * N_LINEAR_SAMPLES);
Mat1f x1 = class1.colRange(0, 1);
Mat1f y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT / 8));
class1 = data.rowRange(0.5 * N_LINEAR_SAMPLES, 1 * N_LINEAR_SAMPLES);
x1 = class1.colRange(0, 1);
y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(7*HEIGHT / 8), Scalar(HEIGHT));
class1 = data.rowRange(N_LINEAR_SAMPLES, 1 * N_SAMPLES_PER_CLASS);
x1 = class1.colRange(0, 1);
y1 = class1.colRange(1, 2);
rng.fill(x1, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y1, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
// Class 2
Mat1f class2 = data.rowRange(N_SAMPLES_PER_CLASS, N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
Mat1f x2 = class2.colRange(0, 1);
Mat1f y2 = class2.colRange(1, 2);
rng.fill(x2, RNG::NORMAL, Scalar(3 * WIDTH / 4), Scalar(WIDTH/16));
rng.fill(y2, RNG::NORMAL, Scalar(HEIGHT / 2), Scalar(HEIGHT/4));
class2 = data.rowRange(N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 2 * N_SAMPLES_PER_CLASS);
x2 = class2.colRange(0, 1);
y2 = class2.colRange(1, 2);
rng.fill(x2, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y2, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
// Class 3
Mat1f class3 = data.rowRange(2 * N_SAMPLES_PER_CLASS, 2 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
Mat1f x3 = class3.colRange(0, 1);
Mat1f y3 = class3.colRange(1, 2);
rng.fill(x3, RNG::NORMAL, Scalar(WIDTH / 4), Scalar(WIDTH/8));
rng.fill(y3, RNG::NORMAL, Scalar(HEIGHT / 2), Scalar(HEIGHT/8));
class3 = data.rowRange(2*N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 3 * N_SAMPLES_PER_CLASS);
x3 = class3.colRange(0, 1);
y3 = class3.colRange(1, 2);
rng.fill(x3, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y3, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
// Class 4
Mat1f class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS, 3 * N_SAMPLES_PER_CLASS + 0.5 * N_LINEAR_SAMPLES);
Mat1f x4 = class4.colRange(0, 1);
Mat1f y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::NORMAL, Scalar(WIDTH / 2), Scalar(WIDTH / 16));
rng.fill(y4, RNG::NORMAL, Scalar(HEIGHT / 4), Scalar(HEIGHT / 16));
class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS + 0.5 * N_LINEAR_SAMPLES, 3 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES);
x4 = class4.colRange(0, 1);
y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::NORMAL, Scalar(WIDTH / 2), Scalar(WIDTH / 16));
rng.fill(y4, RNG::NORMAL, Scalar(3 * HEIGHT / 4), Scalar(HEIGHT / 16));
class4 = data.rowRange(3 * N_SAMPLES_PER_CLASS + N_LINEAR_SAMPLES, 4 * N_SAMPLES_PER_CLASS);
x4 = class4.colRange(0, 1);
y4 = class4.colRange(1, 2);
rng.fill(x4, RNG::UNIFORM, Scalar(1), Scalar(WIDTH));
rng.fill(y4, RNG::UNIFORM, Scalar(1), Scalar(HEIGHT));
// Labels
labels.rowRange(0*N_SAMPLES_PER_CLASS, 1*N_SAMPLES_PER_CLASS).setTo(1);
labels.rowRange(1*N_SAMPLES_PER_CLASS, 2*N_SAMPLES_PER_CLASS).setTo(2);
labels.rowRange(2*N_SAMPLES_PER_CLASS, 3*N_SAMPLES_PER_CLASS).setTo(3);
labels.rowRange(3*N_SAMPLES_PER_CLASS, 4*N_SAMPLES_PER_CLASS).setTo(4);
// Draw training data
Mat3b samples(HEIGHT, WIDTH, Vec3b(0,0,0));
for (int i = 0; i < labels.rows; ++i)
{
circle(samples, Point(data(i, 0), data(i, 1)), 3, colors[labels(i,0) - 1], CV_FILLED);
}
//////////////////////////
// SVM
//////////////////////////
// SVM label 1
Ptr<SVM> svm1 = SVM::create();
svm1->setType(SVM::C_SVC);
svm1->setKernel(KERNEL);
Mat1i labels1 = (labels != 1) / 255;
if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td1 = TrainData::create(data, ROW_SAMPLE, labels1);
svm1->trainAuto(td1);
}
else
{
svm1->setC(0.1);
svm1->setGamma(0.001);
svm1->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm1->train(data, ROW_SAMPLE, labels1);
}
// SVM label 2
Ptr<SVM> svm2 = SVM::create();
svm2->setType(SVM::C_SVC);
svm2->setKernel(KERNEL);
Mat1i labels2 = (labels != 2) / 255;
if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td2 = TrainData::create(data, ROW_SAMPLE, labels2);
svm2->trainAuto(td2);
}
else
{
svm2->setC(0.1);
svm2->setGamma(0.001);
svm2->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm2->train(data, ROW_SAMPLE, labels2);
}
// SVM label 3
Ptr<SVM> svm3 = SVM::create();
svm3->setType(SVM::C_SVC);
svm3->setKernel(KERNEL);
Mat1i labels3 = (labels != 3) / 255;
if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td3 = TrainData::create(data, ROW_SAMPLE, labels3);
svm3->trainAuto(td3);
}
else
{
svm3->setC(0.1);
svm3->setGamma(0.001);
svm3->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm3->train(data, ROW_SAMPLE, labels3);
}
// SVM label 4
Ptr<SVM> svm4 = SVM::create();
svm4->setType(SVM::C_SVC);
svm4->setKernel(KERNEL);
Mat1i labels4 = (labels != 4) / 255;
if (AUTO_TRAIN_ENABLED)
{
Ptr<TrainData> td4 = TrainData::create(data, ROW_SAMPLE, labels4);
svm4->trainAuto(td4);
}
else
{
svm4->setC(0.1);
svm4->setGamma(0.001);
svm4->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, (int)1e7, 1e-6));
svm4->train(data, ROW_SAMPLE, labels4);
}
//////////////////////////
// Show regions
//////////////////////////
Mat3b regions(HEIGHT, WIDTH);
Mat1f R(HEIGHT, WIDTH);
Mat1f R1(HEIGHT, WIDTH);
Mat1f R2(HEIGHT, WIDTH);
Mat1f R3(HEIGHT, WIDTH);
Mat1f R4(HEIGHT, WIDTH);
for (int r = 0; r < HEIGHT; ++r)
{
for (int c = 0; c < WIDTH; ++c)
{
Mat1f sample = (Mat1f(1,2) << c, r);
vector<float> responses(4);
responses[0] = svm1->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[1] = svm2->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[2] = svm3->predict(sample, noArray(), StatModel::RAW_OUTPUT);
responses[3] = svm4->predict(sample, noArray(), StatModel::RAW_OUTPUT);
int best_class = distance(responses.begin(), max_element(responses.begin(), responses.end()));
float best_response = responses[best_class];
// View responses for each SVM, and the best responses
R(r,c) = best_response;
R1(r, c) = responses[0];
R2(r, c) = responses[1];
R3(r, c) = responses[2];
R4(r, c) = responses[3];
if (best_response >= 0) {
regions(r, c) = colorsv[best_class];
}
else {
regions(r, c) = colorsv_shaded[best_class];
}
}
}
imwrite("svm_samples.png", samples);
imwrite("svm_x.png", regions);
imshow("Samples", samples);
imshow("Regions", regions);
waitKey();
return 0;
}
I am trying to render some vertices to an Open GL ES window. My program keeps crashing on the GLDrawElements command. I am trying to pass some VBOs for the vertices "bindPosition", "bindNorml" and "Index" of type GLFloat.
Here is a link to my rendering method:
- (void)render:(CADisplayLink*)displayLink {
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glClearColor(0.3, 0.5, 0.9, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
GLKMatrix4 modelView = GLKMatrix4Make(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -10, -30, 1);
GLKMatrix4 projectionView = GLKMatrix4Make(3.6213202476501465, 0, 0, 0, 0, 2.4142136573791504, 0, 0, 0, 0, -1.0020020008087158, -1, 0, -24.142135620117188, 28.05805778503418, 30);
// Upload Transforms
glUniformMatrix4fv(_modelViewMatrixUniform, 1, 0, modelView.m);
glUniformMatrix4fv(_modelViewProjMatrixUniform, 1, 0, projectionView.m);
// Upload Bones
glUniformMatrix4fv(_bonesUniform, 1, 0, bones);
glBindBuffer(GL_ARRAY_BUFFER, _bindPositionBuffer);
glVertexAttribPointer(_VertexPositionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(bindPosition), 0);
glBindBuffer(GL_ARRAY_BUFFER, _bindNormalBuffer);
glVertexAttribPointer(_VertexNormalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(bindNormal), 0);
// 3
glBindBuffer(GL_ARRAY_BUFFER, _indexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(Index)/sizeof(Index[0]), GL_UNSIGNED_SHORT, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
Setting up VBOS:
- (void)setupVBOs {
glGenBuffers(1, &_bindPositionBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _bindPositionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(bindPosition), bindPosition, GL_STATIC_DRAW);
glGenBuffers(1, &_bindNormalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _bindNormalBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(bindNormal), bindNormal, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Index), Index, GL_STATIC_DRAW);
}
Compiling Shaders:
- (void)compileShaders {
// 1
GLuint vertexShader = [self compileShader:#"SimpleVertex" withType:GL_VERTEX_SHADER];
GLuint fragmentShader = [self compileShader:#"SimpleFragment" withType:GL_FRAGMENT_SHADER];
// 2
GLuint programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
// 3
GLint linkSuccess;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE) {
GLchar messages[256];
glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]);
NSString *messageString = [NSString stringWithUTF8String:messages];
NSLog(#"%#", messageString);
exit(1);
}
// 4
glUseProgram(programHandle);
// 5
// Uniform Locations
_bonesUniform = glGetUniformLocation(programHandle, "Bones[0]");
_modelViewMatrixUniform = glGetUniformLocation(programHandle, "ModelViewMatrix");
_modelViewProjMatrixUniform = glGetUniformLocation(programHandle, "ModelViewProjMatrix");
_textureUniform = glGetUniformLocation(programHandle, "Texture");
// Attribute Locations
_VertexBoneWeightAttribute = glGetAttribLocation(programHandle, "VertexBoneWeight");
_VertexBoneIDAttribute = glGetAttribLocation(programHandle, "VertexBoneID");
_VertexTexCoord0Attribute = glGetAttribLocation(programHandle, "VertexTexCoord0");
_VertexNormalAttribute = glGetAttribLocation(programHandle, "VertexNormal");
_VertexPositionAttribute = glGetAttribLocation(programHandle, "VertexPosition");
// Enable vertex pointers
glEnableVertexAttribArray(_VertexBoneWeightAttribute);
glEnableVertexAttribArray(_VertexBoneIDAttribute);
glEnableVertexAttribArray(_VertexTexCoord0Attribute);
glEnableVertexAttribArray(_VertexNormalAttribute);
glEnableVertexAttribArray(_VertexPositionAttribute);
}
Here is a link to my shaders:
attribute vec3 VertexPosition;
attribute vec3 VertexNormal;
attribute vec2 VertexTexCoord0;
attribute vec4 VertexBoneID;
attribute vec4 VertexBoneWeight;
uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjMatrix;
uniform vec4 Bones[222];
varying vec3 Normal;
varying vec2 TexCoord0;
void main(void)
{
TexCoord0 = VertexTexCoord0;
// Build 4x3 skinning matrix.
vec4 r0 = Bones[int(VertexBoneID.x) * 3 + 0] * VertexBoneWeight.x;
vec4 r1 = Bones[int(VertexBoneID.x) * 3 + 1] * VertexBoneWeight.x;
vec4 r2 = Bones[int(VertexBoneID.x) * 3 + 2] * VertexBoneWeight.x;
r0 += Bones[int(VertexBoneID.y) * 3 + 0] * VertexBoneWeight.y;
r1 += Bones[int(VertexBoneID.y) * 3 + 1] * VertexBoneWeight.y;
r2 += Bones[int(VertexBoneID.y) * 3 + 2] * VertexBoneWeight.y;
r0 += Bones[int(VertexBoneID.z) * 3 + 0] * VertexBoneWeight.z;
r1 += Bones[int(VertexBoneID.z) * 3 + 1] * VertexBoneWeight.z;
r2 += Bones[int(VertexBoneID.z) * 3 + 2] * VertexBoneWeight.z;
r0 += Bones[int(VertexBoneID.w) * 3 + 0] * VertexBoneWeight.w;
r1 += Bones[int(VertexBoneID.w) * 3 + 1] * VertexBoneWeight.w;
r2 += Bones[int(VertexBoneID.w) * 3 + 2] * VertexBoneWeight.w;
// Skin and transform position.
float px = dot(r0, vec4(VertexPosition, 1.0));
float py = dot(r1, vec4(VertexPosition, 1.0));
float pz = dot(r2, vec4(VertexPosition, 1.0));
gl_Position = ModelViewProjMatrix * vec4(px, py, pz, 1.0);
/* Skin and transform normal into view-space. We assume that the modelview matrix
doesn't contain a scale. Should pass pass in the inverse-transpose really. */
float nx = dot(r0, vec4(VertexNormal, 0.0));
float ny = dot(r1, vec4(VertexNormal, 0.0));
float nz = dot(r2, vec4(VertexNormal, 0.0));
Normal = normalize((ModelViewMatrix * vec4(nx, ny, nz, 0.0)).xyz);
}
Frag Shader:
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D Texture;
varying vec3 Normal;
varying vec2 TexCoord0;
void main(void)
{
// Ambient term.
vec3 lighting = vec3(0.5,0.5,0.5) * 0.7;
/* Very cheap lighting. Three directional lights, one shining slighting upwards to illuminate
underneath the chin, and then one each shining from the left and right. Light directional
are in view-space and follow the camera rotation by default. */
lighting += dot(Normal, normalize(vec3( 0.0, -0.2, 0.8))) * vec3(0.8, 0.8, 0.6) * 0.6; // Shines forwards and slightly upwards.
lighting += dot(Normal, normalize(vec3(-0.8, 0.4, 0.8))) * vec3(0.8, 0.8, 0.6) * 0.4; // Shines forwards and from left to right.
lighting += dot(Normal, normalize(vec3( 0.8, 0.4, 0.8))) * vec3(0.8, 0.8, 0.6) * 0.4; // Shines forwards and from right to left.
//gl_FragColor = vec4(Normal * 0.5 + vec3(0.5), 1.0);
gl_FragColor = vec4(texture2D(Texture, TexCoord0).xyz * lighting, 1.0);
}
Can anyone see anything in my render method which i have done wrong?
If you bind GLFloat values to your GL_ELEMENT_ARRAY_BUFFER, that could be a problem. You should bind GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT. http://www.khronos.org/opengles/sdk/docs/man/xhtml/glDrawElements.xml
When binding the index buffer in your render method, you should use GL_ELEMENT_ARRAY_BUFFER instead of GL_ARRAY_BUFFER.
Your call to glDrawElements doesn't look to be correct. Nor does your call to glBufferData.
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Index), Index, GL_STATIC_DRAW);
glDrawElements(GL_TRIANGLE_STRIP, sizeof(Index)/sizeof(Index[0]), GL_UNSIGNED_SHORT, 0);
It looks like you're passing a struct to glBufferData rather than an array of indices. Unless your parameter is called Index but it doesn't seem that way due to how you're using it. You need to build an array of vertex indices and pass this in to tell the GPU what you want to draw.
The second parameter to glDrawElement should be the number of elements you want to render. I had a similar problem in this question which someone helpfully pointed out I was making the same mistake. Hopefully it will be helpful to you too.
Using Cocos2d to draw a thicker circle:
glLineWidth(20);
ccDrawCircle(self.ripplePosition, _radius, 0, 50, NO);
But this is what shows up(notice how it looks like it's created from 4 different segments):
http://i.stack.imgur.com/jYW4s.png
I tried increasing the number of segments to larger values but the result is the same.
Is this a bug in Cocos2D? Any ideas on how to achieve a "perfect" circle?
Here is the implementation of ccDrawCircle from cocos2d 2.0rc2:
void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
{
lazy_init();
int additionalSegment = 1;
if (drawLineToCenter)
additionalSegment++;
const float coef = 2.0f * (float)M_PI/segs;
GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
if( ! vertices )
return;
for(NSUInteger i = 0;i <= segs; i++) {
float rads = i*coef;
GLfloat j = r * cosf(rads + a) + center.x;
GLfloat k = r * sinf(rads + a) + center.y;
vertices[i*2] = j;
vertices[i*2+1] = k;
}
vertices[(segs+1)*2] = center.x;
vertices[(segs+1)*2+1] = center.y;
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}
I went with a slightly modified version of ccDrawCircle and it works pretty well (performs a lot better than using and resizing a sprite):
void ccDrawDonut( CGPoint center, float r1, float r2, NSUInteger segs)
{
lazy_init();
const float coef = 2.0f * (float)M_PI/segs;
GLfloat *vertices = calloc( sizeof(GLfloat)*4*segs+4, 1);
if( ! vertices )
return;
for(NSUInteger i = 0;i <= segs; i++) {
float rads = i*coef;
GLfloat j1 = r1 * cosf(rads) + center.x;
GLfloat k1 = r1 * sinf(rads) + center.y;
vertices[i*4] = j1;
vertices[i*4+1] = k1;
rads+= coef/2;
GLfloat j2 = r2 * cosf(rads) + center.x;
GLfloat k2 = r2 * sinf(rads) + center.y;
vertices[i*4+2] = j2;
vertices[i*4+3] = k2;
}
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei) 2*segs+2);
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}
I can display a texture using shaders, glVertexAttribPointer and glDrawArrays like so:
Init
const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f
};
const GLfloat squareTex[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
glEnableVertexAttribArray(PositionTag);
glEnableVertexAttribArray(TexCoord0Tag);
glVertexAttribPointer(PositionTag, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glVertexAttribPointer(TexCoord0Tag, 2, GL_FLOAT, GL_FALSE, 0, squareTex);
And for draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
But I'm having difficulty converting to VBOs, shaders and glDrawElements. This is the code I have so far, but nothing displays:
Header
typedef struct MyVertex
{
float x, y, z; //Vertex
float nx, ny, nz; //Normal
float s0, t0; //Texcoord0
} MyVertex;
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
Init
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
MyVertex pvertices[4];
//Fill the pvertices array
pvertices[0].x = -0.5f;
pvertices[0].y = -0.33f;
pvertices[0].z = 0.0;
pvertices[0].nx = 0.0;
pvertices[0].ny = 0.0;
pvertices[0].nz = 1.0;
pvertices[0].s0 = 0.0;
pvertices[0].t0 = 0.0;
pvertices[1].x = 0.5f;
pvertices[1].y = -0.33f;
pvertices[1].z = 0.0;
pvertices[1].nx = 0.0;
pvertices[1].ny = 0.0;
pvertices[1].nz = 1.0;
pvertices[1].s0 = 1.0;
pvertices[1].t0 = 0.0;
pvertices[2].x = -0.5f;
pvertices[2].y = 0.33f;
pvertices[2].z = 0.0;
pvertices[2].nx = 0.0;
pvertices[2].ny = 0.0;
pvertices[2].nz = 1.0;
pvertices[2].s0 = 0.0;
pvertices[2].t0 = 1.0;
pvertices[3].x = 0.5f;
pvertices[3].y = 0.33f;
pvertices[3].z = 0.0;
pvertices[3].nx = 0.0;
pvertices[3].ny = 0.0;
pvertices[3].nz = 1.0;
pvertices[3].s0 = 1.0;
pvertices[3].t0 = 1.0;
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex)*4, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(MyVertex)*4, pvertices);
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
int pindices[6];
pindices[0]=0;
pindices[1]=1;
pindices[2]=2;
pindices[3]=2;
pindices[4]=1;
pindices[5]=3;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*6, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(int)*6, pindices);
Draw
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glEnableVertexAttribArray(PositionTag);
glEnableVertexAttribArray(NormalTag);
glEnableVertexAttribArray(TexCoord0Tag);
glVertexAttribPointer(PositionTag, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glVertexAttribPointer(NormalTag, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(12));
glVertexAttribPointer(TexCoord0Tag, 2, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(24));
// glDrawRangeElements(GL_TRIANGLES, x, y, z, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glDrawElements(GL_TRIANGLES, 3, GL_INT, 0);
According to here, GL_INT is not a valid type to use for indices in glDrawElements. Try using unsigned ints for your indices (and of course GL_UNSIGNED_INT in glDrawElements). You may still use your int data as indices, but as glDrawElements needs GL_UNSIGNED_INT, it would be more consistent to make the array unsigned int.
EDIT: After looking into the specification (based on your tags I took the ES 2.0 spec), they seem to further limit it to unsigned byte and unsigned short. I don't know if it is that limited in iOS, but we can conclude that the data has at least to be unsigned. On the other hand I haven't found any statement about a possible GL_INVALID_ENUM error that is thrown on a wrong type argument, but it would be reasonable to get one.
Your code doesn't look terribly wrong, so this time the devil is somewhere in the details. My guess is, that your use of a struct and its data fields' alignments don't match the offsets passed to OpenGL.
I suggest you use the offsetof() macro found in stddef.h to portably get the offsets of the data fields.