这是我当前的代码:
所有包含
#define GLEW_STATIC
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
#include<conio.h>
#include<dos.h>
#include <GL/glew.h>
#include "SceneOpenGL.h"
#include <SDL2/SDL_image.h>
using namespace std;
#ifdef WIN32
#include <GL/glew.h>
#else
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>
#endif
#include <SDL2/SDL.h>
#include <iostream>
无效我的功能
void DrawCircle(float cx, float cy, float r, int num_segments);
void DrawEllipse(float cx, float cy, float a, float b, int num_segments);
void Rotation(float a,float b,float r, float g_theta );
主要功能
int main(int argc, char **argv)
{
SDL_Window* fenetre(0);
SDL_GLContext contexteOpenGL(0);
SDL_Event evenements;
bool terminer(false);
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
fenetre = SDL_CreateWindow("Test SDL 2.0", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if(fenetre == 0)
{
std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
SDL_Quit();
return -1;
}
contexteOpenGL = SDL_GL_CreateContext(fenetre);
if(contexteOpenGL == 0)
{
std::cout << SDL_GetError() << std::endl;
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#ifdef WIN32
GLenum initialisationGLEW( glewInit() );
if(initialisationGLEW != GLEW_OK)
{
std::cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << std::endl;
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return -1;
}
#endif
带有圆形和椭圆形显示的主循环
while(!terminer)
{
SDL_WaitEvent(&evenements);
if(evenements.window.event == SDL_WINDOWEVENT_CLOSE)
terminer = true;
glClear(GL_COLOR_BUFFER_BIT);
float g_theta = 0.0f;
while (g_theta<360)
{
glClear(GL_COLOR_BUFFER_BIT);
g_theta += 1.0f;
DrawCircle(0, 0, 0.3, 50);
Rotation(0.8,0.65,0.1,g_theta);
Rotation(0.5,0.5,0.2,g_theta);
Rotation(0.1,0.1,0.01,g_theta);
glDisableVertexAttribArray(0);
SDL_GL_SwapWindow(fenetre);
}
}
SDL_GL_DeleteContext(contexteOpenGL);
SDL_DestroyWindow(fenetre);
SDL_Quit();
return 0;
}
创建圆的功能
void DrawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0 * M_PI * float(ii) / float(num_segments);
float x_c = r * cosf(theta);//calculate the x component
float y_c = r * sinf(theta);//calculate the y component
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(x_c + cx, y_c + cy);//output vertex
}
glEnd();
}
创建椭圆的功能
void DrawEllipse(float cx, float cy, float a, float b, int num_segments)
{
glBegin(GL_LINE_LOOP);
for(int ii = 0; ii < num_segments; ii++)
{
float theta = 2.0 * M_PI * float(ii) / float(num_segments);
float x_e = a * cosf(theta);//calculate the x component
float y_e = b * sinf(theta);//calculate the y component
glColor3f(0.0f,0.0f,1.0f);
glVertex2f(x_e + cx, y_e + cy);//output vertex
}
glEnd();
}
将圆与椭圆连接的功能
void Rotation(float a,float b,float r, float g_theta )
{
float x = a * cosf(g_theta * M_PI / 180.0f);
float y = b * sinf(g_theta * M_PI / 180.0f);
float d = sqrtf( x*x + y*y );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
glRotatef( g_theta, 0, 0, 1 ); // rotation around the z axis
glTranslatef( d, 0, 0 ); // translation by the distance
DrawCircle(0, 0, r, 50);
glPopMatrix();
DrawEllipse(0, 0, a, b, 50);
}
其结果是:
我希望每个圆圈都有自己的纹理。 我已经下载了SDL2_image并尝试了以下代码:
IMG_Init(IMG_INIT_JPG);
SDL_Surface * image = IMG_Load("PICT3159.JPG");
IMG_Quit();
但是什么也没发生。 如何在移动的圆上渲染纹理?
(PS:我的最终目标是创建我们太阳系的2D表示。)
编辑:
我知道如何显示图像。 我是这样的:
SDL_Renderer *pRenderer =
SDL_CreateRenderer(fenetre,-1,SDL_RENDERER_ACCELERATED); // Création d'un
SDL_Renderer utilisant l'accélération matérielle
if ( pRenderer )
{
SDL_Surface* pSprite = SDL_LoadBMP("earth.bmp");
if ( pSprite )
{
SDL_Texture* pTexture =
SDL_CreateTextureFromSurface(pRenderer,pSprite); // Préparation du sprite
if ( pTexture )
{
SDL_Rect dest = { 640/2 - pSprite->w/2,480/2 - pSprite->h/2, pSprite->w, pSprite->h};
SDL_RenderCopy(pRenderer,pTexture,NULL,&dest); // Copie du sprite grâce au SDL_Renderer
SDL_RenderPresent(pRenderer); // Affichage
SDL_Delay(3000); /* Attendre trois secondes, que l'utilisateur voit la fenêtre */
// SDL_DestroyTexture(pTexture); // Libération de la mémoire associée à la texture
}
else
{
fprintf(stdout,"Échec de création de la texture (%s)\n",SDL_GetError());
}
SDL_FreeSurface(pSprite); // Libération de la ressource occupée par le sprite
}
else
{
fprintf(stdout,"Échec de chargement du sprite (%s)\n",SDL_GetError());
}
SDL_DestroyRenderer(pRenderer); // Libération de la mémoire du SDL_Renderer
}
else
{
fprintf(stdout,"Échec de création du renderer (%s)\n",SDL_GetError());
}
也许调整图像大小,将其切成圆形并在椭圆上旋转会更容易吗? (但我也不知道该怎么做)