Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

stef

Treue Seele

  • »stef« ist der Autor dieses Themas

Beiträge: 246

Wohnort: Kassel

Beruf: Softwareentwickler

  • Private Nachricht senden

1

27.11.2011, 22:25

Perspective Projection with OpenGL

Hallo

Ich habe mich entschloßen mich in OpenGL einzuarbeiten und habe es schon bereut.
Mein erster Versuch einen Würfel drehen zu lassen funktioniert prinzipiell, aber ich bekomme es nicht hin
eine perspektivische Projektion zu aktivieren. Der hintere Teil des Würfels ist genauso groß wie der vordere.
Bin verzweifelt, frustriert und bitte um Hilfe ... ?(

Hier mein code ...

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
float g_fTimeDiff=0.0f;
float g_fTimeElapsed=0.0f;
// Includes
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
// Function Declarations
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);
// WinMainint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) 
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL quit = FALSE;float theta = 0.0f; 
LONGLONG llFrequency;
LONGLONG llTime1;
LONGLONG llTime2;
 
// register window class
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;wc.lpszClassName = "GLSample"; 
RegisterClass( &wc );
 
// create main window
hWnd = CreateWindow( "GLSample", "OpenGL Sample", 
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 800, 600,
NULL, NULL, hInstance, NULL );
 
// enable OpenGL for the window
EnableOpenGL( hWnd, &hDC, &hRC );
 
// program main loop
QueryPerformanceFrequency((LARGE_INTEGER*)(&llFrequency));while ( !quit ) 
{
QueryPerformanceCounter((LARGE_INTEGER*)(&llTime1));
// check for messagesif ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
{
// handle or dispatch messagesif ( msg.message == WM_QUIT ) 
{
quit = TRUE;
} else 
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
} else 
{
// OpenGL animation code goes here
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
// Culling
//glEnable( GL_CULL_FACE );
//glFrontFace(GL_CCW);
//glCullFace(GL_BACK);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glRotatef( theta, 0.2f, 0.4f, 0.6f );
glBegin( GL_TRIANGLES );
// Top rectangles
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( -0.3f, 0.3f, 0.3f ); // 1
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 0.3f, 0.3f, 0.3f ); // 2
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, 0.3f, -0.3f ); // 3
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( -0.3f, 0.3f, 0.3f ); // 11
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, 0.3f, -0.3f ); // 22
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, 0.3f, -0.3f ); // 33
// Bottom rectangles
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, -0.3f, -0.3f ); // 1
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.3f, -0.3f, -0.3f ); // 2
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, -0.3f, 0.3f ); // 3
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, -0.3f, -0.3f ); // 11
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, -0.3f, 0.3f ); // 22
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( -0.3f, -0.3f, 0.3f ); // 33
// Back rectangles
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( -0.3f, 0.3f, -0.3f ); // 1
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 0.3f, 0.3f, -0.3f ); // 2
glColor3f( 1.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, -0.3f, -0.3f ); // 3
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( -0.3f, 0.3f, -0.3f ); // 11
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.3f, -0.3f, -0.3f ); // 22
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, -0.3f, -0.3f ); // 33
// Front rectangles
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.3f, 0.3f, 0.3f ); // 1
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, 0.3f, 0.3f ); // 2
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( -0.3f, -0.3f, 0.3f ); // 3
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.3f, 0.3f, 0.3f ); // 11
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( -0.3f, -0.3f, 0.3f ); // 22
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, -0.3f, 0.3f ); // 33
// Left rectangles
glColor3f( 0.0f, 1.0f, 1.0f );
glVertex3f( -0.3f, 0.3f, 0.3f ); // 1
glVertex3f( -0.3f, 0.3f, -0.3f ); // 2
glVertex3f( -0.3f, -0.3f, -0.3f ); // 3
glVertex3f( -0.3f, 0.3f, 0.3f ); // 11
glVertex3f( -0.3f, -0.3f, -0.3f ); // 22
glVertex3f( -0.3f, -0.3f, 0.3f ); // 33
// Right rectangles
glColor3f( 1.0f, 0.0f, 1.0f );
glVertex3f( 0.3f, 0.3f, -0.3f ); // 1
glVertex3f( 0.3f, 0.3f, 0.3f ); // 2
glVertex3f( 0.3f, -0.3f, 0.3f ); // 3
glVertex3f( 0.3f, 0.3f, -0.3f ); // 11
glVertex3f( 0.3f, -0.3f, 0.3f ); // 22
glVertex3f( 0.3f, -0.3f, -0.3f ); // 33
glEnd();
SwapBuffers( hDC );
}
 
QueryPerformanceCounter((LARGE_INTEGER*)(&llTime2));g_fTimeDiff = ((float)llTime2 - (float)llTime1) / (float)llFrequency; 
g_fTimeElapsed += g_fTimeDiff;
theta += 90.0f * g_fTimeDiff;
}
 
// shutdown OpenGL
DisableOpenGL( hWnd, hDC, hRC );
 
// destroy the window explicitly
DestroyWindow( hWnd );
 return msg.wParam; 
}
// Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{int x, y;switch (message) 
{ 
case WM_CREATE:return 0;case WM_SIZE: 
y = (lParam & 0xFFFF0000) >> 16;
x = (lParam & 0x0000FFFF);
glViewport(0, 0, x/2, y/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 800.0/600.0, 0.0, 0.0);return 0;case WM_CLOSE: 
PostQuitMessage( 0 );
return 0; case WM_DESTROY: 
return 0;case WM_KEYDOWN:switch ( wParam ) 
{case VK_ESCAPE: 
PostQuitMessage(0);return 0; 
}
return 0;default:return DefWindowProc( hWnd, message, wParam, lParam ); 
}
}
// Enable OpenGLvoid EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC) 
{
PIXELFORMATDESCRIPTOR pfd;int format; 
 
// get the device context (DC)
*hDC = GetDC( hWnd );
 
// set the pixel format for the DCZeroMemory( &pfd, sizeof( pfd ) );pfd.nSize = sizeof( pfd ); 
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );
 
// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
// Disable OpenGLvoid DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC) 
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}
"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg." — Bjarne Stroustrup.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

2

27.11.2011, 23:29

Du willst bei gluPerspective() znear nicht gleich zfar setzen ;)

DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

3

28.11.2011, 09:00

Und Tabs währen nett...
Außerdem finde ich es ehrlich gesagt nicht mehr Sinnvoll mit glVertex usw. zu lernen, da man das eh nie wieder benutzen wird. Man bekommt zwar "schneller" ein Ergebnis, aber Buffer zu verstehen ist nicht besonders schwer.
Aber gut, mach erstmal weiter, ich denke mal du hast Lektüre?

stef

Treue Seele

  • »stef« ist der Autor dieses Themas

Beiträge: 246

Wohnort: Kassel

Beruf: Softwareentwickler

  • Private Nachricht senden

4

28.11.2011, 10:02

Hallo

@DeKugelschieber: Die Tabs sind beim copy and paste drauf gegangen. Keine Ahnung was ich da falsch mache.
Meine Lektüre ist übrigens die SuperBible 5th Edition.

@dot: Ich habe die Lösung des Problems gestern selber noch gefunden.
Mir ist klar das far plane = near plane Schwachsinn ist. Das war ein Experiment das beweisen sollte das der Funktionsaufruf keinerlei Wirkung zeigt. (Der Würfel war nähmlich tzotzdem noch zu sehen 8| mit
Orthographischer Projektion ?( )
Nachdem ich gestern mit glGetError die Fehler ausgewertet hatte war klar das die Funktion an der falschen Stelle aufgerufen wird. (GL_INVALID_OPERATION)
Ich habe sie dann vor die MassageLoop gesetzt und siehe da, Funst. (natürlich mit vernünftigen Werten für far und near plane).

trotzdem Danke :thumbsup:
"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg." — Bjarne Stroustrup.

David Scherfgen

Administrator

Beiträge: 10 382

Wohnort: Hildesheim

Beruf: Wissenschaftlicher Mitarbeiter

  • Private Nachricht senden

5

28.11.2011, 11:22

@Tabs:
Benutzt du zufällig Google Chrome?
Das ist ein bekannter Bug.

stef

Treue Seele

  • »stef« ist der Autor dieses Themas

Beiträge: 246

Wohnort: Kassel

Beruf: Softwareentwickler

  • Private Nachricht senden

6

28.11.2011, 15:49

RE Tabs: Ich benutze nur IE8
"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg." — Bjarne Stroustrup.

DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

7

28.11.2011, 17:35

@David: ich benutze Chrome, hab aber trotzdem Tabs...

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

8

28.11.2011, 17:39

Ich denk der Bug ist in Chrome wohl schon lange behoben. Zumindest wärs mir schon ewig nimmer aufgefallen...

Werbeanzeige