Cocular

Member
  • Content count

    2
  • Joined

  • Last visited

Community Reputation

101 Neutral

About Cocular

  • Rank
    Newbie
  1. 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?
  2. 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? [code] #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(); } [/code] Standard.vert [code] #version 330 layout(location = 0) in vec4 position; void main() { gl_Position = position; } [/code] Standard.frag [code] #version 330 out vec4 outputColor; void main() { outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); } [/code] BTW, Is http://www.arcsynthesis.org/gltut/ a good choice for beginner to learn opengl?