#c #arrays #opengl
#c #массивы #opengl
Вопрос:
У меня есть работающий массив, но я не могу разместить каждый шар отдельно, все шары втягиваются друг в друга в начале координат, вот мой код:
Ball.cpp
#include "Ball.h"
#include "Vector2f.h"
#include "Vector3f.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
Ball::Ball(void)
{
Vector3f Temp_position;
position = Temp_position;
Vector3f Temp_velocity;
velocity = Temp_velocity;
}
Ball::~Ball(void)
{
}
void Ball::SetPos(Vector3f New_position)
{
position = New_position;
}
void Ball::Draw()
{
glPushMatrix();
glTranslatef(position.X(), position.Y(), position.Z());
glColor3d(1, 0, 0);
glutSolidSphere(0.3, 50, 50);
glPopMatrix();
}
void Ball::ArrayPosition()
{
Ball *Yellowball = new Ball[8];
Yellowball[0].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[1].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[2].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[3].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[4].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[5].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[6].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[7].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
}
void Ball::DrawYellow()
{
glPushMatrix();
glColor3f(2,1,0);
glutSolidSphere(0.3, 50, 50);
glPopMatrix();
}
void Ball::SetVel(Vector3f New_velocity)
{
velocity = New_velocity;
}
Vector3f Ball::GetPos()
{
Vector3f temp;
temp = position;
return temp;
}
Vector3f Ball::GetVel()
{
Vector3f temp;
temp = velocity;
return temp;
}
Display.cpp
#include "Display.h"
#include "Vector3f.h"
#include "Ball.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include <math.h>
#include "Bitmap.h"
Timer Display::m_Timer = Timer();
static float TableWidth = 4; // Z axis normal = 4
float Display::eyeX = -7.5; //-7.5
float Display::eyeY = 3; //3
float Display::eyeZ = 5; //5
float Display::Position[4] = { 1.0f, 0.0f, -3.5, 1.0f };
float Display::translateZ = -3.5;
float Display::translateX = 0.0;
float Display::lightX = 5.0; //5 2.5
float Display::lightY = 5.0;
float Display::lightZ = 2.5;
float m_TableX = -5.0f;
float m_TableZ = -2.5f;
float m_TableWidth = 2.5f;
float m_TableLength = 5.0f;
float ballx = 0.7;
float bally = 0.1;
float ballz = -0.7;
Ball Redball;
float BALL_RED_START = 0;
float RADIUS_OF_BALL = 0.3;
float BALL_RED_END = 8;
Ball Yellowball;
float BALL_YELLOW_START = 0;
float BALL_YELLOW_END = 8;
Bitmap Display::m_HeightMap;
unsigned int Display::m_TextureID;
void Display::Init(int argc, char ** argv)
{
glutInit(amp;argc, argv); // initializes glut
// sets display mode. These parameter set RGB colour model
// and double buffering.
glutInitWindowSize(500,500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Pool Version 1.0");
// Set glut callback functions
glutDisplayFunc(Display::DisplayScene);
glutIdleFunc(Display::Idle);
glutReshapeFunc(Display::Resize);
glutKeyboardFunc(Display::KeyboardInput);
m_Timer.getSeconds();
glEnable(GL_DEPTH_TEST);
glPointSize(5);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glClearColor(0,0,0,1);
glEnable(GL_COLOR_MATERIAL);
glutFullScreen();
float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glEnable(GL_LIGHT0);
Redball.SetPos(Vector3f(-5.0,0.5,0.0));
Redball.SetVel(Vector3f(1.0,0.0,0.0));
Ball *Yellowball = new Ball[8];
for(int i = BALL_YELLOW_START; i < BALL_YELLOW_START; i )
{
Yellowball[0].SetPos(Vector3f (1,0,0));
Yellowball[1].SetPos(Vector3f (0,0,0));
Yellowball[2].SetPos(Vector3f (0,0,0));
Yellowball[3].SetPos(Vector3f (0,0,0));
Yellowball[4].SetPos(Vector3f (0,0,0));
Yellowball[5].SetPos(Vector3f (0,0,0));
Yellowball[6].SetPos(Vector3f (0,0,0));
Yellowball[7].SetPos(Vector3f (0,0,0));
}
//
//Ball Redball[8];
//
//for(int i = BALL_RED_START; i < BALL_RED_START; i )
//{
// glColor3f(1,0,0);
// Redball[i].SetPos(Vector3f (i 128,RADIUS_OF_BALL,45));
//}
glEnable (GL_TEXTURE_2D);
Bitmap image;
image.loadBMP ("myTexture.bmp");
image.loadBMP ("myTexture2.bmp");
glGenTextures(2, amp;m_TextureID); // change to 2 for 2 textures
glBindTexture ( GL_TEXTURE_2D, m_TextureID);
glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps ( GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
// Begin glut main loop
glutMainLoop();
}
void Display::DisplayScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the back buffer
glPushMatrix();
glLoadIdentity();
glNormal3f(0,1,0);
Vector3f redpos = Redball.GetPos();
gluLookAt(eyeX, eyeY, eyeZ, // eye position
0, 0, 0, // what I'm looking at
0.0, 1.0, 0); // Up direction
float Position[] = {lightX, lightY, lightZ, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, Position);
DrawLight(0, Position);
/* Rendering code goes here */
//Ball Redball[8];
//for (int i = BALL_RED_START; i<BALL_RED_END;i )
//{
//
//float x = 0;
//glTranslatef(x 1,1,0);
//glColor3f(1,0,0);
//Redball[i].DrawRed();
//}
//glPopMatrix();
Redball.Draw();
glPushMatrix();
Ball Yellowball[8];
for (int i = BALL_YELLOW_START; i<BALL_YELLOW_END;i )
{
glColor3f(2,1,0);
glTranslatef(1,0,0); //draws the array in a straight line
Yellowball[i].DrawYellow();
}
glPopMatrix();
drawcue();
drawTable();
drawTableLegFrontLeft();
drawTableLegFrontRight();
drawTableLegBackLeft();
drawTableLegBackRight();
drawCushions();
//drawCircle();
//drawHCircle();
Table(-2,-4.5,2,4.5); // Draws the table top in trianglestrip -4.5, 0.5, -0.5, 9.5
glPopMatrix();
glutSwapBuffers(); // Swap the front and back buffers
}
Ответ №1:
Вы создаете массивы YellowBall три раза, устанавливаете позиции, а затем, ну, вы теряете указатель на массивы. Вы столкнулись не с проблемой OpenGL, а с недостатком понимания того, как программа управляет своими данными.
Также OpenGL не обладает постоянством. Это просто сложный API для рисования. Команды рисования не будут запоминаться.
Небольшой совет: Похоже, вы страдаете от того, что я называю «слепотой, вызванной ООП»: вы пытались поместить все в классы, не понимая, что на самом деле происходит. Попробуйте избавиться от этого класса отображения, от него мало пользы. Написание класса для управления геометрией само по себе неплохая идея, но прежде чем вы это сделаете, вы должны понять, как работает OpenGL и как передаются данные в программе. Передача экземпляров класса по кругу на порядки сложнее, если вы хотите сделать это правильно.