So in my program, I'm using OpenGL/GLSL to construct a square and texturing it.
It's modern OpenGL 4.0+ so doesn't use glBegin/glEnd etc.
My square is made up of 2 triangles, constructed using glDrawArrays(GL_TRIANGLES, 0, 6);
As you can see with my function below, it creates 2 triangles. I'm using 18 vertices in an array when really I only need 12 to create a square because 6 of them are used in both triangles. Its the same with the 24 colours and 8 text coordinates.
void Ground::constructGeometry(Shader* myShader)
{
//Triangle 1 (x,y,z)
vert[0] =-dimY; vert[1] = dimX; vert[2] = dimZ; //Point 2
vert[3] =-dimY; vert[4] =-dimX; vert[5] = dimZ; //Point 1
vert[6] = dimY; vert[7] =-dimX; vert[8] = dimZ; //Point 4
//Triangle 2 (x,y,z)
vert[9] = dimY; vert[10] =-dimX; vert[11] = dimZ; //Point 4
vert[12] = dimY; vert[13] = dimX; vert[14] = dimZ; //Point 3
vert[15] =-dimY; vert[16] = dimX; vert[17] = dimZ; //Point 2
//Colours 1 (r,g,b,a)
col[0] = 1.0f; col[1] = 0.0f; col[2] = 0.0f; col[3] = 1.0f;
col[4] = 1.0f; col[5] = 0.0f; col[6] = 0.0f; col[7] = 1.0f;
col[8] = 1.0f; col[9] = 0.0f; col[10] = 0.0f; col[11] = 1.0f;
//Colours 2 (r,g,b,a)
col[12] = 1.0f; col[13] = 0.0f; col[14] = 0.0f; col[15] = 1.0f;
col[16] = 1.0f; col[17] = 0.0f; col[18] = 0.0f; col[19] = 1.0f;
col[20] = 1.0f; col[21] = 0.0f; col[22] = 0.0f; col[23] = 1.0f;
//(s,t) coords for Tri 1
tex[0] = 0.0; tex[1] = 1.0;
tex[2] = 0.0; tex[3] = 0.0;
tex[4] = 1.0; tex[5] = 0.0;
//(s,t) coords for Tri 2
tex[6] = 1.0; tex[7] = 0.0;
tex[8] = 1.0; tex[9] = 1.0;
tex[10] = 0.0; tex[11] = 1.0;
glGenVertexArrays(2, &m_vaoID[0]);
glBindVertexArray(m_vaoID[0]);
glGenBuffers(3, m_vboID);
GLint vertexLocation= glGetAttribLocation(myShader->handle(), "in_Position");
GLint colorLocation= glGetAttribLocation(myShader->handle(), "in_Color");
GLint texCoordLocation = glGetAttribLocation(myShader->handle(), "in_TexCoord");
glUniform1i(glGetUniformLocation(myShader->handle(), "DiffuseMap"), 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
glBufferData(GL_ARRAY_BUFFER, totalVerts *sizeof(GLfloat), vert, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
glBufferData(GL_ARRAY_BUFFER, totalCols *sizeof(GLfloat), col, GL_STATIC_DRAW);
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_vboID[2]);
glBufferData(GL_ARRAY_BUFFER, totalTexs *sizeof(GLfloat), tex, GL_STATIC_DRAW);
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0,0);
glEnableVertexAttribArray(texCoordLocation);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
How can this be made more efficient so when making the second triangle I can use vert[x] that's already been used instead of declaring the same ones again (points 4 and 2)?
And the same with colours?
And how does this affect my rendering function below?
void Ground::render(GLuint texName, Shader* myShader)
{
glUseProgram(myShader->handle()); //find shader passed
glBindTexture(GL_TEXTURE_2D, texName); //blending needed to use alpha channel
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindVertexArray(m_vaoID[0]); //select first VAO
glDrawArrays(GL_TRIANGLES, 0, 6); //draw first object 0-3, then second 3-6
glDisable(GL_BLEND);
glUseProgram(0);
glBindVertexArray(0); //unbind the vertex array object
}
Obviously I guess it's OK to do it the way I have for only 2 triangles, but I wouldn't want to write out hundreds of vertices if, for some reason, I suddenly wanted a whole bunch of triangles...
Use glDrawElements() instead of glDrawArrays() if you would like to reuse vertices.
glDrawElements takes an index array argument, which allows you to specify the indices of vertices in the vertex array, making it possible to use them multiple times.
As an alternative to the (correct) answers using glDrawElements, you could use glDrawArrays with GL_TRIANGLE_FAN instead of GL_TRIANGLES. A fan uses the first three vertices to draw a triangle, then each subsequent vertex will generate a triangle using the new vertex, the last vertex and the first vertex. Now you only need four vertices (arranged counter-clockwise) in your array, and the call looks like:
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
What you asking for is called indexed rendering. The basic idea is to provide an index buffer those elements reference the vertex IDs in your vertex arrays. So for two triangles sharing one edge, you can define just the 4 corner vertices and can use and index array like {0,1,2, 2,1,3}. You will have to use glDrawElements() to do the drawing.
You might also want to have a look at this wiki page to get an overview of what drawing paths are available in modern GL.
Related
I wrote a simple fragment shader that renders a mandelbrot set. I am doing this in c and with opengl using glsl.
#version 330 core
in vec2 fCoord; //position.x position.y which is -1 to 1 on both axis
uniform int maxIterations;
uniform sampler1D mandiTexture;
out vec4 color;
void main()
{
vec2 c, z;
c.x = fCoord.x;
c.y = fCoord.y;
int i;
z = vec2(0.0f, 0.0f);
for(i=0; i<maxIterations; i++) {
float x = (z.x * z.x - z.y * z.y) + c.x;
float y = (z.y * z.x + z.x * z.y) + c.y;
if((x * x + y * y) > 4.0) break;
z.x = x;
z.y = y;
}
vec4 tcolor;
if (i == maxIterations)
{
tcolor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
else
{
tcolor = texture(mandiTexture, float(i) / float(maxIterations));
}
color = tcolor;
}
i've noticed playing around with the initial z value I get some different results but mostly they extend outside of my quad. With z as 0, 0 I get this result.
as you can see the left side of the set is not being rendered on the quad.
The c value is coming from the vertex shader so i assume it goes for -1 to 1 on both x and y axis and being interpolated in between.
My questions are:
1) How can I center the image on the quad? I am not really sure of
that.
2) How can I say zoom in on some in on the mandelbrot set and a
follow up, lets say I want to zoom in on a specific part of the set?
2B) Let's say I click the screen and get the position in NDC?
3) If I set my max iterations higher the set seems to get really
jaggy, is that normal behavior?
I think if I can understand how to zoom in on the set I can figure out how to zoom in on a specific part but I am unsure.
edit, making sure that my code is
main.c
int maxIterations = 70;
int iterAmount = 1;
char* vshad, *fshad;
GLuint verticesBuffer, colorBuffer, vao, texCoordBuffer, indicesBuffer;
GLuint mandiTextureID, sp;
mat4_s vm, pm, opm, tm;
GLint viewMat = -1;
GLint projMat = -1;
GLint modelMat = -1;
GLint mandiTexture = -1;
GLint maxIterLoc = -1;
void initShaders(void)
{
char* vertexShaderSource = getResource("vert.shad");
char* fragmentShaderSource = getResource("frag.shad");
vshad = readFile(vertexShaderSource);
fshad = readFile(fragmentShaderSource);
free(vertexShaderSource);
free(fragmentShaderSource);
}
int run_game()
{
current_utc_time(&start_time);
while(game_running)
{
current_utc_time(¤t_time);
double frameTime = (diff(start_time,current_time).tv_sec + diff(start_time,current_time).tv_nsec) * .00000001;
//printf("float time: %0.8f\n",frameTime);
if ( frameTime > 0.25 )
{
frameTime = 0.25;
}
current_utc_time(&start_time);
current_time = start_time;
accumulator += frameTime;
while ( accumulator >= dt )
{
accumulator -= dt;
t += dt;
//printf("fixed update dt: %0.8f\n",dt);
}
//render_state = currentState * alpha + previousState * ( 1.0 - alpha );
const double alpha = accumulator / dt;
render();
if(game_running < 1) { break; }
while (SDL_PollEvent(&event))
{
switch (event.type) {
case SDL_QUIT:
game_running = -1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
game_running = -1;
break;
}
break;
}
}
}
return -1;
}
int main(int argc, char const *argv[]) {
initShaders();
mat4_identity(&vm);
vec3_s eye = {0, 0, 0};
vec3_s center = {0, 0, -1};
vec3_s up = {0, 1, 0};
mat4_lookAt(&vm, &eye, ¢er, &up);
mat4_identity(&opm);
mat4_ortho(&opm, 0, 200, 0, 200, 1, 100);
mat4_identity(&tm);
mat4_scalex(&tm, &tm, 100, 100, 0);
mat4_translatex(&tm, &tm, 100.0f, 100.0f, -20);
SDL_Surface* mandiSurface = loadPNG(getResource("mandi.png"));
if(!mandiSurface) {
printf("IMG_Load: %s\n", IMG_GetError());
// handle error
}
GLenum Mode1 = GL_RGB;
if(4 == mandiSurface->format->BytesPerPixel)
{
Mode1 = GL_RGBA;
printf("mode change");
}
sp = getShaderProgram(vshad, fshad);
r = newRenderable2d();
glGenVertexArrays(1, &vao);
glGenBuffers(1, &verticesBuffer);
glGenBuffers(1, &colorBuffer);
glGenBuffers(1, &indicesBuffer);
glGenBuffers(1, &texCoordBuffer);
glBindVertexArray(vao); //bind vertex array buffer
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(r->vertices), r->vertices, GL_STATIC_DRAW);
//bind n setup indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(r->indices), r->indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind indices
//bind n setup colors
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(r->colors), r->colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind colors
//bind n setup texture coords
glBindBuffer(GL_ARRAY_BUFFER, texCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(r->texCoords), r->texCoords, GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind texture coords
glBindVertexArray(0); //unbind vertex array buffer
//mandi 1d texture
glGenTextures(1, &mandiTextureID);
glBindTexture(GL_TEXTURE_1D, mandiTextureID);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage1D(GL_TEXTURE_1D, 0, Mode1, mandiSurface->w, 0, Mode1, GL_UNSIGNED_BYTE, mandiSurface->pixels);
glBindTexture(GL_TEXTURE_1D, 0);
free(mandiSurface);
while(run_game() >= 0);
free(r);
IMG_Quit();
SDL_GL_DeleteContext(maincontext);
SDL_DestroyWindow(window);
return 0;
}
void render()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glUseProgram(sp);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_1D, mandiTextureID);//mandiTexture
mandiTexture = getUniformLocation(sp, "mandiTexture");
glUniform1i(mandiTexture, 0);
glBindVertexArray(verticesBuffer);
viewMat = getUniformLocation(sp, "viewMat");
modelMat = getUniformLocation(sp, "modelMat");
projMat = getUniformLocation(sp, "projMat");
maxIterLoc = getUniformLocation(sp, "maxIterations");
glUniformMatrix4fv(viewMat, 1, GL_FALSE, vm.m);
glUniformMatrix4fv(projMat, 1, GL_FALSE, opm.m);
glUniformMatrix4fv(modelMat, 1, GL_FALSE, tm.m);
glUniform1i(maxIterLoc, maxIterations);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
SDL_GL_SwapWindow(window);
}
int init_sdl(int width, int height, char* title, double fps)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
SDL_Log("sdl failed to init");
SDL_Quit();
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if(window == NULL)
{
SDL_Log("sdl failed to create window");
SDL_Quit();
return -1;
}
maincontext = SDL_GL_CreateContext(window);
if(maincontext == NULL)
{
SDL_Log("sdl failed to create opengl context");
SDL_Quit();
return -1;
}
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
SDL_GL_SetSwapInterval(1);
return 1;
}
vertex shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 icolor;
layout (location = 2) in vec2 vTexCoord;
uniform mat4 modelMat;
uniform mat4 viewMat;
uniform mat4 projMat;
out vec4 fcolor;
out vec2 fTexCoord;
out vec2 fCoord;
void main()
{
gl_Position = projMat * viewMat * modelMat * vec4(position, 1.0);
fCoord = vec2(position);
fTexCoord = vTexCoord;
fcolor = vec4(icolor, 1.0f);
}
thanks to #samgak I was able to fixed the issues that I was having and now I am adding some shots of the Mandelbrot set.
The c value is coming from the vertex shader so i assume it goes for -1 to 1 on both x and y axis and being interpolated in between.
That's correct.
1) How can I center the image on the quad? I am not really sure of that.
You just need to zoom out by a factor of 1.5 and center it on -0.5. The interesting parts of the Mandelbrot set extend from roughly -2 to 1 on the real axis and -i to i on the imaginary axis:
2) How can I say zoom in on some in on the mandelbrot set and a follow up, lets say I want to zoom in on a specific part of the set?
2B) Let's say I click the screen and get the position in NDC?
Put back the 2 uniforms that you had in your previous version:
uniform vec2 center;
uniform float scale;
Declare variables to hold these values in your C code and set them with glUniform2f and glUniform1f. To center the set the initial values should be -0.5, 0.0 for the center, and 1.5 for the scale (larger values zoom out). Then in your fragment shader, just apply them like this:
c = (fCoord * scale) + center;
To click the screen and zoom in on a particular location, turn the mouse location into a value between -1,-1 and 1,1 based on its position on the screen then apply the above equation to it to find the location you clicked on. Set that as the new center and multiply scale by a value less than 1 to zoom in a given amount.
3) If I set my max iterations higher the set seems to get really jaggy, is that normal behavior?
The screenshot you posted looks ok. It would probably look better if you implemented some kind of multi-sampling in your fragment shader (e.g. calculate several values in a loop and add them together so that each pixel is actually the average of a 2x2 or 4x4 block of pixels etc).
If you zoom in far enough eventually you will run into the limits of the precision of the floating point numbers used by the GPU.
I try to switch my openGL code from using display list to using VBO.
In the code, I tried to draw a green-grid floor, which is composed by green lines in parallel with x-axis and green lines in parallel with z-axis.
But when the program runs, floor doesn't display. And I can't find where goes wrong.
Here is my code initialize VBO of floor:
//**********************************
//defined in head of my source code
struct BufferVBO1
{
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat r;
GLfloat g;
GLfloat b;
};
struct IdVBO
{
GLuint id;
size_t bufsiz;
};
IdVBO vboGround;
//**************************************
glGenBuffers(1, &vboGround.id);
int groundSiz1 = ( (floorEdge_x_max-floorEdge_x_min)/(5*LENGTH_UNIT)+1 )*2 ;
int groundSiz2 = ( (floorEdge_z_max-floorEdge_z_min)/(5*LENGTH_UNIT)+1 )*2 ;
vboGround.bufsiz = groundSiz1+groundSiz2 ;
BufferVBO1 *groundBuf = new BufferVBO1 [vboGround.bufsiz];
for(int i=0, idx=0; idx<groundSiz1; ++i)
{
groundBuf[idx].x = floorEdge_x_min+i*5*LENGTH_UNIT;
groundBuf[idx].y = 0;
groundBuf[idx].z = floorEdge_z_min;
groundBuf[idx].r = 75/255.0;
groundBuf[idx].g = 1.0;
groundBuf[idx].b = 63/255.0;
++idx;
groundBuf[idx].x = floorEdge_x_min+i*5*LENGTH_UNIT;
groundBuf[idx].y = 0;
groundBuf[idx].z = floorEdge_z_max;
groundBuf[idx].r = 75/255.0;
groundBuf[idx].g = 1.0;
groundBuf[idx].b = 63/255.0;
++idx;
}
for(int i=0, idx=groundSiz1; idx<vboGround.bufsiz; ++i)
{
groundBuf[idx].x = floorEdge_x_min;
groundBuf[idx].y = 0;
groundBuf[idx].z = floorEdge_z_min+i*5*LENGTH_UNIT;
groundBuf[idx].r = 75/255.0;
groundBuf[idx].g = 1.0;
groundBuf[idx].b = 63/255.0;
++idx;
groundBuf[idx].x = floorEdge_x_max;
groundBuf[idx].y = 0;
groundBuf[idx].z = floorEdge_z_min+i*5*LENGTH_UNIT;
groundBuf[idx].r = 75/255.0;
groundBuf[idx].g = 1.0;
groundBuf[idx].b = 63/255.0;
++idx;
}
glBindBuffer(GL_ARRAY_BUFFER, vboGround.id);
glBufferData(GL_ARRAY_BUFFER, sizeof(BufferVBO1)*vboGround.bufsiz, groundBuf, GL_STATIC_DRAW);
delete [] groundBuf ;
glBindBuffer(GL_ARRAY_BUFFER, 0);
Here is the code to display:
glDisable(GL_LIGHTING);
glBindBuffer(GL_ARRAY_BUFFER, vboGround.id);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(BufferVBO1), (void*)0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer( 3, GL_FLOAT, sizeof(BufferVBO1), (void*)(sizeof(GLfloat)*3) );
for(int i=0; i<vboGround.bufsiz; i+=2)
glDrawArrays(GL_LINE, i, 2);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnable(GL_LIGHTING);
I've tried to draw a simple GL_QUADS floor using VBO, and it works! But when I tried to draw green-grid floor, it doesn't display.
Tell me where is the problem.
The first argument to glDrawArrays() here is invalid:
glDrawArrays(GL_LINE, i, 2);
GL_LINE is not a valid primitive type. You need to use GL_LINES:
glDrawArrays(GL_LINES, i, 2);
GL_LINE may look very similar, but it's one of the possible arguments for glPolygonMode().
You should always call glGetError() when your OpenGL call does not behave as expected. In this case, it should have returned GL_INVALID_ENUM.
BTW, you can draw all the lines in a single glDrawArrays() call, instead of using a loop:
glDrawArrays(GL_LINES, 0, vboGround.bufsize);
I have such a OpenGL-4 code (see below). I have created a buffer for vertices and wanted to initialize it with the help of for-loop in init().
It should be a circle of 30 lines (surrounded by circle later) but I can see only the first line on the screen. And I have done such things before with glVertex. But with VOA I don't really know what to do; I tried a lot but I'm really puzzled; May be It's some stupid mistake or my misunderstanding, I failed to find it. is it possible to do so with VOAs at all?
GLuint lineArrayID;
GLuint lineVertexBuffer;
GLuint numLines = 30;
static GLfloat lineVertexBufferData[30][2] = {};
void init() {
draw_circle();
glClearColor(1.0, 1.0, 1.0, 1.0);//background of the window
GLfloat x, y;
double angle;
GLfloat radius = 5.0;
angle = 2 * PI / 30;
int j = 0;
float i = 0;
for (i = -PI; i < -PI; i += 0.15){
std::cout << " +"<<std:: endl;
x = sin(i);
y = cos(i);
lineVertexBufferData[j][0] = 0.0; lineVertexBufferData[j][1] = 0.0;
lineVertexBufferData[j][0] = x; lineVertexBufferData[j][1] = y;
j++;
}
// compile and activate the desired shader program
shaderProgramID = loadShaders("D.vs", "D.fs");
glUseProgram(shaderProgramID);
// generate Buffers for our objects
prepareLines();
}
void prepareLines(){
glGenVertexArrays(1, &lineArrayID); //gen one array object
glBindVertexArray(lineArrayID); //binds it
glGenBuffers(1, &lineVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, numLines * 60 * sizeof(GLfloat), lineVertexBufferData, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}
static void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
// drawing the lines
glBindVertexArray(lineArrayID);
glCallList(1);
glDrawArrays(GL_LINES, 0, numLines * 2);
glBindVertexArray(0);
transform();
//glRotatef(grad, 0, 0, 1);
//glutSwapBuffers();
glFlush();
}
numLines * 60 * sizeof(GLfloat)
That is way too big, and doesn't match the size of linearVertexBufferData at all. It should be numLines * 2 * sizeof(GLfloat) or even just sizeof(lineVertexBufferData)
glCallList(1);
This is also invalid; you never create any display lists, so it's unlikely that display list one exists. If you're using VAO's, you shouldn't need to create them anyway.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
The second parameter means that each vertex has three components, but judging from lineVertexBufferData, it should only have two.
glDrawArrays(GL_LINES, 0, numLines * 2);
The third parameter is the number of vertices to render, not number of components. This should not be multiplied by two.
//glutSwapBuffers();
glFlush();
SwapBuffers is correct here, not glFlush (which is almost never needed).
You're also calling transform() after you draw, when it should probably be before, otherwise your transformations will be delayed a frame.
Unexpected error for me. I wrote this code:
Eigen::Array <GLfloat, NumVertices, 2> vertices;
vertices(0, 0) = 0.90f; vertices(0, 1) = 0.90f;
vertices(1, 0) = 0.90f; vertices(1, 1) = -0.90f;
vertices(2, 0) = -0.90f; vertices(2, 1) = -0.90f;
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices.data(), GL_STATIC_DRAW);
And I see this:
Why? How correctly use Eigen array for OpenGL?
What I think:
0,0 = x1 0,1 = y1
1,0 = x2 1,1 = y2
2,0 = x3 2,1 = y3
But drawed this:
1,1 = x1 2,1 = y1
2,0 = x2 0,1 = y2
0,0 = x3 1,0 = y3
By default the elements of an Eigen::Array are stored column major. So you have two options:
Use column 2D vectors:
Eigen::Array<float, 2, NumVertices> vertices;
or request for a row-major storage layout:
Eigen::Array<float, NumVertices, 2, Eigen::RowMajor> vertices;
Moreover, in general the number of vertices is not known at compile time, or it is rather large. In these cases, use Eigen::Dynamic instead of NumVertices:
Eigen::Array<float, 2, Eigen::Dynamic> vertices(2, num_vertices);
Below is my program which calculates the rotation vector, rotation matrix from chessboard through OpenCV. And then by creating array of 16 values from this vector and matrix I am passing this to OpenGL. But OpenGL window cant show anything.
Is this way is correct to connect OpenCV and OpenGL?
Why OpenGL not drawing the shape?
// this function for calculating rotation matrix and vector
void calculateOpenCVPoints(IplImage* image)
{
board_w = 4; // Board width in squares
board_h = 5; // Board height
n_boards =3; // Number of boards
board_n = board_w * board_h;
CvSize board_sz = cvSize( board_w, board_h );
CvMat* warp_matrix = cvCreateMat(3,3,CV_32FC1);
CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
int corner_count;
char pressedChar;
//cvNamedWindow("Livevideo",CV_WINDOW_AUTOSIZE);
imgPoints = cvCreateMat(n_boards*board_n,2,CV_32FC1) ;
objectPoints=cvCreateMat(n_boards*board_n,3,CV_32FC1);
pointCounts = cvCreateMat(n_boards,1,CV_32SC1);
intrinsicMatrix = cvCreateMat(3,3,CV_32FC1);
distortionCoeffs = cvCreateMat(5,1,CV_32FC1);
corners = new CvPoint2D32f[ board_n ];
IplImage *grayImage = cvCreateImage(cvGetSize(image),8,1);//subpixel
successes = 0;
while(successes < n_boards)
{
printf("\nIn calculateOpenCVPoints function for calculating image points ==%d",successes);
//Skip every board_dt frames to allow user to move chessboard
if(frame++ % board_dt == 0)
{
//Find chessboard corners:
int found = cvFindChessboardCorners(image, board_sz, corners, &cornerCount,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
cvCvtColor( image, grayImage, CV_BGR2GRAY );
cvFindCornerSubPix( grayImage, corners, cornerCount, cvSize( 11, 11 ),
cvSize( -1, -1 ), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
cvDrawChessboardCorners(image, board_sz, corners,cornerCount, found);
cvShowImage("Calibration", image );
if( cornerCount == board_n )
{
step = successes*board_n;
for( int i=step, j=0; j < board_n; ++i, ++j )
{
CV_MAT_ELEM( *imgPoints, float, i, 0 ) = corners[j].x;
CV_MAT_ELEM( *imgPoints, float, i, 1 ) = corners[j].y;
CV_MAT_ELEM( *objectPoints, float, i, 0 ) = j/board_w;
CV_MAT_ELEM( *objectPoints, float, i, 1 ) = j%board_w;
CV_MAT_ELEM( *objectPoints, float, i, 2 ) = 0.0f;
}
CV_MAT_ELEM( *pointCounts, int, successes, 0 ) = board_n;
successes++;
}
}//if(frame++ % board_dt == 0)
image = cvQueryFrame( cvCapture );
//cvShowImage("liveVideo",image);
presssedChar=cvWaitKey(5);
} //while(successes < n_boards)
cvFindExtrinsicCameraParams2(objectPoints,imgPoints,intrinsic,distortion,rotationVector,translationVector,0);
cvRodrigues2(rotationVector,rotationMatrix,NULL);
printf("\nRotation Vector and Rotation Matrx are calculated\n");
float rv[]={rotationVector->data.fl[0],rotationVector->data.fl[1],rotationVector->data.fl[2]};
float tv[]={translationVector->data.fl[0],translationVector->data.fl[1],translationVector->data.fl[2]};
float rm[9];
for(int i=0;i<9;i++)
rm[i]=rotationMatrix->data.fl[i];
for (int f=0; f<3; f++)
{
for (int c=0; c<3; c++)
{
toGLMatrix[c*4+f] = rm[f*3+c]; //transposed
}
}
toGLMatrix[3] = 0.0;
toGLMatrix[7] = 0.0;
toGLMatrix[11] = 0.0;
toGLMatrix[12] = -tv[0];
toGLMatrix[13] = -tv[1];
toGLMatrix[14] = tv[2];
toGLMatrix[15] = 1.0;
}
// openGL dispaly function
void display
{
calculateOpenCVPoints("Image from camera");
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//argDraw3dCamera( 0, 0 );
glViewport(0, 0, 640,480);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(toGLMatrix);
glClearDepth( 1.0 );
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glMatrixMode(GL_MODELVIEW);
// Loading the calculated matrix from OpenCV rotation matrix and vector
glLoadMatrixd(toGLMatrix);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambi);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_flash);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_flash_shiny);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMatrixMode(GL_MODELVIEW);
glColor3d(1.0f,1.0f,1.0f);
glTranslatef( 0.0, 0.0,0.0 );
glutSolidCube(140);
glDisable( GL_LIGHTING );
glDisable( GL_DEPTH_TEST );
glutSwapBuffers();
}
// and remaining as initialize glut etc
glViewport(0, 0, 640,480);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(toGLMatrix);
glMatrixMode(GL_MODELVIEW);
// Loading the calculated matrix from OpenCV rotation matrix and vector
glLoadMatrixd(toGLMatrix);
You're loading the matrix two times here. That doesn't make sense. It would transform the vertices as
v' = M M v = M² v
Definitely not what you want.
The matrix delivered by OpenCV combines projection and modelview. Ideally you wouldn't use fixed function OpenGL, but use a vertex shader for this. This would make things clearer.
For a simple drop in application, load the matrix from OpenCV just into modelview and make projection a identity transform.
If you want it to be correct, separate the matrix from OpenCV into two parts: A orthonormal modelview part and a matching projection one, with the x and y components of the Z column being zero. Let C be the matrix from OpenCV. For this solve the system of equations:
C = P M
P.zx = 0
P.zy = 0
P.zz =/= 0
P.zw =/= 0
M.x · M.y = 0
M.y · M.z = 0
M.z · M.x = 0
M.xw = 0
M.yw = 0
M.zw = 0
M.ww = 1
| M.x | = 1
| M.y | = 1
| M.z | = 1