So, I've begun a rather instantaneous trip into the world of visual, 3-d programming. I'm currently heavily invested in webgl with a rather strong background in JavaScript and most web-oriented languages but this is my first graphics language.
While trying to draw my first rather simple shape, I've run into an error I can't seem to locate a solution for. It reads in chrome as a:
WebGL: INVALID_OPERATION: drawElements:attribs not setup correctly (repeated n times)
where n is a number that varies seemingly randomly. The code in question is here:
var tessVertexPositionBuffer;
var tessVertexColorBuffer;
var tessVertexIndexBuffer;
function initBuffers () {
tessVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
var vertices = [
//innerfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
//innerleft
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
//innerback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
//innerright
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
//topfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
//topleft
-1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
-1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
//topback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
//topright
1.0, 1.0, 1.0,
2.0, 2.0, 2.0,
1.0, 1.0, -1.0,
2.0, 2.0, -2.0,
//outerfront
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//outerleft
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
-2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
//outerback
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//outerright
2.0, 2.0, 2.0,
2.0, -2.0, 2.0,
2.0, 2.0, -2.0,
2.0, -2.0, -2.0,
//bottomfront
2.0, 2.0, 2.0,
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//bottomleft
-1.0, -1.0, 1.0,
-2.0, -2.0, 2.0,
-1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
//bottomback
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//bottomright
1.0, -1.0, 1.0,
2.0, -2.0, 2.0,
1.0, -1.0, -1.0,
2.0, -2.0, -2.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
tessVertexPositionBuffer.itemSize = 3;
tessVertexPositionBuffer.numItems = 64;
tessVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
var colors = [
[0.7, 0.7, 0.7, 1.0], //all inner sides
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0], //all top sides
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.7, 0.0, 1.0], //all outer sides
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.0, 0.7, 0.7, 1.0], //all bottom sides
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
];
var unpackedColors = [];
for (var i in colors) {
var color = colors[i];
for (var j=0; j< 4; j++) {
unpackedColors = unpackedColors.concat(color);
}
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
tessVertexColorBuffer.itemSize = 4;
tessVertexColorBuffer.numItems = 64;
tessVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
var tessVertexIndices = [
0, 1, 2, 0, 2, 3,
4, 5, 6, 5, 6, 7,
8, 9, 10, 9, 10, 11,
12, 13, 14, 13, 14, 15,
16, 17, 18, 17, 18, 19,
20, 21, 22, 21, 22, 23,
24, 25, 26, 25, 26, 27,
28, 29, 30, 29, 30, 31,
32, 33, 34, 33, 34, 35,
36, 37, 38, 37, 38, 39,
40, 41, 42, 41, 42, 43,
44, 45, 46, 45, 46, 47,
48, 49, 50, 48, 50, 51,
52, 53, 54, 53, 54, 55,
56, 57, 58, 57, 58, 59,
60, 61, 62, 61, 62, 63
];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(tessVertexIndices), gl.STATIC_DRAW);
tessVertexIndexBuffer.itemSize = 1;
tessVertexIndexBuffer.numItems = 96;
}
and the actual drawing of the buffers is here:
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vetexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, tessVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, tessVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
Now, this is relatively verbatim from learningwebgl, I'm really just trying to draw a simple shape. I'm relatively certain that my problem lies in my buffer types because I honestly don't uderstand much about them (and most literature on webGl I've found is either novice with a general understanding of the language or of the HYPERSUPERPRO variety).
I've checked over the actual vertex positions/colors/indices multiple times and unless I've just become familiar with the code enough that I'm blind to the simple errors, I can't find an error there.
Looks like your problem is just a small typo. The second line of your "actual drawing of buffers" code should be he following:
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
You were missing the first r in vertexPositionAttribute on that line.
I modified the lesson 4 LearningWebGL tutorial to use your code (with the fixed typo) and I have a link to it here.
If you notice there is a blue triangle and a yellow triangle being drawn at the exact same position in space which causes some flickering. I'm not sure if that was intentional.
Related
I'm programming a rubik cube in c with SDL and OpenGl. I have done all the implementation, except the rotation of a layer move (now if i press 'r' or 'f', for example, right or front layers change its colours). I don't really know how to implement the rotation of a layer.
Here is my render implementation which draws the cube.
Note: mov is 7 if i want to rotate clockwise right layer, 8 if i want to rotate left layer etc, **s is an array with the rgb color of each sticker of the cube
static void
Render(int mov, double **s)
{
static float color[8][3] = {
{1.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 1.0, 1.0},
{1.0, 1.0, 1.0},
{1.0, 0.0, 1.0},
{0.0, 0.0, 1.0}
};
static float cube[56][3] = {
// Cara 1
{0.6, 0.6, 0.6},
{0.6, 0.6, 0.2},
{0.6, 0.6, -0.2},
{0.6, 0.6, -0.6},
{0.6, 0.2, -0.6},
{0.6, -0.2, -0.6},
{0.6, -0.6, -0.6},
{0.6, -0.6, -0.2},
{0.6, -0.6, 0.2},
{0.6, -0.6, 0.6},
{0.6, -0.2, 0.6},
{0.6, 0.2, 0.6},
{0.6, 0.2, 0.2},
{0.6, 0.2, -0.2},
{0.6, -0.2, -0.2},
{0.6, -0.2, 0.2},
// Cara 2
{0.2, 0.6, 0.6},
{-0.2, 0.6, 0.6},
{-0.6, 0.6, 0.6},
{-0.6, 0.6, 0.2},
{-0.6, 0.6, -0.2},
{-0.6, 0.6, -0.6},
{-0.2, 0.6, -0.6}, // 22
{0.2, 0.6, -0.6},
{0.2, 0.6, -0.2},
{0.2, 0.6, 0.2},
{-0.2, 0.6, 0.2},
{-0.2, 0.6, -0.2}, // 27
// Cara 3
{-0.6, 0.2, 0.6},
{-0.6, 0.2, 0.2},
{-0.6, 0.2, -0.2},
{-0.6, 0.2, -0.6},
{-0.6, -0.2, -0.6},
{-0.6, -0.6, -0.6}, // 33
{-0.6, -0.6, -0.2},
{-0.6, -0.6, 0.2},
{-0.6, -0.6, 0.6},
{-0.6, -0.2, 0.6},
{-0.6, -0.2, 0.2},
{-0.6, -0.2, -0.2}, // 39
// Cara 4
{-0.2, -0.6, 0.6},
{-0.2, -0.6, 0.2},
{-0.2, -0.6, -0.2},
{-0.2, -0.6, -0.6},
{0.2, -0.6, -0.6}, // 44
{0.2, -0.6, -0.2},
{0.2, -0.6, 0.2},
{0.2, -0.6, 0.6}, // 47
// Cara 5
{0.2, 0.2, 0.6},
{0.2, -0.2, 0.6},
{-0.2, -0.2, 0.6},
{-0.2, 0.2, 0.6},
// Cara 6
{0.2, 0.2, -0.6},
{0.2, -0.2, -0.6},
{-0.2, -0.2, -0.6},
{-0.2, 0.2, -0.6}
};
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
// Cara 1
glColor3f(s[2][0], s[2][1], s[2][2]);
glVertex3fv(cube[8]);
glVertex3fv(cube[9]);
glVertex3fv(cube[10]);
glVertex3fv(cube[15]);
glColor3f(s[5][0], s[5][1], s[5][2]);
glVertex3fv(cube[10]);
glVertex3fv(cube[11]);
glVertex3fv(cube[12]);
glVertex3fv(cube[15]);
glColor3f(s[8][0], s[8][1], s[8][2]);
glVertex3fv(cube[0]);
glVertex3fv(cube[1]);
glVertex3fv(cube[12]);
glVertex3fv(cube[11]); // ST2
glColor3f(s[1][0], s[1][1], s[1][2]);
glVertex3fv(cube[7]);
glVertex3fv(cube[8]);
glVertex3fv(cube[15]);
glVertex3fv(cube[14]);
glColor3f(s[4][0], s[4][1], s[4][2]);
glVertex3fv(cube[12]);
glVertex3fv(cube[13]);
glVertex3fv(cube[14]);
glVertex3fv(cube[15]);
glColor3f(s[7][0], s[7][1], s[7][2]);
glVertex3fv(cube[1]);
glVertex3fv(cube[2]);
glVertex3fv(cube[13]);
glVertex3fv(cube[12]);
glColor3f(s[0][0], s[0][1], s[0][2]);
glVertex3fv(cube[5]);
glVertex3fv(cube[6]);
glVertex3fv(cube[7]);
glVertex3fv(cube[14]);
glColor3f(s[3][0], s[3][1], s[3][2]);
glVertex3fv(cube[4]);
glVertex3fv(cube[5]);
glVertex3fv(cube[14]);
glVertex3fv(cube[13]);
glColor3f(s[6][0], s[6][1], s[6][2]);
glVertex3fv(cube[2]);
glVertex3fv(cube[3]);
glVertex3fv(cube[4]);
glVertex3fv(cube[13]);
// Cara 2
glColor3f(s[11][0], s[11][1], s[11][2]);
glVertex3fv(cube[0]);
glVertex3fv(cube[1]);
glVertex3fv(cube[25]);
glVertex3fv(cube[16]);
glColor3f(s[10][0], s[10][1], s[10][2]);
glVertex3fv(cube[1]);
glVertex3fv(cube[2]);
glVertex3fv(cube[24]);
glVertex3fv(cube[25]);
glColor3f(s[9][0], s[9][1], s[9][2]);
glVertex3fv(cube[2]);
glVertex3fv(cube[3]);
glVertex3fv(cube[23]);
glVertex3fv(cube[24]);
glColor3f(s[12][0], s[12][1], s[12][2]);
glVertex3fv(cube[23]);
glVertex3fv(cube[24]);
glVertex3fv(cube[27]);
glVertex3fv(cube[22]);
glColor3f(s[15][0], s[15][1], s[15][2]);
glVertex3fv(cube[22]);
glVertex3fv(cube[27]);
glVertex3fv(cube[20]);
glVertex3fv(cube[21]);
glColor3f(s[16][0], s[16][1], s[16][2]);
glVertex3fv(cube[27]);
glVertex3fv(cube[26]);
glVertex3fv(cube[19]);
glVertex3fv(cube[20]);
glColor3f(s[17][0], s[17][1], s[17][2]);
glVertex3fv(cube[26]);
glVertex3fv(cube[17]);
glVertex3fv(cube[18]);
glVertex3fv(cube[19]);
glColor3f(s[14][0], s[14][1], s[14][2]);
glVertex3fv(cube[25]);
glVertex3fv(cube[16]);
glVertex3fv(cube[17]);
glVertex3fv(cube[26]);
glColor3f(s[13][0], s[13][1], s[13][2]);
glVertex3fv(cube[24]);
glVertex3fv(cube[25]);
glVertex3fv(cube[26]);
glVertex3fv(cube[27]);
// Cara 3
glColor3f(s[47][0], s[47][1], s[47][2]);
glVertex3fv(cube[18]);
glVertex3fv(cube[19]);
glVertex3fv(cube[29]);
glVertex3fv(cube[28]);
glColor3f(s[46][0], s[46][1], s[46][2]);
glVertex3fv(cube[19]);
glVertex3fv(cube[20]);
glVertex3fv(cube[30]);
glVertex3fv(cube[29]);
glColor3f(s[45][0], s[45][1], s[45][2]);
glVertex3fv(cube[20]);
glVertex3fv(cube[21]);
glVertex3fv(cube[31]);
glVertex3fv(cube[30]);
glColor3f(s[48][0], s[48][1], s[48][2]);
glVertex3fv(cube[31]);
glVertex3fv(cube[30]);
glVertex3fv(cube[39]);
glVertex3fv(cube[32]);
glColor3f(s[51][0], s[51][1], s[51][2]);
glVertex3fv(cube[32]);
glVertex3fv(cube[39]);
glVertex3fv(cube[34]);
glVertex3fv(cube[33]);
glColor3f(s[52][0], s[52][1], s[52][2]);
glVertex3fv(cube[39]);
glVertex3fv(cube[38]);
glVertex3fv(cube[35]);
glVertex3fv(cube[34]);
glColor3f(s[53][0], s[53][1], s[53][2]);
glVertex3fv(cube[38]);
glVertex3fv(cube[37]);
glVertex3fv(cube[36]);
glVertex3fv(cube[35]);
glColor3f(s[50][0], s[50][1], s[50][2]);
glVertex3fv(cube[29]);
glVertex3fv(cube[28]);
glVertex3fv(cube[37]);
glVertex3fv(cube[38]);
glColor3f(s[49][0], s[49][1], s[49][2]);
glVertex3fv(cube[30]);
glVertex3fv(cube[29]);
glVertex3fv(cube[38]);
glVertex3fv(cube[39]);
// Cara 4
glColor3f(s[33][0], s[33][1], s[33][2]);
glVertex3fv(cube[35]);
glVertex3fv(cube[36]);
glVertex3fv(cube[40]);
glVertex3fv(cube[41]);
glColor3f(s[34][0], s[34][1], s[34][2]);
glVertex3fv(cube[34]);
glVertex3fv(cube[35]);
glVertex3fv(cube[41]);
glVertex3fv(cube[42]);
glColor3f(s[35][0], s[35][1], s[35][2]);
glVertex3fv(cube[33]);
glVertex3fv(cube[34]);
glVertex3fv(cube[42]);
glVertex3fv(cube[43]);
glColor3f(s[32][0], s[32][1], s[32][2]);
glVertex3fv(cube[43]);
glVertex3fv(cube[42]);
glVertex3fv(cube[45]);
glVertex3fv(cube[44]);
glColor3f(s[29][0], s[29][1], s[29][2]);
glVertex3fv(cube[44]);
glVertex3fv(cube[45]);
glVertex3fv(cube[7]);
glVertex3fv(cube[6]);
glColor3f(s[28][0], s[28][1], s[28][2]);
glVertex3fv(cube[45]);
glVertex3fv(cube[46]);
glVertex3fv(cube[8]);
glVertex3fv(cube[7]);
glColor3f(s[27][0], s[27][1], s[27][2]);
glVertex3fv(cube[46]);
glVertex3fv(cube[47]);
glVertex3fv(cube[9]);
glVertex3fv(cube[8]);
glColor3f(s[30][0], s[30][1], s[30][2]);
glVertex3fv(cube[41]);
glVertex3fv(cube[40]);
glVertex3fv(cube[47]);
glVertex3fv(cube[46]);
glColor3f(s[31][0], s[31][1], s[31][2]);
glVertex3fv(cube[42]);
glVertex3fv(cube[41]);
glVertex3fv(cube[46]);
glVertex3fv(cube[45]);
// Cara 5
glColor3f(s[18][0], s[18][1], s[18][2]);
glVertex3fv(cube[11]);
glVertex3fv(cube[48]);
glVertex3fv(cube[16]);
glVertex3fv(cube[0]);
glColor3f(s[21][0], s[21][1], s[21][2]);
glVertex3fv(cube[48]);
glVertex3fv(cube[51]);
glVertex3fv(cube[17]);
glVertex3fv(cube[16]);
glColor3f(s[24][0], s[24][1], s[24][2]);
glVertex3fv(cube[51]);
glVertex3fv(cube[28]);
glVertex3fv(cube[18]);
glVertex3fv(cube[17]);
glColor3f(s[25][0], s[25][1], s[25][2]);
glVertex3fv(cube[50]);
glVertex3fv(cube[37]);
glVertex3fv(cube[28]);
glVertex3fv(cube[51]);
glColor3f(s[26][0], s[26][1], s[26][2]);
glVertex3fv(cube[40]);
glVertex3fv(cube[36]);
glVertex3fv(cube[37]);
glVertex3fv(cube[50]);
glColor3f(s[23][0], s[23][1], s[23][2]);
glVertex3fv(cube[47]);
glVertex3fv(cube[40]);
glVertex3fv(cube[50]);
glVertex3fv(cube[49]);
glColor3f(s[20][0], s[20][1], s[20][2]);
glVertex3fv(cube[9]);
glVertex3fv(cube[47]);
glVertex3fv(cube[49]);
glVertex3fv(cube[10]);
glColor3f(s[19][0], s[19][1], s[19][2]);
glVertex3fv(cube[10]);
glVertex3fv(cube[49]);
glVertex3fv(cube[48]);
glVertex3fv(cube[11]);
glColor3f(s[22][0], s[22][1], s[22][2]);
glVertex3fv(cube[51]);
glVertex3fv(cube[50]);
glVertex3fv(cube[49]);
glVertex3fv(cube[48]);
// Cara 6
glColor3f(s[36][0], s[36][1], s[36][2]);
glVertex3fv(cube[44]);
glVertex3fv(cube[6]);
glVertex3fv(cube[5]);
glVertex3fv(cube[53]);
glColor3f(s[39][0], s[39][1], s[39][2]);
glVertex3fv(cube[43]);
glVertex3fv(cube[44]);
glVertex3fv(cube[53]);
glVertex3fv(cube[54]);
glColor3f(s[42][0], s[42][1], s[42][2]);
glVertex3fv(cube[33]);
glVertex3fv(cube[43]);
glVertex3fv(cube[54]);
glVertex3fv(cube[32]);
glColor3f(s[43][0], s[43][1], s[43][2]);
glVertex3fv(cube[32]);
glVertex3fv(cube[54]);
glVertex3fv(cube[55]);
glVertex3fv(cube[31]);
glColor3f(s[44][0], s[44][1], s[44][2]);
glVertex3fv(cube[31]);
glVertex3fv(cube[55]);
glVertex3fv(cube[22]);
glVertex3fv(cube[21]);
glColor3f(s[41][0], s[41][1], s[41][2]);
glVertex3fv(cube[55]);
glVertex3fv(cube[52]);
glVertex3fv(cube[23]);
glVertex3fv(cube[22]);
glColor3f(s[38][0], s[38][1], s[38][2]);
glVertex3fv(cube[52]);
glVertex3fv(cube[4]);
glVertex3fv(cube[3]);
glVertex3fv(cube[23]);
glColor3f(s[37][0], s[37][1], s[37][2]);
glVertex3fv(cube[53]);
glVertex3fv(cube[5]);
glVertex3fv(cube[4]);
glVertex3fv(cube[52]);
glColor3f(s[40][0], s[40][1], s[40][2]);
glVertex3fv(cube[53]);
glVertex3fv(cube[54]);
glVertex3fv(cube[55]);
glVertex3fv(cube[52]);
glEnd();
glMatrixMode(GL_MODELVIEW);
if (mov == 1) {
glRotatef(2.0, 0, 0, 0);
}
else if (mov == 2) {
glRotatef(2.0, 1.0, 0, 0);
}
else if (mov == 3) {
glRotatef(2.0, 0, 1.0, 0);
}
else if (mov == 4) {
glRotatef(2.0, 0, 0, 1.0);
}
else
glRotatef(1.0, 0, 0, 0);
}
Rotation is going to be too hard to keep track of unless you refactor your code into some sort of "Block" class, whereby a 3x3 array of blocks will make your Rubik's cube. each block will be exactly the same: white bottom, yellow top, blue left, etc... and when stacked 3x3 on screen will hide the other faces. This is a much easier simplification than keeping track of an array of faces to an array of colors.
Then you will want to make a clear distinction between the code model of your Rubik's cube, and the drawn model of your Rubik's cube. This is important in being able to apply multiply rotations successively.
So in code you will want to have a model of your cube. a 3x3 array is easy, where each row is a face. Then looking at a face, for example:
1 2 3
4 5 6
7 8 9
you will want access to a function that rotates this face (look at something that does the equivalent as numpy.rot90), such that when rotated around the axis that is pointing out of the screen, will produce:
3 6 9
2 5 8
1 4 7
But be careful here. This only changed the layout of the object in memory and hasn't done anything to the drawn model on screen. So you will need to apply the corresponding rotation in OpenGL. After this is done, your code model will now match the drawn model, and you can perform as many successive rotations as you want.
I'm new to Swift and trying to port some code. I have this from an old project:
typedef struct {
float Position[3];
float Normal[3];
float TexCoord[2]; // New
} iconVertex;
const iconVertex iconVertices[] = {
{{0.0,0.0, 0.0}, {0, 0, 1.0}, {0, 0}},
{{1.0, 0.0, 0.0}, {0, 0, 1.0}, {1, 0}},
{{0.0, 1.0, 0.0}, {0, 0, 1.0}, {0, 1}},
{{1.0, 1.0, 0.0}, {0, 0, 1.0}, {1, 1}},
};
Is there any way to do that same array initialization in Swift?
Thanks!
In Swift you can use Structs to define your objects and create an init method that receives the parameters that you need to initialise.
struct IconVertex {
var position: [Double]
var normal: [Double]
var textCoord: [Double]
init(position: [Double], normal: [Double], textCoord: [Double]) {
self.position = position
self.normal = normal
self.textCoord = textCoord
}
}
let iconVertices: [IconVertex] = [
IconVertex(position: [0.0,0.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 0]),
IconVertex(position: [1.0, 0.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 0]),
IconVertex(position: [0.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [0, 1]),
IconVertex(position: [1.0, 1.0, 0.0], normal: [0, 0, 1.0], textCoord: [1, 1])]
Right now I am learning OpenGL and I want to draw a pyramid where each side has a different color. The issue I am having is that I only seem to be able to assign colors to the vertexes and not the sides. Therefore each side has a gradient effect rather than being a solid color. How can I give my sides solid color?
void init_buffer()
{
glGenBuffers(1, &(b.trifan));
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
GLfloat trifan[6][3] =
{
{0.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, -1.0},
{1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);
//base of the pyramid as 2 triangles
glGenBuffers(1, &(b.tribase));
glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
GLfloat tribase[4][3] =
{
{1.0, 0.0, -1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * 3, tribase, GL_STATIC_COPY);
//colors
glGenBuffers(1, &b.colors);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
GLfloat colors[6][3] =
{
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.5, 0.0, 1.0},
{1.0, 0.0, 0.5},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);
check_errors();
}
...
//Inside my display function
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
{
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
You can duplicate the vertices for each face as already suggested in the comments. The same is also achievable with the use of different textures (bitmaps) for each face, without duplicating the vertices.
Refer to
http://oglsuperbible5.googlecode.com/svn/trunk/Src/Chapter05/Pyramid/Pyramid.cpp
Figure at https://www.scss.tcd.ie/Michael.Manzke/CS7055/Lab2/SuperBible.4th.Ed.Ch8-9.pdf
Note: The superbible reference only has one texture mapped on all faces, so additional code is to be written to map different textures to different faces.
I have the following code:
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
#include <stdio.h>
GLfloat ctrlptsBezier[4][4][3] =
{
{
{-2.0, -2.0, 1.0},
{-0.5, -2.0, 0.0},
{0.5, -2.0, 0.0},
{2.0, -2.0, 1.0}},
{
{-2, -0.5, -1.0},
{-0.5, -0.5, 0.0},
{0.5, -0.5, 0.0},
{2.0, -0.5, 0.0}},
{
{-2, 0.5, 0.0},
{-0.5, 0.5, -2.0},
{0.5, 0.5, 0.0},
{2.0, 0.5, 0.0}},
{
{-2.0, 2.0, 1.0},
{-0.5, 2.0, 0.0},
{0.5, 2.0, 0.0},
{2.0, 2.0, 1.0}}
};
int uSteps = 30;
int vSteps = 30;
bool shade = false;
GLfloat black [] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat peach[] = { 1.0, 0.5, 0.5, 1.0 };
GLfloat purple[] = { 0.5, 0.0, 0.5, 1.0 };
GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
//Initial eye/viewpoint coords.
double xPos = -20.0, zPos = -20.0;
double eyeHeight = 4.5;
double eyeIncline = -0.5;
double theta = 0.0; //Rotation angle in rads
//Movement params (camera)
double posIncr = 0.25;
double thetaIncr = 0.1;
#define BUFSIZE 512 //Pick buffer size
void setObjLight( bool s )
{
int shadeNumber = GL_FLAT;
if ( shade == true )
shadeNumber = GL_SMOOTH;
glShadeModel(shadeNumber);
}
void setLights(void)
{
setObjLight(shade);
}
void drawRoom(void)
{
GLfloat floor_color[] = { 0.5, 0.5, 0.5, 1.0 }; //Grey floor
GLfloat blue_wall[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat green_wall[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat purple_wall[] = { 0.5, 0.0, 0.5, 1.0 };
//FLOOR
glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_color);
glBegin(GL_POLYGON);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-25.0, 0.0, -25.0);
glVertex3f( 25.0, 0.0, -25.0);
glVertex3f( 25.0, 0.0, 25.0);
glVertex3f(-25.0, 0.0, 25.0);
glEnd();
//WALLS
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue_wall);
glPushMatrix();
glTranslatef(0.0, 0.0, 25.0);
glBegin(GL_POLYGON);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-25.0,-25.0,0.0);
glVertex3f(25,-25.0,0);
glVertex3f(25, 25, 0);
glVertex3f(-25, 25, 0);
glEnd();
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, green_wall);
glPushMatrix();
glTranslatef(0.0, 0.0, -25.0);
glBegin(GL_POLYGON);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-25.0,-25.0,0.0);
glVertex3f(25,-25.0,0);
glVertex3f(25, 25, 0);
glVertex3f(-25, 25, 0);
glEnd();
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purple_wall);
glPushMatrix();
glTranslatef(25.0, 0.0, 0.0);
glRotatef(90.0, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-25.0,-25.0,0.0);
glVertex3f(25,-25.0,0);
glVertex3f(25, 25, 0);
glVertex3f(-25, 25, 0);
glEnd();
glPopMatrix();
}
void drawShapes()
{
//SPHERE
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
glNormal3f(0.0, 1.0, 0.0);
glTranslatef(0.0, 2.0, 0.0);
glLoadName(1);
glutSolidSphere (2.0, 20, 16);
glPopMatrix();
glFlush();
//CONE
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, purple);
glNormal3f(0.0, 1.0, 0.0);
glTranslatef(5.0, 0.0, 0.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
glLoadName(2);
glutSolidCone (2.0, 3, 20, 16);
glPopMatrix();
glFlush();
glLoadName(0);
}
void drawBezier()
{
glEnable(GL_MAP2_VERTEX_3);
glPushMatrix();
glTranslatef(10, 2, 15);
glMaterialfv(GL_FRONT, GL_DIFFUSE, white);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsBezier[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);
glPopMatrix();
glPushMatrix();
glTranslatef(13, 2, 18);
glRotatef(-90, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsBezier[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);
glPopMatrix();
glPushMatrix();
glTranslatef(7, 2, 18);
glRotatef(90, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsBezier[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);
glPopMatrix();
glPushMatrix();
glTranslatef(10, 2, 21);
glRotatef(180, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, peach);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsBezier[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);
glPopMatrix();
glPushMatrix();
glTranslatef(10, 5, 18);
glRotatef(90.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &ctrlptsBezier[0][0][0]);
glMapGrid2f(uSteps, 0.0, 1.0, vSteps, 0.0, 1.0);
glEvalMesh2(GL_FILL, 0, uSteps, 0, vSteps);
glPopMatrix();
//Sphere for filler
glPushMatrix();
glTranslatef(10, 2, 18);
glRotatef(90.0, 1.0, 0.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, white);
glutSolidSphere(3.0, 20, 16);
glPopMatrix();
glFlush();
}
void drawScene(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
double atx = xPos + cos(theta);
double atz = zPos + sin(theta);
double atHeight = eyeHeight + eyeIncline;
gluLookAt(xPos, eyeHeight, zPos, atx, atHeight, atz, 0.0, 1.0, 0.0);
glPushMatrix();
setLights();
drawRoom();
drawShapes();
glLoadName(3);
drawBezier();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
glutSwapBuffers();
}
void setProjection(void)
{
gluPerspective(60.0, 1.0, 0.1, 100.0);
}
void init(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_LIGHTING);
glEnable(GL_FLAT); //Flat shading initially
glShadeModel(GL_FLAT);
//LIGHTING
GLfloat specular[]= {1.0, 1.0, 1.0, 1.0};
GLfloat diffuse[]= {1.0, 1.0, 1.0, 1.0};
GLfloat ambient[]= {1.0, 1.0, 1.0, 1.0};
GLfloat shininess= {100.0};
GLfloat light_ambient[]= {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[]= {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[]= {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[]= {10.0, 10.0, 10.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
// /LIGHTING
glEnable(GL_NORMALIZE);
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void specialKey(int k, int x, int y) //Camera movement commands with arrow keys
{
switch (k) {
case GLUT_KEY_UP:
xPos += posIncr * cos(theta);
zPos += posIncr * sin(theta);
break;
case GLUT_KEY_DOWN:
xPos -= posIncr * cos(theta);
zPos -= posIncr * sin(theta);
break;
case GLUT_KEY_LEFT:
theta -= thetaIncr;
break;
case GLUT_KEY_RIGHT:
theta += thetaIncr;
break;
case GLUT_KEY_PAGE_UP:
eyeIncline += 0.5;
break;
case GLUT_KEY_PAGE_DOWN:
eyeIncline -= 0.5;
break;
case GLUT_KEY_HOME:
eyeHeight += 0.5;
break;
case GLUT_KEY_END:
eyeHeight -= 0.5;
break;
default:
return;
}
glutPostRedisplay();
}
void key(unsigned char k, int x, int y)
{
if ( k == 27 ) //ESC to close
exit(0);
glutPostRedisplay();
}
void processHits(GLint hits, GLuint buffer[])
{
unsigned int j;
GLuint names, *ptr;
float z1, z2;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
shade = !shade;
setLights();
glutPostRedisplay();
for (int i = 0; i < hits; i++)
{ /* for each hit */
names = *ptr;
printf (" number of names for hit = %d\n", names); ptr++;
z1 = (float) *ptr/0x7fffffff; ptr++;
z2 = (float) *ptr/0x7fffffff; ptr++;
printf(" z1 is %g;",z1);
printf(" z2 is %g\n", z2);
printf (" the name is ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr); ptr++;
}
printf ("\n");
}
}
void pick( int button, int state, int x, int y )
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) {
return;
}
glSelectBuffer(BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); //5x5 pixel area around the mouse for selecting
gluPerspective(75, 1.0, 0.1, 100); //NEED
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glInitNames();
glPushName(0);
drawScene();
glPopMatrix();
glFlush();
hits = glRenderMode(GL_RENDER); //# of hits assigned
processHits(hits, selectBuf);
}
void reshape(int w, int h){
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, w/h,1,150);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Room");
init();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutSpecialFunc(specialKey);
glutReshapeFunc(reshape);
glutMouseFunc(pick);
glutMainLoop();
return 0;
}
Forgive me for the length. I am trying to get picking to work so that I can select one object at a time to do things with. According to my print outs, it is functioning as I expect, except for one hurdle: When you pick an object, you end up extremely zoomed in, basically inside the selected shape. I have combed my code, and made some changes/debugging efforts, but I can't find the problem line. Can anyone point me in the right direction as to why picking gives this strange behavior?
Your function pick change the projection matrix with gluperspective (to zoom on the picking area), but it fails to restore the previous matrix state: it can be easily fixed by wrapping the the drawing part of the function with a set of glPushMatrix and glPopMatrix while in matrix GL_PROJECTION mode.
void pick( int button, int state, int x, int y )
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) {
return;
}
glSelectBuffer(BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // save projection settings
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); //5x5 pixel area around the mouse for selecting
gluPerspective(75, 1.0, 0.1, 100); //NEED
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glInitNames();
glPushName(0);
drawScene();
// restore previous projection settings
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glFlush();
I'm having a tough time figuring out how to get this structure array to appear as a single string "c_str" when I use the printf function. As of now I only can only get c_str to be one part of the line structure array, in this case the 24th line. When I use print(c_str); i would like the output to display all of the data that is in the code. It needs to be stored as a string because I have a function that needs to access n,m,gnm,hnm,dgnm, and dhnm.
Thank you for the help
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i=0,n[90],m[90];
float gnm[90],hnm[90],dgnm[90],dhnm[90];
static char c_str[90];
struct wmm
{
int n;
int m;
float gnm;
float hnm;
float dgnm;
float dhnm;
} book[90]= {{1, 0, -29496.6, 0.0, 11.6, 0.0},
{1, 1, -1586.3, 4944.4, 16.5, -25.9},
{2, 0, -2396.6, 0.0, -12.1, 0.0},
{2, 1, 3026.1, -2707.7, -4.4, -22.5},
{2, 2, 1668.6, -576.1, 1.9, -11.8},
{3, 0, 1340.1, 0.0, 0.4, 0.0},
{3, 1, -2326.2, -160.2, -4.1, 7.3},
{3, 2, 1231.9, 251.9, -2.9, -3.9},
{3, 3, 634.0, -536.6, -7.7, -2.6},
{4, 0, 912.6, 0.0, -1.8, 0.0},
{4, 1, 808.9, 286.4, 2.3, 1.1},
{4, 2, 166.7, -211.2, -8.7, 2.7},
{4, 3, -357.1, 164.3, 4.6, 3.9},
{4, 4, 89.4, -309.1, -2.1, -0.8},
{5, 0, -230.9, 0.0, -1.0, 0.0},
{5, 1, 357.2, 44.6, 0.6, 0.4},
{5, 2, 200.3, 188.9, -1.8, 1.8},
{5, 3, -141.1, -118.2, -1.0, 1.2},
{5, 4, -163.0, 0.0, 0.9, 4.0},
{5, 5, -7.8, 100.9, 1.0, -0.6},
{6, 0, 72.8, 0.0, -0.2, 0.0},
{6, 1, 68.6, -20.8, -0.2, -0.2},
{6, 2, 76.0, 44.1, -0.1, -2.1},
{6, 3, -141.4, 61.5 , 2.0, -0.4},
{6, 4, -22.8, -66.3, -1.7, -0.6},
{6, 5, 13.2, 3.1, -0.3, 0.5},
{6, 6, -77.9, 55.0, 1.7, 0.9},
{7, 0, 80.5, 0.0, 0.1, 0.0},
{7, 1, -75.1, -57.9, -0.1, 0.7},
{7, 2, -4.7, -21.1, -0.6, 0.3},
{7, 3, 45.3, 6.5, 1.3, -0.1},
{7, 4, 13.9, 24.9, 0.4, -0.1},
{7, 5, 10.4, 7.0, 0.3, -0.8},
{7, 6, 1.7, -27.7, -0.7, -0.3},
{7, 7, 4.9, -3.3, 0.6, 0.3},
{8, 0, 24.4, 0.0, -0.1, 0.0},
{8, 1, 8.1, 11.0, 0.1, -0.1},
{8, 2, -14.5, -20.0, -0.6, 0.2},
{8, 3, -5.6, 11.9, 0.2, 0.4},
{8, 4, -19.3, -17.4, -0.2, 0.4},
{8, 5, 11.5, 16.7, 0.3, 0.1},
{8, 6, 10.9, 7.0, 0.3, -0.1},
{8, 7, -14.1, -10.8, -0.6, 0.4},
{8, 8, -3.7, 1.7, 0.2, 0.3},
{9, 0, 5.4, 0.0, 0.0, 0.0},
{9, 1, 9.4, -20.5, -0.1, 0.0},
{9, 2, 3.4, 11.5, 0.0, -0.2},
{9, 3, -5.2, 12.8, 0.3, 0.0},
{9, 4, 3.1, -7.2, -0.4, -0.1},
{9, 5, -12.4, -7.4, -0.3, 0.1},
{9, 6, -0.7, 8.0, 0.1, 0.0},
{9, 7, 8.4, 2.1, -0.1, -0.2},
{9, 8, -8.5, -6.1, -0.4, 0.3},
{9, 9, -10.1, 7.0, -0.2, 0.2},
{10, 0, -2.0, 0.0, 0.0, 0.0},
{10, 1, -6.3, 2.8, 0.0, 0.1},
{10, 2 , 0.9 , -0.1 , -0.1 , -0.1},
{10, 3, -1.1, 4.7, 0.2, 0.0},
{10, 4, -0.2, 4.4, 0.0, -0.1},
{10, 5, 2.5, -7.2, -0.1, -0.1},
{10, 6, -0.3, -1.0 , -0.2 , 0.0},
{10, 7, 2.2, -3.9, 0.0, -0.1},
{10, 8, 3.1, -2.0, -0.1, -0.2},
{10, 9, -1.0, -2.0, -0.2, 0.0},
{10, 10, -2.8, -8.3, -0.2, -0.1},
{11, 0, 3.0, 0.0, 0.0, 0.0},
{11, 1, -1.5, 0.2, 0.0, 0.0},
{11, 2, -2.1, 1.7, 0.0, 0.1},
{11, 3, 1.7, -0.6, 0.1, 0.0},
{11, 4, -0.5, -1.8, 0.0, 0.1},
{11, 5, 0.5, 0.9, 0.0, 0.0},
{11, 6, -0.8, -0.4, 0.0, 0.1},
{11, 7, 0.4, -2.5, 0.0, 0.0},
{11, 8, 1.8, -1.3, 0.0, -0.1},
{11, 9, 0.1, -2.1, 0.0, -0.1},
{11, 10, 0.7, -1.9, -0.1, 0.0},
{11, 11, 3.8, -1.8, 0.0, -0.1},
{12, 0, -2.2, 0.0, 0.0, 0.0},
{12, 1, -0.2, -0.9, 0.0, 0.0},
{12, 2, 0.3, 0.3, 0.1, 0.0},
{12, 3, 1.0 , 2.1 , 0.1 , 0.0},
{12, 4, -0.6, -2.5, -0.1, 0.0},
{12, 5, 0.9, 0.5, 0.0, 0.0},
{12, 6, -0.1, 0.6, 0.0, 0.1},
{12, 7, 0.5, 0.0, 0.0, 0.0},
{12, 8, -0.4, 0.1, 0.0, 0.0},
{12, 9, -0.4, 0.3, 0.0, 0.0},
{12, 10, 0.2, -0.9, 0.0, 0.0},
{12, 11, -0.8, -0.2, -0.1, 0.0},
{12, 12, 0.0, 0.9, 0.1, 0.0}};
sprintf(c_str, " %d %d %lf %lf %lf %lf" ,book[25].n, book[25].m , book[25].gnm , book[25].hnm, book[25].dgnm, book[25].dhnm);
getchar();
return 0;
}
If you are trying to copy all of the data in the struct into c_str, you only need to wrap sprintf() in a loop. Note that you should really switch to snprintf() to avoid accidentally introducing a buffer-overflow.
Since sprintf() and snprintf() return the number of character that they add to the string, you can easily keep track of the next unused location in the string:
int i, n = 90, buf_size = 90, offset = 0;
for (i = 0; i < n && offset < buf_size; ++i) {
offset += snprintf(c_str + offset, buf_size - offset, " %d %d %lf %lf %lf %lf", book[i].n, book[i].m , book[i].gnm , book[i].hnm, book[i].dgnm, book[i].dhnm);
}
If you were to repeatedly pass c_str as the first argument, it would only contain the last line.