Ver Mensaje Individual
  #1  
Antiguo 18-10-2024
navbuoy navbuoy is offline
Miembro
 
Registrado: mar 2024
Posts: 360
Reputación: 3
navbuoy Va por buen camino
Mi primer Cubo 3D con OpenGL y GLEW Library

Excitante, por fin consegui hacer algo en 3D que siempre habia sido una cosa que me motivaba pero no acababa de pillarle el tranquillo

asi que ahora con OpenGL y GLEW lo consegui y sin demasiado esfuerzo

aqui teneis el codigo fuente (compila perfecto en C++ Builder y se ve el cubo incluso rotando en 3D)

Unit1.h:

Código:
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
	TPanel *Panel1;
	void __fastcall FormDestroy(TObject *Sender);
private:	// User declarations
public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
void __fastcall Timer1Timer(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp:

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
}
//---------------------------------------------------------------------------

Utilizo un TPanel como contenedor del dibujado de la salida de pantalla:

aqui se ve como salio el EXE (se ve girando en 3D en funcionamiento)

os adjunto los includes (copiar en donde tengais los include de Embarcadero compiler en Archivos de programa (x86)/Embarcadero/studio/versioncompilador/include:

https://www.quazardev.net/Stardust/i...s_GL_y_glm.rar


Última edición por navbuoy fecha: 18-10-2024 a las 16:03:04.
Responder Con Cita