Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 18-10-2024
navbuoy navbuoy is offline
Miembro
 
Registrado: mar 2024
Posts: 280
Poder: 2
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
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Para imprimir PDF: Free PDF Library for Delphi Developers - Quick PDF Library LITE rrf Varios 5 26-04-2019 18:30:17
Cubo de Rubik roman La Taberna 15 20-08-2016 00:45:39
Cubo de decisiones IVAND Varios 1 16-11-2010 23:17:41
primer programa y primer empleo josi La Taberna 36 17-07-2008 22:30:41
Graficar un cubo en un TImage jose_2057111 Gráficos 4 12-12-2004 02:40:39


La franja horaria es GMT +2. Ahora son las 05:02:58.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi