Jump to content
  • Advertisement
Sign in to follow this  
Cocular

OpenGL Why the efficiency a simple 2D program is so low?

This topic is 2455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm a totally new beginner to OpenGL and I'm learning it by online resource http://www.arcsynthesis.org/gltut/.

It is a 2D example that is a simply 2D spinning triangle around one point. A attach its main code below.

My problem is why a simple program run so slow. It does not run very smooth and I can a few obvious lags. This is much more frequent when I open v-sync (CPU 100% on a laptop is annoying). I also try to sleep sometime before call another glutPostRedisplay. This seems even lagger than v-sync.

My question is how to make this program run more smooth? I can never feel such lag when I play some games written in OpenGL. Also is there a convenient way to limit the FPS?


#define ARRAY_COUNT( array ) (sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))))

GLuint theProgram;

void InitializeProgram()
{
std::vector<GLuint> shaderList;

shaderList.push_back(Framework::LoadShader(GL_VERTEX_SHADER, "standard.vert"));
shaderList.push_back(Framework::LoadShader(GL_FRAGMENT_SHADER, "standard.frag"));

theProgram = Framework::CreateProgram(shaderList);
}

const float vertexPositions[] = {
0.25f, 0.25f, 0.0f, 1.0f,
0.25f, -0.25f, 0.0f, 1.0f,
-0.25f, -0.25f, 0.0f, 1.0f,
};

GLuint positionBufferObject;
GLuint vao;


void InitializeVertexBuffer()
{
glGenBuffers(1, &positionBufferObject);

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

//Called after the window and OpenGL are initialized. Called exactly once, before the main loop.
void init()
{
InitializeProgram();
InitializeVertexBuffer();

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}


void ComputePositionOffsets(float &fXOffset, float &fYOffset)
{
const float fLoopDuration = 1.0f;
const float fScale = 3.14159f * 2.0f / fLoopDuration;

float fElapsedTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;

float fCurrTimeThroughLoop = fmodf(fElapsedTime, fLoopDuration);

fXOffset = cosf(fCurrTimeThroughLoop * fScale) * 0.5f;
fYOffset = sinf(fCurrTimeThroughLoop * fScale) * 0.5f;
}

void AdjustVertexData(float fXOffset, float fYOffset)
{
std::vector<float> fNewData(ARRAY_COUNT(vertexPositions));
memcpy(&fNewData[0], vertexPositions, sizeof(vertexPositions));

for(int iVertex = 0; iVertex < ARRAY_COUNT(vertexPositions); iVertex += 4)
{
fNewData[iVertex] += fXOffset;
fNewData[iVertex + 1] += fYOffset;
}

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertexPositions), &fNewData[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void display()
{
float fXOffset = 0.0f, fYOffset = 0.0f;
ComputePositionOffsets(fXOffset, fYOffset);
AdjustVertexData(fXOffset, fYOffset);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(theProgram);

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glUseProgram(0);

glutSwapBuffers();
glutPostRedisplay();
}


Standard.vert

#version 330

layout(location = 0) in vec4 position;

void main()
{
gl_Position = position;
}


Standard.frag

#version 330

out vec4 outputColor;

void main()
{
outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}



BTW, Is http://www.arcsynthesis.org/gltut/ a good choice for beginner to learn opengl?

Share this post


Link to post
Share on other sites
Advertisement
You are doing all of your calculations on the CPU (slow), using std::vector (also somewhat slow) for a static array (unnecessary) and then moving your vertex data across your system bus (from the CPU to the GPU) each frame (very slow).

If your array isn't dynamic, skip std::vector and use a standard array. Send you vertex data to the GPU once and then either transform it in the vertex shader or use glRotate each frame.

Share this post


Link to post
Share on other sites
Hidden

You are doing all of your calculations on the CPU (slow), using std::vector (also somewhat slow) for a static array (unnecessary) and then moving your vertex data across your system bus (from the CPU to the GPU) each frame (very slow).

If your array isn't dynamic, skip std::vector and use a standard array. Send you vertex data to the GPU once and then either transform it in the vertex shader or use glRotate each frame.


It's not enough data for this to be an issue.
Are you sure it's not your call to glutPostRedisplay..? It seems a bit fishy to call it in your display function! In GLUT, you should probably call it in the Idle callback.

Share this post


Link to post
For 3 verts in a single triangle, doing things on the CPU should be effectively "free" - or at the very least have overhead so low that it's not even measurable. For sure you should move them to the GPU (put them in your vertex shader) but in this case they're not going to be the cause of the symptoms you describe.

Can you post your main function? And in particular elaborate a little on your sleep call - sleep calls are a very bad way of limiting framerate and are not suitable for this kind of use case. They are suitable for reducing battery usage, but you need a fairly light touch with them.

Share this post


Link to post
Share on other sites

For 3 verts in a single triangle, doing things on the CPU should be effectively "free" - or at the very least have overhead so low that it's not even measurable. For sure you should move them to the GPU (put them in your vertex shader) but in this case they're not going to be the cause of the symptoms you describe.


I agree. Since he is learning I felt it was best to steer him away from this practice. It may be free now, but it wont be with a much higher vertex model.

I didn't look at the book he linked to, but if that code is straight from that book, it should raise flags.

Share this post


Link to post
Share on other sites

I agree. Since he is learning I felt it was best to steer him away from this practice. It may be free now, but it wont be with a much higher vertex model.


Good point, I definitely agree there.

Share this post


Link to post
Share on other sites

[quote name='mhagain' timestamp='1332427952' post='4924332']
For 3 verts in a single triangle, doing things on the CPU should be effectively "free" - or at the very least have overhead so low that it's not even measurable. For sure you should move them to the GPU (put them in your vertex shader) but in this case they're not going to be the cause of the symptoms you describe.


I agree. Since he is learning I felt it was best to steer him away from this practice. It may be free now, but it wont be with a much higher vertex model.

I didn't look at the book he linked to, but if that code is straight from that book, it should raise flags.
[/quote]

It is straight from that site, it goes over the proper way to do things later on, i think it is using that method in order to keep the vertex shader simple for the initial example to prevent pushing too much information on the reader at once, I havn't actually looked through it all properly though so i can't vouch for its quality and it does seem a bit odd to start off by showing a retarded way to do things.

Share this post


Link to post
Share on other sites
Thanks for everyone. :)

I understand that the way moving the triangle directly in the CPU is not a good way. But the strange thing is that I can't reprocedure the efficiency problem on the same computer again. I can only believe that this is just my illusion.(Maybe I'm running my computer on powersaving mode?)

Another problem is that when I run two OpenGL applications and enable v-sync, the FPS will drop to 30 and application becomes a little lag. Is there a solution to this problem?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!