Auto Gen Vertices无法正常工作?

时间:2018-03-25 00:11:14

标签: c++ opengl glut glew

我正在使用opengl创建一个程序来定义4个顶点(创建一个盒子)然后将其移动并生成一个新的有效创建一个更大的盒子。但是当我运行它时,顶点变得疯狂,我把它减少了,所以你们可以理解我的意思。我希望它是3个单独的盒子但是使用线框你可以清楚地告诉它是由一个连接的2个盒子。

这是我的代码:

#include "stdafx.h"
#include <GL/freeglut.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <GL/glut.h>
using namespace std;

//variables
const float bork = 0.2;
const float blep = -0.2;
float a = 0.2;
float b = -0.2;
float c = 0.2;
float d = -0.2;
float e = 0.2;
float f = -0.2;
float i = 0.2;
float h = 0.2;
float x = 0;
float y = 0;
float z = 0;
float r = 0;
float g = 0; 
float u = 0;
float base = 0;


void stopGen() {
  cout << "stopped generating" << endl;
}

//Functions for generating he vertices!
void generateVerticesYu() {
  glVertex2f(bork, c);
  glVertex2f(bork, d);
  glVertex2f(blep, c);
  glVertex2f(blep, d);
  glColor3ub(r, g, u);
}
void generateVerticesXr() {
  glVertex2f(e, c);
  glVertex2f(e, d);
  glVertex2f(f, c);
  glVertex2f(f, d);
  glColor3ub(r, g, u);
}
void generateVerticesXl() {
  glVertex2f(a, c);
  glVertex2f(a, d);
  glVertex2f(b, c);
  glVertex2f(b, d);
  glColor3ub(r, g, u);
}
void generateVerticesYd() {
  glVertex2f(bork, c);
  glVertex2f(bork, d);
  glVertex2f(blep, c);
  glVertex2f(blep, d);
  glColor3ub(r, g, u);
}


//Keybinds for wireframe and camera movement
void special(int key, int, int)
{
  const float step = 0.1;

  if (GLUT_KEY_LEFT == key)//camera left
    x -= step;

  if (GLUT_KEY_RIGHT == key)//camera right
    x += step;

  if (GLUT_KEY_UP == key)//camera up
    y += step;

  if (GLUT_KEY_DOWN == key)//camera down
    y -= step;

  if (GLUT_KEY_SHIFT_L == key)//wireframe ON

    cout << "Wireframe Enabled" << endl;
/*
  if (GLUT_KEY_CTRL_L == key)//wireframe OFF
    //glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_FILL);
    glPolygonMode(GL_FRONT, GL_FILL);
    cout << "Wireframe Disabled" << endl;
    */
  if (GLUT_KEY_END == key)//RED rgb value UP
    ++r;
    cout << "Red:";
    cout << r << endl;

  if ((GLUT_KEY_END + GLUT_KEY_SHIFT_R) == key)//RED rgb value DOWN
    --r;

  if (GLUT_KEY_PAGE_UP == key)//GREEN rgb value UP
    ++g;

  if ((GLUT_KEY_PAGE_UP + GLUT_KEY_SHIFT_R) == key)//GREEN rgb value DOWN
    --g;

  if (GLUT_KEY_PAGE_DOWN == key)//BLUE rgb value UP
    ++u;

  if ((GLUT_KEY_PAGE_DOWN + GLUT_KEY_SHIFT_R) == key)//BLUE rgb value DOWN
    --u;

  glutPostRedisplay();

}
//renders the vertices
void display()
{
  glPolygonMode(GL_FRONT, GL_LINE);
  glPolygonMode(GL_BACK, GL_LINE);
  glClearColor(0.5, 0.5, 0.5, 1);
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-2, 2, -2, 2, -1, 1);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(x, y, z);
  //actual rendering starts here
  glBegin(GL_TRIANGLE_STRIP);
  if (base != 5) {
    base += 1;
    generateVerticesXl();
    a -= 0.4;
    cout << "a:";
    cout << (a) << endl;
    b -= 0.4;
    cout << "b:";
    cout << (b) << endl;
    generateVerticesXr();
    e += 0.4;
    cout << "e";
    cout << (e) << endl;        
    f += 0.4;
    cout << "f";
    cout << (f) << endl;
  }
/*      do {


    generateVerticesYd();
    c += 0.4;
    cout << "c:";
    cout << (c) << endl;
    d += 0.4;
    cout << "d:";
    cout << (d) << endl;

    generateVerticesYu();
    i += 0.4;
    cout << "i";
    cout << (i) << endl;
    h += 0.4;
    cout << "h";
    cout << (h) << endl;


    } while (base != 6);*/

  glEnd();

  glutSwapBuffers();
}

//main loop for everything!
int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitWindowSize(600, 600);
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  glutCreateWindow("McDank");
  glutSpecialFunc(special); 
  glutDisplayFunc(display);
  //glutIdleFunc();<----TO BE DETERMINED!
  glutMainLoop();
  return 0;
}

可能有更好的方法可以做到这一点,但这就是我现在想要做的事情,但我得到了这个:screenshot

此外,如果我按任意键,它会扩展它,就像它试图生成另一个方格:screenshot2

0 个答案:

没有答案