gpt4 book ai didi

c++ - Linux 上的 SDL OpenGL 新手,这有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:37 25 4
gpt4 key购买 nike

我已经编写了一些代码来在 Ubuntu 上试验 opengl 编程,这已经有一段时间了,但我曾经对 C 有一个合理的理解。因为有人告诉我 c++ 是游戏编程的首选语言,我正在尝试用它来发展。

这是我第一次真正尝试使用 sdl 的 opengl,我已经走到这一步,它可以编译和运行,但我的相机功能似乎没有做任何事情。我知道可能有很多更好的方法来做这类事情,但我想在学习更高级的东西之前了解基础知识。

主要.cpp

#include <iostream>
#include <cmath>
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"

int screen_width = 640;
int screen_height = 480;
const int screen_bpp = 32;
float rotqube = 0.9f;

float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
float lastx, lasty;

SDL_Surface *screen = NULL; // create a default sdl_surface to render our opengl to

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0); // x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); // y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); // translate the screen to the position
SDL_GL_SwapBuffers();
}

int DrawCube(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(0.0f, 0.0f,-7.0f);
glRotatef(rotqube,0.0f,1.0f,0.0f);
glRotatef(rotqube,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glEnd();
SDL_GL_SwapBuffers();

rotqube +=0.9f;
return true;
}

bool init_sdl(void)
{
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
return false;
}

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

// TODO: Add error check to this screen surface init
screen = SDL_SetVideoMode( screen_width, screen_height, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE );

return true;
}

static void init_opengl()
{
float aspect = (float)screen_width / (float)screen_height;
glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, aspect, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0 ,0.0, 0);
glEnable(GL_DEPTH_TEST);
}

void heartbeat()
{
float xrotrad, yrotrad;
int diffx, diffy;

SDL_Event event;

while(1)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
exit(0);
break;
case SDLK_w:
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += (float)sin(yrotrad);
zpos -= (float)cos(yrotrad);
ypos -= (float)sin(xrotrad);
std::cout << "w pressed" << std::endl;
break;
case SDLK_s:
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= (float)sin(yrotrad);
zpos += (float)cos(yrotrad);
ypos += (float)sin(xrotrad);
break;
case SDLK_d:
yrotrad = (yrot / 180 * 3.141592654f);
xpos += (float)cos(yrotrad) * 0.2;
zpos += (float)sin(yrotrad) * 0.2;
break;
case SDLK_a:
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= (float)cos(yrotrad) * 0.2;
zpos -= (float)sin(yrotrad) * 0.2;
break;
default:
break;
}
break;

case SDL_MOUSEMOTION:
diffx=event.motion.x-lastx; //check the difference between the current x and the last x position
diffy=event.motion.y-lasty; //check the difference between the current y and the last y position
lastx=event.motion.x; //set lastx to the current x position
lasty=event.motion.y; //set lasty to the current y position
xrot += (float)diffy; //set the xrot to xrot with the addition of the difference in the y position
yrot += (float)diffx; //set the xrot to yrot with the addition of the difference in the x position
break;

case SDL_QUIT:
exit(0);
break;

case SDL_VIDEORESIZE:
screen = SDL_SetVideoMode( event.resize.w, event.resize.h, screen_bpp, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE );
screen_width = event.resize.w;
screen_height = event.resize.h;
init_opengl();
std::cout << "Resized to width: " << event.resize.w << " height: " << event.resize.h << std::endl;
break;

default:
break;
}
}

DrawCube();
camera();

SDL_Delay( 50 );
}
}

int main(int argc, char* argv[])
{
if( init_sdl() != false )
{
std::cout << "SDL Init Successful" << std::endl;
}

init_opengl();

std::cout << "Hello World" << std::endl;

heartbeat(); // this is essentially the main loop

SDL_Quit();

return 0;
}

生成文件

all:
g++ -o test main.cpp -lSDL -lGL -lGLU

它编译并运行,我想我只是需要一些帮助来进行相机翻译。谢谢

最佳答案

从 DrawCube() 中删除 glLoadIdentity() 调用。将其替换为开头的 glPushMatrix() 和结尾的 glPopMatrix()。现在按'w'会做一些事情。 (我不完全确定它应该做什么。)

问题是 glLoadIdentity 清除了之前使用 glTranslatef 等设置的所有转换。详细描述:http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/pushmatrix.html

关于c++ - Linux 上的 SDL OpenGL 新手,这有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com