OpenGL 4.1:三角形在被绘制后立即闪烁并消失

时间:2016-02-19 04:35:15

标签: c++ opengl

我想渲染绿色三角形。

  • OpenGL版本:4.1
  • 着色语言版本:4.10

问题

下面的代码在执行时会显示一个绿色三角形,会立即闪烁并消失

我看到这篇帖子:A red rectangle drawn on 2D texture disappears right after being drawn,其中有一个类似的消失三角形的问题,但他的原因是他多次调用Typeface font = Typeface.createFromAsset(getAssets(), "fonts/PrimeRegular.otf"); SpannableString mNewTitle = new SpannableString(menuItem.getTitle()); mNewTitle.setSpan(new CustomMenuItemTypeface("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); menuItem.setTitle(mNewTitle); public class CustomMenuItemTypeface extends TypefaceSpan { private final Typeface newType; public CustomMenuItemTypeface(String family, Typeface type) { super(family); newType = type; } private static void applyCustomTypeFace(Paint paint, Typeface tf) { int oldStyle; Typeface old = paint.getTypeface(); if (old == null) { oldStyle = 0; } else { oldStyle = old.getStyle(); } int fake = oldStyle & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) { paint.setFakeBoldText(true); } if ((fake & Typeface.ITALIC) != 0) { paint.setTextSkewX(-0.25f); } paint.setTypeface(tf); } @Override public void updateDrawState(TextPaint ds) { applyCustomTypeFace(ds, newType); } @Override public void updateMeasureState(TextPaint paint) { applyCustomTypeFace(paint, newType); }} ,但我只有一个实例Swap Buffer中的glutSwapBuffers()

C ++代码:

displayFunc()

在代码中,#include <iostream> #include "OpenGLMatrix.h" #include "BasicPipelineProgram.h" using namespace std; int windowWidth = 1280; int windowHeight = 720; char windowTitle[512] = "Simple Green Triangle"; // global variables OpenGLMatrix *matrix; GLuint buffer; BasicPipelineProgram *pipelineProgram; GLint program; GLuint vao; // objects to render int numVertices = 3; float positions[9] = { -1, -1, -1, 1, -1, -1, -1, 1, -1 }; // 3 vertices of triangle to render float colors[12] = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; // all vertices green with alpha = 1 void bindProgram() { // upload model view matrix to shader float m[16]; matrix->SetMatrixMode(OpenGLMatrix::ModelView); matrix->GetMatrix(m); pipelineProgram->SetModelViewMatrix(m); // upload projection matrix to shader float p[16]; matrix->SetMatrixMode(OpenGLMatrix::Projection); matrix->GetMatrix(p); pipelineProgram->SetProjectionMatrix(p); } void displayFunc() { // computing modelview matrix glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); matrix->LoadIdentity(); // camera at (0,0,1), looking in -z direction, up vector y matrix->LookAt(0, 0, 1, 0, 0, -1, 0, 1, 0); bindProgram(); // use the VAO pipelineProgram->Bind(); glBindVertexArray(vao); GLint first = 0; GLsizei count = numVertices; glDrawArrays(GL_TRIANGLES, first, count); glBindVertexArray(0); glutSwapBuffers(); } void idleFunc() { // make the screen update glutPostRedisplay(); } void reshapeFunc(int w, int h) { GLfloat aspect = (GLfloat) w / (GLfloat) h; glViewport(0, 0, w, h); // setup perspective matrix matrix->SetMatrixMode(OpenGLMatrix::Projection); matrix->LoadIdentity(); matrix->Perspective(60.0, aspect, 0.01, 1000.0); matrix->SetMatrixMode(OpenGLMatrix::ModelView); } void initPipelineProgram() { // initialize shader pipeline program pipelineProgram = new BasicPipelineProgram(); pipelineProgram->Init(); pipelineProgram->Bind(); program = pipelineProgram->GetProgramHandle(); // VAO (vertex array objects) to contain the VBOs glGenVertexArrays(1, &vao); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, buffer); // get location index of the "position" shader variable GLuint loc = glGetAttribLocation(program, "position"); glEnableVertexAttribArray(loc); const void *offset = (const void*) 0; GLsizei stride = 0; GLboolean normalized = GL_FALSE; glVertexAttribPointer(loc, 3, GL_FLOAT, normalized, stride, offset); // get location index of the "color" shader variable loc = glGetAttribLocation(program, "color"); glEnableVertexAttribArray(loc); offset = (const void*) sizeOfPositions; stride = 0; normalized = GL_FALSE; glVertexAttribPointer(loc, 4, GL_FLOAT, normalized, stride, offset); glBindVertexArray(0); } void initVBO() { glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeOfPositions + sizeOfColors, NULL, GL_STATIC_DRAW); // upload position data glBufferSubData(GL_ARRAY_BUFFER, 0, sizeOfPositions, positions); // upload color data glBufferSubData(GL_ARRAY_BUFFER, sizeOfPositions, sizeOfColors, colors); } void initScene(int argc, char *argv[]) { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_DEPTH_TEST); matrix = new OpenGLMatrix(); initVBO(); initPipelineProgram(); } int main(int argc, char *argv[]) { cout << "Initializing GLUT..." << endl; glutInit(&argc,argv); cout << "Initializing OpenGL..." << endl; #ifdef __APPLE__ glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL); #else glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL); #endif glutInitWindowSize(windowWidth, windowHeight); glutInitWindowPosition(0, 0); glutCreateWindow(windowTitle); // glut callback functions glutDisplayFunc(displayFunc); glutIdleFunc(idleFunc); glutReshapeFunc(reshapeFunc); initScene(argc, argv); glutMainLoop(); } OpenGLMatrix *matrix是在别处定义的类,除非有要求,否则我们的代码我认为不需要担心。 BasicPipelineProgram *pipelineProgram中的"position""color"引用 GLSL着色器规范中的变量:

initPipelineProgram()

我不明白为什么三角形闪烁然后立即消失?

我是OpenGL的初学者,非常感谢任何帮助。谢谢!

0 个答案:

没有答案