Excitante, por fin consegui hacer algo en 3D que siempre habia sido una cosa que me motivaba pero no acababa de pillarle el tranquillo
aqui teneis el codigo fuente (compila perfecto en C++ Builder y se ve el cubo incluso rotando en 3D)
Código:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
// Incluir GLFW
#include <GL/glfw3.h>
// Incluir GLM
#include <glm/glm.hpp>
using namespace glm;
#include <GL/glut.h>
#include <GL/gl.h> // OpenGL
#include <GL/glu.h> // Utilidades de OpenGL
//Estos include puede que no sean del todo necesarios ya que los planeo usar para cuando texturice
#include <iostream>
#include <fstream> // Necesario para std::ifstream
#include <vector> // Necesario para std::vector
#include <algorithm> // Necesario para std::swap
#include <string>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// Variables globales para el manejo del contexto de OpenGL
HDC hdc;
HGLRC hrc;
void InitializeOpenGL(TPanel* Panel);
void DrawCube(void);
void CloseOpenGL(void);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
InitializeOpenGL(Panel1);
TTimer *Timer1 = new TTimer(this);
Timer1->Interval = 16; // Aproximadamente 60 fps
Timer1->OnTimer = Timer1Timer;
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
DrawCube(); // Dibuja el cubo cada vez que el temporizador se activa
}
//---------------------------------------------------------------------------
void InitializeOpenGL(TPanel* Panel) {
hdc = GetDC(Panel->Handle);
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int iPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, iPixelFormat, &pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
GLenum err = glewInit();
if (GLEW_OK != err) {
ShowMessage("Error al inicializar GLEW");
return;
}
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)Panel->Width / (double)Panel->Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void DrawCube(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Trasladar y rotar el cubo
glTranslatef(0.0f, 0.0f, -5.0f);
static float angleX = 0.0f, angleY = 0.0f;
glRotatef(angleX, 1.0f, 0.0f, 0.0f); // Rotación en el eje X
glRotatef(angleY, 0.0f, 1.0f, 0.0f); // Rotación en el eje Y
// Actualizar ángulos para la animación
angleX += 1.0f;
angleY += 1.0f;
// Dibujo del cubo
glBegin(GL_QUADS);
// Cara frontal (rojo)
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// Cara trasera (verde)
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
// Cara izquierda (azul)
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// Cara derecha (amarillo)
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
// Cara superior (magenta)
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, 0.5f);
glVertex3f( 0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Cara inferior (cian)
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, -0.5f);
glVertex3f( 0.5f, -0.5f, 0.5f);
glEnd();
// Intercambiar buffers
SwapBuffers(hdc);
}
void CloseOpenGL(void) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hrc);
ReleaseDC(NULL, hdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
CloseOpenGL(); // Cierra OpenGL al cerrar el formulario
}
//---------------------------------------------------------------------------
os adjunto los includes (copiar en donde tengais los include de Embarcadero compiler en Archivos de programa (x86)/Embarcadero/studio/versioncompilador/include: